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.

454 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
  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("stepmotor45_wait_to_reach_pos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  125. // stepmotor45_rotate motorid direction
  126. CHECK_ARGC(2);
  127. int motorid = atoi(argv[1]);
  128. if (motorid < 1 || motorid > 6) {
  129. ZLOGE(TAG, "motorid out of range");
  130. return;
  131. }
  132. while (true) {
  133. if (g_step_motor45[motorid].isReachTargetPos()) {
  134. break;
  135. }
  136. ZLOGI(TAG, "stepmotor45_wait_to_reach_pos %d", motorid);
  137. osDelay(100);
  138. }
  139. });
  140. // stepmotor45
  141. cmdScheduler.registerCmd("stepmotor45_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  142. // stepmotor45_rotate motorid direction
  143. CHECK_ARGC(2);
  144. int motorid = atoi(argv[1]);
  145. bool direction = atoi(argv[2]);
  146. if (motorid < 1 || motorid > 6) {
  147. ZLOGE(TAG, "motorid out of range");
  148. return;
  149. }
  150. g_step_motor45[motorid].rotate(direction);
  151. });
  152. cmdScheduler.registerCmd("stepmotor45_readPos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  153. // stepmotor45_rotate motorid direction
  154. CHECK_ARGC(2);
  155. int motorid = atoi(argv[1]);
  156. if (motorid < 1 || motorid > 6) {
  157. ZLOGE(TAG, "motorid out of range");
  158. return;
  159. }
  160. ZLOGI(TAG, "motorid %d pos %d", motorid, g_step_motor45[motorid].getPos());
  161. });
  162. cmdScheduler.registerCmd("stepmotor45_moveTo", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  163. CHECK_ARGC(2);
  164. int motorid = atoi(argv[1]);
  165. int pos = atoi(argv[2]);
  166. if (motorid < 1 || motorid > 6) {
  167. ZLOGE(TAG, "motorid out of range");
  168. return;
  169. }
  170. g_step_motor45[motorid].moveTo(pos);
  171. });
  172. cmdScheduler.registerCmd("stepmotor45_moveBy", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  173. CHECK_ARGC(2);
  174. int motorid = atoi(argv[1]);
  175. int pos = atoi(argv[2]);
  176. if (motorid < 1 || motorid > 6) {
  177. ZLOGE(TAG, "motorid out of range");
  178. return;
  179. }
  180. g_step_motor45[motorid].moveBy(pos);
  181. });
  182. cmdScheduler.registerCmd("stepmotor45_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  183. CHECK_ARGC(1);
  184. int motorid = atoi(argv[1]);
  185. if (motorid < 1 || motorid > 6) {
  186. ZLOGE(TAG, "motorid out of range");
  187. return;
  188. }
  189. g_step_motor45[motorid].stop();
  190. });
  191. cmdScheduler.registerCmd("stepmotor45_setSpeed", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  192. CHECK_ARGC(1);
  193. int motorid = atoi(argv[1]);
  194. int speed = atoi(argv[2]);
  195. if (motorid < 1 || motorid > 6) {
  196. ZLOGE(TAG, "motorid out of range");
  197. return;
  198. }
  199. g_step_motor45[motorid].setDefaultSpeed(speed);
  200. });
  201. /*******************************************************************************
  202. * *
  203. *******************************************************************************/
  204. #define GET_MOTOR(motor) \
  205. { \
  206. int motorid = atoi(argv[1]); \
  207. motor = &g_step_motor[motorid]; \
  208. if (motorid >= 6 || motorid < 1) { \
  209. ZLOGE(TAG, "motor %d not found", motorid); \
  210. context->breakflag = true; \
  211. return; \
  212. } \
  213. }
  214. /**
  215. * @brief STEPMOTOR
  216. */
  217. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  218. CHECK_ARGC(2);
  219. StepMotor* motor = NULL;
  220. GET_MOTOR(motor);
  221. motor->setVelocity(atoi(argv[2]));
  222. });
  223. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  224. CHECK_ARGC(2);
  225. StepMotor* motor = NULL;
  226. GET_MOTOR(motor);
  227. motor->setAcc(atoi(argv[2]));
  228. });
  229. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  230. CHECK_ARGC(2);
  231. StepMotor* motor = NULL;
  232. GET_MOTOR(motor);
  233. motor->setDec(atoi(argv[2]));
  234. });
  235. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  236. CHECK_ARGC(2);
  237. StepMotor* motor = NULL;
  238. GET_MOTOR(motor);
  239. motor->moveTo(atoi(argv[2]));
  240. });
  241. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  242. CHECK_ARGC(2);
  243. StepMotor* motor = NULL;
  244. GET_MOTOR(motor);
  245. motor->moveBy(atoi(argv[2]));
  246. });
  247. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  248. CHECK_ARGC(2);
  249. StepMotor* motor = NULL;
  250. GET_MOTOR(motor);
  251. motor->rotate(atoi(argv[2]));
  252. });
  253. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  254. CHECK_ARGC(1);
  255. StepMotor* motor = NULL;
  256. GET_MOTOR(motor);
  257. motor->moveToZero();
  258. });
  259. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  260. CHECK_ARGC(1);
  261. StepMotor* motor = NULL;
  262. GET_MOTOR(motor);
  263. HAL_Delay(100);
  264. while (!motor->isIdleState()) {
  265. HAL_Delay(300);
  266. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  267. }
  268. });
  269. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  270. CHECK_ARGC(1);
  271. StepMotor* motor = NULL;
  272. GET_MOTOR(motor);
  273. motor->clearException();
  274. });
  275. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  276. CHECK_ARGC(2);
  277. StepMotor* motor = NULL;
  278. GET_MOTOR(motor);
  279. int32_t step;
  280. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  281. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  282. context->breakflag = true;
  283. return;
  284. }
  285. motor->moveTo(step);
  286. });
  287. static int main_servo_motor_velocity = 400;
  288. cmdScheduler.registerCmd("main_servo_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  289. // main_servo_motor_rotate direction
  290. CHECK_ARGC(1);
  291. int direction = atoi(argv[1]);
  292. if (direction == 0) {
  293. // ����תλ��0��ֹͣ�豸
  294. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  295. g_eq20servomotor.writePn(1, 614, 0);
  296. g_eq20servomotor.writePn(1, 1501, 0);
  297. g_eq20servomotor.writePn(1, 1501, 1);
  298. } else if (direction > 0) {
  299. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  300. g_eq20servomotor.writePn(1, 614, 9999999);
  301. g_eq20servomotor.writePn(1, 1501, 0);
  302. g_eq20servomotor.writePn(1, 1501, 1);
  303. } else if (direction < 0) {
  304. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  305. g_eq20servomotor.writePn(1, 614, -9999999);
  306. g_eq20servomotor.writePn(1, 1501, 0);
  307. g_eq20servomotor.writePn(1, 1501, 1);
  308. }
  309. });
  310. cmdScheduler.registerCmd("main_servo_motor_set_velocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  311. CHECK_ARGC(1);
  312. main_servo_motor_velocity = atoi(argv[1]);
  313. return;
  314. });
  315. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  316. // main_servo_motor_move_to v
  317. CHECK_ARGC(0);
  318. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  319. g_eq20servomotor.writePn(1, 614, 0);
  320. g_eq20servomotor.writePn(1, 1501, 0);
  321. g_eq20servomotor.writePn(1, 1501, 1);
  322. });
  323. cmdScheduler.registerCmd("sleep_ms", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  324. CHECK_ARGC(1);
  325. HAL_Delay(atoi(argv[1]));
  326. });
  327. }
  328. void Main::run() {
  329. /*******************************************************************************
  330. * ϵͳʼ *
  331. *******************************************************************************/
  332. chip_init(&chipcfg);
  333. zos_cfg_t zoscfg;
  334. zos_init(&zoscfg);
  335. auto config = m_IflytopCanMaster.createDefaultConfig(1);
  336. m_IflytopCanMaster.initialize(config);
  337. int i = 1;
  338. g_step_motor[i++].initialize(11, 10000, &m_IflytopCanMaster);
  339. g_step_motor[i++].initialize(12, 10000, &m_IflytopCanMaster);
  340. g_step_motor[i++].initialize(13, 10000, &m_IflytopCanMaster);
  341. g_step_motor[i++].initialize(14, 10000, &m_IflytopCanMaster);
  342. g_step_motor[i++].initialize(15, 10000, &m_IflytopCanMaster);
  343. g_step_motor[i++].initialize(16, 10000, &m_IflytopCanMaster);
  344. g_step_motor45[0].initialize(cfg1);
  345. g_step_motor45[1].initialize(cfg1);
  346. g_step_motor45[2].initialize(cfg2);
  347. g_step_motor45[3].initialize(cfg3);
  348. g_step_motor45[4].initialize(cfg4);
  349. g_step_motor45[5].initialize(cfg5);
  350. g_step_motor45[6].initialize(cfg6);
  351. StepMotor45Scheduler step_motor45_scheduler;
  352. step_motor45_scheduler.initialize(&htim10, 1000);
  353. step_motor45_scheduler.addMotor(&g_step_motor45[1]);
  354. step_motor45_scheduler.addMotor(&g_step_motor45[2]);
  355. step_motor45_scheduler.addMotor(&g_step_motor45[3]);
  356. step_motor45_scheduler.addMotor(&g_step_motor45[4]);
  357. step_motor45_scheduler.addMotor(&g_step_motor45[5]);
  358. step_motor45_scheduler.addMotor(&g_step_motor45[6]);
  359. // g_step_motor45_1.rotate(true, 1000);
  360. step_motor45_scheduler.start();
  361. cmdScheduler.initialize(&DEBUG_UART, 1000);
  362. g_modbusblockhost.initialize(&huart2);
  363. g_eq20servomotor.init(&g_modbusblockhost);
  364. regfn();
  365. while (true) {
  366. OSDefaultSchduler::getInstance()->loop();
  367. cmdScheduler.schedule();
  368. m_IflytopCanMaster.periodicJob();
  369. // m_IflytopCanMaster.writeReg(1,1,1,10);
  370. osDelay(1);
  371. }
  372. }