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

436 lines
13 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
  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 = false,
  41. .zeroPin = PinNull,
  42. .zeroPinMirror = false,
  43. .driverPin = {PB15, PD11, PD12, PD13},
  44. .driverPinMirror = false,
  45. };
  46. static StepMotor45::cfg_t cfg2 = {
  47. .max_pos = -1,
  48. .enable_zero_limit = false,
  49. .enable_max_pos_limit = false,
  50. .mirror = false,
  51. .zeroPin = PinNull,
  52. .zeroPinMirror = false,
  53. .driverPin = {PG2, PG3, PG4, PG5},
  54. .driverPinMirror = false,
  55. };
  56. static StepMotor45::cfg_t cfg3 = {
  57. .max_pos = -1,
  58. .enable_zero_limit = false,
  59. .enable_max_pos_limit = false,
  60. .mirror = false,
  61. .zeroPin = PinNull,
  62. .zeroPinMirror = false,
  63. .driverPin = {PG6, PG7, PG8, PC6},
  64. .driverPinMirror = false,
  65. };
  66. static StepMotor45::cfg_t cfg4 = {
  67. .max_pos = -1,
  68. .enable_zero_limit = false,
  69. .enable_max_pos_limit = false,
  70. .mirror = false,
  71. .zeroPin = PinNull,
  72. .zeroPinMirror = false,
  73. .driverPin = {PC7, PC8, PC9, PA8},
  74. .driverPinMirror = false,
  75. };
  76. static StepMotor45::cfg_t cfg5 = {
  77. .max_pos = -1,
  78. .enable_zero_limit = false,
  79. .enable_max_pos_limit = false,
  80. .mirror = false,
  81. .zeroPin = PinNull,
  82. .zeroPinMirror = false,
  83. .driverPin = {PA13, PA14, PA15, PC10},
  84. .driverPinMirror = false,
  85. };
  86. static StepMotor45::cfg_t cfg6 = {
  87. .max_pos = -1,
  88. .enable_zero_limit = false,
  89. .enable_max_pos_limit = false,
  90. .mirror = false,
  91. .zeroPin = PinNull,
  92. .zeroPinMirror = false,
  93. .driverPin = {PC12, PD3, PD5, PD7},
  94. .driverPinMirror = false,
  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. // stepmotor45
  125. cmdScheduler.registerCmd("stepmotor45_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  126. // stepmotor45_rotate motorid direction
  127. CHECK_ARGC(2);
  128. int motorid = atoi(argv[1]);
  129. bool direction = atoi(argv[2]);
  130. if (motorid < 1 || motorid > 6) {
  131. ZLOGE(TAG, "motorid out of range");
  132. return;
  133. }
  134. g_step_motor45[motorid].rotate(direction);
  135. });
  136. cmdScheduler.registerCmd("stepmotor45_readPos", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  137. // stepmotor45_rotate motorid direction
  138. CHECK_ARGC(2);
  139. int motorid = atoi(argv[1]);
  140. if (motorid < 1 || motorid > 6) {
  141. ZLOGE(TAG, "motorid out of range");
  142. return;
  143. }
  144. ZLOGI(TAG, "motorid %d pos %d", motorid, g_step_motor45[motorid].getPos());
  145. });
  146. cmdScheduler.registerCmd("stepmotor45_moveTo", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  147. CHECK_ARGC(2);
  148. int motorid = atoi(argv[1]);
  149. int pos = atoi(argv[2]);
  150. if (motorid < 1 || motorid > 6) {
  151. ZLOGE(TAG, "motorid out of range");
  152. return;
  153. }
  154. g_step_motor45[motorid].moveTo(pos);
  155. });
  156. cmdScheduler.registerCmd("stepmotor45_moveBy", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  157. CHECK_ARGC(2);
  158. int motorid = atoi(argv[1]);
  159. int pos = atoi(argv[2]);
  160. if (motorid < 1 || motorid > 6) {
  161. ZLOGE(TAG, "motorid out of range");
  162. return;
  163. }
  164. g_step_motor45[motorid].moveBy(pos);
  165. });
  166. cmdScheduler.registerCmd("stepmotor45_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  167. CHECK_ARGC(1);
  168. int motorid = atoi(argv[1]);
  169. if (motorid < 1 || motorid > 6) {
  170. ZLOGE(TAG, "motorid out of range");
  171. return;
  172. }
  173. g_step_motor45[motorid].stop();
  174. });
  175. cmdScheduler.registerCmd("stepmotor45_setSpeed", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  176. CHECK_ARGC(1);
  177. int motorid = atoi(argv[1]);
  178. int speed = atoi(argv[2]);
  179. if (motorid < 1 || motorid > 6) {
  180. ZLOGE(TAG, "motorid out of range");
  181. return;
  182. }
  183. g_step_motor45[motorid].setDefaultSpeed(speed);
  184. });
  185. /*******************************************************************************
  186. * *
  187. *******************************************************************************/
  188. #define GET_MOTOR(motor) \
  189. { \
  190. int motorid = atoi(argv[1]); \
  191. motor = &g_step_motor[motorid]; \
  192. if (motorid >= 6 || motorid < 1) { \
  193. ZLOGE(TAG, "motor %d not found", motorid); \
  194. context->breakflag = true; \
  195. return; \
  196. } \
  197. }
  198. /**
  199. * @brief STEPMOTOR
  200. */
  201. cmdScheduler.registerCmd("step_motor_setvelocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  202. CHECK_ARGC(2);
  203. StepMotor* motor = NULL;
  204. GET_MOTOR(motor);
  205. motor->setVelocity(atoi(argv[2]));
  206. });
  207. cmdScheduler.registerCmd("step_motor_set_acc", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  208. CHECK_ARGC(2);
  209. StepMotor* motor = NULL;
  210. GET_MOTOR(motor);
  211. motor->setAcc(atoi(argv[2]));
  212. });
  213. cmdScheduler.registerCmd("step_motor_set_dec", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  214. CHECK_ARGC(2);
  215. StepMotor* motor = NULL;
  216. GET_MOTOR(motor);
  217. motor->setDec(atoi(argv[2]));
  218. });
  219. cmdScheduler.registerCmd("step_motor_moveto", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  220. CHECK_ARGC(2);
  221. StepMotor* motor = NULL;
  222. GET_MOTOR(motor);
  223. motor->moveTo(atoi(argv[2]));
  224. });
  225. cmdScheduler.registerCmd("step_motor_moveby", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  226. CHECK_ARGC(2);
  227. StepMotor* motor = NULL;
  228. GET_MOTOR(motor);
  229. motor->moveBy(atoi(argv[2]));
  230. });
  231. cmdScheduler.registerCmd("step_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  232. CHECK_ARGC(2);
  233. StepMotor* motor = NULL;
  234. GET_MOTOR(motor);
  235. motor->rotate(atoi(argv[2]));
  236. });
  237. cmdScheduler.registerCmd("step_motor_movetozero", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  238. CHECK_ARGC(1);
  239. StepMotor* motor = NULL;
  240. GET_MOTOR(motor);
  241. motor->moveToZero();
  242. });
  243. cmdScheduler.registerCmd("step_motor_wait_for_idle", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  244. CHECK_ARGC(1);
  245. StepMotor* motor = NULL;
  246. GET_MOTOR(motor);
  247. HAL_Delay(100);
  248. while (!motor->isIdleState()) {
  249. HAL_Delay(300);
  250. ZLOGI(TAG, "step_motor_wait_for_idle %d", atoi(argv[1]));
  251. }
  252. });
  253. cmdScheduler.registerCmd("step_motor_clear_exception", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  254. CHECK_ARGC(1);
  255. StepMotor* motor = NULL;
  256. GET_MOTOR(motor);
  257. motor->clearException();
  258. });
  259. cmdScheduler.registerCmd("step_motor_moveto_mm", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  260. CHECK_ARGC(2);
  261. StepMotor* motor = NULL;
  262. GET_MOTOR(motor);
  263. int32_t step;
  264. if (!distance_mm_to_step(atoi(argv[1]), atof(argv[2]), &step)) {
  265. ZLOGE(TAG, "step_motor_moveto_mm %d %f failed", atoi(argv[1]), atof(argv[2]));
  266. context->breakflag = true;
  267. return;
  268. }
  269. motor->moveTo(step);
  270. });
  271. static int main_servo_motor_velocity = 400;
  272. cmdScheduler.registerCmd("main_servo_motor_rotate", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  273. // main_servo_motor_rotate direction
  274. CHECK_ARGC(1);
  275. int direction = atoi(argv[1]);
  276. if (direction == 0) {
  277. // ����תλ��0��ֹͣ�豸
  278. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  279. g_eq20servomotor.writePn(1, 614, 0);
  280. g_eq20servomotor.writePn(1, 1501, 0);
  281. g_eq20servomotor.writePn(1, 1501, 1);
  282. } else if (direction > 0) {
  283. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  284. g_eq20servomotor.writePn(1, 614, 9999999);
  285. g_eq20servomotor.writePn(1, 1501, 0);
  286. g_eq20servomotor.writePn(1, 1501, 1);
  287. } else if (direction < 0) {
  288. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  289. g_eq20servomotor.writePn(1, 614, -9999999);
  290. g_eq20servomotor.writePn(1, 1501, 0);
  291. g_eq20servomotor.writePn(1, 1501, 1);
  292. }
  293. });
  294. cmdScheduler.registerCmd("main_servo_motor_set_velocity", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  295. CHECK_ARGC(1);
  296. main_servo_motor_velocity = atoi(argv[1]);
  297. return;
  298. });
  299. cmdScheduler.registerCmd("main_servo_motor_stop", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  300. // main_servo_motor_move_to v
  301. CHECK_ARGC(0);
  302. g_eq20servomotor.writePn(1, 615, main_servo_motor_velocity);
  303. g_eq20servomotor.writePn(1, 614, 0);
  304. g_eq20servomotor.writePn(1, 1501, 0);
  305. g_eq20servomotor.writePn(1, 1501, 1);
  306. });
  307. cmdScheduler.registerCmd("sleep_ms", [](int argc, char** argv, CmdScheduler::CmdProcessContext* context) {
  308. CHECK_ARGC(1);
  309. HAL_Delay(atoi(argv[1]));
  310. });
  311. }
  312. void Main::run() {
  313. /*******************************************************************************
  314. * ϵͳʼ *
  315. *******************************************************************************/
  316. chip_init(&chipcfg);
  317. zos_cfg_t zoscfg;
  318. zos_init(&zoscfg);
  319. auto config = m_IflytopCanMaster.createDefaultConfig(1);
  320. m_IflytopCanMaster.initialize(config);
  321. int i = 1;
  322. g_step_motor[i++].initialize(11, 10000, &m_IflytopCanMaster);
  323. g_step_motor[i++].initialize(12, 10000, &m_IflytopCanMaster);
  324. g_step_motor[i++].initialize(13, 10000, &m_IflytopCanMaster);
  325. g_step_motor[i++].initialize(14, 10000, &m_IflytopCanMaster);
  326. g_step_motor[i++].initialize(15, 10000, &m_IflytopCanMaster);
  327. g_step_motor[i++].initialize(16, 10000, &m_IflytopCanMaster);
  328. // g_step_motor45[0].initialize(cfg1);
  329. g_step_motor45[1].initialize(cfg1);
  330. g_step_motor45[2].initialize(cfg2);
  331. g_step_motor45[3].initialize(cfg3);
  332. g_step_motor45[4].initialize(cfg4);
  333. g_step_motor45[5].initialize(cfg5);
  334. g_step_motor45[6].initialize(cfg6);
  335. StepMotor45Scheduler step_motor45_scheduler;
  336. step_motor45_scheduler.initialize(&htim10, 1000);
  337. step_motor45_scheduler.addMotor(&g_step_motor45[1]);
  338. step_motor45_scheduler.addMotor(&g_step_motor45[2]);
  339. step_motor45_scheduler.addMotor(&g_step_motor45[3]);
  340. step_motor45_scheduler.addMotor(&g_step_motor45[4]);
  341. step_motor45_scheduler.addMotor(&g_step_motor45[5]);
  342. step_motor45_scheduler.addMotor(&g_step_motor45[6]);
  343. // g_step_motor45_1.rotate(true, 1000);
  344. step_motor45_scheduler.start();
  345. cmdScheduler.initialize(&DEBUG_UART, 1000);
  346. g_modbusblockhost.initialize(&huart2);
  347. g_eq20servomotor.init(&g_modbusblockhost);
  348. regfn();
  349. while (true) {
  350. OSDefaultSchduler::getInstance()->loop();
  351. cmdScheduler.schedule();
  352. m_IflytopCanMaster.periodicJob();
  353. // m_IflytopCanMaster.writeReg(1,1,1,10);
  354. osDelay(1);
  355. }
  356. }