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.

205 lines
7.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
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
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. #include <stddef.h>
  2. #include <stdio.h>
  3. #include "sdk/components/step_motor_ctrl_module/step_motor_ctrl_module.hpp"
  4. #include "sdk/components/zcancmder_module/zcan_step_motor_ctrl_module.hpp"
  5. #include "sdk/os/zos.hpp"
  6. #include "sdk\components\flash\zsimple_flash.hpp"
  7. #include "sdk\components\mini_servo_motor\feite_servo_motor.hpp"
  8. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  9. #include "sdk\components\xy_robot_ctrl_module\xy_robot_ctrl_module.hpp"
  10. #include "sdk\components\zcancmder\zcanreceiver.hpp"
  11. #include "sdk\components\zcancmder_module\zcan_basic_order_module.hpp"
  12. #include "sdk\components\zcancmder_module\zcan_xy_robot_module.hpp"
  13. //
  14. #include "sdk\components\flash\znvs.hpp"
  15. //
  16. #define TAG "main"
  17. using namespace iflytop;
  18. using namespace std;
  19. extern void umain();
  20. extern "C" {
  21. void StartDefaultTask(void const* argument) { umain(); }
  22. }
  23. #define TMC_MOTOR_SPI hspi1
  24. static TMC4361A motora;
  25. static TMC4361A motorb;
  26. static ZCanCmder zcanCmder;
  27. static ZCanBasicOrderModule zcanBasicOrderModule;
  28. static ZCANXYRobotCtrlModule zcanXYRobotCtrlModule;
  29. static XYRobotCtrlModule xyRobotCtrlModule;
  30. static StepMotorCtrlModule stepMotorCtrlModule;
  31. static ZCanStepMotorCtrlModule zcanStepMotorCtrlModule;
  32. void umain() {
  33. chip_cfg_t chipcfg;
  34. chipcfg.us_dleay_tim = &DELAY_US_TIMER;
  35. chipcfg.tim_irq_scheduler_tim = &TIM_IRQ_SCHEDULER_TIMER;
  36. chipcfg.huart = &DEBUG_UART;
  37. chipcfg.debuglight = DEBUG_LIGHT_GPIO;
  38. chip_init(&chipcfg);
  39. zos_cfg_t zoscfg;
  40. zos_init(&zoscfg);
  41. ZNVS::ins().initialize();
  42. // ZNVS::ins().set_config_int32("test", 1234);
  43. ZNVS::ins().dumpcfg();
  44. osDelay(1000);
  45. {
  46. TMC4361A::cfg_t cfg = {
  47. .spi = &TMC_MOTOR_SPI, //
  48. .csgpio = TMC_MOTOR1_SPI_SELECT1_IO, //
  49. .resetPin = TMC_MOTOR1_nRESET_IO, //
  50. .fREEZEPin = TMC_MOTOR1_nFREEZE_IO, //
  51. .ennPin = PinNull, //
  52. .driverIC_ennPin = TMC_MOTOR1_SUB_IC_ENN_IO, //
  53. .driverIC_resetPin = PinNull, //
  54. };
  55. motora.initialize(&cfg);
  56. motora.setMotorShaft(false);
  57. ZLOGI(TAG, "motora initialize TMC4361A:%x DriverIC:%x", motora.readICVersion(), motora.readSubICVersion());
  58. }
  59. {
  60. TMC4361A::cfg_t cfg = {
  61. .spi = &TMC_MOTOR_SPI, //
  62. .csgpio = TMC_MOTOR2_SPI_SELECT1_IO, //
  63. .resetPin = TMC_MOTOR2_nRESET_IO, //
  64. .fREEZEPin = TMC_MOTOR2_nFREEZE_IO, //
  65. .ennPin = PinNull, //
  66. .driverIC_ennPin = TMC_MOTOR2_SUB_IC_ENN_IO, //
  67. .driverIC_resetPin = PinNull, //
  68. };
  69. motorb.initialize(&cfg);
  70. motorb.setMotorShaft(false);
  71. ZLOGI(TAG, "motorb initialize TMC4361A:%x DriverIC:%x", motorb.readICVersion(), motorb.readSubICVersion());
  72. }
  73. motora.setAcceleration(300000);
  74. motora.setDeceleration(300000);
  75. motora.setIHOLD_IRUN(0, 2, 10);
  76. motorb.setAcceleration(300000);
  77. motorb.setDeceleration(300000);
  78. motorb.setIHOLD_IRUN(0, 2, 10);
  79. motora.rotate(0);
  80. motorb.rotate(0);
  81. auto zcanCmder_cfg = zcanCmder.createCFG(DEVICE_ID);
  82. zcanCmder.init(zcanCmder_cfg);
  83. /*******************************************************************************
  84. * zcanBasicOrderModule *
  85. *******************************************************************************/
  86. zcanBasicOrderModule.initialize(&zcanCmder);
  87. zcanBasicOrderModule.reg_set_io(1, [](bool val) { ZLOGI(TAG, "write io 1:%d", val); });
  88. zcanBasicOrderModule.reg_read_io(1, []() {
  89. ZLOGI(TAG, "read io 1");
  90. return 1;
  91. });
  92. zcanBasicOrderModule.reg_read_adc(1, []() {
  93. ZLOGI(TAG, "read adc 1");
  94. return 123;
  95. });
  96. /*******************************************************************************
  97. * zcanXYRobotCtrlModule *
  98. *******************************************************************************/
  99. ZGPIO input[8];
  100. input[0].initAsInput(ARM_SENSOR1_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  101. input[1].initAsInput(ARM_SENSOR2_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  102. input[2].initAsInput(ARM_SENSOR3_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  103. input[3].initAsInput(ARM_SENSOR4_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  104. input[4].initAsInput(ARM_SENSOR5_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  105. input[5].initAsInput(ARM_SENSOR6_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  106. input[6].initAsInput(ARM_SENSOR7_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  107. input[7].initAsInput(ARM_SENSOR8_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  108. {
  109. xyRobotCtrlModule.initialize(&motora, &motorb, &input[6], &input[7], 1.0f);
  110. XYRobotCtrlModule::run_param_t param;
  111. xyRobotCtrlModule.read_run_param(param);
  112. param.robot_type = XYRobotCtrlModule::corexy;
  113. param.distance_scale = 1000; // = 1.0
  114. param.x_shaft = false;
  115. param.y_shaft = false;
  116. param.ihold = 1;
  117. param.irun = 3;
  118. param.maxspeed = 1000000;
  119. param.acc = 3000000;
  120. param.dec = 3000000;
  121. xyRobotCtrlModule.set_run_param(param);
  122. XYRobotCtrlModule::run_to_zero_param_t run_to_zero_param;
  123. xyRobotCtrlModule.read_run_to_zero_param(run_to_zero_param);
  124. run_to_zero_param.move_to_zero_max_d = 5120000;
  125. run_to_zero_param.leave_from_zero_max_d = 5120000;
  126. run_to_zero_param.speed = 30000;
  127. run_to_zero_param.dec = 600000;
  128. xyRobotCtrlModule.set_run_to_zero_param(run_to_zero_param);
  129. zcanXYRobotCtrlModule.initialize(&zcanCmder, 1, &xyRobotCtrlModule);
  130. }
  131. {
  132. stepMotorCtrlModule.initialize(1, &motora, &input[6], NULL);
  133. zcanStepMotorCtrlModule.initialize(&zcanCmder, 1, &stepMotorCtrlModule);
  134. }
  135. extern DMA_HandleTypeDef hdma_usart2_rx;
  136. extern DMA_HandleTypeDef hdma_usart2_tx;
  137. FeiTeServoMotor servo;
  138. servo.initialize(&huart2, &hdma_usart2_rx, &hdma_usart2_tx);
  139. OSDefaultSchduler::getInstance()->regPeriodJob(
  140. [&](OSDefaultSchduler::Context& context) { //
  141. // FeiTeServoMotor::status_t status = {0};
  142. // servo.read_status(1, &status);
  143. // servo.dump_status(&status);
  144. // FeiTeServoMotor::detailed_status_t detailed_status = {0};
  145. // servo.read_detailed_status(1, &detailed_status);
  146. // servo.dump_detailed_status(&detailed_status);
  147. },
  148. 1000);
  149. //
  150. servo.ping(1);
  151. // servo.reCalibration(1, 1000);
  152. // servo.moveTo(1, 4000, 0, 0);
  153. // servo.moveWithTorque(1, -1000);
  154. int16_t poscalibration = 0;
  155. // servo.getServoCalibration(1, poscalibration);
  156. // ZLOGI(TAG, "poscalibration:%d", poscalibration);
  157. while (true) {
  158. OSDefaultSchduler::getInstance()->loop();
  159. zcanCmder.loop();
  160. // zcanCmder.sendPacket(data, 4);
  161. #if 0
  162. static uint8_t rxbuf[1024];
  163. static uint16_t rxlen;
  164. rxlen = 0;
  165. HAL_UARTEx_ReceiveToIdle(&huart2, rxbuf, 1000, &rxlen, 1000);
  166. if (rxlen > 0) {
  167. for (size_t i = 0; i < rxlen; i++) {
  168. printf("0x%02x ", rxbuf[i]);
  169. }
  170. printf("\n");
  171. }
  172. #endif
  173. #if 0
  174. osDelay(100);
  175. ZLOGI(TAG, "input:%d %d %d %d %d %d %d %d", //
  176. input[0].getState(), input[1].getState(), input[2].getState(), input[3].getState(), input[4].getState(), input[5].getState(), input[6].getState(), input[7].getState());
  177. #endif
  178. }
  179. }