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.

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