Laser.c
7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#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;
}
// Uart_Printf(COM1,"速度 = %.3f 角度 = %.3f ^^^\r\n",(float)(DriverSteering1.Public.encoderSpeed) / 3600,(float)(DriverSteering1.Public.encoderPose)/10/57.3);
if(DriverSteering1.Public.encoderPose > 0)
{
DriverSteering1.Public.encoderPose += A;
}
else if(DriverSteering1.Public.encoderPose < 0)
{
DriverSteering1.Public.encoderPose -= A;
}
Xspeed = 1*(float)(DriverSteering1.Public.encoderSpeed) / 3600*cos((float)(DriverSteering1.Public.encoderPose)/10/57.3);
if(agv.Command.CurDirection == 3 || agv.Command.CurDirection == 6)
{
Xspeed = 0;
}
string_len = sprintf((char *)string_out, "$%.3f,%d,%.3f,%d,%d,%d,%d*", Xspeed, 0, 0.012, 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;
len = Prepare_UartToROS_Send(atSendBuf);
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,Theta = 0,locT = 0,palstance = 0;
int Location_State_ = 0;
int SLAMerror = 0,Online_State_ = 0;
static float lastXdata = 0,lastYdata = 0;
static int laserState1 = 0,laserState2 = 0,laserState3 = 0;
if (strncmp("$", (const char *)RecvBuffer, 1) == 0) //地标动作命令
{
unsigned char Sum = 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 P_SETUP_LIDAR2BASE_THETA != 0
TheataFromros += ((float)P_SETUP_LIDAR2BASE_THETA / 10) * 0.017453; //得到车体正前方角度
#endif
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 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 ProccessCameraInfo(unsigned char *RecvBuffer, unsigned short BufferLength)
{
unsigned char i = 1;
float posOffsetValue = 0, distanceTarget = 0,unkown = 0;
int Location_State_ = 0,MateriaFlag = 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*%02X#", &posOffsetValue, &distanceTarget, &unkown, &MateriaFlag, &GetSum);
while (RecvBuffer[i] != '*') /*校验起始到结束信号*/
{
Sum += RecvBuffer[i];
i++;
if (i > BufferLength)
{
return;
}
}
// if (Sum == GetSum)
// {
// agv.Public.posOffsetValue = posOffsetValue;
//
// agv.Public.distanceTarget = distanceTarget;
//
// agv.Public.MateriaFlag = MateriaFlag;
//
// }
}
}
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 ProccessAGVCameraInfo(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 == '#') // #
{
ProccessCameraInfo((unsigned char *)Buffer2, iii);
iii = 0;
}
}
void ProcessDataFormUartCamera()
{
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++)
{
ProccessAGVCameraInfo(buff + i);
}
ConnectSlamTime = 0;
Reset_Alarm(0x8000);
}
else
{
ConnectSlamTime++;
if (ConnectSlamTime >= 500)
{
SetAlarm(0x8000);
if (iii++ > 500)
{
iii = 0;
Uart_Printf(COM1, "视觉相机通讯!!!\r\n");
}
}
}
}
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);
ProccessAGVCameraInfo(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数据
// ProcessDataFormUartCamera();
}
#endif