-
Notifications
You must be signed in to change notification settings - Fork 0
/
adafruit_uC.h
198 lines (147 loc) · 4.14 KB
/
adafruit_uC.h
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
#ifndef ADAFRUIT_UC_H
#define ADAFRUIT_UC_H
/******************/
/*** ADAFRUIT DRIVERS ***/
/******************/
/******************/
#include <QSerialPort>
#include <QSerialPortInfo>
#include <iostream>
#include <QTime>
#define SUCCESS 0
#define ERR_BAUDRATE 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
/*
(0) *help?
(1) *info?
(2) *baud[:{<rate>|list}]?
(3) *mode[:{list|text|bin}]?
(4) *endian?
(5) *reset?
(6) *status?
(7) *unit[:{list | what | what:set}]?
(8) *accel?
(9) *euler?
(A) *grav?
(B) *gyro?
(C) *mag?
(D) *quat?
(E) *temp[:A|G|M]?
(F) *fw?
(G) *echo[:{on|off}]?
(I) *cal?
*/
#include <QWidget>
#include <QThread>
#include <eigen3/Eigen/Geometry>
class Adafruit_uC : public QSerialPort
{
Q_OBJECT
public:
// Constructor
explicit Adafruit_uC(QWidget *parent = 0) : QSerialPort(parent)
{
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;
}
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::UnknownStopBits))
return ERR_STOPBITS;
if (!setFlowControl(QSerialPort::HardwareControl))
return ERR_FLOWCONTROL;
Question(QString("*echo:off?"));
return SUCCESS;
}
QString GetCal()
{
// send *cal?, get answer and return it
return Ask(QString("*cal?"));
}
Eigen::Quaternionf GetQuat()
{
// send *quat?, get answer, convert it and return it
return ConvertToQuat(Ask(QString("*quat?")));
}
Eigen::Quaternionf ConvertToQuat(QString answer)
{
// convert string in quaternions
QStringList quatList = answer.section("!",0,0).split(":");
Eigen::Quaternionf quat;
if (quatList.size() > 4)
{
quat.w() = quatList.at(1).toFloat();
quat.x() = quatList.at(2).toFloat();
quat.y() = quatList.at(3).toFloat();
quat.z() = quatList.at(4).toFloat();
}
return quat;
}
QString Ask(QString question)
{
if (Question(question) != SUCCESS)
return QString();
return Answer();
}
int Question(QString question)
{
// check opening
if (!isOpen() || !plugged)
return ERR_OPEN;
// remove previous message
readAll();
// Ask for quaternion string
if (write(question.toLocal8Bit()) == -1)
return ERR_MESSAGE;
return SUCCESS;
}
QString Answer()
{
// init Answer
QString answer;
// get answer in a single string
while (waitForReadyRead (100) && !(answer.endsWith("!")) )
answer.append(QString(readAll()));
// return answer
return answer;
}
private:
bool plugged;
};
#endif // ADAFRUIT_UC_H