From 2cae4aa4bb0a33fa0c5d701fc0a928104755ccc7 Mon Sep 17 00:00:00 2001 From: DAsamhan <23522019@std.stei.itb.ac.id> Date: Sun, 7 Jul 2024 03:45:25 +0700 Subject: [PATCH] pass compile pwm --- debug-app/app/prj.conf | 3 ++ .../drivers/servo-driver/src/servo-driver.cpp | 42 ++++++++++++++++++- debug-app/include/servo-driver.h | 2 + .../module/driver-hook/src/driver-hook.cpp | 5 ++- .../debug-handler/src/debug-handler.cpp | 2 +- .../handler/main-handler/src/main-handler.cpp | 25 ++++++++--- 6 files changed, 68 insertions(+), 11 deletions(-) diff --git a/debug-app/app/prj.conf b/debug-app/app/prj.conf index 0afc48b..70d43fa 100644 --- a/debug-app/app/prj.conf +++ b/debug-app/app/prj.conf @@ -33,6 +33,9 @@ CONFIG_ZBUS_LOG_LEVEL_INF=y CONFIG_ZBUS_RUNTIME_OBSERVERS=y CONFIG_ZBUS_CHANNEL_NAME=y +#peripheral +CONFIG_PWM=y + #ble CONFIG_BT=y CONFIG_BT_OBSERVER=y diff --git a/debug-app/drivers/servo-driver/src/servo-driver.cpp b/debug-app/drivers/servo-driver/src/servo-driver.cpp index 3ea914d..968c021 100644 --- a/debug-app/drivers/servo-driver/src/servo-driver.cpp +++ b/debug-app/drivers/servo-driver/src/servo-driver.cpp @@ -1,7 +1,43 @@ #include "servo-driver.h" +#include <zephyr/drivers/pwm.h> +#include <zephyr/logging/log.h> + +LOG_MODULE_REGISTER(Servo); + +#define MIN_PERIOD PWM_SEC(1U) / 128U +#define MAX_PERIOD PWM_SEC(1U) + +#define MAX_PWM 100 + +static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0)); + ServoDriver::ServoDriver(){ + uint8_t dir = 0U; + int ret; + + if (!pwm_is_ready_dt(&pwm_led0)) { + LOG_ERR("Error: PWM device %s is not ready\n", + pwm_led0.dev->name); + } + /* + * In case the default MAX_PERIOD value cannot be set for + * some PWM hardware, decrease its value until it can. + * + * Keep its value at least MIN_PERIOD * 4 to make sure + * the sample changes frequency at least once. + */ + LOG_INF("Calibrating for channel %d...\n", pwm_led0.channel); + max_period = MAX_PERIOD; + while (pwm_set_dt(&pwm_led0, max_period, max_period / 2U) < 0) { + max_period /= 2U; + if (max_period < (4U * MIN_PERIOD)) { + LOG_INF("Error: PWM device " + "does not support a period at least %lu\n", + 4U * MIN_PERIOD); + } + } } ServoDriver::~ServoDriver(){ @@ -9,9 +45,11 @@ ServoDriver::~ServoDriver(){ } void ServoDriver::SetPwm(uint8_t pwm){ - + if(pwm_set_dt(&pwm_led0, max_period, max_period * (pwm/MAX_PWM)) == 0){ + period = pwm; + } } uint8_t ServoDriver::GetPwm(){ - return 0; + return period; } \ No newline at end of file diff --git a/debug-app/include/servo-driver.h b/debug-app/include/servo-driver.h index ef7ca12..ca663a2 100644 --- a/debug-app/include/servo-driver.h +++ b/debug-app/include/servo-driver.h @@ -20,4 +20,6 @@ public: uint8_t GetPwm(); private: + uint32_t max_period; + uint8_t period; }; \ No newline at end of file diff --git a/debug-app/module/driver-hook/src/driver-hook.cpp b/debug-app/module/driver-hook/src/driver-hook.cpp index 6327f33..977b796 100644 --- a/debug-app/module/driver-hook/src/driver-hook.cpp +++ b/debug-app/module/driver-hook/src/driver-hook.cpp @@ -47,11 +47,11 @@ void DriverHook::IrSensorCallback(uint8_t data){ .value = sensorData }; - zbus_chan_pub(&driverResponseChannel, &sentMessage, K_MSEC(200)); + zbus_chan_pub(&driverResponseChannel, &sentMessage, K_FOREVER); } void DriverHook::BleDataCallback(std::string addr, std::vector<uint8_t> data, int8_t rssi){ - LOG_HEXDUMP_INF(data.data(), data.size(), "BLE data:"); + // LOG_HEXDUMP_INF(data.data(), data.size(), "BLE data:"); BleData_t sentMessage = { .address = addr, .data = data @@ -132,6 +132,7 @@ void DriverHook::DriverRequestListener(const struct zbus_channel *chan){ case LED: if(msg->type == REQUEST_SET){ ledState = msg->value[0]; + sentMessage.value.push_back(ledState); } else if(msg->type == REQUEST_GET){ sentMessage.value.push_back(ledState); diff --git a/debug-app/module/handler/debug-handler/src/debug-handler.cpp b/debug-app/module/handler/debug-handler/src/debug-handler.cpp index ed11081..1c89d52 100644 --- a/debug-app/module/handler/debug-handler/src/debug-handler.cpp +++ b/debug-app/module/handler/debug-handler/src/debug-handler.cpp @@ -136,7 +136,7 @@ void DebugHandler::DebugThread(DebugHandler *context){ uint16_t driverLen = 0; if(isDriverAvailable){ - driverData.insert(driverData.end(), &driverArray[2], &driverArray[driverLen]); + driverData.insert(driverData.end(), &driverArray[2], &driverArray[2] + driverLen); DriverMessage_t sentMessage = { .number = (DriverNumb_e)driverArray[0], .type = (ExchangeType_e)driverArray[1], diff --git a/debug-app/module/handler/main-handler/src/main-handler.cpp b/debug-app/module/handler/main-handler/src/main-handler.cpp index 04f45a4..8f7248b 100644 --- a/debug-app/module/handler/main-handler/src/main-handler.cpp +++ b/debug-app/module/handler/main-handler/src/main-handler.cpp @@ -53,7 +53,7 @@ int MainHandler::Deinit(){ void MainHandler::MainThread(MainHandler *context){ // const struct zbus_channel *chan; - uint8_t stateCounter = 0; + context->currentState = STANDBY; // while (!zbus_sub_wait(&stateSubs, &chan, K_MSEC(200))) { // StateMessage_t state; @@ -91,6 +91,11 @@ void MainHandler::MainThread(MainHandler *context){ configVector.value.push_back(0); context->DriverRequest(&configVector, &responseVector, K_MSEC(200)); + configVector.number = BLUETOOTH; + configVector.type = REQUEST_SET; + configVector.value.push_back(0); + context->DriverRequest(&configVector, &responseVector, K_MSEC(200)); + break; } @@ -113,6 +118,8 @@ void MainHandler::MainThread(MainHandler *context){ configVector.type = REQUEST_SET; configVector.value.push_back(0); context->DriverRequest(&configVector, &responseVector, K_MSEC(200)); + + context->currentState = SCANNING; break; case USER_2: @@ -126,6 +133,8 @@ void MainHandler::MainThread(MainHandler *context){ configVector.type = REQUEST_SET; configVector.value.push_back(100); context->DriverRequest(&configVector, &responseVector, K_MSEC(200)); + + context->currentState = SCANNING; break; default: @@ -152,11 +161,15 @@ void MainHandler::DriverResponseListener(const struct zbus_channel *chan){ } if(driver->number == IR_DRIVER){ - if(driver->value[0] == 0 && context->currentState == SCANNING){ - context->currentState = STANDBY; - } - else if(driver->value[0] == 1){ - context->currentState = SCANNING; + if(driver->value.size() > 0){ + if(driver->value[0] == 1){ + if(context->currentState == SCANNING){ + context->currentState = STANDBY; + } + else if(context->currentState == STANDBY){ + context->currentState = SCANNING; + } + } } } } -- GitLab