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.

145 lines
6.0 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
  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/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\ztmc5130.hpp"
  9. #include "sdk\components\zcancmder\zcanreceiver.hpp"
  10. #include "sdk\components\zcancmder_module\zcan_basic_order_module.hpp"
  11. #include "sdk\components\zprotocols\zcancmder_v2\protocol_parser.hpp"
  12. //
  13. #include "sdk\components\flash\znvs.hpp"
  14. //
  15. #define TAG "main"
  16. using namespace iflytop;
  17. using namespace std;
  18. extern void umain();
  19. extern "C" {
  20. void StartDefaultTask(void const* argument) { umain(); }
  21. }
  22. static TMC5130 g_motor;
  23. static ZCanCmder g_zcanCmder;
  24. static StepMotorCtrlModule g_stepMotorCtrlModule;
  25. static ZIProtocolParser g_ziProtocolParser;
  26. uint8_t getId() {
  27. static bool init = false;
  28. static ZGPIO ID0;
  29. static ZGPIO ID1;
  30. static ZGPIO ID2;
  31. static ZGPIO ID3;
  32. static ZGPIO ID4;
  33. if (!init) {
  34. ID0.initAsInput(ID0_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  35. ID1.initAsInput(ID1_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  36. ID2.initAsInput(ID2_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  37. ID3.initAsInput(ID3_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  38. ID4.initAsInput(ID4_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  39. init = true;
  40. }
  41. uint8_t id = ID0.getState() * 1 + ID1.getState() * 2 + ID2.getState() * 4 + ID3.getState() * 8 + ID4.getState() * 16;
  42. return id;
  43. }
  44. void umain() {
  45. chip_cfg_t chipcfg;
  46. chipcfg.us_dleay_tim = &DELAY_US_TIMER;
  47. chipcfg.tim_irq_scheduler_tim = &TIM_IRQ_SCHEDULER_TIMER;
  48. chipcfg.huart = &DEBUG_UART;
  49. chipcfg.debuglight = DEBUG_LIGHT_GPIO;
  50. chip_init(&chipcfg);
  51. zos_cfg_t zoscfg;
  52. zos_init(&zoscfg);
  53. uint8_t deviceId = getId();
  54. ZLOGI(TAG, "motorId:%d", deviceId);
  55. /*******************************************************************************
  56. * NVSINIT *
  57. *******************************************************************************/
  58. ZNVS::ins().initialize(IFLYTOP_NVS_CONFIG_FLASH_SECTOR);
  59. {
  60. static I_StepMotorCtrlModule::flash_config_t cfg;
  61. StepMotorCtrlModule::create_default_cfg(cfg);
  62. ZNVS::ins().alloc_config(MOTOR_CFG_FLASH_MARK, (uint8_t*)&cfg, sizeof(cfg));
  63. }
  64. ZNVS::ins().init_config();
  65. // Pin_t enn_pin;
  66. // Pin_t csn_pin;
  67. osDelay(1000);
  68. {
  69. TMC5130::cfg_t cfg = {
  70. .spi = &TMC_MOTOR_SPI, //
  71. .csgpio = MOTOR0_CSN, //
  72. .ennPin = MOTOR0_ENN, //
  73. .spi_mode_select = MOTOR1_SPI_MODE_SELECT, //
  74. };
  75. g_motor.initialize(&cfg);
  76. g_motor.setMotorShaft(false);
  77. ZLOGI(TAG, "motora initialize 5160:%x ", g_motor.readICVersion());
  78. }
  79. g_motor.setAcceleration(100);
  80. g_motor.setDeceleration(100);
  81. g_motor.setIHOLD_IRUN(0, 8, 10);
  82. g_motor.rotate(0);
  83. // g_motor.enable(false);
  84. auto zcanCmder_cfg = g_zcanCmder.createCFG( deviceId);
  85. g_zcanCmder.init(zcanCmder_cfg);
  86. /*******************************************************************************
  87. * zcanBasicOrderModule *
  88. *******************************************************************************/
  89. /*******************************************************************************
  90. * zcanXYRobotCtrlModule *
  91. *******************************************************************************/
  92. static ZGPIO input[10];
  93. input[0].initAsInput(SENSOR_INT0, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  94. input[1].initAsInput(SENSOR_INT1, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  95. input[2].initAsInput(SENSOR_INT2, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  96. input[3].initAsInput(SENSOR_INT3, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  97. input[4].initAsInput(SENSOR_INT4, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  98. input[5].initAsInput(SENSOR_INT5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  99. input[6].initAsInput(SENSOR_INT6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  100. input[7].initAsInput(SENSOR_INT7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  101. input[8].initAsInput(SENSOR_INT8, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  102. input[9].initAsInput(SENSOR_INT9, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  103. OSDefaultSchduler::getInstance()->regPeriodJob(
  104. [](OSDefaultSchduler::Context& context) {
  105. #if 0
  106. ZLOGI(TAG, "[0]:%d [1]:%d [2]:%d [3]:%d [4]:%d [5]:%d [6]:%d [7]:%d [8]:%d [9]:%d", //
  107. input[0].getState(), //
  108. input[1].getState(), //
  109. input[2].getState(), //
  110. input[3].getState(), //
  111. input[4].getState(), //
  112. input[5].getState(), //
  113. input[6].getState(), //
  114. input[7].getState(), //
  115. input[8].getState(), //
  116. input[9].getState());
  117. #endif
  118. },
  119. 1000);
  120. g_stepMotorCtrlModule.initialize(deviceId, &g_motor, input, ZARRAY_SIZE(input), "MOTOR_CFG_FLASH_MARK");
  121. g_ziProtocolParser.initialize(&g_zcanCmder);
  122. g_ziProtocolParser.registerModule(&g_stepMotorCtrlModule);
  123. while (true) {
  124. OSDefaultSchduler::getInstance()->loop();
  125. g_zcanCmder.loop();
  126. }
  127. }