chassis.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. ESP-SparkBot 的底座
  3. https://gitee.com/esp-friends/esp_sparkbot/tree/master/example/tank/c2_tracked_chassis
  4. */
  5. #include "sdkconfig.h"
  6. #include "iot/thing.h"
  7. #include "board.h"
  8. #include <driver/gpio.h>
  9. #include <driver/uart.h>
  10. #include <esp_log.h>
  11. #include <cstring>
  12. #include "boards/esp-sparkbot/config.h"
  13. #define TAG "Chassis"
  14. namespace iot {
  15. class Chassis : public Thing {
  16. private:
  17. light_mode_t light_mode_ = LIGHT_MODE_ALWAYS_ON;
  18. void SendUartMessage(const char * command_str) {
  19. uint8_t len = strlen(command_str);
  20. uart_write_bytes(ECHO_UART_PORT_NUM, command_str, len);
  21. ESP_LOGI(TAG, "Sent command: %s", command_str);
  22. }
  23. void InitializeEchoUart() {
  24. uart_config_t uart_config = {
  25. .baud_rate = ECHO_UART_BAUD_RATE,
  26. .data_bits = UART_DATA_8_BITS,
  27. .parity = UART_PARITY_DISABLE,
  28. .stop_bits = UART_STOP_BITS_1,
  29. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  30. .source_clk = UART_SCLK_DEFAULT,
  31. };
  32. int intr_alloc_flags = 0;
  33. ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
  34. ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
  35. ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, UART_ECHO_TXD, UART_ECHO_RXD, UART_ECHO_RTS, UART_ECHO_CTS));
  36. SendUartMessage("w2");
  37. }
  38. public:
  39. Chassis() : Thing("Chassis", "小机器人的底座:有履带可以移动;可以调整灯光效果"), light_mode_(LIGHT_MODE_ALWAYS_ON) {
  40. InitializeEchoUart();
  41. // 定义设备的属性
  42. properties_.AddNumberProperty("light_mode", "灯光效果编号", [this]() -> int {
  43. return (light_mode_ - 2 <= 0) ? 1 : light_mode_ - 2;
  44. });
  45. // 定义设备可以被远程执行的指令
  46. methods_.AddMethod("GoForward", "向前走", ParameterList(), [this](const ParameterList& parameters) {
  47. SendUartMessage("x0.0 y1.0");
  48. });
  49. methods_.AddMethod("GoBack", "向后退", ParameterList(), [this](const ParameterList& parameters) {
  50. SendUartMessage("x0.0 y-1.0");
  51. });
  52. methods_.AddMethod("TurnLeft", "向左转", ParameterList(), [this](const ParameterList& parameters) {
  53. SendUartMessage("x-1.0 y0.0");
  54. });
  55. methods_.AddMethod("TurnRight", "向右转", ParameterList(), [this](const ParameterList& parameters) {
  56. SendUartMessage("x1.0 y0.0");
  57. });
  58. methods_.AddMethod("Dance", "跳舞", ParameterList(), [this](const ParameterList& parameters) {
  59. SendUartMessage("d1");
  60. light_mode_ = LIGHT_MODE_MAX;
  61. });
  62. methods_.AddMethod("SwitchLightMode", "打开灯", ParameterList({
  63. Parameter("lightmode", "1到6之间的整数", kValueTypeNumber, true)
  64. }), [this](const ParameterList& parameters) {
  65. char command_str[5] = {'w', 0, 0};
  66. char mode = static_cast<char>(parameters["lightmode"].number()) + 2;
  67. ESP_LOGI(TAG, "Input Light Mode: %c", (mode + '0'));
  68. if (mode >= 3 && mode <= 8) {
  69. command_str[1] = mode + '0';
  70. SendUartMessage(command_str);
  71. }
  72. });
  73. }
  74. };
  75. } // namespace iot
  76. DECLARE_THING(Chassis);