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.

233 lines
9.4 KiB

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