You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
9.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include "board.h"
  4. #include "sdk\components\subcanmodule\zcancmder_subboard_initer.hpp"
  5. /*******************************************************************************
  6. * PROJECT_INCLUDE *
  7. *******************************************************************************/
  8. #include "sdk/components/step_motor_ctrl_module/step_motor_ctrl_module.hpp"
  9. #include "sdk\components\mini_servo_motor\feite_servo_motor.hpp"
  10. #include "sdk\components\mini_servo_motor\mini_servo_motor_ctrl_module.hpp"
  11. #include "sdk\components\modbus\modbus_client.hpp"
  12. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  13. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  14. //
  15. #include "iwdg.h"
  16. #include "sdk\components\hardware\uart\zuart_dma_receiver.hpp"
  17. #include "serial485_to_analog.hpp"
  18. #include "temperature_sensor.hpp"
  19. #define TAG "main"
  20. using namespace iflytop;
  21. using namespace std;
  22. static ZCancmderSubboardIniter initer;
  23. static Serial485ToAnalog serial485_to_analog;
  24. static ZThread temperature_capture_thread;
  25. extern DMA_HandleTypeDef hdma_usart2_rx;
  26. extern DMA_HandleTypeDef hdma_usart2_tx;
  27. extern void umain();
  28. extern "C" {
  29. void StartDefaultTask(void const* argument) { umain(); }
  30. class UARTSender : public ZIUartSender {
  31. UART_HandleTypeDef* m_huart;
  32. public:
  33. void init(UART_HandleTypeDef* huart) { m_huart = huart; }
  34. virtual void send(const uint8_t* data, size_t len) { HAL_UART_Transmit(m_huart, (uint8_t*)data, len, 1000); }
  35. };
  36. extern DMA_HandleTypeDef hdma_usart3_rx;
  37. extern DMA_HandleTypeDef hdma_usart3_tx;
  38. }
  39. /*******************************************************************************
  40. * GET_DEVICE_ID *
  41. *******************************************************************************/
  42. static int32_t getDeviceId() {
  43. static bool init = false;
  44. static ZGPIO ID0;
  45. static ZGPIO ID1;
  46. static ZGPIO ID2;
  47. static ZGPIO ID3;
  48. static ZGPIO ID4;
  49. if (!init) {
  50. ID0.initAsInput(ID0_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  51. ID1.initAsInput(ID1_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  52. ID2.initAsInput(ID2_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  53. ID3.initAsInput(ID3_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  54. ID4.initAsInput(ID4_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  55. init = true;
  56. }
  57. uint8_t id = ID0.getState() * 1 + ID1.getState() * 2 + ID2.getState() * 4 + ID3.getState() * 8 + ID4.getState() * 16;
  58. return id;
  59. }
  60. /*******************************************************************************
  61. * INIT_SUBMODULE *
  62. *******************************************************************************/
  63. void nvs_init_cb() {}
  64. TemperatureSensor m_temperature_sensor[8];
  65. static void initsubmodule() {
  66. osDelay(1000);
  67. {
  68. m_temperature_sensor[0].initialize(&hadc1, ADC_CHANNEL_12);
  69. m_temperature_sensor[1].initialize(&hadc1, ADC_CHANNEL_13);
  70. m_temperature_sensor[2].initialize(&hadc1, ADC_CHANNEL_11);
  71. m_temperature_sensor[3].initialize(&hadc1, ADC_CHANNEL_10);
  72. m_temperature_sensor[4].initialize(&hadc1, ADC_CHANNEL_14);
  73. temperature_capture_thread.init("temperature_capture_thread", 1024, osPriorityAboveNormal);
  74. for (int i = 0; i < 5; i++) {
  75. m_temperature_sensor[i].loop();
  76. }
  77. temperature_capture_thread.start([]() {
  78. while (1) {
  79. for (int i = 0; i < 5; i++) {
  80. m_temperature_sensor[i].loop();
  81. osDelay(10);
  82. }
  83. osDelay(100);
  84. ZLOGI(TAG, "%d %d %d %d %d", m_temperature_sensor[0].readAdcVal(), //
  85. m_temperature_sensor[1].readAdcVal(), //
  86. m_temperature_sensor[2].readAdcVal(), //
  87. m_temperature_sensor[3].readAdcVal(), //
  88. m_temperature_sensor[4].readAdcVal() //
  89. );
  90. }
  91. });
  92. }
  93. {
  94. /*******************************************************************************
  95. * 4->20MAת485 *
  96. *******************************************************************************/
  97. /**
  98. * @brief
  99. * ڲ:
  100. * 115200
  101. * żУλ:У
  102. * ֹͣλ:һֹͣλ
  103. */
  104. ZASSERT(PC_MODBUS_UART.Init.BaudRate == 115200);
  105. ZASSERT(PC_MODBUS_UART.Init.Parity == UART_PARITY_NONE);
  106. ZASSERT(PC_MODBUS_UART.Init.StopBits == UART_STOPBITS_1);
  107. serial485_to_analog.initialize(&PC_MODBUS_UART, NULL, NULL);
  108. serial485_to_analog.reg(&m_temperature_sensor[0]);
  109. serial485_to_analog.reg(&m_temperature_sensor[1]);
  110. serial485_to_analog.reg(&m_temperature_sensor[2]);
  111. serial485_to_analog.reg(&m_temperature_sensor[3]);
  112. serial485_to_analog.reg(&m_temperature_sensor[4]);
  113. serial485_to_analog.start();
  114. // static ZUARTDmaReceiver dmaUartReceiver;
  115. // static ZUARTDmaReceiver::hardware_config_t cfg = {
  116. // .huart = &PC_MODBUS_UART,
  117. // .dma_rx = &PC_MODBUS_UART_DMA_HANDLER,
  118. // .rxbuffersize = PC_MODBUS_UART_RX_BUF_SIZE,
  119. // .rxovertime_ms = 1,
  120. // };
  121. // dmaUartReceiver.initialize(&cfg);
  122. // static UARTSender uartSender;
  123. // uartSender.init(&PC_MODBUS_UART);
  124. // ModulebusClient::Inst()->init(
  125. // &dmaUartReceiver, &uartSender, 1, //
  126. // [](uint16_t regadd, uint16_t& val) {
  127. // int32_t sendval = 0;
  128. // if (regadd == 11) {
  129. // m_temperature_sensor[0].getTemperature(sendval);
  130. // val = sendval;
  131. // } else if (regadd == 12) {
  132. // m_temperature_sensor[1].getTemperature(sendval);
  133. // val = sendval;
  134. // } else if (regadd == 13) {
  135. // m_temperature_sensor[2].getTemperature(sendval);
  136. // val = sendval;
  137. // } else if (regadd == 14) {
  138. // m_temperature_sensor[3].getTemperature(sendval);
  139. // val = sendval;
  140. // } else if (regadd == 15) {
  141. // m_temperature_sensor[4].getTemperature(sendval);
  142. // val = sendval;
  143. // }
  144. // }, //
  145. // [](uint16_t regadd, uint16_t val) {});
  146. // OSDefaultSchduler::getInstance()->regPeriodJob([](OSDefaultSchduler::Context& context) { ModulebusClient::Inst()->loop(); }, 1);
  147. }
  148. {
  149. // 115200
  150. /*******************************************************************************
  151. * Թñж *
  152. *******************************************************************************/
  153. static FeiTeServoMotor feiteservomotor_bus; // ���ض�������
  154. // ���ض�������
  155. ZASSERT(huart3.Init.BaudRate == 115200);
  156. feiteservomotor_bus.initialize(&huart3, &hdma_usart3_rx, &hdma_usart3_tx);
  157. {
  158. static MiniRobotCtrlModule mini_servo;
  159. static MiniRobotCtrlModule::flash_config_t cfg = {0};
  160. cfg.default_torque = 300;
  161. cfg.default_velocity = 200;
  162. mini_servo.initialize(initer.get_module_id(1), &feiteservomotor_bus, 1, &cfg);
  163. initer.register_module(&mini_servo);
  164. }
  165. {
  166. static MiniRobotCtrlModule mini_servo;
  167. static MiniRobotCtrlModule::flash_config_t cfg = {0};
  168. cfg.default_torque = 300;
  169. cfg.default_velocity = 200;
  170. mini_servo.initialize(initer.get_module_id(2), &feiteservomotor_bus, 2, &cfg);
  171. initer.register_module(&mini_servo);
  172. }
  173. {
  174. static MiniRobotCtrlModule mini_servo;
  175. static MiniRobotCtrlModule::flash_config_t cfg = {0};
  176. cfg.default_torque = 300;
  177. cfg.default_velocity = 200;
  178. mini_servo.initialize(initer.get_module_id(3), &feiteservomotor_bus, 3, &cfg);
  179. initer.register_module(&mini_servo);
  180. }
  181. {
  182. static MiniRobotCtrlModule mini_servo;
  183. static MiniRobotCtrlModule::flash_config_t cfg = {0};
  184. cfg.default_torque = 300;
  185. cfg.default_velocity = 200;
  186. mini_servo.initialize(initer.get_module_id(4), &feiteservomotor_bus, 4, &cfg);
  187. initer.register_module(&mini_servo);
  188. }
  189. {
  190. static MiniRobotCtrlModule mini_servo;
  191. static MiniRobotCtrlModule::flash_config_t cfg = {0};
  192. cfg.default_torque = 300;
  193. cfg.default_velocity = 200;
  194. mini_servo.initialize(initer.get_module_id(5), &feiteservomotor_bus, 5, &cfg);
  195. initer.register_module(&mini_servo);
  196. }
  197. }
  198. }
  199. /*******************************************************************************
  200. * MAIN *
  201. *******************************************************************************/
  202. void umain() {
  203. ZCancmderSubboardIniter::cfg_t cfg = //
  204. {
  205. .deviceId = getDeviceId(),
  206. .input_gpio = {},
  207. .output_gpio = {},
  208. .temperature_sensor =
  209. {
  210. &m_temperature_sensor[0],
  211. &m_temperature_sensor[1],
  212. &m_temperature_sensor[2],
  213. &m_temperature_sensor[3],
  214. &m_temperature_sensor[4],
  215. },
  216. };
  217. OSDefaultSchduler::getInstance()->regPeriodJob([](OSDefaultSchduler::Context&) { HAL_IWDG_Refresh(&hiwdg); }, 1000);
  218. initer.init(&cfg);
  219. initsubmodule();
  220. initer.loop();
  221. }