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.

162 lines
6.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
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
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/step_motor_ctrl_module/step_motor_ctrl_script_cmder_module.hpp"
  5. #include "sdk/components/step_motor_ctrl_module/zcan_master_step_motor_ctrl_module.hpp"
  6. #include "sdk/components/step_motor_ctrl_module/zcan_step_motor_ctrl_module.hpp"
  7. #include "sdk/os/zos.hpp"
  8. #include "sdk\components\flash\zsimple_flash.hpp"
  9. #include "sdk\components\mini_servo_motor\feite_servo_motor.hpp"
  10. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  11. #include "sdk\components\zcancmder\zcanreceiver.hpp"
  12. #include "sdk\components\zcancmder_module\zcan_basic_order_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. static TMC5130 g_motor;
  24. static ZCanCmder g_zcanCmder;
  25. static StepMotorCtrlModule g_stepMotorCtrlModule;
  26. static ZCanStepMotorCtrlModule g_zcanStepMotorCtrlModule;
  27. uint8_t getId() {
  28. static bool init = false;
  29. static ZGPIO ID0;
  30. static ZGPIO ID1;
  31. static ZGPIO ID2;
  32. static ZGPIO ID3;
  33. static ZGPIO ID4;
  34. if (!init) {
  35. ID0.initAsInput(ID0_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  36. ID1.initAsInput(ID1_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  37. ID2.initAsInput(ID2_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  38. ID3.initAsInput(ID3_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  39. ID4.initAsInput(ID4_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  40. init = true;
  41. }
  42. uint8_t id = ID0.getState() * 1 + ID1.getState() * 2 + ID2.getState() * 4 + ID3.getState() * 8 + ID4.getState() * 16;
  43. return id;
  44. }
  45. void umain() {
  46. chip_cfg_t chipcfg;
  47. chipcfg.us_dleay_tim = &DELAY_US_TIMER;
  48. chipcfg.tim_irq_scheduler_tim = &TIM_IRQ_SCHEDULER_TIMER;
  49. chipcfg.huart = &DEBUG_UART;
  50. chipcfg.debuglight = DEBUG_LIGHT_GPIO;
  51. chip_init(&chipcfg);
  52. zos_cfg_t zoscfg;
  53. zos_init(&zoscfg);
  54. uint8_t deviceId = getId();
  55. ZLOGI(TAG, "motorId:%d boardId", deviceId, deviceId + ZCAN_CMD_PUBLIC_DEVICE_ID_STEP_MOTOR_BOARD_OFFEST);
  56. if (deviceId == 0) {
  57. chip_set_error();
  58. while (true) {
  59. osDelay(1000);
  60. }
  61. }
  62. /*******************************************************************************
  63. * NVSINIT *
  64. *******************************************************************************/
  65. ZNVS::ins().initialize(IFLYTOP_NVS_CONFIG_FLASH_SECTOR);
  66. {
  67. static I_StepMotorCtrlModule::flash_config_t cfg;
  68. StepMotorCtrlModule::create_default_cfg(cfg);
  69. ZNVS::ins().alloc_config(MOTOR_CFG_FLASH_MARK, (uint8_t*)&cfg, sizeof(cfg));
  70. }
  71. ZNVS::ins().init_config();
  72. // Pin_t enn_pin;
  73. // Pin_t csn_pin;
  74. osDelay(1000);
  75. {
  76. TMC5130::cfg_t cfg = {
  77. .spi = &TMC_MOTOR_SPI, //
  78. .csgpio = MOTOR0_CSN, //
  79. .ennPin = MOTOR0_ENN, //
  80. .spi_mode_select = MOTOR1_SPI_MODE_SELECT, //
  81. };
  82. g_motor.initialize(&cfg);
  83. g_motor.setMotorShaft(false);
  84. ZLOGI(TAG, "motora initialize 5160:%x ", g_motor.readICVersion());
  85. }
  86. g_motor.setAcceleration(300000);
  87. g_motor.setDeceleration(300000);
  88. g_motor.setIHOLD_IRUN(0, 8, 10);
  89. g_motor.rotate(0);
  90. // g_motor.enable(false);
  91. auto zcanCmder_cfg = g_zcanCmder.createCFG(deviceId + ZCAN_CMD_PUBLIC_DEVICE_ID_STEP_MOTOR_BOARD_OFFEST);
  92. g_zcanCmder.init(zcanCmder_cfg);
  93. /*******************************************************************************
  94. * zcanBasicOrderModule *
  95. *******************************************************************************/
  96. #if 0
  97. zcanBasicOrderModule.initialize(&zcanCmder);
  98. zcanBasicOrderModule.reg_set_io(1, [](bool val) { ZLOGI(TAG, "write io 1:%d", val); });
  99. zcanBasicOrderModule.reg_read_io(1, []() {
  100. ZLOGI(TAG, "read io 1");
  101. return 1;
  102. });
  103. zcanBasicOrderModule.reg_read_adc(1, []() {
  104. ZLOGI(TAG, "read adc 1");
  105. return 123;
  106. });
  107. #endif
  108. /*******************************************************************************
  109. * zcanXYRobotCtrlModule *
  110. *******************************************************************************/
  111. static ZGPIO input[10];
  112. input[0].initAsInput(SENSOR_INT0, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  113. input[1].initAsInput(SENSOR_INT1, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  114. input[2].initAsInput(SENSOR_INT2, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  115. input[3].initAsInput(SENSOR_INT3, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  116. input[4].initAsInput(SENSOR_INT4, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  117. input[5].initAsInput(SENSOR_INT5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  118. input[6].initAsInput(SENSOR_INT6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  119. input[7].initAsInput(SENSOR_INT7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  120. input[8].initAsInput(SENSOR_INT8, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  121. input[9].initAsInput(SENSOR_INT9, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  122. OSDefaultSchduler::getInstance()->regPeriodJob(
  123. [](OSDefaultSchduler::Context& context) {
  124. #if 0
  125. ZLOGI(TAG, "[0]:%d [1]:%d [2]:%d [3]:%d [4]:%d [5]:%d [6]:%d [7]:%d [8]:%d [9]:%d", //
  126. input[0].getState(), //
  127. input[1].getState(), //
  128. input[2].getState(), //
  129. input[3].getState(), //
  130. input[4].getState(), //
  131. input[5].getState(), //
  132. input[6].getState(), //
  133. input[7].getState(), //
  134. input[8].getState(), //
  135. input[9].getState());
  136. #endif
  137. },
  138. 1000);
  139. g_stepMotorCtrlModule.initialize(deviceId, &g_motor, input, ZARRAY_SIZE(input), "MOTOR_CFG_FLASH_MARK");
  140. g_zcanStepMotorCtrlModule.initialize(&g_zcanCmder, 1, &g_stepMotorCtrlModule);
  141. while (true) {
  142. OSDefaultSchduler::getInstance()->loop();
  143. g_zcanCmder.loop();
  144. }
  145. }