Laser.c 7.92 KB
#include "Laser.h"
#if P_SETUP_NAV_TYPE == 2 || P_SETUP_NAV_TYPE == 4
uint16_t SlamAddCheckSumCRC(unsigned char *cBuffer, unsigned short iBufLen)
{
    uint16_t wCrc = 0;
    unsigned char Sum = 0, i = 0;
    for (i = 1; i < iBufLen; i++)
    {
        Sum += cBuffer[i];
    }
    return Sum;
}

float X1speed = 0, X2speed = 0, Xspeed = 0;
unsigned int Prepare_UartToROS_Send(unsigned char string_out[])
{
    short i = 0;
    unsigned char sum = 0;
    static unsigned char string_len = 0;
	
//		static float A = 0,B = 0,R1 = 0,R2 = 0;//中间舵轮和右舵轮角度差
//		if(navi.Private.SetCalculationRadius != 0)
//		{
//			A = fabs(atan2(DISWHEELTOCENTER,navi.Private.SetCalculationRadius) - atan2(DISWHEELTOCENTER,navi.Private.SetCalculationRadius + RIGHTSTEERDISTANCE));
//			
//			R1 = sqrt(pow(navi.Private.SetCalculationRadius + RIGHTSTEERDISTANCE,2) + pow(DISWHEELTOCENTER,2));
//			
//			R2 = sqrt(pow(navi.Private.SetCalculationRadius,2) + pow(DISWHEELTOCENTER,2));
//			
//			B = R2/R1;
//		}
//		else 
//		{
//			A = 0;
//			B = 1;
//		}

		float d1,d2;
		if(DriverSteering1.Public.encoderPose != 0)
		{
			 d1 = DISWHEELTOCENTER/sin((float)(DriverSteering1.Public.encoderPose)/10/57.3);
			
			 d2 = d1*cos((float)(DriverSteering1.Public.encoderPose)/10/57.3) - RIGHTSTEERDISTANCE;
		}
		else
		{
			d1 = 1;
			d2 = 1;
		}
			
		Xspeed = 1*(float)(DriverSteering1.Public.encoderSpeed) / 3600*d2/d1;
		
		
//		if(agv.Command.CurDirection == 3 || agv.Command.CurDirection == 4||agv.Command.CurDirection == 5 || agv.Command.CurDirection == 6)
//		{
//			Xspeed = 0;
//		}
    //  double TimeStamp = ((double)TIM_GetCounter(TIM7)) / (4000.0f / 0.95);
    //	TIM_SetCounter(TIM7, 0);

    string_len = sprintf((char *)string_out, "$%.3f,%d,%.3f,%d,%d,%d,%d*", Xspeed, 0, 0.02, 1,0,0,0);
		

    for (i = 1; i < string_len - 1; i++)
    {
        sum += string_out[i];
    }
    string_len += sprintf((char *)string_out + string_len, "%02X#\r\n", sum);
    return string_len;
}

void UartToROS_Send_Info_To_Server()
{
    unsigned char atSendBuf[400] = {0};
    unsigned int len;
		static int lastTime = 0;
    len = Prepare_UartToROS_Send(atSendBuf);
		if(agv.Public.SystemTime - lastTime > 19)
		{	
			WriteUart(COM2, atSendBuf, len);
			lastTime = agv.Public.SystemTime;
		}
    
}

void ProccessAGVInfo(unsigned char *RecvBuffer, unsigned short BufferLength)
{
    if (strncmp("$", (const char *)RecvBuffer, 1) == 0) //地标动作命令
    {
        unsigned char Sum = 0;
			 unsigned char i = 1;
    float XbiasFromros = 0, YbiasFromros = 0, TheataFromros = 0,Theta = 0,locT = 0,palstance = 0;
    int Location_State_ = 0;
    int SLAMerror = 0,Online_State_ = 0,datax1 = 0,datay1 = 0;
		static int laserState1 = 0,laserState2 = 0,laserState3 = 0;
//        unsigned char GetSum = 0;
				DATA GetSum;
				int tmp = 0;
        sscanf((const char *)RecvBuffer + 1, "%f,%f,%f,%d,%d,%f,%f,%f,%d,%d,%d*%02X#", &XbiasFromros, &YbiasFromros, &TheataFromros, &Online_State_, &SLAMerror, &locT, &Theta,&palstance,&laserState1,&laserState2,&laserState3,&tmp);


			GetSum.a = (u8)tmp;
			
        while (RecvBuffer[i] != '*') /*校验起始到结束信号*/
        {
            Sum += RecvBuffer[i];
            i++;
            if (i > BufferLength)
            {
                return;
            }
        }

        if (Sum == GetSum.a)
        {

				if(agv.Parameter.W_compensation != 0)
            TheataFromros += (agv.Parameter.W_compensation / 10) * 0.017453; //得到车体正前方角度

						TheataFromros += PI;
            if (TheataFromros >= 2.0f * PI)
            {
                TheataFromros -= 2.0f * PI;
            }
            else if (TheataFromros < 0)
            {
                TheataFromros += 2.0f * PI;
            }
						if(TheataFromros > PI)
						{
							TheataFromros -= 2*PI;
						}
						else if(TheataFromros < -PI)
						{
							TheataFromros += 2*PI;
						}

            agv.Public.Coordinate_X = XbiasFromros * 1000;

            agv.Public.Coordinate_Y = YbiasFromros * 1000;

            agv.Public.Coordinate_W = (TheataFromros);
						
						if((agv.Parameter.AGVID == 0xB001) &&(fabs)(agv.Public.Coordinate_W*57.3) < 110 && (fabs)(agv.Public.Coordinate_W*57.3) > 70)//正负90度方向做补偿
						{
							datax1 = -15;
							datay1 = -10;
						}
						else if((agv.Parameter.AGVID == 0xB002) &&(fabs)(agv.Public.Coordinate_W*57.3) < 110 && (fabs)(agv.Public.Coordinate_W*57.3) > 70)//正负90度方向做补偿
						{
							datax1 = -15;
							datay1 = 0;
						}
						else
						{
							datax1 = 0;
							datay1 = 0;
						}

#if P_SETUP_LIDAR2BASE_X != 0//+65
            agv.Public.Coordinate_X -= cos(agv.Public.Coordinate_W) * (float)(P_SETUP_LIDAR2BASE_X + datax1);
            agv.Public.Coordinate_Y -= sin(agv.Public.Coordinate_W) * (float)(P_SETUP_LIDAR2BASE_X + datax1);
#endif

#if P_SETUP_LIDAR2BASE_Y != 0//+17
            agv.Public.Coordinate_X += sin(TheataFromros) * (float)(P_SETUP_LIDAR2BASE_Y + datay1);
            agv.Public.Coordinate_Y -= cos(TheataFromros) * (float)(P_SETUP_LIDAR2BASE_Y + datay1);
#endif

						
						//叉车第四层库位补偿
						if(DriverLifter1.Public.encoderPose > 4700)//第四层  
						{
							if(agv.Parameter.AGVID != 0xB003)
							{
								if(agv.Public.Coordinate_W*57.3 > 75 && agv.Public.Coordinate_W*57.3 < 105)
								{
									agv.Public.Coordinate_Y +=  70;
								}
								else if(agv.Public.Coordinate_W*57.3 > -105 && agv.Public.Coordinate_W*57.3 < -75)
								{
									agv.Public.Coordinate_Y -= 70;
								}
							}
							else
							{
								if(agv.Public.Coordinate_W*57.3 > 75 && agv.Public.Coordinate_W*57.3 < 105)
								{
									agv.Public.Coordinate_Y +=  90;
								}
								else if(agv.Public.Coordinate_W*57.3 > -105 && agv.Public.Coordinate_W*57.3 < -75)
								{
									agv.Public.Coordinate_Y -= 90;
								}
							}
						}
						else if(DriverLifter1.Public.encoderPose > 3000)
						{
							if(agv.Public.Coordinate_W*57.3 > 75 && agv.Public.Coordinate_W*57.3 < 105)
							{
								agv.Public.Coordinate_Y += 30;
							}
							else if(agv.Public.Coordinate_W*57.3 > -105 && agv.Public.Coordinate_W*57.3 < -75)
							{
								agv.Public.Coordinate_Y -= 30;
							}
						}
							//一层
							else if(DriverLifter1.Public.encoderPose < 130 && DriverLifter1.Public.encoderPose > 110 )
							{
								if(agv.Public.Coordinate_W*57.3 > 75 && agv.Public.Coordinate_W*57.3 < 105)
								{
									agv.Public.Coordinate_Y -= 35;
								}
								else if(agv.Public.Coordinate_W*57.3 > -105 && agv.Public.Coordinate_W*57.3 < -75)
								{
									agv.Public.Coordinate_Y += 35;
								}
							}
						
						//定位补偿
//						if(agv.Parameter.AGVID == 0xB002)
//						{
//							//P007输送线
//							if(agv.Command.NextLandMarkID == 1203)
//							
//						}
    }
	}
}

void ProccessAGVDATAInfo(unsigned char *Res) //获取到完整一帧数据
{
    static unsigned char Buffer2[120];
    static unsigned short iii = 0;

    if (iii > 100)
    {
        iii = 0;
    }
    if (*Res == '$') // $
    {
        iii = 0;
    }
    Buffer2[iii++] = *Res;
    if (*Res == '#') // #
    {
        ProccessAGVInfo((unsigned char *)Buffer2, iii);
				memset(Buffer2,0,sizeof(Buffer2));
        iii = 0;
    }
}

void ProcessDataFormUartSlam()
{
    static int ConnectSlamTime = 0, iii = 0;
    unsigned char buff[64];
    unsigned int len;
    unsigned i;
    len = ReadUart(COM2, buff, 64);

    if (len > 0)
    {
        for (i = 0; i < len; i++)
        {
            ProccessAGVDATAInfo(buff + i);
        }
        ConnectSlamTime = 0;
        agv.Public.SlamConnectError = 0;
        Reset_Alarm(0x2000);
    }
    else
    {
        ConnectSlamTime++;

        if (ConnectSlamTime >= 500)
        {
            agv.Public.SlamConnectError = 1;
            SetAlarm(0x2000);
            if (iii++ > 500)
            {
                iii = 0;
                Uart_Printf(COM1, "激光定位丢失!!!\r\n");
            }
        }
    }
}

void SlamDataProcess()
{
    UartToROS_Send_Info_To_Server(); //串口发送给ROS

    ProcessDataFormUartSlam(); //串口读取ROS数据
}
#endif