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

352 lines
11 KiB

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