Laser.c 4.83 KB
#include "Laser.h"
#if P_SETUP_NAV_TYPE == 2
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;

    X1speed = (float)DriverMotor1.Public.encoderSpeed / 1000.0f;
    X2speed = (float)DriverMotor2.Public.encoderSpeed / 1000.0f;
    Xspeed = (X1speed + X2speed) / 2;

    //  double TimeStamp = ((double)TIM_GetCounter(TIM7)) / (4000.0f / 0.95);
    //	TIM_SetCounter(TIM7, 0);

    string_len = sprintf((char *)string_out, "$%.3f,%d,%.3f,%d*", Xspeed, 0, 0.011, 1);
    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()
{
    static u32 LastTime = 0;
    unsigned char atSendBuf[400] = {0};
    unsigned int len;
    len = Prepare_UartToROS_Send(atSendBuf);
    if (agv.Public.SystemTime - LastTime >= 12)
    {
        LastTime = agv.Public.SystemTime;
        WriteUart(COM2, atSendBuf, len);
    }
}

int QRDataFlag;
float QR_XbiasFromros = 0, QR_YbiasFromros = 0;
void ProccessAGVInfo(unsigned char *RecvBuffer, unsigned short BufferLength)
{
    unsigned char i = 1;
    float XbiasFromros = 0, YbiasFromros = 0, TheataFromros = 0;
    int Location_State_ = 0;
    int SLAMerror = 0;

    if (strncmp("$", (const char *)RecvBuffer, 1) == 0) //地标动作命令
    {
        unsigned char Sum = 0;
        unsigned char GetSum = 0;

        sscanf((const char *)RecvBuffer + 1, "%f,%f,%f,%d,%d,%f,%f,%d*%02X#", &XbiasFromros, &YbiasFromros, &TheataFromros, &Location_State_, &SLAMerror, &QR_XbiasFromros, &QR_YbiasFromros, &QRDataFlag, &GetSum);

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

        if (Sum == GetSum)
        {

#if P_SETUP_LIDAR2BASE_THETA != 0
            TheataFromros += ((float)P_SETUP_LIDAR2BASE_THETA / 10) * 0.017453; //得到车体正前方角度
#endif

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

            agv.Public.Coordinate_X = XbiasFromros * 1000;

            agv.Public.Coordinate_Y = YbiasFromros * 1000;

            agv.Public.Coordinate_W = (TheataFromros);

#if P_SETUP_LIDAR2BASE_X != 0
            agv.Public.Coordinate_X -= cos(TheataFromros) * (float)P_SETUP_LIDAR2BASE_X;
            agv.Public.Coordinate_Y -= sin(TheataFromros) * (float)P_SETUP_LIDAR2BASE_X;
#endif

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

            if (QRDataFlag) //悬臂轴扫到二位码补偿并自动纠偏
            {
                agv.Public.QR_XbiasFromros = QR_XbiasFromros + agv.Public.QR_Compensation_X; //向左

                agv.Public.QR_YbiasFromros = QR_YbiasFromros + agv.Public.QR_Compensation_Y; //向下
            }
            else //无二维码不补偿
            {
                agv.Public.QR_XbiasFromros = 0;

                agv.Public.QR_YbiasFromros = 0;
            }
        }
    }
}

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);
        iii = 0;
    }
}

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

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

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

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

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