-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestModule.cpp
213 lines (206 loc) · 9.43 KB
/
TestModule.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
#include "Drive_Json.hpp"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include "src/MPUProcess.hpp"
void configWrite(const char *configDir, const char *Target, double obj);
double configSettle(const char *configDir, const char *Target);
inline int GetTimestamp()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * (uint64_t)1000000 + tv.tv_usec;
}
int main(int argc, char *argv[])
{
int argvs;
//
int TimeStart;
int TimeEnd;
int TimeNext;
int TimeMax;
//
MPUData myData;
//
while ((argvs = getopt(argc, argv, "cthsi")) != -1)
{
switch (argvs)
{
case 'c':
{
std::cout << "Start MPU Calibration\n";
TimeMax = 500;
MPUConfig option;
option.MPUType = MPUTypeSPI;
option.GyroScope = SensorType::AUTO;
option.GyroSPIChannel = "/dev/spidev0.0";
option.MPUI2CAddress = 0x68;
option.MPU9250_SPI_Freq = 1000 * 1000;
option.TargetFreqency = 1000.f;
option.GyroToAccelBeta = 0.2;
option.GyroDynamicAnalyse = false;
option.GyroHardwareFilterFreq = 250;
option.GyroFilterType = FilterLPFPT1;
option.GyroFilterTypeST2 = FilterLPFPT1;
option.GyroFilterCutOff = 100;
option.GyroFilterCutOffST2 = 180;
option.GyroFilterNotchCenterFreq = 0;
option.GyroFilterNotchCutOff = 0;
option.DynamicNotchQ = 0;
option.DynamicNotchEnable = false;
option.DynamicNotchMinFreq = 0;
option.AccTargetFreqency = 1000.f;
option.AccelFilterType = FilterLPFBiquad;
option.AccelFilterCutOff = 15;
option.MPU_Flip___Yaw = 270;
RPiMPU9250 *myMPUTest = new RPiMPU9250(option);
int a;
double tmp[50];
std::cout << "start calibration Nose Up and Type int and enter:"
<< " \n";
std::cin >> a;
myMPUTest->MPUAccelCalibration(MPUAccelNoseUp, tmp);
std::cout << "start calibration Nose Down and Type int and enter:"
<< " \n";
std::cin >> a;
myMPUTest->MPUAccelCalibration(MPUAccelNoseDown, tmp);
std::cout << "start calibration Nose Right Up and Type int and enter:"
<< " \n";
std::cin >> a;
myMPUTest->MPUAccelCalibration(MPUAccelNoseRight, tmp);
std::cout << "start calibration Nose Left Up and Type int and enter:"
<< " \n";
std::cin >> a;
myMPUTest->MPUAccelCalibration(MPUAccelNoseLeft, tmp);
std::cout << "start calibration Nose Top and Type int and enter:"
<< " \n";
std::cin >> a;
myMPUTest->MPUAccelCalibration(MPUAccelNoseTop, tmp);
std::cout << "start calibration Nose Rev and Type int and enter:"
<< " \n";
std::cin >> a;
myMPUTest->MPUAccelCalibration(MPUAccelNoseRev, tmp);
myMPUTest->MPUAccelCalibration(MPUAccelCaliGet, tmp);
for (size_t i = 0; i < 15; i++)
{
std::cout << tmp[i] << " \n";
}
configWrite("./MPUCali.json", "_flag_MPU9250_A_X_Cali", tmp[MPUAccelCaliX]);
configWrite("./MPUCali.json", "_flag_MPU9250_A_Y_Cali", tmp[MPUAccelCaliY]);
configWrite("./MPUCali.json", "_flag_MPU9250_A_Z_Cali", tmp[MPUAccelCaliZ]);
configWrite("./MPUCali.json", "_flag_MPU9250_A_X_Scal", tmp[MPUAccelScalX]);
configWrite("./MPUCali.json", "_flag_MPU9250_A_Y_Scal", tmp[MPUAccelScalY]);
configWrite("./MPUCali.json", "_flag_MPU9250_A_Z_Scal", tmp[MPUAccelScalZ]);
}
break;
case 't':
{
double AccelCaliData[30];
TimeMax = 1000;
std::cout << "Start MPU Monitor\n";
std::cout << "Setting UP ... ";
std::cout.flush();
MPUConfig option;
option.GyroScope = SensorType::AUTO;
option.GyroSPIChannel = "/dev/spidev0.0";
option.MPUI2CAddress = 0x68;
option.MPU9250_SPI_Freq = 1000 * 1000;
option.TargetFreqency = 1000.f;
option.GyroToAccelBeta = 0.2;
option.GyroDynamicAnalyse = false;
option.GyroHardwareFilterFreq = 250;
option.GyroFilterType = FilterLPFPT1;
option.GyroFilterTypeST2 = FilterLPFPT1;
option.GyroFilterCutOff = 100;
option.GyroFilterCutOffST2 = 180;
option.GyroFilterNotchCenterFreq = 0;
option.GyroFilterNotchCutOff = 0;
option.DynamicNotchQ = 0;
option.DynamicNotchEnable = false;
option.DynamicNotchMinFreq = 0;
option.AccTargetFreqency = 1000.f;
option.AccelFilterType = FilterLPFBiquad;
option.AccelFilterCutOff = 15;
option.MPU_Flip___Yaw = 270;
RPiMPU9250 *myMPUTest = new RPiMPU9250(option);
//
auto gyroDetected = myMPUTest->GetMPUTypeDetected();
std::cout << "0x" << std::hex << (int)gyroDetected << " ... Done!\n";
//
AccelCaliData[MPUAccelCaliX] = configSettle("./MPUCali.json", "_flag_MPU9250_A_X_Cali");
AccelCaliData[MPUAccelCaliY] = configSettle("./MPUCali.json", "_flag_MPU9250_A_Y_Cali");
AccelCaliData[MPUAccelCaliZ] = configSettle("./MPUCali.json", "_flag_MPU9250_A_Z_Cali");
AccelCaliData[MPUAccelScalX] = configSettle("./MPUCali.json", "_flag_MPU9250_A_X_Scal");
AccelCaliData[MPUAccelScalY] = configSettle("./MPUCali.json", "_flag_MPU9250_A_Y_Scal");
AccelCaliData[MPUAccelScalZ] = configSettle("./MPUCali.json", "_flag_MPU9250_A_Z_Scal");
AccelCaliData[MPUAccelTRIM_Roll] = 0;
AccelCaliData[MPUAccelTRIMPitch] = 0;
std::cout << "Calibration Gryo ......";
std::cout.flush();
myMPUTest->MPUCalibration(AccelCaliData);
std::cout << " Done!\n";
myData = myMPUTest->MPUSensorsDataGet();
std::cout << "MPUTypeID: 0x" << std::hex << myData.DeviceType << std::dec << " \n";
sleep(2);
system("clear");
while (true)
{
TimeStart = GetTimestamp();
TimeNext = TimeStart - TimeEnd;
//
myData = myMPUTest->MPUSensorsDataGet();
//
std::cout << "\033[20A";
std::cout << "\033[K";
std::cout << "Accel Roll: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Accel__Roll << "|"
<< "AccelPitch: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Accel_Pitch << "| \n";
std::cout << "ACC X: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_MPU9250_A_X << "|"
<< "ACC Y: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_MPU9250_A_Y << "|"
<< "ACC Z: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_MPU9250_A_Z << "| \n";
std::cout << "Gryo Roll: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Gryo__Roll << "|"
<< "Gryo Pitch: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Gryo_Pitch << "|"
<< "Gryo Yaw: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Gryo___Yaw << "| \n";
std::cout << "Real Roll: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Real__Roll << "|"
<< "Real Pitch: " << std::setw(7) << std::setfill(' ') << (int)myData._uORB_Real_Pitch << "| \n";
//
TimeEnd = GetTimestamp();
if (TimeMax < ((TimeEnd - TimeStart) + TimeNext) || (TimeNext) < 0)
usleep(1); // In fact ,this is 50us call by linux kerenl
else
usleep(TimeMax - (TimeEnd - TimeStart) - TimeNext);
TimeEnd = GetTimestamp();
}
}
break;
case 'h':
std::cout << "Usage: 'RaspberryPiMPU [option]'";
std::cout << "[option] -c to calibration accel and save to MPUCali.json , -t is start to check mpu data on console\n";
break;
default:
std::cout << "Usage: 'RaspberryPiMPU [option]'";
std::cout << "[option] -c to calibration accel and save to MPUCali.json , -t is start to check mpu data on console\n";
break;
}
}
}
double configSettle(const char *configDir, const char *Target)
{
std::ifstream config(configDir);
std::string content((std::istreambuf_iterator<char>(config)),
(std::istreambuf_iterator<char>()));
nlohmann::json Configdata = nlohmann::json::parse(content);
return Configdata[Target].get<double>();
}
void configWrite(const char *configDir, const char *Target, double obj)
{
std::ifstream config(configDir);
std::string content((std::istreambuf_iterator<char>(config)),
(std::istreambuf_iterator<char>()));
nlohmann::json Configdata = nlohmann::json::parse(content);
Configdata[Target] = obj;
std::ofstream configs;
configs.open(configDir);
configs << std::setw(4) << Configdata << std::endl;
configs.close();
}