-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMUGPSComm.cs
583 lines (481 loc) · 18.6 KB
/
IMUGPSComm.cs
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Globalization;
namespace DrRobot.JaguarControl
{
public partial class JaguarCtrl : Form
{
private TcpClient clientSocketIMU = null;
private Thread threadClientIMU = null;
private NetworkStream cmdStreamIMU = null;
private StreamReader readerIMU = null;
private StreamWriter writerIMU = null;
private delegate void updateSensorDataInfo(string data);
private TcpClient clientSocketGPS = null;
private Thread threadClientGPS = null;
private NetworkStream cmdStreamGPS = null;
private StreamReader readerGPS = null;
//private bool firstDataIMU = true;
//private bool firstDataGPS = true;
private bool receivingGPS = false;
private bool receivingIMU = false;
private bool firstSetupComm = true;
//private int DATA_LEN = 14;
private int seqNo = 0;
private double accel_x = 0;
private double accel_y = 0;
private double accel_z = 0;
private double accel_x_offset = 0;
private double accel_y_offset = 0;
private double accel_z_offset = 0;
private double gyro_x = 0;
private double gyro_y = 0;
private double gyro_z = 0;
private double gyro_x_offset = 0; //377
private double gyro_y_offset = 0;
private double gyro_z_offset = 0;
public struct IMURecord
{
public int index;
public double accel_x;
public double accel_y;
public double accel_z;
public double gyro_x;
public double gyro_y;
public double gyro_z;
public double magn_x;
public double magn_y;
public double magn_z;
public double eRoll;
public double ePitch;
public double eYaw;
}
public IMURecord imuRecord = new IMURecord();
public int recordCnt = 0;
public bool startRecord = false;
public StreamWriter SW;
public string fileNme = "c:\\DrRobotAppFile\\DrRobotGPSIMURec";
public int fileCnt = 0;
public const int MAXFILELEN = 32767;
public struct GPSRecord
{
public double latitude;
public double longitude;
public double altitude;
public double seaHeight;
public double geoidalHeight;
public double vog;
public double cog;
public int satNum;
public double timeStamp;
public string latHemi;
public string lngHemi;
public double h_pre;
public double v_pre;
public double p_pre;
public string status;
public int qi;
public string recRaw;
}
public GPSRecord gpsRecord = new GPSRecord();
public const double KNNOT2MS = 0.514444444;
private delegate void updateGPSSensorDataInfo(string data);
private const int DrawDataLen = 200; //around 4 second
private double[] draw_AccelX = new double[DrawDataLen];
private double[] draw_AccelY = new double[DrawDataLen];
private double[] draw_AccelZ = new double[DrawDataLen];
private double[] draw_GyroX = new double[DrawDataLen];
private double[] draw_GyroY = new double[DrawDataLen];
private double[] draw_GyroZ = new double[DrawDataLen];
private int drawStartPoint = 0;
private int drawEndPoint = 0;
public double getAccel_x()
{
return imuRecord.accel_x;
}
public double getAccel_y()
{
return imuRecord.accel_y;
}
public double getAccel_z()
{
return imuRecord.accel_z;
}
// Get Gyro Data
public double getGyro_x()
{
return imuRecord.gyro_x;
}
public double getGyro_y()
{
return imuRecord.gyro_y;
}
public double getGyro_z()
{
return imuRecord.gyro_z;
}
private void startComm()
{
int remotePortGPS = jaguarSetting.GPSPort;
String IPAddrGPS = jaguarSetting.GPSIP;
try
{
clientSocketGPS = new TcpClient();
IAsyncResult result = clientSocketGPS.BeginConnect(IPAddrGPS, remotePortGPS, null, null);
bool success = result.AsyncWaitHandle.WaitOne(500, true);
if (!success)
{
receivingGPS = false;
}
else
{
//firstDataGPS = true;
receivingGPS = true;
threadClientGPS = new Thread(new ThreadStart(HandleClientGPS));
threadClientGPS.CurrentCulture = new CultureInfo("en-US");
threadClientGPS.Start();
}
}
catch
{
receivingGPS = false;
}
//for IMU
int remotePort = jaguarSetting.IMUPort;
String IPAddr = jaguarSetting.IMUIP;
firstSetupComm = true;
try
{
clientSocketIMU = new TcpClient();
IAsyncResult result = clientSocketIMU.BeginConnect(IPAddr, remotePort, null, null);
bool success = result.AsyncWaitHandle.WaitOne(500, true);
if (!success)
{
receivingIMU = false;
}
else
{
//firstDataIMU = true;
receivingIMU = true;
threadClientIMU = new Thread(new ThreadStart(HandleClientIMU));
threadClientIMU.CurrentCulture = new CultureInfo("en-US");
threadClientIMU.Start();
}
}
catch
{
receivingIMU = false;
}
if (receivingGPS && receivingIMU)
{
pictureBoxIMUGPS.Image = imageList1.Images[0];
}
else
{
pictureBoxIMUGPS.Image = imageList1.Images[1];
}
}
private void stopCommunication()
{
if (clientSocketIMU != null)
clientSocketIMU.Close();
pictureBoxIMUGPS.Image = imageList1.Images[1];
receivingIMU = false;
if (writerIMU != null)
{
try
{
writerIMU.Close();
}
catch
{
}
}
if (readerIMU != null)
readerIMU.Close();
if (cmdStreamIMU != null)
cmdStreamIMU.Close();
if (threadClientIMU != null)
threadClientIMU.Abort();
if (clientSocketGPS != null)
clientSocketGPS.Close();
receivingGPS = false;
if (readerGPS != null)
readerGPS.Close();
if (cmdStreamGPS != null)
cmdStreamGPS.Close();
if (threadClientGPS != null)
threadClientGPS.Abort();
}
//for ASCII output
private void HandleClientIMU()
{
try
{
//here process all receive data from client
cmdStreamIMU = clientSocketIMU.GetStream();
readerIMU = new StreamReader(cmdStreamIMU);
writerIMU = new StreamWriter(cmdStreamIMU);
string strRec = "";
while (receivingIMU)
{
try
{
if (!cmdStreamIMU.DataAvailable) //there is no data
{
//need to sleep for other thread
Thread.Sleep(5); //50 Hz
}
else
{
// Reads NetworkStream into a byte buffer.
strRec = readerIMU.ReadLine();
if (strRec != null)
processDataIMU(strRec);
}
}
catch (Exception e)
{
string temp = e.ToString();
}
finally
{
}
}
receivingIMU = false;
readerIMU.Close();
cmdStreamIMU.Close();
clientSocketIMU.Close();
}
catch
{
receivingIMU = false;
}
}
private void processDataIMU(string msg)
{
string[] data = msg.Split(',');
if (data.Length == 10) //the whole package here
{
Invoke(new updateSensorDataInfo(updateSensor), msg);
data[0] = data[0].Remove(0,1);
seqNo = int.Parse(data[0]);
accel_x = double.Parse(data[1] );
accel_y = double.Parse(data[2]);
accel_z = double.Parse(data[3]);
gyro_x = double.Parse(data[4]);
gyro_y = double.Parse(data[5]);
gyro_z = double.Parse(data[6]);
imuRecord.accel_x = accel_x;
imuRecord.accel_y = accel_y;
imuRecord.accel_z = accel_z;
imuRecord.gyro_x = gyro_x;
imuRecord.gyro_y = gyro_y;
imuRecord.gyro_z = gyro_z;
imuRecord.index = seqNo;
imuRecord.magn_x = double.Parse(data[7]);
imuRecord.magn_y = double.Parse(data[8]);
data[9] = data[9].Remove(data[9].Length - 1, 1);
imuRecord.magn_z = double.Parse(data[9]);
//for drawing
draw_AccelX[drawEndPoint] = accel_x - accel_x_offset;
draw_AccelY[drawEndPoint] = accel_y - accel_y_offset;
draw_AccelZ[drawEndPoint] = accel_z - accel_z_offset;
draw_GyroX[drawEndPoint] = gyro_x - gyro_x_offset;
draw_GyroY[drawEndPoint] = gyro_y - gyro_y_offset;
draw_GyroZ[drawEndPoint] = gyro_z - gyro_z_offset;
drawEndPoint++;
if (drawEndPoint >= DrawDataLen)
{
drawEndPoint = 0;
}
if (drawEndPoint == drawStartPoint)
{
drawStartPoint++;
if (drawStartPoint >= DrawDataLen)
drawStartPoint = 0;
}
if (startRecord)
{
String recTemp = imuRecord.index.ToString() + "," +
imuRecord.accel_x.ToString() + "," +
imuRecord.accel_y.ToString() + "," +
imuRecord.accel_z.ToString() + "," +
imuRecord.gyro_x.ToString() + "," +
imuRecord.gyro_y.ToString() + "," +
imuRecord.gyro_z.ToString() + "," +
imuRecord.magn_x.ToString() + "," +
imuRecord.magn_y.ToString() + "," +
imuRecord.magn_z.ToString();
SW.WriteLine(recTemp);
recordCnt++;
if (recordCnt > MAXFILELEN)
{
recordCnt = 0;
SW.Close();
//open next file
SW = File.CreateText(fileNme + fileCnt.ToString() + ".txt");
fileCnt++;
}
}
}
}
private void updateSensor(string msg)
{
textBoxRCV.Text = msg;
}
private void HandleClientGPS()
{
//here process all receive data from client
try
{
cmdStreamGPS = clientSocketGPS.GetStream();
readerGPS = new StreamReader(cmdStreamGPS);
string strRec = "";
while (receivingGPS)
{
try
{
if (!cmdStreamGPS.DataAvailable) //there is no data
{
//need to sleep for other thread
Thread.Sleep(40); //5 Hz
}
else
{
// Reads NetworkStream into a byte buffer.
strRec = readerGPS.ReadLine();
if (strRec != null)
processDataGPS(strRec);
}
}
catch (Exception e)
{
string temp = e.ToString();
}
finally
{
}
}
receivingGPS = false;
readerGPS.Close();
cmdStreamGPS.Close();
clientSocketGPS.Close();
}
catch
{
receivingGPS = false;
}
}
private void updateSensorGPS(string rcv)
{
textBoxGPSRCV.Text = rcv;
}
private void processDataGPS(string input)
{
if ((input.Substring(0, 3) == "$GP") || (input.Substring(0, 3) == "$PG"))
{
gpsRecord.recRaw = input;
Invoke(new updateGPSSensorDataInfo(updateSensorGPS), input);
if (startRecord)
{
SW.WriteLine(gpsRecord.recRaw);
recordCnt++;
if (recordCnt > MAXFILELEN)
{
recordCnt = 0;
SW.Close();
//open next file
SW = File.CreateText(fileNme + fileCnt.ToString() + ".txt");
fileCnt++;
}
}
}
String[] strData = input.Split(',');
double longitude = 0;
double lat = 0;
if ((strData[0] == "$GPRMC") && (strData[3] != "") && (strData[5] != ""))
{
gpsRecord.timeStamp = double.Parse(strData[1]); //time
gpsRecord.status = strData[2]; //A- valid, V = invalid
gpsRecord.latitude = double.Parse(strData[3]);
gpsRecord.latHemi = strData[4];
gpsRecord.longitude = double.Parse(strData[5]); //dddmm.mmmm
gpsRecord.lngHemi = strData[6];
gpsRecord.vog = double.Parse(strData[7]) * KNNOT2MS;
gpsRecord.cog = double.Parse(strData[8]);
gEarth.curLatitude = ToDegree(gpsRecord.latitude);
gEarth.curLongitude = -ToDegree(gpsRecord.longitude);
if ((gEarth.preLatitude == 0) && (gEarth.preLongtitude == 0))
{
}
else
{
//display real time GPS information here
if ((gEarth.preLatitude != 0) && (gEarth.preLongtitude != 0))
{
kmlLineDataMakeLoad(gEarth.setColor.SetBlue, 4, gEarth.preLatitude, gEarth.preLongtitude, gEarth.curLatitude, gEarth.curLongitude);
}
}
gEarth.preEstLatitude = lat;
gEarth.preEstLongitude = longitude;
gEarth.preLatitude = gEarth.curLatitude;
gEarth.preLongtitude = gEarth.curLongitude;
}
else if (strData[0] == "$GPGGA")
{
gpsRecord.timeStamp = double.Parse(strData[1]); //time
gpsRecord.latitude = double.Parse(strData[2]);
gpsRecord.latHemi = strData[3];
gpsRecord.longitude = double.Parse(strData[4]); //dddmm.mmmm
gpsRecord.lngHemi = strData[5];
gpsRecord.qi = int.Parse(strData[6]);
gpsRecord.satNum = int.Parse(strData[7]);
gpsRecord.h_pre = double.Parse(strData[8]);
gpsRecord.seaHeight = double.Parse(strData[9]);
gpsRecord.geoidalHeight = double.Parse(strData[10]);
gEarth.curLatitude = ToDegree(gpsRecord.latitude);
gEarth.curLongitude = -ToDegree(gpsRecord.longitude);
}
else if (strData[0] == "$GPGSA")
{
gpsRecord.p_pre = double.Parse(strData[4]);
gpsRecord.h_pre = double.Parse(strData[5]);
gpsRecord.v_pre = double.Parse(strData[6]);
}
else if (strData[0] == "$GPGSV")
{
int len = 0;
len = strData.Length;
gpsRecord.satNum = int.Parse(strData[3]);
}
}
private double ToDegree(double angle)
{
//to
double deg = 0;
int degree = (int)(angle / 100);
double min = ((angle - degree * 100));
deg = degree + ((double)min) / 60;
return deg;
}
private void kmlLineDataMakeLoad(string settingColor, int setWidth, double preLatitude, double preLongtitude, double lat, double longitude)
{
//make data
gEarth.kmlFileCoordinate = @"<coordinates>" + preLongtitude.ToString() + "," + preLatitude.ToString() + "," + "0" +
" " + longitude.ToString() + "," + lat.ToString() + "," + "0</coordinates>";
gEarth.kmlFileStyle = @"<Style id=""MyLineStyle""><LineStyle><color>" + settingColor + @"</color><width>" + setWidth.ToString() + @"</width></LineStyle></Style>";
string data = gEarth.kmlFileStr1 + gEarth.kmlFileStyle + gEarth.kmlFilePlacemark + gEarth.kmlFileCoordinate + gEarth.kmlFileEnd;
gEarth.googleEarth.LoadKmlData(ref data);
}
}
}