Vision.c 5.38 KB
#include "Vision.h"
#if P_SETUP_NAV_TYPE == 3
#define PACK_POSE_BUFF 17
u8 vision_write[PACK_POSE_BUFF];
u8 relocation[14] = {0xAC, 0xED, 0x0C, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 StartSendRelocationSig = 1;

void SendDiffInfo(void)
{
    static u8 i = 0, i1 = 0;
    static bool send_ready = false;
    static int last_time = 0, last_time1 = 0, last_time2 = 0;
    u8 head1 = 0xac;
    u8 head2 = 0xed;
    u8 length = 14;
    u8 cmd = 10;
    int left = 0;
    int right = 0;
    int other = 0;
    u8 check = 0;

    left = (int)(DriverMotor1.Public.encoderSpeed);
    right = (int)(DriverMotor2.Public.encoderSpeed);
    if (i++ > 100)
    {
        i = 0;
        //		Uart_Printf(COM1,"实际速度 = %d %d  电流 = %.1f %.1f\r\n",left,right,DriverMotor1.Public.ampere,DriverMotor2.Public.ampere);
    }

    memset(vision_write, 0, sizeof(vision_write));
    vision_write[0] = head1;
    vision_write[1] = head2;
    vision_write[2] = length;
    vision_write[3] = cmd;

    u8 *pdata = (u8 *)&left;
    int temp_i = 0;
    for (temp_i = 0; temp_i != 4; ++temp_i)
    {
        vision_write[7 - temp_i] = pdata[temp_i];
    }
    pdata = (u8 *)&right;
    for (temp_i = 0; temp_i != 4; ++temp_i)
    {
        vision_write[11 - temp_i] = pdata[temp_i];
    }
    pdata = (u8 *)&other;
    for (temp_i = 0; temp_i != 4; ++temp_i)
    {
        vision_write[15 - temp_i] = pdata[temp_i];
    }
    vision_write[16] = 0;
    if (!send_ready && agv.Public.SystemTime - last_time < 50)
        return;
    send_ready = true;
    last_time = agv.Public.SystemTime;
    static uint16_t index = 0;
    if (agv.Public.SystemTime - last_time2 >= 20)
    {
        WriteUart(COM3, vision_write, 17);
        last_time2 = agv.Public.SystemTime;
    }
    //重定位上报**************
    if (agv.Public.SystemTime - last_time1 > 5000)
    {
        if (agv.Public.OnlinePointSig && StartSendRelocationSig)
        {
            relocation[4] = agv.Public.OnlinePointSig;
            WriteUart(COM3, relocation, 14);
            Uart_Printf(COM1, "重定位ING*********\r\n");
        }
        else if (agv.Public.OnlinePointSig == 0)
        {
            StartSendRelocationSig = 1;
        }
        last_time1 = agv.Public.SystemTime;
    }

    if (index == PACK_POSE_BUFF)
    {
        send_ready = false;
    }
}

void ProccessVisionDATAInfo(unsigned char *Res)
{
    static unsigned char Buffer2[30];
    static unsigned short iii = 0, count = 0;
    static unsigned char LastRes = 0;
    static unsigned char RecvSig = 0;
    unsigned char i = 0, check_value = 0;
    int XXX = 0, YYY = 0;
    short WWW = 0;
    static float LastX = 0, LastY = 0, LastW = 0;
    //	if(iii > 20)
    //	{
    //		iii = 0;
    //	}
    if (*Res == 0xac)
    {
        LastRes = *Res;
    }
    if (LastRes == 0xac && *Res == 0xed)
    {
        LastRes = 0;
        iii = 0;
        RecvSig = 1;
    }
    if (RecvSig)
    {
        Buffer2[iii++] = *Res;
        if (iii > 15)
        {
            RecvSig = 0;
            {
                for (i = 1; i < iii - 1; i++)
                {
                    check_value ^= Buffer2[i];
                }
                if (check_value == Buffer2[iii - 1])
                {
                    if (Buffer2[2] == 1)
                    {
                        WWW = (Buffer2[11] << 8) | Buffer2[12];
                        XXX = (Buffer2[3] << 24) | (Buffer2[4] << 16) | (Buffer2[5] << 8) | Buffer2[6];
                        YYY = (Buffer2[7] << 24) | (Buffer2[8] << 16) | (Buffer2[9] << 8) | Buffer2[10];
                        agv.Public.Coordinate_W = (float)WWW / 1000;

                        agv.Public.Coordinate_X = (float)XXX;
                        agv.Public.Coordinate_Y = (float)YYY;
                        //						Uart_Printf(COM1,"X = %.2f  Y = %.2f  W = %.2f  X变化 = %.2f  Y变化 = %.2f  W变化 = %.2f\r\n",agv.Public.Coordinate_X,agv.Public.Coordinate_Y,agv.Public.Coordinate_W,
                        //						agv.Public.Coordinate_X - LastX,agv.Public.Coordinate_Y - LastY,agv.Public.Coordinate_W - LastW);
                        LastX = agv.Public.Coordinate_X;
                        LastY = agv.Public.Coordinate_Y;
                        LastW = agv.Public.Coordinate_W;
                    }
                    else if (Buffer2[2] == 2) //重定位下发
                    {
                        if (Buffer2[13] == 0)
                        {
                            StartSendRelocationSig = 0;
                            Uart_Printf(COM1, "视觉定位成功********\r\n");
                        }
                    }
                }
            }
        }
    }
}

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

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

        if (ConnectSlamTime >= 1000)
        {
            agv.Public.SlamConnectError = 1; //SLAM连接中断
            SetAlarm(0x2000);
            if (iii++ > 300)
            {
                iii = 0;
                Uart_Printf(COM1, "视觉通讯中断*********\r\n");
            }
        }
    }
}

void SlamDataProcess()
{
    SendDiffInfo();
    ProcessDataFormUartVision();
}
#endif