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

527 lines
16 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
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
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
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
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 "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. 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. static StepMotor45::cfg_t cfg1 = {
  38. .max_pos = -1,
  39. .enable_zero_limit = false,
  40. .enable_max_pos_limit = false,
  41. .mirror = true,
  42. .zeroPin = PinNull,
  43. .zeroPinMirror = false,
  44. .driverPin = {PB15, PD11, PD12, PD13},
  45. .driverPinMirror = true,
  46. };
  47. static StepMotor45::cfg_t cfg2 = {
  48. .max_pos = -1,
  49. .enable_zero_limit = false,
  50. .enable_max_pos_limit = false,
  51. .mirror = true,
  52. .zeroPin = PinNull,
  53. .zeroPinMirror = false,
  54. .driverPin = {PG2, PG3, PG4, PG5},
  55. .driverPinMirror = true,
  56. };
  57. static StepMotor45::cfg_t cfg3 = {
  58. .max_pos = -1,
  59. .enable_zero_limit = false,
  60. .enable_max_pos_limit = false,
  61. .mirror = true,
  62. .zeroPin = PinNull,
  63. .zeroPinMirror = false,
  64. .driverPin = {PG6, PG7, PG8, PC6},
  65. .driverPinMirror = true,
  66. };
  67. static StepMotor45::cfg_t cfg4 = {
  68. .max_pos = -1,
  69. .enable_zero_limit = false,
  70. .enable_max_pos_limit = false,
  71. .mirror = true,
  72. .zeroPin = PinNull,
  73. .zeroPinMirror = false,
  74. .driverPin = {PE0, PE2, PE4, PE6},
  75. .driverPinMirror = true,
  76. };
  77. static StepMotor45::cfg_t cfg5 = {
  78. .max_pos = -1,
  79. .enable_zero_limit = false,
  80. .enable_max_pos_limit = false,
  81. .mirror = true,
  82. .zeroPin = PinNull,
  83. .zeroPinMirror = false,
  84. .driverPin = {PC13, PE5, PE3, PE1},
  85. .driverPinMirror = true,
  86. };
  87. static StepMotor45::cfg_t cfg6 = {
  88. .max_pos = -1,
  89. .enable_zero_limit = false,
  90. .enable_max_pos_limit = false,
  91. .mirror = true,
  92. .zeroPin = PinNull,
  93. .zeroPinMirror = false,
  94. .driverPin = {PC12, PD3, PD5, PD7},
  95. .driverPinMirror = true,
  96. };
  97. map<int, float> screw_lead = {
  98. {1, 10.0}, //
  99. {2, 10.0}, //
  100. {3, 10.0}, //
  101. {4, 10.0}, //
  102. {5, 10.0}, //
  103. {6, 10.0}, //
  104. };
  105. /*******************************************************************************
  106. * *
  107. *******************************************************************************/
  108. static StepMotor45 g_step_motor45[7];
  109. StepMotor g_step_motor[10];
  110. IflytopCanMaster m_IflytopCanMaster;
  111. static CmdScheduler cmdScheduler;
  112. ModbusBlockHost g_modbusblockhost;
  113. Eq20ServoMotor g_eq20servomotor;
  114. FeiTeServoMotor g_feiteservomotor;
  115. bool distance_mm_to_step(int motorid, float distance_mm, int32_t* distance) {
  116. if (screw_lead.find(motorid) == screw_lead.end()) {
  117. return false;
  118. }
  119. float lead = screw_lead[motorid];
  120. *distance = distance_mm / lead * 51200;
  121. return true;
  122. }
  123. void regfn() {
  124. cmdScheduler.registerCmd("help", //
  125. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { ZLOGI(TAG, "do_help"); });
  126. cmdScheduler.registerCmd("reset_board", //
  127. [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) { NVIC_SystemReset(); });
  128. cmdScheduler.registerCmd("stepmotor45_wait_to_reach_pos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  129. // stepmotor45_rotate motorid direction
  130. CHECK_ARGC(2);
  131. int motorid = atoi(argv[1]);
  132. if (motorid < 1 || motorid > 6) {
  133. ZLOGE(TAG, "motorid out of range");
  134. return;
  135. }
  136. while (true) {
  137. if (g_step_motor45[motorid].isReachTargetPos()) {
  138. break;
  139. }
  140. ZLOGI(TAG, "stepmotor45_wait_to_reach_pos %d", motorid);
  141. osDelay(100);
  142. }
  143. });
  144. cmdScheduler.registerCmd("stepmotor45_reset_pos_all", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  145. // stepmotor45_rotate motorid direction
  146. CHECK_ARGC(0);
  147. g_step_motor45[1].setPos(0);
  148. g_step_motor45[2].setPos(0);
  149. g_step_motor45[3].setPos(0);
  150. g_step_motor45[4].setPos(0);
  151. g_step_motor45[5].setPos(0);
  152. g_step_motor45[6].setPos(0);
  153. });
  154. // stepmotor45
  155. cmdScheduler.registerCmd("stepmotor45_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  156. // stepmotor45_rotate motorid direction
  157. CHECK_ARGC(2);
  158. int motorid = atoi(argv[1]);
  159. int direction = atoi(argv[2]);
  160. if (motorid < 1 || motorid > 6) {
  161. ZLOGE(TAG, "motorid out of range");
  162. return;
  163. }
  164. g_step_motor45[motorid].rotate(direction);
  165. });
  166. cmdScheduler.registerCmd("stepmotor45_readPos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  167. // stepmotor45_rotate motorid direction
  168. CHECK_ARGC(2);
  169. int motorid = atoi(argv[1]);
  170. if (motorid < 1 || motorid > 6) {
  171. ZLOGE(TAG, "motorid out of range");
  172. return;
  173. }
  174. ZLOGI(TAG, "motorid %d pos %d", motorid, g_step_motor45[motorid].getPos());
  175. });
  176. cmdScheduler.registerCmd("stepmotor45_moveTo", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  177. CHECK_ARGC(2);
  178. int motorid = atoi(argv[1]);
  179. int pos = atoi(argv[2]);
  180. if (motorid < 1 || motorid > 6) {
  181. ZLOGE(TAG, "motorid out of range");
  182. return;
  183. }
  184. g_step_motor45[motorid].moveTo(pos);
  185. });
  186. cmdScheduler.registerCmd("stepmotor45_moveBy", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  187. CHECK_ARGC(2);
  188. int motorid = atoi(argv[1]);
  189. int pos = atoi(argv[2]);
  190. if (motorid < 1 || motorid > 6) {
  191. ZLOGE(TAG, "motorid out of range");
  192. return;
  193. }
  194. g_step_motor45[motorid].moveBy(pos);
  195. });
  196. cmdScheduler.registerCmd("stepmotor45_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  197. CHECK_ARGC(1);
  198. int motorid = atoi(argv[1]);
  199. if (motorid < 1 || motorid > 6) {
  200. ZLOGE(TAG, "motorid out of range");
  201. return;
  202. }
  203. g_step_motor45[motorid].stop();
  204. });
  205. cmdScheduler.registerCmd("stepmotor45_setSpeed", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  206. CHECK_ARGC(1);
  207. int motorid = atoi(argv[1]);
  208. int speed = atoi(argv[2]);
  209. if (motorid < 1 || motorid > 6) {
  210. ZLOGE(TAG, "motorid out of range");
  211. return;
  212. }
  213. g_step_motor45[motorid].setDefaultSpeed(speed);
  214. });
  215. /*******************************************************************************
  216. * *
  217. *******************************************************************************/
  218. #define GET_MOTOR(motor) \
  219. { \
  220. int motorid = atoi(argv[1]); \
  221. motor = &g_step_motor[motorid]; \
  222. if (motorid >= 6 || motorid < 1) { \
  223. ZLOGE(TAG, "motor %d not found", motorid); \
  224. context->breakflag = true; \
  225. return; \
  226. } \
  227. }
  228. /**
  229. * @brief STEPMOTOR
  230. */
  231. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  232. CHECK_ARGC(2);
  233. StepMotor* motor = NULL;
  234. GET_MOTOR(motor);
  235. motor->setVelocity(atoi(argv[2]));
  236. });
  237. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  238. CHECK_ARGC(2);
  239. StepMotor* motor = NULL;
  240. GET_MOTOR(motor);
  241. motor->setAcc(atoi(argv[2]));
  242. });
  243. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  244. CHECK_ARGC(2);
  245. StepMotor* motor = NULL;
  246. GET_MOTOR(motor);
  247. motor->setDec(atoi(argv[2]));
  248. });
  249. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  250. CHECK_ARGC(2);
  251. StepMotor* motor = NULL;
  252. GET_MOTOR(motor);
  253. motor->moveTo(atoi(argv[2]));
  254. });
  255. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  256. CHECK_ARGC(2);
  257. StepMotor* motor = NULL;
  258. GET_MOTOR(motor);
  259. motor->moveBy(atoi(argv[2]));
  260. });
  261. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  262. CHECK_ARGC(2);
  263. StepMotor* motor = NULL;
  264. GET_MOTOR(motor);
  265. motor->rotate(atoi(argv[2]));
  266. });
  267. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  268. CHECK_ARGC(1);
  269. StepMotor* motor = NULL;
  270. GET_MOTOR(motor);
  271. motor->moveToZero();
  272. });
  273. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  274. CHECK_ARGC(1);
  275. StepMotor* motor = NULL;
  276. GET_MOTOR(motor);
  277. HAL_Delay(100);
  278. while (!motor->isIdleState()) {
  279. HAL_Delay(300);
  280. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  281. }
  282. });
  283. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  284. CHECK_ARGC(1);
  285. StepMotor* motor = NULL;
  286. GET_MOTOR(motor);
  287. motor->clearException();
  288. });
  289. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  290. CHECK_ARGC(2);
  291. StepMotor* motor = NULL;
  292. GET_MOTOR(motor);
  293. int32_t step;
  294. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  295. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  296. context->breakflag = true;
  297. return;
  298. }
  299. motor->moveTo(step);
  300. });
  301. static int main_servo_motor_velocity = 400;
  302. cmdScheduler.registerCmd("main_servo_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  303. // main_servo_motor_rotate direction
  304. CHECK_ARGC(1);
  305. int direction = atoi(argv[1]);
  306. if (direction == 0) {
  307. // ����תλ��0��ֹͣ�豸
  308. g_eq20servomotor.writePn(1, 614, 1);
  309. g_eq20servomotor.writePn(1, 1501, 0);
  310. g_eq20servomotor.writePn(1, 1501, 1);
  311. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  312. } else if (direction > 0) {
  313. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  314. g_eq20servomotor.writePn(1, 614, -1);
  315. g_eq20servomotor.writePn(1, 1501, 0);
  316. g_eq20servomotor.writePn(1, 1501, 1);
  317. g_eq20servomotor.writePn(1, 614, 999999);
  318. g_eq20servomotor.writePn(1, 1501, 0);
  319. g_eq20servomotor.writePn(1, 1501, 1);
  320. } else if (direction < 0) {
  321. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  322. g_eq20servomotor.writePn(1, 614, 1);
  323. g_eq20servomotor.writePn(1, 1501, 0);
  324. g_eq20servomotor.writePn(1, 1501, 1);
  325. g_eq20servomotor.writePn(1, 614, -999999);
  326. g_eq20servomotor.writePn(1, 1501, 0);
  327. g_eq20servomotor.writePn(1, 1501, 1);
  328. }
  329. });
  330. cmdScheduler.registerCmd("main_servo_motor_set_velocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  331. CHECK_ARGC(1);
  332. main_servo_motor_velocity = atoi(argv[1]);
  333. return;
  334. });
  335. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  336. // main_servo_motor_move_to v
  337. CHECK_ARGC(0);
  338. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  339. g_eq20servomotor.writePn(1, 614, 0);
  340. g_eq20servomotor.writePn(1, 1501, 0);
  341. g_eq20servomotor.writePn(1, 1501, 1);
  342. });
  343. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  344. // main_servo_motor_move_to v
  345. CHECK_ARGC(0);
  346. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  347. g_eq20servomotor.writePn(1, 614, 0);
  348. g_eq20servomotor.writePn(1, 1501, 0);
  349. g_eq20servomotor.writePn(1, 1501, 1);
  350. });
  351. // setzero
  352. // set4095
  353. // moveto
  354. cmdScheduler.registerCmd("mini_servo_set_zero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  355. CHECK_ARGC(1);
  356. int id = atoi(argv[1]);
  357. ZLOGI(TAG, "mini_servo_set_zero %d", id);
  358. g_feiteservomotor.reCalibration(id, 0);
  359. ZLOGI(TAG, "mini_servo_set_zero %d done", id);
  360. });
  361. cmdScheduler.registerCmd("mini_servo_set_4095", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  362. CHECK_ARGC(1);
  363. int id = atoi(argv[1]);
  364. ZLOGI(TAG, "mini_servo_set_4095 %d", id);
  365. g_feiteservomotor.reCalibration(id, 4095);
  366. ZLOGI(TAG, "mini_servo_set_4095 %d done", id);
  367. });
  368. cmdScheduler.registerCmd("mini_servo_move_to", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  369. CHECK_ARGC(3);
  370. int id = atoi(argv[1]);
  371. int pos = atoi(argv[2]);
  372. int v = atoi(argv[3]);
  373. ZLOGI(TAG, "mini_servo_move_to %d %d %d", id, pos, v);
  374. g_feiteservomotor.moveTo(id, pos, v, 0);
  375. });
  376. #if 0
  377. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  378. // main_servo_motor_move_to v
  379. CHECK_ARGC(0);
  380. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  381. g_eq20servomotor.writePn(1, 614, 0);
  382. g_eq20servomotor.writePn(1, 1501, 0);
  383. g_eq20servomotor.writePn(1, 1501, 1);
  384. });
  385. #endif
  386. cmdScheduler.registerCmd("sleep_ms", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  387. CHECK_ARGC(1);
  388. osDelay(atoi(argv[1]));
  389. });
  390. }
  391. extern "C" {
  392. extern DMA_HandleTypeDef hdma_usart3_rx;
  393. extern DMA_HandleTypeDef hdma_usart3_tx;
  394. }
  395. void Main::run() {
  396. /*******************************************************************************
  397. * ϵͳʼ *
  398. *******************************************************************************/
  399. chip_init(&chipcfg);
  400. zos_cfg_t zoscfg;
  401. zos_init(&zoscfg);
  402. auto config = m_IflytopCanMaster.createDefaultConfig(1);
  403. m_IflytopCanMaster.initialize(config);
  404. int i = 1;
  405. g_step_motor[i++].initialize(11, 10000, &m_IflytopCanMaster);
  406. g_step_motor[i++].initialize(12, 10000, &m_IflytopCanMaster);
  407. g_step_motor[i++].initialize(13, 10000, &m_IflytopCanMaster);
  408. g_step_motor[i++].initialize(14, 10000, &m_IflytopCanMaster);
  409. g_step_motor[i++].initialize(15, 10000, &m_IflytopCanMaster);
  410. g_step_motor[i++].initialize(16, 10000, &m_IflytopCanMaster);
  411. g_step_motor45[0].initialize(cfg1);
  412. g_step_motor45[1].initialize(cfg1);
  413. g_step_motor45[2].initialize(cfg2);
  414. g_step_motor45[3].initialize(cfg3);
  415. g_step_motor45[4].initialize(cfg4);
  416. g_step_motor45[5].initialize(cfg5);
  417. g_step_motor45[6].initialize(cfg6);
  418. StepMotor45Scheduler step_motor45_scheduler;
  419. step_motor45_scheduler.initialize(&htim10, 1000);
  420. step_motor45_scheduler.addMotor(&g_step_motor45[1]);
  421. step_motor45_scheduler.addMotor(&g_step_motor45[2]);
  422. step_motor45_scheduler.addMotor(&g_step_motor45[3]);
  423. step_motor45_scheduler.addMotor(&g_step_motor45[4]);
  424. step_motor45_scheduler.addMotor(&g_step_motor45[5]);
  425. step_motor45_scheduler.addMotor(&g_step_motor45[6]);
  426. // g_step_motor45_1.rotate(true, 1000);
  427. step_motor45_scheduler.start();
  428. cmdScheduler.initialize(&DEBUG_UART, 1000);
  429. g_modbusblockhost.initialize(&huart2);
  430. g_eq20servomotor.init(&g_modbusblockhost);
  431. g_feiteservomotor.initialize(&huart3, &hdma_usart3_rx, &hdma_usart3_tx);
  432. regfn();
  433. while (true) {
  434. OSDefaultSchduler::getInstance()->loop();
  435. cmdScheduler.schedule();
  436. m_IflytopCanMaster.periodicJob();
  437. // m_IflytopCanMaster.writeReg(1,1,1,10);
  438. // osDelay(1);
  439. }
  440. }