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

354 lines
11 KiB

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