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.

148 lines
5.6 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
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include "sdk/os/zos.hpp"
  4. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  5. #include "sdk\components\xy_robot_ctrl_module\xy_robot_ctrl_module.hpp"
  6. #include "sdk\components\zcancmder\zcanreceiver.hpp"
  7. #include "sdk\components\zcancmder_module\zcan_basic_order_module.hpp"
  8. #include "sdk\components\zcancmder_module\zcan_xy_robot_module.hpp"
  9. #define TAG "main"
  10. using namespace iflytop;
  11. using namespace std;
  12. extern void umain();
  13. extern "C" {
  14. void StartDefaultTask(void const* argument) { umain(); }
  15. }
  16. #define TMC_MOTOR_SPI hspi1
  17. static TMC4361A motora;
  18. static TMC4361A motorb;
  19. static ZCanCmder zcanCmder;
  20. static ZCanBasicOrderModule zcanBasicOrderModule;
  21. static ZCANXYRobotCtrlModule zcanXYRobotCtrlModule;
  22. static XYRobotCtrlModule xyRobotCtrlModule;
  23. void umain() {
  24. chip_cfg_t chipcfg;
  25. chipcfg.us_dleay_tim = &DELAY_US_TIMER;
  26. chipcfg.tim_irq_scheduler_tim = &TIM_IRQ_SCHEDULER_TIMER;
  27. chipcfg.huart = &DEBUG_UART;
  28. chipcfg.debuglight = DEBUG_LIGHT_GPIO;
  29. chip_init(&chipcfg);
  30. zos_cfg_t zoscfg;
  31. zos_init(&zoscfg);
  32. osDelay(1000);
  33. {
  34. TMC4361A::cfg_t cfg = {
  35. .spi = &TMC_MOTOR_SPI, //
  36. .csgpio = TMC_MOTOR1_SPI_SELECT1_IO, //
  37. .resetPin = TMC_MOTOR1_nRESET_IO, //
  38. .fREEZEPin = TMC_MOTOR1_nFREEZE_IO, //
  39. .ennPin = PinNull, //
  40. .driverIC_ennPin = TMC_MOTOR1_SUB_IC_ENN_IO, //
  41. .driverIC_resetPin = PinNull, //
  42. };
  43. motora.initialize(&cfg);
  44. motora.setMotorShaft(false);
  45. ZLOGI(TAG, "motora initialize TMC4361A:%x DriverIC:%x", motora.readICVersion(), motora.readSubICVersion());
  46. }
  47. {
  48. TMC4361A::cfg_t cfg = {
  49. .spi = &TMC_MOTOR_SPI, //
  50. .csgpio = TMC_MOTOR2_SPI_SELECT1_IO, //
  51. .resetPin = TMC_MOTOR2_nRESET_IO, //
  52. .fREEZEPin = TMC_MOTOR2_nFREEZE_IO, //
  53. .ennPin = PinNull, //
  54. .driverIC_ennPin = TMC_MOTOR2_SUB_IC_ENN_IO, //
  55. .driverIC_resetPin = PinNull, //
  56. };
  57. motorb.initialize(&cfg);
  58. motorb.setMotorShaft(false);
  59. ZLOGI(TAG, "motorb initialize TMC4361A:%x DriverIC:%x", motorb.readICVersion(), motorb.readSubICVersion());
  60. }
  61. motora.setAcceleration(300000);
  62. motora.setDeceleration(300000);
  63. motora.setIHOLD_IRUN(0, 2, 10);
  64. motorb.setAcceleration(300000);
  65. motorb.setDeceleration(300000);
  66. motorb.setIHOLD_IRUN(0, 2, 10);
  67. motora.rotate(0);
  68. motorb.rotate(0);
  69. auto zcanCmder_cfg = zcanCmder.createCFG(DEVICE_ID);
  70. zcanCmder.init(zcanCmder_cfg);
  71. /*******************************************************************************
  72. * zcanBasicOrderModule *
  73. *******************************************************************************/
  74. zcanBasicOrderModule.initialize(&zcanCmder);
  75. zcanBasicOrderModule.reg_set_io(1, [](bool val) { ZLOGI(TAG, "write io 1:%d", val); });
  76. zcanBasicOrderModule.reg_read_io(1, []() {
  77. ZLOGI(TAG, "read io 1");
  78. return 1;
  79. });
  80. zcanBasicOrderModule.reg_read_adc(1, []() {
  81. ZLOGI(TAG, "read adc 1");
  82. return 123;
  83. });
  84. /*******************************************************************************
  85. * zcanXYRobotCtrlModule *
  86. *******************************************************************************/
  87. ZGPIO input[8];
  88. input[0].initAsInput(ARM_SENSOR1_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  89. input[1].initAsInput(ARM_SENSOR2_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  90. input[2].initAsInput(ARM_SENSOR3_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  91. input[3].initAsInput(ARM_SENSOR4_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  92. input[4].initAsInput(ARM_SENSOR5_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  93. input[5].initAsInput(ARM_SENSOR6_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  94. input[6].initAsInput(ARM_SENSOR7_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  95. input[7].initAsInput(ARM_SENSOR8_GPIO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false);
  96. {
  97. xyRobotCtrlModule.initialize(&motora, &motorb, &input[6], &input[7], 1.0f);
  98. XYRobotCtrlModule::run_param_t param;
  99. xyRobotCtrlModule.read_run_param(param);
  100. param.robot_type = XYRobotCtrlModule::corexy;
  101. param.distance_scale = 1000; // = 1.0
  102. param.x_shaft = false;
  103. param.y_shaft = false;
  104. param.ihold = 1;
  105. param.irun = 3;
  106. param.maxspeed = 1000000;
  107. param.acc = 3000000;
  108. param.dec = 3000000;
  109. xyRobotCtrlModule.set_run_param(param);
  110. XYRobotCtrlModule::run_to_zero_param_t run_to_zero_param;
  111. xyRobotCtrlModule.read_run_to_zero_param(run_to_zero_param);
  112. run_to_zero_param.move_to_zero_max_d = 5120000;
  113. run_to_zero_param.leave_from_zero_max_d = 5120000;
  114. run_to_zero_param.speed = 30000;
  115. run_to_zero_param.dec = 600000;
  116. xyRobotCtrlModule.set_run_to_zero_param(run_to_zero_param);
  117. zcanXYRobotCtrlModule.initialize(&zcanCmder, 1, &xyRobotCtrlModule);
  118. }
  119. while (true) {
  120. OSDefaultSchduler::getInstance()->loop();
  121. zcanCmder.loop();
  122. // zcanCmder.sendPacket(data, 4);
  123. #if 0
  124. osDelay(100);
  125. ZLOGI(TAG, "input:%d %d %d %d %d %d %d %d", //
  126. input[0].getState(), input[1].getState(), input[2].getState(), input[3].getState(), input[4].getState(), input[5].getState(), input[6].getState(), input[7].getState());
  127. #endif
  128. }
  129. }