-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalibration.ino
190 lines (155 loc) · 5.12 KB
/
calibration.ino
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
// ToDo - replace compare values with STICK_CENTER THRESHOLD ETC
// Set default timeout if not configured
#if !defined(CAL_TIMEOUT)
#define CAL_TIMEOUT 5000
#endif
// Set default calibration trigger cannel if not configured
#if !defined(CAL_CHANNEL)
#if CHANNELS > 2
#define CAL_CHANNEL 3 // To CH 3 whenever is possible
#else
#define CAL_CHANNEL 1 // Otherwise to CH 1
#endif
#endif
#if defined(CAL_DISABLE)
Serial.println("cal disable");
// When calibration disabled set min-max values to manually configured
void rcCalibrate() { // Fake CALIBRATION
for (uint8_t e=0;e<CHANNELS;e++) {
rc_min_values[e] = STICK_CENTER - STICK_HALFWAY; // Mins
rc_max_values[e] = STICK_CENTER + STICK_HALFWAY; // Maxs
}
}
#else // Real CALIBRATION FUNCTIONS begins here...
bool cal_mode = false;
uint32_t cal_timer = 0L;
// Function to check and get calibration values
void rcCalibrate() {
cal_mode = checkIfCal(); // Check if calibration necessary
if (cal_mode) {
#if defined(SERIAL_DEBUG)
Serial.println("CALIBRATION ACTIVE");
#endif
// Set initial values probably off-range for calibration
for (uint8_t i=0;i<CHANNELS;i++) {
rc_min_values[i] = 2500;
rc_max_values[i] = 0;
}
}else{
Serial.println("cal NOT ACTIVE");
}
while (cal_mode) {
#if defined(PWM_RECEIVER)
rcProcessPwm();
#endif
unsigned long curtime = millis(); // Make a record of the current time
waveLed(500);
// ToDo - only process here ch's with new flag, like the process function of pwm
for (uint8_t i=0;i<CHANNELS;i++) {
if (rc_values[i] < 2500 && rc_values[i] > rc_max_values[i]) { // ...looking for the MAX's,
rc_max_values[i] = rc_values[i];
cal_timer = curtime + CAL_TIMEOUT;
}
else if (rc_values[i] > 0 && rc_values[i] < rc_min_values[i]) { // ...then for the MIN's
rc_min_values[i] = rc_values[i];
cal_timer = curtime + CAL_TIMEOUT;
}
}
// When there is no new mins or maxs since given timeout
if (cal_timer && cal_timer <= curtime) {
uint16_t diffvalues[CHANNELS];
boolean diffresult = true;
#if defined(SERIAL_DEBUG)
calPrintValues();
#endif
for (uint8_t x=0;x<CHANNELS;x++) {
// Calculate min-max differences, to see...
diffvalues[x] = rc_max_values[x] - rc_min_values[x];
// ... whether it is "enough" or not
if (diffvalues[x] < 360) {
diffvalues[x] = 0;
diffresult = false;
#if defined(SERIAL_DEBUG)
Serial.print("\t X ");
#endif
}
#if defined(SERIAL_DEBUG)
else {
Serial.print("\tOK ");
}
#endif
}
if (diffresult) {
blinkLed(4, 60); // Led blinks when calibration succeeded
writeMem(); // Store calibration values in the eeprom
#if defined(SERIAL_DEBUG)
Serial.print("\n\nCALIBRATION FINISHED\n");
#endif
cal_mode = false; // Reset the flag
}
else {
#if defined(SERIAL_DEBUG)
Serial.print("\n\nALL CHANNEL MUST BE \"OK\" TO AUTOSAVE!\n\n");
#endif
cal_timer = 0; // Reset timeout
}
}
}
}
bool checkIfCal() {
#if defined(SERIAL_DEBUG)
Serial.println("start nexttime");
uint32_t nexttime = millis();
#endif
Serial.print("using rc cal channel : ");
Serial.println(CAL_CHANNEL-1);
// Wait here until valid signal on CAL_CHANNEL pin
while (!rc_values[CAL_CHANNEL-1] && rc_values[CAL_CHANNEL-1] < 360) {
#if defined(SERIAL_DEBUG)
uint32_t curtime = millis();
if (curtime >= nexttime) {
Serial.println("NO SIGNAL ON CAL_CHANNEL!");
nexttime = curtime + 1000;
}
#endif
#if defined(PWM_RECEIVER)
rcProcessPwm();
#endif
}
// Check if calibration necessary or triggered with full CAL_CHANNEL
for (uint8_t d=0;d<CHANNELS;d++) {
if (rc_min_values[d] < 360 || rc_max_values[d] > 2500
|| rc_min_values[d] > 1500 || rc_max_values[d] < 1500) {
return true;
}
}
if (rc_values[2] >= 1600) {
return true;
}
else {
#if defined(SERIAL_DEBUG)
calPrintValues();
Serial.println("CALIBRATION DATA LOADED FROM EEPROM.");
#endif
return false;
}
}
#if defined(SERIAL_DEBUG)
void calPrintValues() {
static char str[CHANNELS*7+2];
for (uint8_t c=0;c<CHANNELS;c++) {
sprintf(str, "%s\t%d", str, rc_max_values[c]);
}
Serial.print("MAX:");
Serial.print(str);
Serial.println();
str[0] = (char)0; // Clean the char[array]
for (uint8_t c=0;c<CHANNELS;c++) {
sprintf(str, "%s\t%d", str, rc_min_values[c]);
}
Serial.print("MIN:");
Serial.print(str);
Serial.println();
}
#endif
#endif