正点原子开发板 alientek_develop_board cancmder
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.

356 lines
11 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
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include "main.hpp"
  4. //
  5. #include "feite_servo_motor.hpp"
  6. #include "sdk/os/zos.hpp"
  7. #include "sdk\components\cmdscheduler\cmd_scheduler.hpp"
  8. #include "sdk\components\eq_20_asb_motor\eq20_servomotor.hpp"
  9. #include "sdk\components\iflytop_can_slave_module_master_end\stepmotor.hpp"
  10. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_master.hpp"
  11. #include "sdk\components\step_motor_45\step_motor_45.hpp"
  12. #include "sdk\components\step_motor_45\step_motor_45_scheduler.hpp"
  13. #define TAG "main"
  14. namespace iflytop {
  15. using namespace std;
  16. };
  17. using namespace iflytop;
  18. #define CHECK_ARGC(n) \
  19. if (argc != (n + 1)) { \
  20. ZLOGE(TAG, "argc != %d", n); \
  21. context->breakflag = true; \
  22. return; \
  23. }
  24. static map<int, float> screw_lead = {
  25. {1, 10.0}, //
  26. {2, 10.0}, //
  27. {3, 10.0}, //
  28. {4, 10.0}, //
  29. {5, 10.0}, //
  30. {6, 10.0}, //
  31. };
  32. /*******************************************************************************
  33. * *
  34. *******************************************************************************/
  35. namespace iflytop {
  36. extern StepMotor45 g_step_motor45[7];
  37. extern StepMotor g_step_motor[10];
  38. extern IflytopCanMaster m_IflytopCanMaster;
  39. extern CmdScheduler cmdScheduler;
  40. extern ModbusBlockHost g_modbusblockhost;
  41. extern Eq20ServoMotor g_eq20servomotor;
  42. extern FeiTeServoMotor g_feiteservomotor;
  43. } // namespace iflytop
  44. #if 0
  45. static bool distance_mm_to_step(int motorid, float distance_mm, int32_t* distance) {
  46. if (screw_lead.find(motorid) == screw_lead.end()) {
  47. return false;
  48. }
  49. float lead = screw_lead[motorid];
  50. *distance = distance_mm / lead * 51200;
  51. return true;
  52. }
  53. #endif
  54. void step_motor_cmd_reg() {
  55. #if 0
  56. cmdScheduler.registerCmd("help", //
  57. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { ZLOGI(TAG, "do_help"); });
  58. cmdScheduler.registerCmd("reset_board", //
  59. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { NVIC_SystemReset(); });
  60. cmdScheduler.registerCmd("stepmotor45_wait_to_reach_pos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  61. // stepmotor45_rotate motorid direction
  62. CHECK_ARGC(2);
  63. int motorid = atoi(argv[1]);
  64. if (motorid < 1 || motorid > 6) {
  65. ZLOGE(TAG, "motorid out of range");
  66. return;
  67. }
  68. while (true) {
  69. if (g_step_motor45[motorid].is_reach_target_pos()) {
  70. break;
  71. }
  72. ZLOGI(TAG, "stepmotor45_wait_to_reach_pos %d", motorid);
  73. osDelay(100);
  74. }
  75. });
  76. cmdScheduler.registerCmd("stepmotor45_reset_pos_all", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  77. // stepmotor45_rotate motorid direction
  78. CHECK_ARGC(0);
  79. g_step_motor45[1].setPos(0);
  80. g_step_motor45[2].setPos(0);
  81. g_step_motor45[3].setPos(0);
  82. g_step_motor45[4].setPos(0);
  83. g_step_motor45[5].setPos(0);
  84. g_step_motor45[6].setPos(0);
  85. });
  86. // stepmotor45
  87. cmdScheduler.registerCmd("stepmotor45_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  88. // stepmotor45_rotate motorid direction
  89. CHECK_ARGC(2);
  90. int motorid = atoi(argv[1]);
  91. int direction = atoi(argv[2]);
  92. if (motorid < 1 || motorid > 6) {
  93. ZLOGE(TAG, "motorid out of range");
  94. return;
  95. }
  96. g_step_motor45[motorid].rotate(direction);
  97. });
  98. cmdScheduler.registerCmd("stepmotor45_readPos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  99. // stepmotor45_rotate motorid direction
  100. CHECK_ARGC(2);
  101. int motorid = atoi(argv[1]);
  102. if (motorid < 1 || motorid > 6) {
  103. ZLOGE(TAG, "motorid out of range");
  104. return;
  105. }
  106. ZLOGI(TAG, "motorid %d pos %d", motorid, g_step_motor45[motorid].getPos());
  107. });
  108. cmdScheduler.registerCmd("stepmotor45_moveTo", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  109. CHECK_ARGC(2);
  110. int motorid = atoi(argv[1]);
  111. int pos = atoi(argv[2]);
  112. if (motorid < 1 || motorid > 6) {
  113. ZLOGE(TAG, "motorid out of range");
  114. return;
  115. }
  116. g_step_motor45[motorid].moveTo(pos);
  117. });
  118. cmdScheduler.registerCmd("stepmotor45_moveBy", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  119. CHECK_ARGC(2);
  120. int motorid = atoi(argv[1]);
  121. int pos = atoi(argv[2]);
  122. if (motorid < 1 || motorid > 6) {
  123. ZLOGE(TAG, "motorid out of range");
  124. return;
  125. }
  126. g_step_motor45[motorid].moveBy(pos);
  127. });
  128. cmdScheduler.registerCmd("stepmotor45_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  129. CHECK_ARGC(1);
  130. int motorid = atoi(argv[1]);
  131. if (motorid < 1 || motorid > 6) {
  132. ZLOGE(TAG, "motorid out of range");
  133. return;
  134. }
  135. g_step_motor45[motorid].stop();
  136. });
  137. cmdScheduler.registerCmd("stepmotor45_setSpeed", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  138. CHECK_ARGC(1);
  139. int motorid = atoi(argv[1]);
  140. int speed = atoi(argv[2]);
  141. if (motorid < 1 || motorid > 6) {
  142. ZLOGE(TAG, "motorid out of range");
  143. return;
  144. }
  145. g_step_motor45[motorid].setDefaultSpeed(speed);
  146. });
  147. /*******************************************************************************
  148. * *
  149. *******************************************************************************/
  150. #define GET_MOTOR(motor) \
  151. { \
  152. int motorid = atoi(argv[1]); \
  153. motor = &g_step_motor[motorid]; \
  154. if (motorid >= 6 || motorid < 1) { \
  155. ZLOGE(TAG, "motor %d not found", motorid); \
  156. context->breakflag = true; \
  157. return; \
  158. } \
  159. }
  160. /**
  161. * @brief STEPMOTOR
  162. */
  163. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  164. CHECK_ARGC(2);
  165. StepMotor* motor = NULL;
  166. GET_MOTOR(motor);
  167. motor->setVelocity(atoi(argv[2]));
  168. });
  169. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  170. CHECK_ARGC(2);
  171. StepMotor* motor = NULL;
  172. GET_MOTOR(motor);
  173. motor->setAcc(atoi(argv[2]));
  174. });
  175. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  176. CHECK_ARGC(2);
  177. StepMotor* motor = NULL;
  178. GET_MOTOR(motor);
  179. motor->setDec(atoi(argv[2]));
  180. });
  181. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  182. CHECK_ARGC(2);
  183. StepMotor* motor = NULL;
  184. GET_MOTOR(motor);
  185. motor->moveTo(atoi(argv[2]));
  186. });
  187. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  188. CHECK_ARGC(2);
  189. StepMotor* motor = NULL;
  190. GET_MOTOR(motor);
  191. motor->moveBy(atoi(argv[2]));
  192. });
  193. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  194. CHECK_ARGC(2);
  195. StepMotor* motor = NULL;
  196. GET_MOTOR(motor);
  197. motor->rotate(atoi(argv[2]));
  198. });
  199. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  200. CHECK_ARGC(1);
  201. StepMotor* motor = NULL;
  202. GET_MOTOR(motor);
  203. motor->moveToZero();
  204. });
  205. cmdScheduler.registerCmd("step_motor_readpos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  206. CHECK_ARGC(1);
  207. StepMotor* motor = NULL;
  208. GET_MOTOR(motor);
  209. int32_t pos = motor->readpos();
  210. ZLOGI(TAG, "step_motor_readpos %d %d", atoi(argv[1]), pos);
  211. });
  212. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  213. CHECK_ARGC(1);
  214. StepMotor* motor = NULL;
  215. GET_MOTOR(motor);
  216. HAL_Delay(100);
  217. while (!motor->isIdleState()) {
  218. HAL_Delay(300);
  219. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  220. }
  221. });
  222. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  223. CHECK_ARGC(1);
  224. StepMotor* motor = NULL;
  225. GET_MOTOR(motor);
  226. motor->clearException();
  227. });
  228. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  229. CHECK_ARGC(2);
  230. StepMotor* motor = NULL;
  231. GET_MOTOR(motor);
  232. int32_t step;
  233. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  234. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  235. context->breakflag = true;
  236. return;
  237. }
  238. motor->moveTo(step);
  239. });
  240. static int main_servo_motor_velocity = 400;
  241. cmdScheduler.registerCmd("main_servo_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  242. // main_servo_motor_rotate direction
  243. CHECK_ARGC(1);
  244. int direction = atoi(argv[1]);
  245. if (direction == 0) {
  246. // ����תλ��0��ֹͣ�豸
  247. g_eq20servomotor.writePn(1, 614, 1);
  248. g_eq20servomotor.writePn(1, 1501, 0);
  249. g_eq20servomotor.writePn(1, 1501, 1);
  250. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  251. } else if (direction > 0) {
  252. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  253. g_eq20servomotor.writePn(1, 614, -1);
  254. g_eq20servomotor.writePn(1, 1501, 0);
  255. g_eq20servomotor.writePn(1, 1501, 1);
  256. g_eq20servomotor.writePn(1, 614, 999999);
  257. g_eq20servomotor.writePn(1, 1501, 0);
  258. g_eq20servomotor.writePn(1, 1501, 1);
  259. } else if (direction < 0) {
  260. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  261. g_eq20servomotor.writePn(1, 614, 1);
  262. g_eq20servomotor.writePn(1, 1501, 0);
  263. g_eq20servomotor.writePn(1, 1501, 1);
  264. g_eq20servomotor.writePn(1, 614, -999999);
  265. g_eq20servomotor.writePn(1, 1501, 0);
  266. g_eq20servomotor.writePn(1, 1501, 1);
  267. }
  268. });
  269. cmdScheduler.registerCmd("main_servo_motor_set_velocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  270. CHECK_ARGC(1);
  271. main_servo_motor_velocity = atoi(argv[1]);
  272. return;
  273. });
  274. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  275. // main_servo_motor_move_to v
  276. CHECK_ARGC(0);
  277. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  278. g_eq20servomotor.writePn(1, 614, 0);
  279. g_eq20servomotor.writePn(1, 1501, 0);
  280. g_eq20servomotor.writePn(1, 1501, 1);
  281. });
  282. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  283. // main_servo_motor_move_to v
  284. CHECK_ARGC(0);
  285. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  286. g_eq20servomotor.writePn(1, 614, 0);
  287. g_eq20servomotor.writePn(1, 1501, 0);
  288. g_eq20servomotor.writePn(1, 1501, 1);
  289. });
  290. // setzero
  291. // set4095
  292. // moveto
  293. cmdScheduler.registerCmd("sleep_ms", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  294. CHECK_ARGC(1);
  295. osDelay(atoi(argv[1]));
  296. });
  297. #endif
  298. }