From fed8cb4d868df0910791ccfd2c11a4bebd4ba26a Mon Sep 17 00:00:00 2001 From: Li Junru <68437239+lijunru-hub@users.noreply.github.com> Date: Fri, 14 Feb 2025 01:30:00 +0800 Subject: [PATCH] feat: Add ESP SparkBot tracked chassis Support (#144) * feat: Add ESP SparkBot tracked chassis Support * feat: remove chassis.cc file to esp-sparkbot --------- Co-authored-by: Liu Ruichao Co-authored-by: Xiaoxia --- main/boards/esp-sparkbot/chassis.cc | 98 +++++++++++++++++++ main/boards/esp-sparkbot/config.h | 27 +++++ .../boards/esp-sparkbot/esp_sparkbot_board.cc | 1 + 3 files changed, 126 insertions(+) create mode 100644 main/boards/esp-sparkbot/chassis.cc diff --git a/main/boards/esp-sparkbot/chassis.cc b/main/boards/esp-sparkbot/chassis.cc new file mode 100644 index 00000000..d970ad88 --- /dev/null +++ b/main/boards/esp-sparkbot/chassis.cc @@ -0,0 +1,98 @@ +/* + ESP-SparkBot 的底座 + https://gitee.com/esp-friends/esp_sparkbot/tree/master/example/tank/c2_tracked_chassis +*/ + +#include "sdkconfig.h" +#include "iot/thing.h" +#include "board.h" + +#include +#include +#include +#include + +#include "boards/esp-sparkbot/config.h" + +#define TAG "Chassis" + +namespace iot { + +class Chassis : public Thing { +private: + light_mode_t light_mode_ = LIGHT_MODE_ALWAYS_ON; + + void SendUartMessage(const char * command_str) { + uint8_t len = strlen(command_str); + uart_write_bytes(ECHO_UART_PORT_NUM, command_str, len); + ESP_LOGI(TAG, "Sent command: %s", command_str); + } + + void InitializeEchoUart() { + uart_config_t uart_config = { + .baud_rate = ECHO_UART_BAUD_RATE, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_DEFAULT, + }; + int intr_alloc_flags = 0; + + ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags)); + ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config)); + ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, UART_ECHO_TXD, UART_ECHO_RXD, UART_ECHO_RTS, UART_ECHO_CTS)); + + SendUartMessage("w2"); + } + +public: + Chassis() : Thing("Chassis", "小机器人的底座:有履带可以移动;可以调整灯光效果"), light_mode_(LIGHT_MODE_ALWAYS_ON) { + InitializeEchoUart(); + + // 定义设备的属性 + properties_.AddNumberProperty("light_mode", "灯光效果编号", [this]() -> int { + return (light_mode_ - 2 <= 0) ? 1 : light_mode_ - 2; + }); + + // 定义设备可以被远程执行的指令 + methods_.AddMethod("GoForward", "向前走", ParameterList(), [this](const ParameterList& parameters) { + SendUartMessage("x0.0 y1.0"); + }); + + methods_.AddMethod("GoBack", "向后退", ParameterList(), [this](const ParameterList& parameters) { + SendUartMessage("x0.0 y-1.0"); + }); + + methods_.AddMethod("TurnLeft", "向左转", ParameterList(), [this](const ParameterList& parameters) { + SendUartMessage("x-1.0 y0.0"); + }); + + methods_.AddMethod("TurnRight", "向右转", ParameterList(), [this](const ParameterList& parameters) { + SendUartMessage("x1.0 y0.0"); + }); + + methods_.AddMethod("Dance", "跳舞", ParameterList(), [this](const ParameterList& parameters) { + SendUartMessage("d1"); + light_mode_ = LIGHT_MODE_MAX; + }); + + methods_.AddMethod("SwitchLightMode", "打开灯", ParameterList({ + Parameter("lightmode", "1到6之间的整数", kValueTypeNumber, true) + }), [this](const ParameterList& parameters) { + char command_str[5] = {'w', 0, 0}; + char mode = static_cast(parameters["lightmode"].number()) + 2; + + ESP_LOGI(TAG, "Input Light Mode: %c", (mode + '0')); + + if (mode >= 3 && mode <= 8) { + command_str[1] = mode + '0'; + SendUartMessage(command_str); + } + }); + } +}; + +} // namespace iot + +DECLARE_THING(Chassis); diff --git a/main/boards/esp-sparkbot/config.h b/main/boards/esp-sparkbot/config.h index 40878526..2664815b 100644 --- a/main/boards/esp-sparkbot/config.h +++ b/main/boards/esp-sparkbot/config.h @@ -2,6 +2,7 @@ #define _BOARD_CONFIG_H_ #include +#include #define AUDIO_INPUT_SAMPLE_RATE 16000 #define AUDIO_OUTPUT_SAMPLE_RATE 16000 @@ -42,5 +43,31 @@ #define DISPLAY_BACKLIGHT_PIN GPIO_NUM_46 #define DISPLAY_BACKLIGHT_OUTPUT_INVERT false +#define UART_ECHO_TXD GPIO_NUM_38 +#define UART_ECHO_RXD GPIO_NUM_48 +#define UART_ECHO_RTS (-1) +#define UART_ECHO_CTS (-1) + +#define MOTOR_SPEED_MAX 100 +#define MOTOR_SPEED_80 80 +#define MOTOR_SPEED_60 60 +#define MOTOR_SPEED_MIN 0 + +#define ECHO_UART_PORT_NUM UART_NUM_1 +#define ECHO_UART_BAUD_RATE (115200) +#define BUF_SIZE (1024) + +typedef enum { + LIGHT_MODE_CHARGING_BREATH = 0, + LIGHT_MODE_POWER_LOW, + LIGHT_MODE_ALWAYS_ON, + LIGHT_MODE_BLINK, + LIGHT_MODE_WHITE_BREATH_SLOW, + LIGHT_MODE_WHITE_BREATH_FAST, + LIGHT_MODE_FLOWING, + LIGHT_MODE_SHOW, + LIGHT_MODE_SLEEP, + LIGHT_MODE_MAX +} light_mode_t; #endif // _BOARD_CONFIG_H_ diff --git a/main/boards/esp-sparkbot/esp_sparkbot_board.cc b/main/boards/esp-sparkbot/esp_sparkbot_board.cc index 26f8bf9d..1a83a06a 100644 --- a/main/boards/esp-sparkbot/esp_sparkbot_board.cc +++ b/main/boards/esp-sparkbot/esp_sparkbot_board.cc @@ -127,6 +127,7 @@ private: auto& thing_manager = iot::ThingManager::GetInstance(); thing_manager.AddThing(iot::CreateThing("Speaker")); thing_manager.AddThing(iot::CreateThing("Backlight")); + thing_manager.AddThing(iot::CreateThing("Chassis")); } public: