-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAxisCalibration.cpp
270 lines (225 loc) · 9.99 KB
/
AxisCalibration.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "AxisCalibration.h"
Axis::Axis(int motorLeftPin, int motorRightPin, bool isRoll, Encoder* encoder, Multiplexer* multiplexerPtr)
: motorPinLeft(motorLeftPin), motorPinRight(motorRightPin), blIsRoll(isRoll), encoder(encoder), speed(1), lastMovementTime(millis()), multiplexer(multiplexerPtr)
{
}
// Method to move the motor in a given direction
void Axis::MoveMotor(bool direction) {
if (direction) {
analogWrite(motorPinLeft, 0);
analogWrite(motorPinRight, speed);
} else {
analogWrite(motorPinLeft, speed);
analogWrite(motorPinRight, 0);
}
}
// Method to stop the motor
void Axis::StopMotor() {
analogWrite(motorPinLeft, 0);
analogWrite(motorPinRight, 0);
delay(waitDelayMotorStops); // Small delay to ensure motor completely stops
}
// Helper method to manage motor movement
void Axis::ManageMovement( bool direction, unsigned long& lastMovementTime, int& lastEncoderValue, bool& speedIncreased) {
ReadMultiplexer(); // Update end switch states
int currentEncoderValue = encoder->read();
// Check for speed increase
if (abs(currentEncoderValue - lastEncoderValue)<=20) {
if (speed < maxSpeed) speed++; // Increment speed by 1
} else {
lastMovementTime = millis();
if (!speedIncreased) {
speed += speedIncrement; // Increase speed by defined increment
if (speed > maxSpeed) speed = maxSpeed; // Ensure speed doesn't exceed maxSpeed
speedIncreased = true; // Flag for speed increase
}
}
lastEncoderValue = currentEncoderValue;
MoveMotor(direction);
}
void Axis::ReadMultiplexer(){
multiplexer->ReadMux(); // Update end switch states
if(blIsRoll)
{
blEndSwitchLeft=multiplexer->EndSwitchRollLeft();
blEndSwitchRight=multiplexer->EndSwitchRollRight();
}else{
blEndSwitchLeft=multiplexer->EndSwitchPitchUp();
blEndSwitchRight=multiplexer->EndSwitchPitchDown();
}
}
int Axis::ResetEncoder(){
encoder->write(0);
return 0;
}
// Check Timeouts
bool Axis::CheckTimeouts(unsigned long lastMovementTime, unsigned long calibrationStartTime)
{
// Movement Timeout?
if (millis() - lastMovementTime >= timeout) {
StopMotor();
config.blError = true;
config.blAxisTimeout = true;
return true;
}
// General Timeout?
if (millis() - calibrationStartTime >= calibrationTimeout) {
StopMotor();
config.blError = true;
config.blTimeout=true;
return true;
}
return false;
}
// Calibration method for the axis
void Axis::Calibrate() {
config = {false, 0, 0, false, false, false, false}; // Reset config
calibrationStartTime = millis(); // Mark the calibration start time
int lastEncoderValue = ResetEncoder();
bool direction = true; // Start by moving in the positive direction
bool speedIncreased = false; // Flag for speed increase
speed = 1;
ReadMultiplexer(); // Update end switch states
StopMotor(); // Stop the motor once an end switch is hit
//**********************************************************************
// Step 1: Move away from the end switch if the axis is at an end switch
//**********************************************************************
if (blEndSwitchLeft || blEndSwitchRight) {
lastMovementTime = millis();
while (blEndSwitchLeft || blEndSwitchRight) {
ManageMovement(direction, lastMovementTime, lastEncoderValue, speedIncreased);
if(CheckTimeouts(lastMovementTime, calibrationStartTime))
break;
delay(whileDelay);
}
StopMotor(); // Stop the motor after leaving the end switch
delay(waitDelayAfterMoveOutEndstop); // Short delay for stabilization
}
// On error leave
if(config.blError)
{
return;
}
//**********************************************************************
// Step 2: Move towards the first end switch
//**********************************************************************
speed=1;
lastMovementTime = millis();
lastEncoderValue = ResetEncoder();
speedIncreased = false; // Reset for next loop
while (!blEndSwitchLeft && !blEndSwitchRight) {
// Serial.print(", direction:");
// Serial.print(direction);
// Serial.print(", lastMovementTime:");
// Serial.print(lastMovementTime);
// Serial.print(", lastEncoderValue:");
// Serial.print(lastEncoderValue);
// Serial.print(", speedIncreased:");
// Serial.print(speedIncreased);
// Serial.print(", speed:");
// Serial.println(speed);
ManageMovement(direction, lastMovementTime, lastEncoderValue, speedIncreased);
if(CheckTimeouts(lastMovementTime, calibrationStartTime))
break;
delay(whileDelay);
}
StopMotor(); // Stop the motor once an end switch is hit
// Determine which end switch was triggered and set motor inversion if necessary
if(blEndSwitchLeft)
{
config.blMotorInverted = true;
config.blError = true;
}
// On error leave
if(config.blError)
{
return;
}
//**********************************************************************
// Step 3: Move to the opposite end switch
//**********************************************************************
direction = !direction; // Move in the opposite direction
delay(1000);
speed=1;
lastMovementTime = millis();
lastEncoderValue = ResetEncoder();
speedIncreased = false; // Reset for next loop
while (!blEndSwitchLeft) {
// Serial.print(", direction:");
// Serial.print(direction);
// Serial.print(", lastMovementTime:");
// Serial.print(lastMovementTime);
// Serial.print(", lastEncoderValue:");
// Serial.print(lastEncoderValue);
// Serial.print(", speedIncreased:");
// Serial.print(speedIncreased);
// Serial.print(", speed:");
// Serial.println(speed);
ManageMovement(direction, lastMovementTime, lastEncoderValue, speedIncreased);
if(CheckTimeouts(lastMovementTime, calibrationStartTime))
break;
delay(whileDelay);
}
StopMotor(); // Stop the motor once an end switch is hit
if(encoder->read()<0)
{
config.blEncoderInverted = true;
config.blError = true;
}
// On error leave
if(config.blError)
{
return;
}
// Calculate iMin and iMax based on the encoder value
config.iMax = (encoder->read() / 2); // Set iMax as half of the maximum encoder value
config.iMin = -config.iMax; // Set iMin as the negative value of iMax
encoder->write(config.iMax); // Set the encoder to the iMax value at the end of the calibration
// Step 4: Move back until the encoder reaches 0
delay(1000);
speed = 1;
direction = !direction; // Move in the opposite direction
lastMovementTime = millis();
lastEncoderValue = config.iMax;
// Check the sign of the last encoder value
bool isLastValuePositive = (lastEncoderValue > 0);
//**********************************************************************
// 4. Move axis to the middle
//**********************************************************************
while (((isLastValuePositive && encoder->read() >= 0) || (!isLastValuePositive && encoder->read() <= 0)) && !blEndSwitchRight) {
// Serial.print(", direction:");
// Serial.print(direction);
// Serial.print(", lastMovementTime:");
// Serial.print(lastMovementTime);
// Serial.print(", lastEncoderValue:");
// Serial.print(lastEncoderValue);
// Serial.print(", speedIncreased:");
// Serial.print(speedIncreased);
// Serial.print(", speed:");
// Serial.println(speed);
ManageMovement( direction, lastMovementTime, lastEncoderValue, speedIncreased);
if (millis() - lastMovementTime >= timeout) {
StopMotor();
#ifdef SERIAL_DEBUG
Serial.println("Error: No movement detected for 4 seconds. Calibration aborted.");
#endif
config.blError = true;
return;
}
if (millis() - calibrationStartTime >= calibrationTimeout) {
StopMotor();
#ifdef SERIAL_DEBUG
Serial.println("Error: Calibration timeout (20 seconds). Aborted.");
#endif
config.blError = true;
return;
}
delay(whileDelay);
}
// Stop the motor when the encoder reaches 0
StopMotor();
}
// Method to get the current axis configuration
AxisConfiguration Axis::GetConfiguration() {
return config;
}