From f2caaae8c8d065a48c4133129caa9630d4cc702b Mon Sep 17 00:00:00 2001 From: bmy <2583236812@qq.com> Date: Sun, 28 Jul 2024 16:24:31 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E4=B8=BA=E4=B8=89=E4=B8=AA=E6=96=B9?= =?UTF-8?q?=E5=90=91=E8=AE=BE=E7=BD=AE=E7=8B=AC=E7=AB=8B=E7=9A=84=E6=A0=87?= =?UTF-8?q?=E5=BF=97=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 保证移动距离完整,防止下发某一方向速度指令时,导致其他方向无法正常退出距离控制模式的问题 --- .eide/eide.json | 6 +-- app/by_messy.c | 12 ++--- app/by_motion.c | 119 +++++++++++++++++++++++++++++++++++------------- app/by_motion.h | 5 +- 4 files changed, 99 insertions(+), 43 deletions(-) diff --git a/.eide/eide.json b/.eide/eide.json index 1235b45..33f0dc6 100644 --- a/.eide/eide.json +++ b/.eide/eide.json @@ -3,9 +3,7 @@ "type": "ARM", "dependenceList": [], "srcDirs": [ - ".eide/deps", "3rd-part", - "middlewares", "app", "libraries/drivers", "project", @@ -69,14 +67,12 @@ "libraries/cmsis/cm4/device_support", "project/inc", ".cmsis/include", - ".eide/deps", "3rd-part/dwt_delay", "3rd-part/lwprintf", "app", "3rd-part/lwrb" ], "libList": [], - "sourceDirList": [], "defineList": [ "AT32F403ARCT7", "USE_STDPERIPH_DRIVER", @@ -85,5 +81,5 @@ } } }, - "version": "3.3" + "version": "3.4" } \ No newline at end of file diff --git a/app/by_messy.c b/app/by_messy.c index f4c3a54..f9217ef 100644 --- a/app/by_messy.c +++ b/app/by_messy.c @@ -25,37 +25,37 @@ void by_messy_loop(void) switch (cmd) { case 0x31: // 设置速度 x memcpy(&motion_speed_struct.v_x, buff, sizeof(motion_speed_struct.v_x)); - by_motion_set_mode(0); + by_motion_set_mode_x(0); break; case 0x32: // 设置速度 y memcpy(&motion_speed_struct.v_y, buff, sizeof(motion_speed_struct.v_y)); - by_motion_set_mode(0); + by_motion_set_mode_y(0); break; case 0x33: // 设置速度 omega memcpy(&motion_speed_struct.v_w, buff, sizeof(motion_speed_struct.v_w)); - by_motion_set_mode(0); + by_motion_set_mode_w(0); break; case 0x34: // 设置移动距离 x by_frame_send(cmd, buff); // 正确接收后直接返回原文 memcpy(&motion_speed_struct.v_x, buff, sizeof(motion_speed_struct.v_x)); - by_motion_set_mode(1); + by_motion_set_mode_x(1); motion_time_struct.t_x += buff[1]; break; case 0x35: // 设置移动距离 y by_frame_send(cmd, buff); // 正确接收后直接返回原文 memcpy(&motion_speed_struct.v_y, buff, sizeof(motion_speed_struct.v_y)); - by_motion_set_mode(1); + by_motion_set_mode_y(1); motion_time_struct.t_y += buff[1]; break; case 0x36: // 设置旋转角度 omega by_frame_send(cmd, buff); // 正确接收后直接返回原文 memcpy(&motion_speed_struct.v_w, buff, sizeof(motion_speed_struct.v_w)); - by_motion_set_mode(1); + by_motion_set_mode_w(1); motion_time_struct.t_w += buff[1]; break; diff --git a/app/by_motion.c b/app/by_motion.c index c7bb76f..762c6eb 100644 --- a/app/by_motion.c +++ b/app/by_motion.c @@ -19,6 +19,12 @@ /*** 控制模式 ***/ uint8_t control_mode = 0; // 0-速度模式 1-位置模式 +struct by_motion_mode_struct { + uint8_t x; + uint8_t y; + uint8_t w; +} by_motion_mode = {0,0,0}; + /*** 位置控制 ***/ uint8_t control_timer = 0; // 位置控制定时器状态 uint32_t control_timer_cnt = 0; // 位置控制计数 @@ -74,20 +80,38 @@ void by_motion_update_speed(void) */ void by_motion_loop(void) { - if (control_mode == 0) { // 速度控制模式 - memset(&motion_time_struct, 0, sizeof(motion_time_struct)); - } else { // 位置控制模式 - if (0 == motion_time_struct.t_x) { - motion_speed_struct.v_x = 0; - } - if (0 == motion_time_struct.t_y) { - motion_speed_struct.v_y = 0; - } - if (0 == motion_time_struct.t_w) { - motion_speed_struct.v_w = 0; - } + if (by_motion_mode.x == 0) { + motion_time_struct.t_x = 0; + } else if (by_motion_mode.x == 1 && motion_time_struct.t_x == 0) { + motion_speed_struct.v_x = 0; } + if (by_motion_mode.y == 0) { + motion_time_struct.t_y = 0; + } else if (by_motion_mode.y == 1 && motion_time_struct.t_y == 0) { + motion_speed_struct.v_y = 0; + } + + if (by_motion_mode.w == 0) { + motion_time_struct.t_w = 0; + } else if (by_motion_mode.w == 1 && motion_time_struct.t_w == 0) { + motion_speed_struct.v_w = 0; + } + + // if (control_mode == 0) { // 速度控制模式 + // memset(&motion_time_struct, 0, sizeof(motion_time_struct)); + // } else { // 位置控制模式 + // if (0 == motion_time_struct.t_x) { + // motion_speed_struct.v_x = 0; + // } + // if (0 == motion_time_struct.t_y) { + // motion_speed_struct.v_y = 0; + // } + // if (0 == motion_time_struct.t_w) { + // motion_speed_struct.v_w = 0; + // } + // } + if ((motion_speed_struct.v_x != motion_speed_struct_last.v_x) || (motion_speed_struct.v_y != motion_speed_struct_last.v_y) || (motion_speed_struct.v_w != motion_speed_struct_last.v_w)) { by_motion_update_speed(); memcpy(&motion_speed_struct_last, &motion_speed_struct, sizeof(motion_speed_type)); @@ -100,29 +124,62 @@ void by_motion_loop(void) */ void by_motion_timer_handle(void) { - if (control_mode == 0) { + // if (control_mode == 0) { + // motion_time_struct.t_x = 0; + // motion_time_struct.t_y = 0; + // motion_time_struct.t_w = 0; + // } else { + // if (motion_time_struct.t_x > 0) { + // motion_time_struct.t_x--; + // } + // if (motion_time_struct.t_y > 0) { + // motion_time_struct.t_y--; + // } + // if (motion_time_struct.t_w > 0) { + // motion_time_struct.t_w--; + // } + // } + + if (by_motion_mode.x == 0) { motion_time_struct.t_x = 0; + } else if(motion_time_struct.t_x > 0) { + motion_time_struct.t_x--; + } + + if(by_motion_mode.y == 0) { motion_time_struct.t_y = 0; + } else if(motion_time_struct.t_y > 0) { + motion_time_struct.t_y--; + } + + if(by_motion_mode.w == 0) { motion_time_struct.t_w = 0; - } else { - if (motion_time_struct.t_x > 0) { - motion_time_struct.t_x--; - } - if (motion_time_struct.t_y > 0) { - motion_time_struct.t_y--; - } - if (motion_time_struct.t_w > 0) { - motion_time_struct.t_w--; - } + } else if(motion_time_struct.t_w > 0) { + motion_time_struct.t_w--; } } -/** - * @brief 设置电机控制模式 - * - * @param mode 0-速度模式 1-位置模式 - */ -void by_motion_set_mode(uint8_t mode) +// /** +// * @brief 设置电机控制模式 +// * +// * @param mode 0-速度模式 1-位置模式 +// */ +// void by_motion_set_mode(uint8_t mode) +// { +// control_mode = mode; +// } + +void by_motion_set_mode_x(uint8_t mode) { - control_mode = mode; -} \ No newline at end of file + by_motion_mode.x = mode; +} + +void by_motion_set_mode_y(uint8_t mode) +{ + by_motion_mode.y = mode; +} + +void by_motion_set_mode_w(uint8_t mode) +{ + by_motion_mode.w = mode; +} diff --git a/app/by_motion.h b/app/by_motion.h index 42a6b2d..d83d817 100644 --- a/app/by_motion.h +++ b/app/by_motion.h @@ -20,7 +20,10 @@ extern void by_motion_update_speed(void); extern void by_motion_loop(void); extern void by_motion_set_mode(uint8_t mode); extern void by_motion_timer_handle(void); -extern void by_motion_set_mode(uint8_t mode); +// extern void by_motion_set_mode(uint8_t mode); +extern void by_motion_set_mode_x(uint8_t mode); +extern void by_motion_set_mode_y(uint8_t mode); +extern void by_motion_set_mode_w(uint8_t mode); extern motion_speed_type motion_speed_struct; extern motion_time_type motion_time_struct;