|
|
|
@ -1,10 +1,11 @@ |
|
|
|
/*
|
|
|
|
* robot_state.c |
|
|
|
* robot_state.c 手动及自动控制 换道 PID |
|
|
|
* |
|
|
|
* Created on: Aug 2, 2024 |
|
|
|
* Author: akeguo |
|
|
|
*/ |
|
|
|
#include "robot_state.h" |
|
|
|
#include "FSM.h" |
|
|
|
#include "bsp_GPIO.h" |
|
|
|
#include <stdio.h> |
|
|
|
#include <math.h> |
|
|
|
@ -12,7 +13,7 @@ |
|
|
|
int Swing_Count=0; |
|
|
|
int Home_Flag=0; |
|
|
|
|
|
|
|
|
|
|
|
SystemMode_t system_mode = SYSTEM_NORMAL; // 默认开机是正常模式
|
|
|
|
|
|
|
|
ChgRoad_Parameters Chg_Pa = |
|
|
|
{ |
|
|
|
@ -166,8 +167,9 @@ void HALT_State_Do(void) |
|
|
|
Chg_Pa.change_road_status_flag = 0; |
|
|
|
GV.Chg_Flag = 0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
|
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
// Polish_Flag = 0;
|
|
|
|
// Spray_Flag = 0;
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -302,7 +304,6 @@ void VehicleAutoForward(double InclinometerAngle, double offsetAngle_left, dou |
|
|
|
GV.RightFrontMotor.Target_Velcity=GV.RightFrontMotor.Target_Velcity+AM_Pa.KP1 * deltaSpeed; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (deltaAngle >= 0 && deltaAngle <= AM_Pa.deltaAngle1)
|
|
|
|
// {
|
|
|
|
// Motor[0].Target_Velcity = Motor[0].Target_Velcity + AM_Pa.KP1 * deltaSpeed;
|
|
|
|
@ -337,7 +338,6 @@ void VehicleAutoBackward(double InclinometerAngle, double offsetAngle_left, dou |
|
|
|
} |
|
|
|
GV.LeftFrontMotor.Target_Velcity = -GV.Robot_AutoSpeed; |
|
|
|
GV.RightFrontMotor.Target_Velcity = GV.Robot_AutoSpeed; |
|
|
|
//
|
|
|
|
GV.LeftFrontMotor.Target_Velcity=GV.LeftFrontMotor.Target_Velcity+AM_Pa.KP1 * deltaSpeed; |
|
|
|
GV.RightFrontMotor.Target_Velcity=GV.RightFrontMotor.Target_Velcity+AM_Pa.KP1 * deltaSpeed; |
|
|
|
|
|
|
|
@ -383,144 +383,191 @@ void VehicleAutoTurn(double InclinometerAngle, double expectationAngle, double K |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ChgUp() |
|
|
|
{ |
|
|
|
//上换道
|
|
|
|
double angle; |
|
|
|
angle = GV.Robot_Angle.RF_Angle_Roll / 100.0; |
|
|
|
if(Chg_Pa.change_road_finish_flag==0) |
|
|
|
{ |
|
|
|
switch(Chg_Pa.change_road_status_flag) |
|
|
|
{ |
|
|
|
case 0: |
|
|
|
|
|
|
|
if(angle >= -45 |
|
|
|
&& (angle <= 45)) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=1; |
|
|
|
GV.Chg_Flag = 0; |
|
|
|
Chg_Pa.ChgWidth = GV.PV.Robot_ChgLength-10; |
|
|
|
WheelMotorSpeedInit(); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 1: |
|
|
|
VehicleAutoTurn((angle), 90, Chg_Pa.KPCR1, Chg_Pa.KDCR1); //没到-90左转
|
|
|
|
continueTurn_up(90, (angle)); |
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=2; |
|
|
|
GV.LeftFrontMotor.Target_Velcity=0; |
|
|
|
GV.RightFrontMotor.Target_Velcity=0; |
|
|
|
Chg_Pa.isTurn=false;//换完置位
|
|
|
|
// 上换道
|
|
|
|
double angle = GV.Robot_Angle.RF_Angle_Roll / 100.0; |
|
|
|
|
|
|
|
if (Chg_Pa.change_road_finish_flag == 0) |
|
|
|
{ |
|
|
|
switch (Chg_Pa.change_road_status_flag) |
|
|
|
{ |
|
|
|
case 0: // 初始化换道
|
|
|
|
if (angle >= -45 && angle <= 45) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag = 1; |
|
|
|
GV.Chg_Flag = 0; |
|
|
|
Chg_Pa.ChgWidth = GV.PV.Robot_ChgLength - 10; // 使用 PV 配置的换道距离
|
|
|
|
WheelMotorSpeedInit(); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 1: // 上转
|
|
|
|
VehicleAutoTurn(angle, 90, Chg_Pa.KPCR1, Chg_Pa.KDCR1); // 转到90°前
|
|
|
|
continueTurn_up(90, angle); |
|
|
|
|
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag = 2; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
Chg_Pa.isTurn = false; // 转完置位
|
|
|
|
|
|
|
|
// 记录电机换道位置,根据左右换道标志调整
|
|
|
|
if (GV.PV.Robot_LaneChange_Direction == 0) // 左换道
|
|
|
|
{ |
|
|
|
Chg_Pa.Ini_Postition_L = GV.LeftFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_L = GV.LeftFrontMotor.Position - WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
Chg_Pa.Ini_Postition_R = GV.RightFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_R = GV.RightFrontMotor.Position + WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
} |
|
|
|
else if (GV.PV.Robot_LaneChange_Direction == 1) // 右换道
|
|
|
|
{ |
|
|
|
Chg_Pa.Ini_Postition_L = GV.LeftFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_L = GV.LeftFrontMotor.Position + WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
Chg_Pa.Ini_Postition_R = GV.RightFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_R = GV.RightFrontMotor.Position - WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 2: // 水平后退
|
|
|
|
if (GV.PV.Robot_LaneChange_Direction == 0) // 左换道
|
|
|
|
{ |
|
|
|
GV.LeftFrontMotor.Target_Velcity = -WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
GV.RightFrontMotor.Target_Velcity = WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
} |
|
|
|
else if (GV.PV.Robot_LaneChange_Direction == 1) // 右换道
|
|
|
|
{ |
|
|
|
GV.LeftFrontMotor.Target_Velcity = WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
GV.RightFrontMotor.Target_Velcity = -WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
} |
|
|
|
|
|
|
|
//记录电机换道位置
|
|
|
|
Chg_Pa.Ini_Postition_L = GV.LeftFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_L = GV.LeftFrontMotor.Position - WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
Chg_Pa.Ini_Postition_L = GV.RightFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_R = GV.RightFrontMotor.Position + WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 2: //到了90°之后就开始水平后退
|
|
|
|
GV.LeftFrontMotor.Target_Velcity= -WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
GV.RightFrontMotor.Target_Velcity= WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
int m = Chg_Pa.Final_Postition_L - GV.LeftFrontMotor.Position; |
|
|
|
int n = Chg_Pa.Final_Postition_R - GV.RightFrontMotor.Position; |
|
|
|
if (m > 0 && n < 0) |
|
|
|
if ((GV.PV.Robot_LaneChange_Direction == 0 && m > 0 && n < 0) || |
|
|
|
(GV.PV.Robot_LaneChange_Direction == 1 && m < 0 && n > 0)) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=3; |
|
|
|
Chg_Pa.change_road_straight_count=0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity=0; |
|
|
|
GV.RightFrontMotor.Target_Velcity=0; |
|
|
|
} |
|
|
|
break; |
|
|
|
case 3: |
|
|
|
VehicleAutoTurn((angle),0,Chg_Pa.KPCR1,Chg_Pa.KDCR1); //转回来,没到0右转
|
|
|
|
continueTurn_up(0, (angle)); |
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity=0; |
|
|
|
GV.RightFrontMotor.Target_Velcity=0; |
|
|
|
Chg_Pa.change_road_finish_flag=1; |
|
|
|
GV.Chg_Flag = 1; |
|
|
|
Chg_Pa.isTurn=false; |
|
|
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Chg_Pa.change_road_status_flag = 3; |
|
|
|
Chg_Pa.change_road_straight_count = 0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 3: // 转回来
|
|
|
|
VehicleAutoTurn(angle, 0, Chg_Pa.KPCR1, Chg_Pa.KDCR1); // 转回0°前
|
|
|
|
continueTurn_up(0, angle); |
|
|
|
|
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag = 0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
Chg_Pa.change_road_finish_flag = 1; |
|
|
|
GV.Chg_Flag = 1; |
|
|
|
Chg_Pa.isTurn = false; |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ChgDown() |
|
|
|
{ |
|
|
|
//上换道
|
|
|
|
double angle; |
|
|
|
angle = GV.Robot_Angle.RF_Angle_Roll / 100.0; |
|
|
|
if(Chg_Pa.change_road_finish_flag==0) |
|
|
|
{ |
|
|
|
switch(Chg_Pa.change_road_status_flag) |
|
|
|
{ |
|
|
|
case 0: |
|
|
|
|
|
|
|
if(angle >= -45 |
|
|
|
&& (angle <= 45)) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=1; |
|
|
|
GV.Chg_Flag = 0; |
|
|
|
Chg_Pa.ChgWidth = GV.PV.Robot_ChgLength-10; |
|
|
|
WheelMotorSpeedInit(); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 1: |
|
|
|
VehicleAutoTurn((angle), -90, Chg_Pa.KPCR1, Chg_Pa.KDCR1); //没到-90左转
|
|
|
|
continueTurn_up(-90, (angle)); |
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=2; |
|
|
|
GV.LeftFrontMotor.Target_Velcity=0; |
|
|
|
GV.RightFrontMotor.Target_Velcity=0; |
|
|
|
Chg_Pa.isTurn=false;//换完置位
|
|
|
|
// 下换道
|
|
|
|
double angle = GV.Robot_Angle.RF_Angle_Roll / 100.0; |
|
|
|
|
|
|
|
if (Chg_Pa.change_road_finish_flag == 0) |
|
|
|
{ |
|
|
|
switch (Chg_Pa.change_road_status_flag) |
|
|
|
{ |
|
|
|
case 0: // 初始化换道
|
|
|
|
if (angle >= -45 && angle <= 45) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag = 1; |
|
|
|
GV.Chg_Flag = 0; |
|
|
|
Chg_Pa.ChgWidth = GV.PV.Robot_ChgLength - 10; // 使用 PV 配置的换道距离
|
|
|
|
WheelMotorSpeedInit(); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 1: // 下转
|
|
|
|
VehicleAutoTurn(angle, -90, Chg_Pa.KPCR1, Chg_Pa.KDCR1); // 转到-90°前
|
|
|
|
continueTurn_up(-90, angle); |
|
|
|
|
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag = 2; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
Chg_Pa.isTurn = false; // 转完置位
|
|
|
|
|
|
|
|
// 记录电机换道位置,根据左右换道标志调整
|
|
|
|
if (GV.PV.Robot_LaneChange_Direction == 0) // 左换道
|
|
|
|
{ |
|
|
|
Chg_Pa.Ini_Postition_L = GV.LeftFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_L = GV.LeftFrontMotor.Position - WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
Chg_Pa.Ini_Postition_R = GV.RightFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_R = GV.RightFrontMotor.Position + WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
} |
|
|
|
else if (GV.PV.Robot_LaneChange_Direction == 1) // 右换道
|
|
|
|
{ |
|
|
|
Chg_Pa.Ini_Postition_L = GV.LeftFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_L = GV.LeftFrontMotor.Position + WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
Chg_Pa.Ini_Postition_R = GV.RightFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_R = GV.RightFrontMotor.Position - WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 2: // 水平后退
|
|
|
|
if (GV.PV.Robot_LaneChange_Direction == 0) // 左换道
|
|
|
|
{ |
|
|
|
GV.LeftFrontMotor.Target_Velcity = -WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
GV.RightFrontMotor.Target_Velcity = WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
} |
|
|
|
else if (GV.PV.Robot_LaneChange_Direction == 1) // 右换道
|
|
|
|
{ |
|
|
|
GV.LeftFrontMotor.Target_Velcity = WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
GV.RightFrontMotor.Target_Velcity = -WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
} |
|
|
|
|
|
|
|
//记录电机换道位置
|
|
|
|
Chg_Pa.Ini_Postition_L = GV.LeftFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_L = GV.LeftFrontMotor.Position + WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
Chg_Pa.Ini_Postition_L = GV.RightFrontMotor.Position; |
|
|
|
Chg_Pa.Final_Postition_R = GV.RightFrontMotor.Position - WheelMotorPositionGet(Chg_Pa.ChgWidth); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 2: //到了-90°之后就开始水平前进
|
|
|
|
GV.LeftFrontMotor.Target_Velcity= WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
GV.RightFrontMotor.Target_Velcity= -WheelMotorSpeedGet(Chg_Pa.speed); |
|
|
|
int m = Chg_Pa.Final_Postition_L - GV.LeftFrontMotor.Position; |
|
|
|
int n = Chg_Pa.Final_Postition_R - GV.RightFrontMotor.Position; |
|
|
|
if (m < 0 && n > 0) |
|
|
|
if ((GV.PV.Robot_LaneChange_Direction == 0 && m > 0 && n < 0) || |
|
|
|
(GV.PV.Robot_LaneChange_Direction == 1 && m < 0 && n > 0)) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=3; |
|
|
|
Chg_Pa.change_road_straight_count=0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity=0; |
|
|
|
GV.RightFrontMotor.Target_Velcity=0; |
|
|
|
} |
|
|
|
break; |
|
|
|
case 3: |
|
|
|
VehicleAutoTurn((angle),0,Chg_Pa.KPCR1,Chg_Pa.KDCR1); //转回来,没到0右转
|
|
|
|
continueTurn_up(0, (angle)); |
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag=0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity=0; |
|
|
|
GV.RightFrontMotor.Target_Velcity=0; |
|
|
|
Chg_Pa.change_road_finish_flag=1; |
|
|
|
GV.Chg_Flag = 1; |
|
|
|
Chg_Pa.isTurn=false; |
|
|
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
Chg_Pa.change_road_status_flag = 3; |
|
|
|
Chg_Pa.change_road_straight_count = 0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 3: // 转回来
|
|
|
|
VehicleAutoTurn(angle, 0, Chg_Pa.KPCR1, Chg_Pa.KDCR1); // 转回0°前
|
|
|
|
continueTurn_up(0, angle); |
|
|
|
|
|
|
|
if (Chg_Pa.isTurn) |
|
|
|
{ |
|
|
|
Chg_Pa.change_road_status_flag = 0; |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
Chg_Pa.change_road_finish_flag = 1; |
|
|
|
GV.Chg_Flag = 1; |
|
|
|
Chg_Pa.isTurn = false; |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -595,6 +642,7 @@ void ChgLeft() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ChgRight() |
|
|
|
{ |
|
|
|
//上换道
|
|
|
|
@ -665,10 +713,10 @@ void ChgRight() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ChgFinish() |
|
|
|
{ |
|
|
|
GV.LeftFrontMotor.Target_Velcity = 0; |
|
|
|
|
|
|
|
GV.RightFrontMotor.Target_Velcity = 0; |
|
|
|
} |
|
|
|
|
|
|
|
|