-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_imu.cpp
More file actions
190 lines (146 loc) · 5.05 KB
/
system_imu.cpp
File metadata and controls
190 lines (146 loc) · 5.05 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
#include "system_imu.h"
#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
#include <pinDefinitions.h>
#define IMU_OBC_NSS PinNameToIndex(PB_4)
#define IMU_OBC_RST PinNameToIndex(PI_14)
#define IMU_OBC_INT PinNameToIndex(PI_15)
#define IMU_OBC_WAKE PinNameToIndex(PB_3)
#endif
void CIMU::loop(){runOnce();}
bool CIMU::runOnce(){
if((_ostate!="READY")&&(_ostate!="PLAY") )setup();
if((_ostate!="READY")&&(_ostate!="PLAY") ) return false;
if (myIMU.dataAvailable() == true) {
GetData();
return true;
}
else {
if(getTime()>(_dataUpdatedOn+100*1000)){ //Check if nothing for 100s
setState("NONE"); //Reset IMU
writeconsoleln("========================== IMU RESET NOT HEARD IN 100 SECS ==============================");
}
}
return false;
}
void CIMU::GetData(){
//Look for reports from the IMU
// writeconsoleln(" --------------------------------------------------------------------------------- GET IMU DATA ---------------------------------------------------------------------------");
_dataUpdatedOn=getTime();
PRY.archiveData();
Quat.archiveData();
Lin.archiveData();
Gyro.archiveData();
Mag.archiveData();
PRY.Pitch=(myIMU.getPitch()) * 180.0 / PI; // Convert pitch to degrees
PRY.Roll=(myIMU.getRoll()) * 180.0 / PI; // Convert roll to degrees
PRY.Yaw=(myIMU.getYaw()) * 180.0 / PI; // Convert yaw / heading to degrees
Quat.I=myIMU.getQuatI();
Quat.J = myIMU.getQuatJ();
Quat.K = myIMU.getQuatK();
Quat.R = myIMU.getQuatReal();
Quat.Acc = myIMU.getQuatRadianAccuracy();
Lin.X = myIMU.getLinAccelX();
Lin.Y = myIMU.getLinAccelY();
Lin.Z = myIMU.getLinAccelZ();
Lin.Acc = myIMU.getLinAccelAccuracy();
// gyrox = myIMU.getGyroX();
// gyroy = myIMU.getGyroY();
// Gyro.z = myIMU.getGyroZ();
Gyro.X = myIMU.getFastGyroX();
Gyro.Y = myIMU.getFastGyroY();
Gyro.Z = myIMU.getFastGyroZ();
Mag.X = myIMU.getMagX();
Mag.Y = myIMU.getMagY();
Mag.Z = myIMU.getMagZ();
Mag.Acc = myIMU.getMagAccuracy();
Mag.changedOn=getTime();
Quat.changedOn=getTime();
Lin.changedOn=getTime();
PRY.changedOn=getTime();
Gyro.changedOn=getTime();
CMsg mGyro,mLin,mMag,mPRY;
if (Gyro.anythingNew()){
mGyro=Gyro.makeMessage("GYRO");
// respondCallBack(m);
}
/*
if (Lin.anythingNew()){
mLin=Lin.makeMessage("LIN");
// respondCallBack(m);
}
if (Mag.anythingNew()){
mMag=Mag.makeMessage("Mag");
// respondCallBack(m);
}
if (PRY.anythingNew()){
mPRY=PRY.makeMessage("PRY");
// respondCallBack(m);
}
mGyro.appendParams(mLin.Parameters);
mGyro.appendParams(mMag.Parameters);
mGyro.appendParams(mPRY.Parameters);
*/
if(mGyro.Parameters.size())
respondCallBack(mGyro);
}
void CIMU::Output(CMsg &msg){
CMsg mGyro;
mGyro=Gyro.makeMessage("GYRO");
addTransmitList(mGyro);
}
void CIMU::init(){
Name("IMU");
_INTERVAL = 5;
_forever=true;
}
void CIMU::setup(){
// setupSPI();
setupI2C();
}
void CIMU::setupSPI(){
#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
Name("IMUSPI");
// myIMU.enableDebugging(Serial); //Pipe debug messages to Serial port
delay(1000);
for(int retries=0;retries<5;retries++){
if(myIMU.beginSPI(IMU_OBC_NSS, IMU_OBC_WAKE, IMU_OBC_INT, IMU_OBC_RST) == false)
{
writeconsoleln("BNO080 over SPI not detected. Are you sure you have all 6 connections? Freezing...");
retrycount++;
setState("ERROR");
}
else{
// myIMU.enableRotationVector(50); //Send data update every 50ms
// myIMU.enableGyro(50); //Send data update every 50ms
myIMU.enableGyroIntegratedRotationVector(50); //Send data update every 50ms
setState("READY");
writeconsole("IMU Ready");
return;
}
}
#endif
}
void CIMU::setupI2C(){
Name("IMU");
delay(100);
Wire.flush();
for(int retries=0;retries<5;retries++){
if (myIMU.begin(IMUADDRESS1) == false) { //IMUADDRESS2
unsigned int counter=10000;
while(Wire.available()&&counter) { Wire.read();counter--;} //Flushes wire. Then try again Wire.flush() should do the same thing
delay(1000);
retrycount++;
setState("ERROR");
writeconsoleln("IMU Error...............................");
}
else{
Wire.setClock(400000);
// myIMU.enableRotationVector(50); //Send data update every 50ms
// myIMU.enableGyro(50); //Send data update every 50ms
myIMU.enableGyroIntegratedRotationVector(50); //Send data update every 50ms
setState("READY");
writeconsoleln("IMU Ready...............................");
return ;
}
}
}