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.

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