TMC5160电机驱动单机版本
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.

203 lines
7.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
  1. #include "main.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. //
  5. #include "sdk/os/zos.hpp"
  6. #include "sdk\components\cmdscheduler\cmd_scheduler.hpp"
  7. #include "sdk\components\eq_20_asb_motor\eq20_servomotor.hpp"
  8. #include "sdk\components\iflytop_can_slave_module_master_end\stepmotor.hpp"
  9. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_master.hpp"
  10. #include "sdk\components\step_motor_45\step_motor_45.hpp"
  11. #include "sdk\components\step_motor_45\step_motor_45_scheduler.hpp"
  12. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  13. #define TAG "main"
  14. namespace iflytop {
  15. Main gmain;
  16. };
  17. using namespace iflytop;
  18. using namespace std;
  19. #define CHECK_ARGC(n) \
  20. if (argc != (n + 1)) { \
  21. ZLOGE(TAG, "argc != %d", n); \
  22. context->breakflag = true; \
  23. return; \
  24. }
  25. extern "C" {
  26. void StartDefaultTask(void const* argument) { umain(); }
  27. }
  28. /*******************************************************************************
  29. * *
  30. *******************************************************************************/
  31. static chip_cfg_t chipcfg = {
  32. .us_dleay_tim = &DELAY_US_TIMER,
  33. .tim_irq_scheduler_tim = &TIM_IRQ_SCHEDULER_TIMER,
  34. .huart = &DEBUG_UART,
  35. .debuglight = DEBUG_LIGHT_GPIO,
  36. };
  37. TMC5130 m_motor1;
  38. static int32_t m_velocity;
  39. /*******************************************************************************
  40. * *
  41. *******************************************************************************/
  42. static StepMotor45 g_step_motor45[7];
  43. static CmdScheduler cmdScheduler;
  44. void regfn() {
  45. cmdScheduler.registerCmd("help", //
  46. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { ZLOGI(TAG, "do_help"); });
  47. cmdScheduler.registerCmd("reset_board", //
  48. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { NVIC_SystemReset(); });
  49. cmdScheduler.registerCmd("sleep_ms", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  50. CHECK_ARGC(1);
  51. osDelay(atoi(argv[1]));
  52. });
  53. /*******************************************************************************
  54. * *
  55. *******************************************************************************/
  56. #define GET_MOTOR(motor) \
  57. { \
  58. int motorid = atoi(argv[1]); \
  59. motor = &g_step_motor[motorid]; \
  60. if (motorid >= 6 || motorid < 1) { \
  61. ZLOGE(TAG, "motor %d not found", motorid); \
  62. context->breakflag = true; \
  63. return; \
  64. } \
  65. }
  66. /**
  67. * @brief STEPMOTOR
  68. */
  69. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  70. CHECK_ARGC(2);
  71. int motorid = atoi(argv[1]);
  72. m_velocity = atoi(argv[2]);
  73. });
  74. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  75. CHECK_ARGC(2);
  76. m_motor1.setAcceleration(atoi(argv[2]));
  77. });
  78. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  79. CHECK_ARGC(2);
  80. m_motor1.setDeceleration(atoi(argv[2]));
  81. });
  82. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  83. CHECK_ARGC(2);
  84. ZLOGI(TAG, "step_motor_moveto %d %d", atoi(argv[2]), m_velocity);
  85. m_motor1.moveTo(atoi(argv[2]), m_velocity);
  86. });
  87. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  88. CHECK_ARGC(2);
  89. ZLOGI(TAG, "step_motor_moveby %d %d", atoi(argv[2]), m_velocity);
  90. m_motor1.moveBy(atoi(argv[2]), m_velocity);
  91. });
  92. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  93. CHECK_ARGC(2);
  94. ZLOGI(TAG, "step_motor_rotate %d %d", atoi(argv[2]), m_velocity);
  95. if (atoi(argv[2]) > 0) {
  96. m_motor1.rotate(m_velocity);
  97. } else if (atoi(argv[2]) == 0) {
  98. m_motor1.stop();
  99. } else {
  100. m_motor1.rotate(-m_velocity);
  101. }
  102. });
  103. #if 0
  104. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  105. CHECK_ARGC(1);
  106. StepMotor* motor = NULL;
  107. GET_MOTOR(motor);
  108. motor->moveToZero();
  109. });
  110. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  111. CHECK_ARGC(1);
  112. StepMotor* motor = NULL;
  113. GET_MOTOR(motor);
  114. HAL_Delay(100);
  115. while (!motor->isIdleState()) {
  116. HAL_Delay(300);
  117. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  118. }
  119. });
  120. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  121. CHECK_ARGC(1);
  122. StepMotor* motor = NULL;
  123. GET_MOTOR(motor);
  124. motor->clearException();
  125. });
  126. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  127. CHECK_ARGC(2);
  128. StepMotor* motor = NULL;
  129. GET_MOTOR(motor);
  130. int32_t step;
  131. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  132. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  133. context->breakflag = true;
  134. return;
  135. }
  136. motor->moveTo(step);
  137. });
  138. #endif
  139. }
  140. #define MOTOR_SPI hspi1
  141. #define MOTOR0_CSN PA4
  142. #define MOTOR0_ENN PB7
  143. #define MOTOR1_SPI_MODE_SELECT PB4
  144. // TMC5130HImpl m_tmc5130motor;
  145. void Main::run() {
  146. /*******************************************************************************
  147. * ϵͳʼ *
  148. *******************************************************************************/
  149. chip_init(&chipcfg);
  150. zos_cfg_t zoscfg;
  151. zos_init(&zoscfg);
  152. // MOTOR1_SPI_MODE_SELECT.initAsOutput(false); // ѡ��SPIģʽ����SDģʽ��Ĭ�ϸ�false���ɡ�
  153. // m_tmc5130motor.initialize("motor1", this, &MOTOR_SPI, &MOTOR0_CSN, &MOTOR0_ENN);
  154. {
  155. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR0_ENN, .csn_pin = MOTOR0_CSN};
  156. m_motor1.initialize(&cfg);
  157. int32_t chipv = m_motor1.readChipVERSION();
  158. ZLOGI(TAG, "m_motor1:%lx", chipv);
  159. m_motor1.setIHOLD_IRUN(1, 20, 0);
  160. m_motor1.setMotorShaft(true);
  161. m_motor1.setIHOLD_IRUN(MOTOR_IHOLD, MOTOR_IRUN, 0);
  162. m_motor1.setAcceleration(CFG_ACC);
  163. m_motor1.setDeceleration(CFG_DEC);
  164. m_velocity = CFG_VELOCITY;
  165. }
  166. int i = 1;
  167. cmdScheduler.initialize(&DEBUG_UART, 1000);
  168. regfn();
  169. while (true) {
  170. OSDefaultSchduler::getInstance()->loop();
  171. cmdScheduler.schedule();
  172. // m_IflytopCanMaster.writeReg(1,1,1,10);
  173. // osDelay(1);
  174. }
  175. }