-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMU.cpp
219 lines (209 loc) · 6.99 KB
/
IMU.cpp
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
#include "IMU.h"
#define Pi 3.14159265359
CJY901::CJY901(){
serialIMU = new QSerialPort();
}
CJY901::~CJY901(){
if(serialIMU->isOpen())
{
serialIMU->clear();
serialIMU->close();
}
delete serialIMU;
}
// check serial port list
QList<QString> CJY901::getSerialList(){
QList<QSerialPortInfo> serialList = QSerialPortInfo::availablePorts();
QList<QString> stringList;
QList<QSerialPortInfo>::Iterator iter = serialList.begin();
for(;iter!=serialList.end();iter++){
stringList.push_back(iter->description());
qDebug()<<"serial::";
qDebug()<<iter->description();
}
qDebug()<<"serial check end";
qDebug()<<stringList.size();
return stringList;
}
void CJY901::openPort(QString portName){
if(serialIMU->isOpen())
{
serialIMU->clear();
serialIMU->close();
}
serialIMU->setPortName(portName);
if(!serialIMU->open(QIODevice::ReadWrite))
{
qDebug()<<portName<<"打开失败!";
qDebug()<<"serial state is :";
qDebug()<<serialIMU->isOpen();
return;
}
serialIMU->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);
qDebug()<<"success";
qDebug()<<"serial state is :";
qDebug()<<serialIMU->isOpen();
}
QString CJY901::recieveData(){
if(!serialIMU->isOpen())
{
qDebug()<<"port is not open";
return "error, port is not opem";
}
else
{
QByteArray data = serialIMU->readAll();
QString dataStr = QString::fromStdString(data.toStdString());
return dataStr;
}
}
//convert serial data to jy901 data
void CJY901::CopeSerialData(std::string str_in, unsigned short usLength)
{
unsigned int str_length = str_in.size();
static unsigned char chrTemp[2000];
static unsigned char ucRxCnt = 0;
static unsigned int usRxLength = 0;
// str_in.data();
memmove(chrTemp,str_in.data(),str_length);
// for(int j=0;j<=str_length-1;j++){
// chrTemp[j]=str_in.data()[j];
// }
usRxLength += str_length;
while (usRxLength >= 11)
{
if (chrTemp[0] != 0x55)
{
usRxLength--;
memmove(&chrTemp[0],&chrTemp[1],usRxLength);
// for(int j=0;j<usRxLength;j++){
// chrTemp[j]=chrTemp[j+1];
// }
continue;
}
// unsigned char temp[8];
////////////////////////////////////////////////////////////////////
// switch(chrTemp[1])
// {
// case 0x50: memmove(&stcTime,&chrTemp[2],8);
// break;
// case 0x51:
// {
// memmove(&temp,&chrTemp[2],8);
//// for(int j=0;j<=7;j++){
//// temp[j]=chrTemp[j+1];
//// }
// short temp1=(short)temp[1];
// short temp2=(short)temp[3];
// short temp3=(short)temp[5];
// qDebug()<<"---------";
// qDebug()<<temp[5];
// qDebug()<<temp[4];
// qDebug()<<"---------";
// short ax = ((temp1<<8)|temp[0])/32768.*16.;
// short ay = ((temp2<<8)|temp[2])/32768.*16.;
// short az = ((temp3<<8)|temp[4])/32768.*16.;
// short tempreture = (((short)temp[7]<<8)|temp[6])/100;
// stcAcc.a[0]=ax;
// stcAcc.a[1]=ay;
// stcAcc.a[2]=az;
// stcAcc.T=tempreture;
// qDebug()<<"---------";
//// for(int j=0;j<=7;j++){
//// qDebug("test:%x",temp[j]);
//// }
// qDebug()<<ax;
// qDebug()<<ay;
// qDebug()<<az;
// qDebug()<<"---------";
// break;
// }
// case 0x52:
// {
// memmove(&temp,&chrTemp[2],8);
// short wx = (((short)temp[1]<<8)|temp[0])/32768.*2000.;
// short wy = (((short)temp[3]<<8)|temp[2])/32768.*2000.;
// short wz = (((short)temp[5]<<8)|temp[4])/32768.*2000.;
// short tempreture = (((short)temp[7]<<8)|temp[6])/100;
// stcGyro.w[0]=wx;
// stcGyro.w[1]=wy;
// stcGyro.w[2]=wz;
// stcGyro.T=tempreture;
// break;
// }
// case 0x53:
// {
// memmove(&temp,&chrTemp[2],8);
// short wx = (((short)temp[1]<<8)|temp[0])/32768.*180;
// short wy = (((short)temp[3]<<8)|temp[2])/32768.*180;
// short wz = (((short)temp[5]<<8)|temp[4])/32768.*180;
// short tempreture = (((short)temp[7]<<8)|temp[6])/100;
// stcAngle.Angle[0]=wx;
// stcAngle.Angle[1]=wy;
// stcAngle.Angle[2]=wz;
// stcAngle.T=tempreture;
// break;
// }
// case 0x54: memmove(&stcMag,&chrTemp[2],8);
// break;
// case 0x55: memmove(&stcDStatus,&chrTemp[2],8);
// break;
// case 0x56: memmove(&stcPress,&chrTemp[2],8);
// break;
// case 0x57: memmove(&stcLonLat,&chrTemp[2],8);
// break;
// case 0x58: memmove(&stcGPSV,&chrTemp[2],8);
// break;
// }
////////////////////////////////////////////////////////////////////
switch(chrTemp[1])
{
case 0x50: memmove(&stcTime,&chrTemp[2],8);break;
case 0x51: memmove(&stcAcc,&chrTemp[2],8);break;
case 0x52: memmove(&stcGyro,&chrTemp[2],8);break;
case 0x53: memmove(&stcAngle,&chrTemp[2],8);break;
case 0x54: memmove(&stcMag,&chrTemp[2],8);break;
case 0x55: memmove(&stcDStatus,&chrTemp[2],8);break;
case 0x56: memmove(&stcPress,&chrTemp[2],8);break;
case 0x57: memmove(&stcLonLat,&chrTemp[2],8);break;
case 0x58: memmove(&stcGPSV,&chrTemp[2],8);break;
}
usRxLength -= 11;
memmove(&chrTemp[0],&chrTemp[11],usRxLength);
// for(int j=0;j<usRxLength;j++){
// chrTemp[j]=chrTemp[11+j];
// }
}
}
//EulerToQuaternion, euler in rad
template<typename T>
vector<T> EulerToQuaternion(T roll, T pitch, T yaw)
{
vector<T> q;
double x, y, z, w;
double a = roll/2.0;
double b = pitch/2.0;
double g = yaw/2.0;
w = cos(a)*cos(b)*cos(g) + sin(a)*sin(b)*sin(g);
x = sin(a)*cos(b)*cos(g) - cos(a)*sin(b)*sin(g);
y = cos(a)*sin(b)*cos(g) + sin(a)*cos(b)*sin(g);
z = cos(a)*cos(b)*sin(g) - sin(a)*sin(b)*cos(g);
q.push_back(w);
q.push_back(x);
q.push_back(y);
q.push_back(z);
return q;
}
//QuaternionToEuler, euler in rad
template<typename T>
vector<T> QuaternionToEuler(vector<T> q)
{
vector<T> e;
T roll = atan2(2*(q[0]*q[1]+q[2]*q[3]),1-2*(q[1]*q[1]+q[2]*q[2]));
T pitch = asin(2*(q[0]*q[2]-q[1]*q[3]));
T yaw = atan2(2*(q[0]*q[3]+q[1]*q[2]),1-2*(q[2]*q[2]+q[3]*q[3]));
e.push_back(roll);
e.push_back(pitch);
e.push_back(yaw);
return e;
}