-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.ino
263 lines (215 loc) · 7.41 KB
/
motor.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
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
#include "motor.h"
/**
* \brief Creates a motor
*
* \param none
*
* \return none
*/
Motor::Motor(void):
stepperMotor(AccelStepper::DRIVER, 3, 2)
{
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(Motor::SleepPowerPin, OUTPUT);
digitalWrite(Motor::SleepPowerPin, HIGH);
awakeState = false;
this->stepperMotor.setCurrentPosition(0);
//Set a default acceleration
this->stepperMotor.setAcceleration(Motor::StepperAccell);
}
/**
* \brief Starts the motor
*
* \param speed
* How fast to run the motor
*
* \return none
*/
void Motor::Start(float speed, Probe probe)
{
this->targetSpeed = speed;
if (this->awakeState == false)
{
digitalWrite(Motor::SleepPowerPin, LOW);
delay(3);
this->awakeState = true;
}
// If we'll be accelerating, just set the new max speed, as the motor driver
// will manage accelerating
if (this->targetSpeed > this->stepperMotor.speed())
{
// Reassign activity reporting tag.
activityTag = 1;
// Report
String message = "Motor: accelerating";
ProbeSubtype2 probe_subtype2 = ProbeSubtype2::Accelerating;
checkProbeMotor(activityTag, message, probe, probe_subtype2);
Report(this->targetSpeed, this->currentAccel, activityTag, message);
this->stepperMotor.setMaxSpeed(this->targetSpeed);
this->stepperMotor.move(Motor::nSteps);
this->state = State::Accelerating;
}
// Else, if we'll be decelerating, we'll need to coast the motor down from its
// current speed manually using stop()
else if (this->targetSpeed < this->stepperMotor.speed())
{
// Reassign activity reporting tag.
activityTag = 2;
// Report
String message = "Motor: decelerating";
ProbeSubtype2 probe_subtype2 = ProbeSubtype2::Decelerating;
checkProbeMotor(activityTag, message, probe, probe_subtype2);
Report(this->targetSpeed, this->currentAccel, activityTag, message);
//this->stepperMotor.stop();
this->RoundedStop();
this->state = State::Decelerating;
}
// Else, if speed is staying the same
else
{
// Reassign activity reporting tag.
activityTag = 3;
// Report
String message = "Motor: maintaining current speed ";
ProbeSubtype2 probe_subtype2 = ProbeSubtype2::Maintaining;
checkProbeMotor(activityTag, message, probe, probe_subtype2);
Report(this->targetSpeed, this->currentAccel, activityTag, message);
this->stepperMotor.setMaxSpeed(this->targetSpeed);
this->stepperMotor.move(Motor::nSteps);
this->state = State::Running;
}
}
/**
* \brief Stops the motor
*
* \param none
*
* \return none
*/
void Motor::Stop(Probe probe)
{
this->targetSpeed = 0;
// Reassign activity reporting tag.
activityTag = 4;
// Report
String message = "Motor: stopping ";
ProbeSubtype2 probe_subtype2 = ProbeSubtype2::Stopping;
checkProbeMotor(activityTag, message, probe, probe_subtype2);
Report(this->targetSpeed, this->currentAccel, activityTag, message);
// Stop our motor
//this->stepperMotor.stop();
RoundedStop();
this->state = State::Stopping;
}
/**
* \brief Handles controlling the motor
*
* \param none
*
* \return none
*/
void Motor::RunOnce(void)
{
switch (this->state)
{
case State::Idle:
{
// Make sure enable pin is high during waiting for trigger period.
digitalWrite(Motor::SleepPowerPin, HIGH);
// Nothing else to do
return;
}
case State::Stopping:
{
// When motor has reached final position
if (this->stepperMotor.distanceToGo() == 0)
{
// Reassign activity reporting tag.
activityTag = 5;
// Report
String message = "Motor: finished stopping ";
Report(this->targetSpeed, this->currentAccel, activityTag, message);
// this->stepperMotor.setCurrentPosition(0);
// Turn off the motor's power
digitalWrite(Motor::SleepPowerPin, HIGH);
awakeState = false;
// go to Idle state
this->state = State::Idle;
}
break;
}
case State::Accelerating:
{
// If the motor has reached the target speed
if (this->stepperMotor.speed() >= this->targetSpeed)
{
// Reassign activity reporting tag.
activityTag = 6;
// Report
String message = "Motor: reached faster speed";
Report(this->targetSpeed, this->currentAccel, activityTag, message);
// the AccelStepper library doesn't need to reset the set speed after accelerating, like decelerating does
this->state = State::Running;
}
break;
}
case State::Decelerating:
{
// If we've coasted enough and we're ready to start moving again, do so
if (this->stepperMotor.speed() <= this->targetSpeed)
{
// Reassign activity reporting tag.
activityTag = 7;
// Report
String message = "Motor: reached slower speed ";
Report(this->targetSpeed, this->currentAccel, activityTag, message);
this->stepperMotor.setMaxSpeed(this->targetSpeed);
this->stepperMotor.move(Motor::nSteps);
this->state = State::Running;
}
break;
}
case State::Running:
{
break;
}
}
// This holds in its 'memory' where the motor is supposed to go and checks if the
// stepper is due for another step; runs for every iteration of the loop
this->stepperMotor.run();
}
// Stops the motor on a whole step (always make divisible by 4) to avoid jerking on next start-up.
void Motor::RoundedStop(void)
{
if (this->stepperMotor.speed() != 0.0)
{
// Get the current position.
long currentPos = this->stepperMotor.currentPosition();
long stepsToStop = (long)((this->stepperMotor.speed() *this->stepperMotor.speed()) / (2.0 *this->currentAccel)) + 1; // Equation 16 (+integer rounding)
long roundedSteps = RoundUp(currentPos, stepsToStop, 4);
if (this->stepperMotor.speed() > 0)
{
this->stepperMotor.move(roundedSteps);
}
else
{
this->stepperMotor.move(-roundedSteps);
}
}
}
long Motor::RoundUp(long currentPos, long stepsToStop, int multiple)
{
if (stepsToStop == 0) return 0;
else{
// Create a holder number that incorporates where the motor is now/if it's not on a whole step AND the distance to stop.
long holder = currentPos + stepsToStop;
// Round holder number, subtract out the current position. Should return a whole-step number that takes both into account.
return ((holder + multiple - 1) / multiple) * multiple - currentPos;
}
}
void Motor::setAccel(float input_accel)
{
this->stepperMotor.setAcceleration(input_accel);
this->currentAccel = input_accel;
}