SingleSteering.c 8.89 KB
#include "SingleSteering.h"
#if P_SETUP_CHASSIS_MODE == 2


void chassisControlManual()//单舵轮点动
{
	static int lastDir = 0;
	DriverSteering1.Command.angle = usRegHoldingBuf[22];//设置舵轮角度
	
	switch (agv.Command.HandMotorState)
	{
	case 0x00:
		break;
	case 10:
		DriverSteering1.Command.speed = 0;
		DriverSteering1.Command.angle = 0;
		break;
	case 0x02://前动力左转	
		DriverSteering1.Command.speed = agv.Parameter.HandSpeed*lastDir;
		DriverSteering1.Command.angle = usRegHoldingBuf[22];//设置舵轮角度
	
	break;		
	case 0x01://前动力右转	
		DriverSteering1.Command.speed = agv.Parameter.HandSpeed*lastDir;
		DriverSteering1.Command.angle = -usRegHoldingBuf[22];//设置舵轮角度
	
	break;		
	case 0x03://前进
		DriverSteering1.Command.speed = agv.Parameter.HandSpeed;
		DriverSteering1.Command.angle = 0;
		lastDir = 1;

		break;
	case 0x04://后退 
		DriverSteering1.Command.speed = -agv.Parameter.HandSpeed;
		lastDir = -1;
		DriverSteering1.Command.angle = 0;
		
		break;
	default:
		break;
	}
	setMotorSpeedSlope(agv.Parameter.HandSpeedSlope);
}

//设置驱动器自动速度
float AngleDifValue;
int angleOffset = 0;
void chassisControlAuto()
{	
//	LaserSlowDownProcess();//雷达避障减速处理
		
	if(navi.Private.OutputOffset > 0)
	{
		angleOffset = (int)(mapping(navi.Private.OutputOffset,0,100,2,20)*10);
	}
	else if(navi.Private.OutputOffset < 0)
	{
		angleOffset = (int)(mapping(navi.Private.OutputOffset,-100,0,-20,-2)*10);
	}
	//根据转弯半径大小,计算打舵角度
	if(navi.Private.SetCalculationRadius != 0)
	AngleDifValue = atan(DISWHEELTOCENTER/navi.Private.SetCalculationRadius)*57.3*10;//打舵角度
	if(AngleDifValue>0) AngleDifValue+=20;
	if(AngleDifValue<0) AngleDifValue-=20;
	
	switch (agv.Command.CurDirection)
	{
	case 0:
		DriverSteering1.Command.speed = 0;
		break;
	
	case 1:
		if(agv.Command.SetBaseSpeed == 0)
		{
			DriverSteering1.Command.speed = 0;
		}
		else
		{
			DriverSteering1.Command.angle = -angleOffset;
			DriverSteering1.Command.speed = agv.Command.SetBaseSpeed;
		}
		break;
	
	case 2:
		if(agv.Command.SetBaseSpeed == 0)
		{
			DriverSteering1.Command.speed = 0;
		}
		else
		{
			DriverSteering1.Command.angle = -angleOffset;
			DriverSteering1.Command.speed = -agv.Command.SetBaseSpeed;
		}
		break;
	
	case 3:
		
		break;
	
	case 4:
		
		break;
	
	case 5:
		
		break;
	
	case 6:
		
		break;
	
	case 7://前进弧线左转,左轮设定速度-固定速度差-补偿值
			DriverSteering1.Command.angle = -AngleDifValue;
			DriverSteering1.Command.speed = agv.Command.SetBaseSpeed;
		break;
	
	case 8://前进弧线右转,左轮设定速度+固定速度差+补偿值
			DriverSteering1.Command.angle = -AngleDifValue;
			DriverSteering1.Command.speed = agv.Command.SetBaseSpeed;
		break;
	
	case 9://后退弧线左转,左轮设定速度+固定速度差-补偿值,给负速度
			DriverSteering1.Command.angle = -AngleDifValue;
			DriverSteering1.Command.speed = -agv.Command.SetBaseSpeed;
		break;
	
	case 10://后退弧线右转,左轮设定速度-固定速度差+补偿值,给负速度
			DriverSteering1.Command.angle = -AngleDifValue;
			DriverSteering1.Command.speed = -agv.Command.SetBaseSpeed;
		break;
	
	default:
			DriverSteering1.Command.angle = 0;
			DriverSteering1.Command.speed = 0;
		break;
	}
}

//单舵轮直行
void SingleSteeringRunStraight()
{	
	if (agv.Command.CurDirection == agv.Command.NextDirection) //如果当前方向和下次方向相同
    {
        agv.Command.SetBaseSpeed = agv.Command.DispatchSpeed; //设定速度为系统下发速度
    }                                                         //如果记下来是弧线转弯,做减速处理
    else if (agv.Command.NextDirection == 7 || agv.Command.NextDirection == 8 || agv.Command.NextDirection == 9 || agv.Command.NextDirection == 10)
    {
        if (navi.Public.VerticalDistanceAgvToTARGET >= agv.Parameter.AdvanceParkPos) //agv到终点距离大于预停车距离
        {
            agv.Command.SetBaseSpeed = agv.Command.DispatchSpeed; //按系统下发速度走
        }
        else //agv到终点距离小于预停车距离,减速到设定自动转弯速度
        {
            agv.Command.SetBaseSpeed = mapping(navi.Public.VerticalDistanceAgvToTARGET, 0, agv.Parameter.AdvanceParkPos, agv.Command.NextDispatchSpeed, agv.Command.DispatchSpeed);
        }
    }
    else //停车或者原地转
    {
        if (navi.Public.VerticalDistanceAgvToTARGET >= agv.Parameter.AdvanceParkPos) //如果agv到目标点距离大于预停车距离
        {
            agv.Command.SetBaseSpeed = agv.Command.DispatchSpeed; //设定速度等于系统下发速度
        }
        else if (navi.Public.VerticalDistanceAgvToTARGET <= agv.Parameter.AdvanceParkPos && navi.Public.VerticalDistanceAgvToTARGET > POSARRIVE_RANGE) //小于预停车距离,大于到点判断范围10MM,驱动器不能给太小速度,不能直接到0,设置了预停车速度
        {
            agv.Command.SetBaseSpeed = mapping(navi.Public.VerticalDistanceAgvToTARGET, 0, agv.Parameter.AdvanceParkPos, agv.Parameter.AdvanceParkSpeed, agv.Command.DispatchSpeed);
        }
        else
            agv.Command.SetBaseSpeed = 0;
    }
    if (navi.Public.VerticalDistanceAgvToSTART < START_SPEEDUP_RANGE)
    {
        if (fabs(DriverMotor1.Public.encoderSpeed - DriverMotor2.Public.encoderSpeed) > START_SPEEDDIFF_LIMIT) //速度差太大
        {
            agv.Command.SetBaseSpeed = fabs(DriverMotor1.Public.encoderSpeed + DriverMotor2.Public.encoderSpeed) / 2;
        }
        setMotorSpeedSlope(2);
    }
    else
        setMotorSpeedSlope(5);
}

//单舵轮弧线转弯
void SingleSteeringRunTurnning()
{	
	if (fabs(navi.Public.AngleDifference) >= ADVANCE_STOPSLOPE_ANGLE) //如果agv角度差大于预停车角度差
    {
        agv.Command.SetBaseSpeed = agv.Command.DispatchSpeed;
        setMotorSpeedSlope(2);
    }
    else //如果agv角度差小于预停车角度差,开始减速至设定出弯速度
    {
        agv.Command.SetBaseSpeed = mapping(fabs(navi.Public.AngleDifference), 0, ADVANCE_STOPSLOPE_ANGLE, ARC_TURNSPEED_MIN, agv.Command.DispatchSpeed);
        setMotorSpeedSlope(5);
    }
}

//单舵轮原地转弯
void SingleSteeringPirouette()
{
    static float agvInAngle = 0,turnAngleDiff = 0,lastAngleDiff = 0;
    static u8 TurnStep = 0, TurnArriveFlag = 0;
    if (TurnStep == 0) //求出位置信息
    {
        navi.Private.TarAngle = CalculatingDirectionAngle(StartPoint, TargetPoint); //求出目标方向角度

        agv.Command.SetBaseSpeed = 0;

        TurnArriveFlag = 0;
				
				if(agv.Command.CurDirection == 3||agv.Command.CurDirection == 6)//打舵
					DriverSteering1.Command.angle = 900;
				else
					DriverSteering1.Command.angle = -900;
				if(fabs(DriverSteering1.Public.encoderPose*57.3*10 - DriverSteering1.Command.angle)<10)//打舵到位,目标舵轮角度和实际舵轮角度差小于1度
				{
					TurnStep = 1;
				}
    }
    else if (TurnStep == 1) //开始转弯
    {
        if (agv.Command.CurDirection == 3 || agv.Command.CurDirection == 4) //前进左右转
				{
					turnAngleDiff = CalculatingCurrentAndTargetAngle(CurrentCenterPoint.CurAngle, navi.Private.TarAngle);
				
					agvInAngle = fabs(CalculatingCurrentAndTargetAngle(CurrentCenterPoint.CurAngle, navi.Private.TarAngle));
				}
        else //后退左右转
				{
					turnAngleDiff = CalculatingCurrentAndTargetAngle(CurrentCenterPoint.CurAngle + PI, navi.Private.TarAngle);
					
					agvInAngle = fabs(CalculatingCurrentAndTargetAngle(CurrentCenterPoint.CurAngle + PI, navi.Private.TarAngle));
				}
        if (agvInAngle >= PI * 2)

            agvInAngle -= PI * 2;

        if (agvInAngle >= ADVANCE_STOPSLOPE_ANGLE)
        {
            agv.Command.SetBaseSpeed = TURNSPEED_MAX;
        }
        else if ((agvInAngle < ADVANCE_STOPSLOPE_ANGLE) && (agvInAngle > TURNINGOFF_ANGLE))
        {
            agv.Command.SetBaseSpeed = (int)mapping(agvInAngle, TURNINGOFF_ANGLE, ADVANCE_STOPSLOPE_ANGLE, TURNSPEED_MIN, TURNSPEED_MAX);
        }
        else if ((agvInAngle <= TURNINGOFF_ANGLE)||(turnAngleDiff*lastAngleDiff < 0))
        {
            TurnArriveFlag = 1;
        }
				lastAngleDiff = turnAngleDiff;
        //出弯
        if (TurnArriveFlag == 1)
        {
            //出弯
					lastAngleDiff = 0;
					
					agv.Command.SetBaseSpeed = 0;
					
					if (fabs(DriverMotor1.Public.encoderSpeed) < 5 && fabs(DriverMotor2.Public.encoderSpeed) < 5)
					{
							DriverSteering1.Command.angle = 0;//回舵
						
							if(fabs(DriverSteering1.Public.encoderPose)<0.03)//回舵完成
							{
									if (agv.Command.CurDirection == 3 || agv.Command.CurDirection == 4)
									{
											traffic_land_marks.land_marks[agv.Command.standSiteID - 1].Direction = 1;

											TurnStep = 0;
									}
									else if (agv.Command.CurDirection == 5 || agv.Command.CurDirection == 6)
									{
											traffic_land_marks.land_marks[agv.Command.standSiteID - 1].Direction = 2;

											TurnStep = 0;
									}
							}
					}
        }
    }
    setMotorSpeedSlope(5);
}

void chassisGetAutoSpeed(u8 *CurrentDir)
{
	if(*CurrentDir == 1||*CurrentDir == 2)//直行
	{
		SingleSteeringRunStraight();//硬差速直行
	}
	else if(*CurrentDir == 3||*CurrentDir == 4||*CurrentDir == 5||*CurrentDir == 6)//原地旋转
	{
	}
	else if(*CurrentDir == 7||*CurrentDir == 8||*CurrentDir == 9||*CurrentDir == 10)//弧线转弯	
	{				
		SingleSteeringRunTurnning();//弧线转弯
	}
}


#endif