pref: 为三个方向设置独立的标志位
保证移动距离完整,防止下发某一方向速度指令时,导致其他方向无法正常退出距离控制模式的问题
This commit is contained in:
119
app/by_motion.c
119
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user