-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestModule.cpp
198 lines (189 loc) · 6.54 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
#include <fstream>
#include "src/SingleAPM.hpp"
#include "json/APMJsonConfig.hpp"
#include "json/Drive_Json.hpp"
#include "UserImpTestModule.hpp"
using namespace SingleAPMAPI;
#ifndef GIT_COMMIT_HASH
#define GIT_COMMIT_HASH "0000000" // 0000000 means uninitialized
#endif
#define CONFIGDIR "/etc/APMconfig.json"
void configWrite(const char *configDir, const char *substr, const char *Target, double obj);
void configSettle(const char *configDir, const char *substr, APMSettinngs &APMInit);
void SignalCatch(int Signal);
int main(int argc, char *argv[])
{
int argvs;
double data[20] = {0};
APMSettinngs setting;
while ((argvs = getopt(argc, argv, "e:E:C:r:s:a:jh")) != -1)
{
switch (argvs)
{
case 'h':
{
std::cout << "[RPiSingleAPM] version 0.9.0 Beta, build: "
<< GIT_COMMIT_HASH
<< " , Acess By TSKangetsu\n"
<< " checkout : https://github.com/TSKangetsu/RPiSingleAPM \n";
}
break;
case 'r':
{
// system("clear");
char cmd[50];
sprintf(cmd, "mkdir -p %s", BlackBoxLogDir);
std::cout << "[RPiSingleAPM] Create log dir: " << cmd << "\n";
system(cmd);
RPiSingleAPM APM_Settle;
configSettle(CONFIGDIR, optarg, setting);
APM_Settle.RPiSingleAPMInit(setting);
// Because of PiGPIO ,if you must handle Signal, should be call after RPiSingleAPMInit()
std::signal(SIGINT, SignalCatch);
std::signal(SIGTERM, SignalCatch);
//
APM_Settle.RPiSingleAPMStartUp();
APM_Settle.TaskThreadBlock();
}
break;
case 's':
{
FlightControllMain APM_Settle;
configSettle(CONFIGDIR, optarg, setting);
APM_Settle.RPiSingleAPMInit(setting);
// Because of PiGPIO ,if you must handle Signal, should be call after RPiSingleAPMInit()
std::signal(SIGINT, SignalCatch);
std::signal(SIGTERM, SignalCatch);
APM_Settle.ServoControllInit();
//
APM_Settle.RPiSingleAPMStartUp();
APM_Settle.TaskThreadBlock();
//
APM_Settle.stopAndWaitForExit();
}
break;
case 'e':
{
RPiSingleAPM APM_Settle;
configSettle(CONFIGDIR, optarg, setting);
APM_Settle.RPiSingleAPMInit(setting);
APM_Settle.APMCalibrator(ESCCalibration, CaliESCStart, 0, data);
}
break;
case 'E':
{
RPiSingleAPM APM_Settle;
configSettle(CONFIGDIR, optarg, setting);
APM_Settle.RPiSingleAPMInit(setting);
while (true)
{
int PIN, VALUE;
std::cin >> PIN;
std::cin >> VALUE;
data[0] = PIN;
APM_Settle.APMCalibrator(ESCCalibration, CaliESCUserDefine, VALUE, data);
}
}
break;
case 'C':
{
RPiSingleAPM APM_Settle;
configSettle(CONFIGDIR, optarg, setting);
APM_Settle.RPiSingleAPMInit(setting);
std::signal(SIGINT, SignalCatch);
std::cout << "start Calibrating COMPASS , Input Any to stop ...";
std::cout.flush();
APM_Settle.APMCalibrator(COMPASSCalibration, -1, -1, data);
std::cout << " Done\n";
//
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_X_Offset", (int)data[CompassXOffset]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_X_Scaler", (int)data[CompassXScaler]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_Y_Offset", (int)data[CompassYOffset]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_Y_Scaler", (int)data[CompassYScaler]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_Z_Offset", (int)data[CompassZOffset]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_Z_Scaler", (int)data[CompassZScaler]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_V_Offset", (int)data[CompassVOffset]);
configWrite(CONFIGDIR, optarg, "_flag_COMPASS_V_Scaler", (int)data[CompassVScaler]);
}
break;
case 'a':
{
int a;
double tmp[50] = {0};
RPiSingleAPM APM_Settle;
configSettle(CONFIGDIR, optarg, setting);
APM_Settle.RPiSingleAPMInit(setting);
std::cout << "start calibration Nose Up and Type int and enter:"
<< " \n";
std::cin >> a;
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelNoseUp, a, tmp);
std::cout << "start calibration Nose Down and Type int and enter:"
<< " \n";
std::cin >> a;
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelNoseDown, a, tmp);
std::cout << "start calibration Nose Right Up and Type int and enter:"
<< " \n";
std::cin >> a;
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelNoseRight, a, tmp);
std::cout << "start calibration Nose Left Up and Type int and enter:"
<< " \n";
std::cin >> a;
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelNoseLeft, a, tmp);
std::cout << "start calibration Nose Top and Type int and enter:"
<< " \n";
std::cin >> a;
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelNoseTop, a, tmp);
std::cout << "start calibration Nose Rev and Type int and enter:"
<< " \n";
std::cin >> a;
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelNoseRev, a, tmp);
APM_Settle.APMCalibrator(ACCELCalibration, MPUAccelCaliGet, a, tmp);
configWrite(CONFIGDIR, optarg, "_flag_MPU9250_A_X_Cali", tmp[MPUAccelCaliX]);
configWrite(CONFIGDIR, optarg, "_flag_MPU9250_A_Y_Cali", tmp[MPUAccelCaliY]);
configWrite(CONFIGDIR, optarg, "_flag_MPU9250_A_Z_Cali", tmp[MPUAccelCaliZ]);
configWrite(CONFIGDIR, optarg, "_flag_MPU9250_A_X_Scal", tmp[MPUAccelScalX]);
configWrite(CONFIGDIR, optarg, "_flag_MPU9250_A_Y_Scal", tmp[MPUAccelScalY]);
configWrite(CONFIGDIR, optarg, "_flag_MPU9250_A_Z_Scal", tmp[MPUAccelScalZ]);
}
break;
case 'j':
{
std::ifstream config(CONFIGDIR);
std::string content((std::istreambuf_iterator<char>(config)),
(std::istreambuf_iterator<char>()));
nlohmann::json Mas = nlohmann::json::parse(content);
std::ofstream configs;
configs.open(CONFIGDIR);
configs << std::setw(4) << Mas << std::endl;
configs.close();
}
break;
}
}
}
void configSettle(const char *configDir, const char *substr, APMSettinngs &APMInit)
{
std::ifstream config(configDir);
std::string content((std::istreambuf_iterator<char>(config)),
(std::istreambuf_iterator<char>()));
nlohmann::json Mas = nlohmann::json::parse(content);
APMInit = SingleAPMAPI::readConfigFromJson(Mas[substr]);
}
void configWrite(const char *configDir, const char *substr, 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);
nlohmann::json subdata = Configdata[substr]["SensorConfig"];
subdata[Target] = obj;
Configdata[substr]["SensorConfig"] = subdata;
std::ofstream configs;
configs.open(configDir);
configs << std::setw(4) << Configdata << std::endl;
configs.close();
}
void SignalCatch(int Signal)
{
SingleAPMAPI::SystemSignal = Signal;
};