-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadafruit_uart.h
More file actions
641 lines (478 loc) · 16.7 KB
/
adafruit_uart.h
File metadata and controls
641 lines (478 loc) · 16.7 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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#ifndef ADAFRUIT_UART_H
#define ADAFRUIT_UART_H
/******************/
/*** ADAFRUIT DRIVERS ***/
/******************/
/******************/
#include <QSerialPort>
#include <QSerialPortInfo>
#include <iostream>
#include <iomanip>
#include <QTime>
#define SUCCESS 0
#define ERROR 1
#define ERR_DATABITS 2
#define ERR_PARITY 3
#define ERR_STOPBITS 4
#define ERR_FLOWCONTROL 5
#define ERR_OPEN 6
#define ERR_PLUG 7
#define ERR_MESSAGE 8
#define ERR_BAUDRATE 9
#define REG_PAGE_ID 0x07
#define REG_START_RAW_DATA 0x08
#define REG_START_ACC_DATA 0x08
#define REG_START_MAG_DATA 0x0E
#define REG_START_GYR_DATA 0x14
#define REG_START_EUL_DATA 0x1A
#define REG_START_QUA_DATA 0x20
#define REG_START_LIA_DATA 0x28
#define REG_START_GRV_DATA 0x2E
#define REG_START_TMP_DATA 0x34
#define REG_OPR_MODE 0x3D
#define REG_PWR_MODE 0x3E
#define REG_SYS_TRIGGER 0x3F
#include <QWidget>
#include <QThread>
#include <eigen3/Eigen/Geometry>
class Adafruit_Data {
public:
explicit Adafruit_Data(){}
Eigen::Vector3f m_accelerometer;
Eigen::Vector3f m_magnetometer;
Eigen::Vector3f m_gyroscope;
Eigen::Vector3f m_euler_angles;
Eigen::Quaternionf m_quaternion;
Eigen::Vector3f m_linear_acceleration;
Eigen::Vector3f m_gravity;
float m_temperature;
};
class Adafruit_UART : public QSerialPort
{
Q_OBJECT
public:
// Constructor
explicit Adafruit_UART(QWidget *parent = 0) : QSerialPort(parent) {}
// Open the Serial connection
int Open()
{
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
if (info.description() == QString("USB-Serial Controller"))
if (info.manufacturer() == QString("Prolific Technology Inc."))
if(info.vendorIdentifier() == 0x067b)
if(info.productIdentifier() == 0x2303)
setPortName(info.portName());
}
plugged = true;
if (this->portName().isEmpty())
plugged = false;
if (!plugged)
return ERR_PLUG;
if (!open(QIODevice::ReadWrite))
return ERR_OPEN;
if (!setBaudRate(QSerialPort::Baud115200))
return ERR_BAUDRATE;
if (!setDataBits(QSerialPort::Data8))
return ERR_DATABITS;
if (!setParity(QSerialPort::NoParity))
return ERR_PARITY;
if (!setStopBits(QSerialPort::OneStop))
return ERR_STOPBITS;
if (!setFlowControl(QSerialPort::NoFlowControl))
return ERR_FLOWCONTROL;
return SUCCESS;
}
// Try first communication and initilize the device
int Init()
{
std::cout << " --> INIT <-- " << std::endl;
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
std::cout << "Checking device ID, FW and BootLoader" << std::endl;
if ( CheckDevice() != SUCCESS )
{
std::cout << " -> Error Checking Device" << std::endl;
return ERROR;
}
std::cout << "Set OPR_MODE to CONFIG MODE" << std::endl;
if ( WriteRegister((quint8) REG_OPR_MODE, (quint8)0x00) != SUCCESS )
{
std::cout << " -> Error entering in config mode" << std::endl;
return ERROR;
}
std::cout << "Set REG_SYS_TRIGGER to 20" << std::endl;
if ( WriteRegister((quint8) REG_SYS_TRIGGER, (quint8)0x20, true) != SUCCESS )
{
std::cout << " -> Error setting REG_SYS_TRIGGER to 20" << std::endl;
return ERROR;
}
QThread::usleep(1000*1000);
std::cout << "Checking device ID, FW and BootLoader" << std::endl;
if ( CheckDevice() != SUCCESS )
{
std::cout << " -> Error Checking Device" << std::endl;
return ERROR;
}
std::cout << "Set REG_POWER_MODE to 00" << std::endl;
if ( WriteRegister((quint8) REG_PWR_MODE, (quint8)0x00) != SUCCESS )
{
std::cout << " -> Error setting REG_POWER_MODE to 00" << std::endl;
return ERROR;
}
QThread::usleep(1000*1000);
std::cout << "Set REG_PAGE_ID to 00" << std::endl;
if ( WriteRegister((quint8) REG_PAGE_ID, (quint8)0x00) != SUCCESS )
{
std::cout << " -> Error setting REG_PAGE_ID to 00" << std::endl;
return ERROR;
}
std::cout << "Set REG_SYS_TRIGGER to 80" << std::endl;
if ( WriteRegister((quint8) REG_SYS_TRIGGER, (quint8)0x80) != SUCCESS )
{
std::cout << " -> Error setting REG_SYS_TRIGGER to 20" << std::endl;
return ERROR;
}
std::cout << "Set OPR_MODE to FUSION MODE" << std::endl;
if ( WriteRegister((quint8) REG_OPR_MODE, (quint8)0x0C) != SUCCESS )
{
std::cout << " -> Error entering in FUSION MODE" << std::endl;
return ERROR;
}
QThread::usleep(1000*1000);
return SUCCESS;
}
// Read all the 45 data available
int ReadAllData(Adafruit_Data * result)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_RAW_DATA, 0x2d);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)0x2d)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
int16_t s[4];
int off = 2;
// plain accelerometer, assumes [a] = m/s^2
result->m_accelerometer = convertQByteArrayInVector3f(input.mid(2,6));
// magnetometer, always [B] = uT
result->m_magnetometer = convertQByteArrayInVector3f(input.mid(8,6));
// gyroscope, assumes [w] = deg/s
result->m_gyroscope = convertQByteArrayInVector3f(input.mid(14,6));
// euler angles, assumes [e] = deg/s
result->m_euler_angles = convertQByteArrayInVector3f(input.mid(20,6));
// quaternion, no unit
result->m_quaternion = convertQByteArrayInQuaternionf(input.mid(26,8));
// linear acceleration, assumes [a] = m/s^2
result->m_linear_acceleration = convertQByteArrayInVector3f(input.mid(34,6));
// gravity, assumes [a] = m/s^2
result->m_gravity = convertQByteArrayInVector3f(input.mid(40,6));
// temperature, assuming [T] = C
result->m_temperature = (float) ((int16_t)static_cast<const quint8>(input[46]));
return SUCCESS;
}
// Read the accelerometer;
int GetAcc(Eigen::Vector3f * acc)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_ACC_DATA, 6);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)6)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*acc = convertQByteArrayInVector3f(input.mid(2,6));
return SUCCESS;
}
// Read the magnetometer;
int GetMag(Eigen::Vector3f * mag)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_MAG_DATA, 6);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)6)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*mag = convertQByteArrayInVector3f(input.mid(2,6));
return SUCCESS;
}
// Read the linear_acceleration;
int GetLia(Eigen::Vector3f * lia)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_LIA_DATA, 6);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)6)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*lia = convertQByteArrayInVector3f(input.mid(2,6));
return SUCCESS;
}
// Read the gyroscope;
int GetGyr(Eigen::Vector3f * gyr)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_GYR_DATA, 6);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)6)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*gyr = convertQByteArrayInVector3f(input.mid(2,6));
return SUCCESS;
}
// Read the euler_angles;
int GetEul(Eigen::Vector3f * eul)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_EUL_DATA, 6);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)6)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*eul = convertQByteArrayInVector3f(input.mid(2,6));
return SUCCESS;
}
// Read the quaternion
int GetQuat(Eigen::Quaternionf * quat)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_QUA_DATA, 8);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)8)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*quat = convertQByteArrayInQuaternionf(input.mid(2,8));
return SUCCESS;
}
// Read the gravity;
int GetGrv(Eigen::Vector3f * grv)
{
if ( !isOpen() )
{
std::cout << " -> Error Device not opened" << std::endl;
return ERROR;
}
QByteArray input ;
ReadRegister(input, (quint8) REG_START_GRV_DATA, 6);
if( !(((quint8)input[0] == (quint8)0xBB) &&
((quint8)input[1] == (quint8)6)) )
{
std::cout << "error" << std::endl;
return ERROR;
}
*grv = convertQByteArrayInVector3f(input.mid(2,6));
return SUCCESS;
}
private:
void PlotAnswer(QByteArray answer)
{
std::cout << "Total Answer is: " <<std::endl;
int line = 0;
std::cout << std::hex << " " << (static_cast<quint16>(answer[0])&0xFF) << std::endl;
std::cout << std::hex << " " << (static_cast<quint16>(answer[1])&0xFF) << std::endl;
for (int i = 2; i < answer.length(); i++)
{
std::cout << std::hex << " " << (static_cast<quint16>(answer[i])&0xFF);
line ++;
if (line > 15)
{
std::cout << std::endl;
line = 0;
}
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
return;
}
int CheckDevice()
{
QByteArray answer;
ReadRegister(answer, (quint8) 0x00, 8);
if ( !(((quint8)answer[0] == (quint8)0xBB) &&
((quint8)answer[1] == (quint8)0x08) &&
((quint8)answer[2] == (quint8)0xA0) &&
((quint8)answer[3] == (quint8)0xFB) &&
((quint8)answer[4] == (quint8)0x32) &&
((quint8)answer[5] == (quint8)0x0F)) )
{
PlotAnswer(answer);
std::cout << " -> Error in device ID" << std::endl;
return ERROR;
}
return SUCCESS;
}
// Data Conversion
Eigen::Vector3f convertQByteArrayInVector3f(QByteArray m_bytearray)
{
Eigen::Vector3f m_vec;
int16_t m_temp;
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[0]) |
(((int16_t)static_cast<const quint8>(m_bytearray[1]))<<8);
m_vec.data()[0] = ( (float) m_temp ) / 100.0f;
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[2]) |
(((int16_t)static_cast<const quint8>(m_bytearray[3]))<<8);
m_vec.data()[1] = ( (float) m_temp ) / 100.0f;
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[4]) |
(((int16_t)static_cast<const quint8>(m_bytearray[5]))<<8);
m_vec.data()[2] = ( (float) m_temp ) / 100.0f;
return m_vec;
}
Eigen::Quaternionf convertQByteArrayInQuaternionf(QByteArray m_bytearray)
{
Eigen::Quaternionf m_quat;
int16_t m_temp;
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[0]) |
(((int16_t)static_cast<const quint8>(m_bytearray[1]))<<8);
m_quat.w() = ((float)m_temp);
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[2]) |
(((int16_t)static_cast<const quint8>(m_bytearray[3]))<<8);
m_quat.x() = ((float)m_temp);
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[4]) |
(((int16_t)static_cast<const quint8>(m_bytearray[5]))<<8);
m_quat.y() = ((float)m_temp);
m_temp = (int16_t)static_cast<const quint8>(m_bytearray[6]) |
(((int16_t)static_cast<const quint8>(m_bytearray[7]))<<8);
m_quat.z() = ((float)m_temp);
double norm = m_quat.w()*m_quat.w() +
m_quat.x()*m_quat.x() +
m_quat.y()*m_quat.y() +
m_quat.z()*m_quat.z();
norm = (norm>=0.0 ? 1.0/sqrt(norm) : 0.0);
m_quat.w()*=float(norm);
m_quat.x()*=float(norm);
m_quat.y()*=float(norm);
m_quat.z()*=float(norm);
return m_quat;
}
// Read in the register
int ReadRegister(QByteArray& receive, quint8 reg, quint8 size = 0x01)
{
QThread::usleep(1*1000);
int pause = 0;
//send a message in hexa
char cmdBuf[] = { (char)0xAA,(char)0x01,(char)reg ,(char) size};
QByteArray data = QByteArray::fromRawData(cmdBuf, sizeof(cmdBuf));
int timeout= 20;
//empty the buffer
// std::cout << "Buffer was still : " << readAll().size() << std::endl;
readAll();
clear();
// write the message
write(data);
if (!waitForBytesWritten(4*timeout))
std::cout << "All the byte has not been written!!!" << std::endl;
QThread::usleep(pause*1000);
//QByteArray receive;
int excepted_data = ( static_cast<int16_t>(data[3]) ) + 2;
while (receive.size() < excepted_data)
{
if (!waitForReadyRead(timeout))
break;
receive.append(readAll());
}
if ( (quint8) receive[0] == (quint8) 0xEE &&
(quint8) receive[1] == (quint8) 0x07 )
{
std::cout << " overrun error" << std::endl;
return ERROR;
}
else if ( !((quint8)receive[0] == (quint8)0xBB) )
{
std::cout << " error reading" << std::endl;
return ERROR;
}
return SUCCESS;
}
// Write in the register
int WriteRegister(quint8 reg, quint8 value, bool simple = false)
{
int pause = 0;
//send a message in hexa
char cmdBuf[] = { (char)0xAA,(char)0x00,(char)reg , (char)0x01, (char) value};
QByteArray data = QByteArray::fromRawData(cmdBuf, sizeof(cmdBuf));
int timeout = 1000;
//empty the buffer
// std::cout << "Buffer was still : " << readAll().size() << std::endl;
readAll();
clear();
// write the message
write(data);
if (!waitForBytesWritten(4*timeout))
std::cout << "All the byte has not been written!!!" << std::endl;
if (simple)
return SUCCESS;
QThread::usleep(pause*1000);
QByteArray receive;
// int excepted_data = ( static_cast<int16_t>(data[3]) ) + 2;
while (receive.size() < 2)
{
if (!waitForReadyRead(timeout))
break;
receive.append(readAll());
}
QThread::usleep(pause*1000);
if ( !((receive.size() == 2 ) &&
((quint8)receive[0] == (quint8)0xEE) &&
((quint8)receive[1] == (quint8)0x01)) )
{
std::cout << "error in writing" << std::endl;
PlotAnswer(receive);
return ERROR;
}
return SUCCESS;
}
bool plugged;
};
#endif // ADAFRUIT_UART_H