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.4 KiB

2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year 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 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year 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 <stdlib.h>
  4. #include <string.h>
  5. #include "board.h"
  6. #include "sdk\components\subcanmodule\zcancmder_subboard_initer.hpp"
  7. /*******************************************************************************
  8. * PROJECT_INCLUDE *
  9. *******************************************************************************/
  10. #include "sdk/components/step_motor_ctrl_module/step_motor_ctrl_module.hpp"
  11. #include "sdk\components\mini_servo_motor\feite_servo_motor.hpp"
  12. #include "sdk\components\mini_servo_motor\mini_servo_motor_ctrl_module.hpp"
  13. #include "sdk\components\step_motor_ctrl_module\tmc51x0_motor.hpp"
  14. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  15. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  16. #define TAG "main"
  17. using namespace iflytop;
  18. using namespace std;
  19. static ZCancmderSubboardIniter initer;
  20. extern void umain();
  21. extern "C" {
  22. void StartDefaultTask(void const* argument) { umain(); }
  23. extern DMA_HandleTypeDef hdma_usart3_rx;
  24. extern DMA_HandleTypeDef hdma_usart3_tx;
  25. }
  26. /*******************************************************************************
  27. * GET_DEVICE_ID *
  28. *******************************************************************************/
  29. static int32_t getDeviceId() {
  30. static bool init = false;
  31. static ZGPIO ID0;
  32. static ZGPIO ID1;
  33. static ZGPIO ID2;
  34. static ZGPIO ID3;
  35. static ZGPIO ID4;
  36. if (!init) {
  37. ID0.initAsInput(ID0_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  38. ID1.initAsInput(ID1_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  39. ID2.initAsInput(ID2_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  40. ID3.initAsInput(ID3_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  41. ID4.initAsInput(ID4_IO, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  42. init = true;
  43. }
  44. uint8_t id = ID0.getState() * 1 + ID1.getState() * 2 + ID2.getState() * 4 + ID3.getState() * 8 + ID4.getState() * 16;
  45. return id;
  46. }
  47. /*******************************************************************************
  48. * INIT_SUBMODULE *
  49. *******************************************************************************/
  50. void nvs_init_cb() {}
  51. static void initsubmodule() {
  52. osDelay(1000);
  53. {
  54. static TMC5130 g_motor;
  55. static TMC51X0Motor g_stepMotorCtrlModule;
  56. TMC5130::cfg_t cfg = {
  57. .spi = &TMC_MOTOR_SPI, //
  58. .csgpio = MOTOR0_CSN, //
  59. .ennPin = MOTOR0_ENN, //
  60. .spi_mode_select = MOTOR0_SPI_MODE_SELECT, //
  61. };
  62. g_motor.initialize(&cfg);
  63. ZLOGI(TAG, "motora initialize 5160:%x ", g_motor.readICVersion());
  64. g_motor.enable(false);
  65. g_motor.setMotorShaft(false);
  66. #if 0
  67. // g_motor.setMotorShaft(true);
  68. // void writeInt(uint8_t address, int32_t value);
  69. // int32_t readInt(uint8_t address);
  70. int32_t enc_const_integral = 12;
  71. float enc_const_fractional = 0.8;
  72. int32_t setval = (enc_const_integral << 16) + enc_const_fractional * 10000;
  73. setval = -setval;
  74. ZLOGI(TAG, "setval:%d", setval);
  75. g_motor.writeInt(TMC5130_ENC_CONST, setval);
  76. g_motor.writeInt(TMC5130_ENCMODE, 0x1 << 10);
  77. g_motor.setIHOLD_IRUN(2, 6, 1000);
  78. g_motor.setENCVAL(0);
  79. g_motor.enable(true);
  80. osDelay(100);
  81. g_motor.setENCVAL(0);
  82. // g_motor.moveBy(51200, 100000);
  83. g_motor.moveBy(51200, 100000);
  84. while (true) {
  85. int32_t enc = g_motor.readInt(TMC5130_XENC);
  86. int32_t xactual = g_motor.readInt(TMC5130_XACTUAL);
  87. ZLOGI(TAG, "enc:%d %d %d", enc, xactual, enc - xactual);
  88. osDelay(1000);
  89. }
  90. #endif
  91. static ZGPIO input[10];
  92. input[0].initAsInput(MOTOR0_REFL /*REFL*/, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  93. input[1].initAsInput(MOTOR0_REFR /*REFR*/, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true);
  94. TMC51X0Motor::flash_config_t smcm_cfg = {0};
  95. TMC51X0Motor::create_default_cfg(smcm_cfg);
  96. smcm_cfg.base_param.motor_one_circle_pulse = 1000;
  97. smcm_cfg.base_param.motor_one_circle_pulse_denominator = 1;
  98. smcm_cfg.base_param.stepmotor_ihold = 1;
  99. smcm_cfg.base_param.stepmotor_irun = 1;
  100. smcm_cfg.base_param.motor_shaft = 0;
  101. smcm_cfg.base_param.motor_default_velocity = 1;
  102. smcm_cfg.base_param.motor_run_to_zero_speed = 0;
  103. smcm_cfg.base_param.max_d = 0;
  104. smcm_cfg.base_param.min_d = 0;
  105. smcm_cfg.base_param.motor_run_to_zero_max_d = 0;
  106. smcm_cfg.base_param.motor_run_to_zero_speed = 1;
  107. smcm_cfg.base_param.motor_run_to_zero_dec = 0;
  108. smcm_cfg.base_param.motor_look_zero_edge_max_d = 0;
  109. smcm_cfg.base_param.motor_look_zero_edge_speed = 1;
  110. smcm_cfg.base_param.motor_look_zero_edge_dec = 0;
  111. g_stepMotorCtrlModule.initialize(initer.get_module_id(1), &g_motor, input, ZARRAY_SIZE(input), nullptr, &smcm_cfg);
  112. g_stepMotorCtrlModule.enable(false);
  113. initer.register_module(&g_stepMotorCtrlModule);
  114. }
  115. {
  116. // 115200
  117. /*******************************************************************************
  118. * Թñж *
  119. *******************************************************************************/
  120. static FeiTeServoMotor feiteservomotor_bus; // ���ض�������
  121. static MiniRobotCtrlModule mini_servo;
  122. // ���ض�������
  123. ZASSERT(huart3.Init.BaudRate == 115200);
  124. feiteservomotor_bus.initialize(&huart3, &hdma_usart3_rx, &hdma_usart3_tx);
  125. static MiniRobotCtrlModule::flash_config_t cfg = {0};
  126. cfg.default_torque = 330;
  127. mini_servo.initialize(initer.get_module_id(2), &feiteservomotor_bus, 1, &cfg);
  128. initer.register_module(&mini_servo);
  129. }
  130. }
  131. /*******************************************************************************
  132. * MAIN *
  133. *******************************************************************************/
  134. void umain() {
  135. ZCancmderSubboardIniter::cfg_t cfg = //
  136. {
  137. .deviceId = getDeviceId(),
  138. .input_gpio = {},
  139. .output_gpio = {},
  140. };
  141. initer.init(&cfg);
  142. initsubmodule();
  143. initer.loop();
  144. }