-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotor.h
321 lines (258 loc) · 8.62 KB
/
Motor.h
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (C) 2018 Lakoja on github.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MOTOR_H__
#define __MOTOR_H__
#include <math.h>
#include <PID_v1.h>
#include "Task.h"
#include "MotorWatcher.h"
/**
* This one operates two wheels. Both with the feedback of an encoder and controlled by a PID.
* NOTE the PID_v1 must be patched to incorporate a decaying error (line 70: outputSum = 0.9 * outputSum; // fade old values out).
*/
class Motor: public Task
{
private:
const float DEAD_ZONE_SPEED_HIGH = 0.92; // do not go above this normally: leave room for a slightly slower motor of a pair to keep up
const float DEAD_ZONE_SPEED_LOW = 0.20; // motor doesn't move properly (too weak) below this
const float LIVE_ZONE_RANGE = DEAD_ZONE_SPEED_HIGH - DEAD_ZONE_SPEED_LOW;
uint8_t MOTOR_R1;
uint8_t MOTOR_R2;
uint8_t MOTOR_L1;
uint8_t MOTOR_L2;
MotorWatcher watcher;
// set points are rpm
double pidInputRight = 0, pidOutputRight = 0, pidSetpointRight = 0, pidInputLeft = 0, pidOutputLeft = 0, pidSetpointLeft = 0;
double p = 14, i = 80, d = 0;
PID *pidRight = NULL;
PID *pidLeft = NULL;
uint32_t systemStart;
// the externally (or automatically) requested values
float motorRSpeedDesired = 0;
float motorLSpeedDesired = 0;
double currentPwmRight = 0;
double currentPwmLeft = 0;
uint32_t motorREndTime;
uint32_t motorLEndTime;
uint16_t maxSpeedInt;
uint32_t lastDriveLoopTime = 0;
uint16_t motorMaxTurns = 0;
uint32_t lastCounterOutTime = 0;
public:
Motor()
{
pidRight = new PID(&pidInputRight, &pidOutputRight, &pidSetpointRight, p, i, d, DIRECT);
pidLeft = new PID(&pidInputLeft, &pidOutputLeft, &pidSetpointLeft, p, i, d, DIRECT);
}
void setup(
uint8_t forePinRight,
uint8_t backPinRight,
uint8_t interruptRight,
uint8_t forePinLeft,
uint8_t backPinLeft,
uint8_t interruptLeft,
uint16_t umin,
uint16_t reduction)
{
MOTOR_R1 = forePinRight;
MOTOR_R2 = backPinRight;
MOTOR_L1 = forePinLeft;
MOTOR_L2 = backPinLeft;
uint8_t precision = 10;
maxSpeedInt = pow(2, precision) - 1;
outputPin(MOTOR_R1);
outputPin(MOTOR_R2);
outputPin(MOTOR_L1);
outputPin(MOTOR_L2);
watcher.setup(interruptRight, interruptLeft, reduction);
pidRight->SetOutputLimits(0, maxSpeedInt);
pidLeft->SetOutputLimits(0, maxSpeedInt);
// a sensible frequency for my motors
uint16_t maxRpm = umin * 2;
motorMaxTurns = umin;
// NOTE there are groups at work here: The setting for channel 1 also resets the one for 0.
// Some documentation would have been fine here generally...
ledcSetup(0, maxRpm, precision); // precision bits 8: means we have 0..255 as steps, 4: 0..15
ledcSetup(1, maxRpm, precision);
ledcSetup(2, maxRpm, precision);
ledcSetup(3, maxRpm, precision);
ledcAttachPin(MOTOR_R1, 0);
ledcAttachPin(MOTOR_R2, 1);
ledcAttachPin(MOTOR_L1, 2);
ledcAttachPin(MOTOR_L2, 3);
ledcWrite(0, 0);
ledcWrite(1, 0);
ledcWrite(2, 0);
ledcWrite(3, 0);
pidRight->SetMode(AUTOMATIC);
pidLeft->SetMode(AUTOMATIC);
systemStart = millis();
}
void run()
{
while (true) {
uint32_t now = millis();
watcher.drive();
if (lastDriveLoopTime > 0) {
uint16_t passed = now - lastDriveLoopTime;
if (now >= motorREndTime) {
motorRSpeedDesired = 0;
}
if (now >= motorLEndTime) {
motorLSpeedDesired = 0;
}
}
setPidSetpoints(motorRSpeedDesired, motorLSpeedDesired);
setPidInputs(watcher.currentTurnsRight(), watcher.currentTurnsLeft());
bool rightValueNew = pidRight->Compute();
bool leftValueNew = pidLeft->Compute();
if (rightValueNew) {
double signR = motorRSpeedDesired >= 0 ? 1 : -1;
switchMotorR(signR * pidOutputRight);
}
if (leftValueNew) {
double signL = motorLSpeedDesired >= 0 ? 1 : -1;
switchMotorL(signL * pidOutputLeft);
}
if (now - lastCounterOutTime > 1200) {
float dtL = getCurrentlyDesiredTurns(motorLSpeedDesired);
float dtR = getCurrentlyDesiredTurns(motorRSpeedDesired);
Serial.print("Turns R c"+String(watcher.currentTurnsRight()) + "r" +String(dtL) + " p"+String( pidOutputRight));
Serial.println(" L c"+String(watcher.currentTurnsLeft())+ "r" +String(dtR) + " p"+String( pidOutputLeft));
lastCounterOutTime = now;
}
lastDriveLoopTime = now;
sleepAfterLoop(4, now);
}
}
void requestMovement(float forward, float right, uint16_t durationMillis = 1000) {
// This is the only one with value range -1 .. 1
float rightSpeed = forward;
float leftSpeed = forward;
rightSpeed -= right / 2;
leftSpeed += right / 2;
requestRight(rightSpeed, durationMillis);
requestLeft(leftSpeed, durationMillis);
}
void requestRight(float value, uint16_t durationMillis = 1000)
{
// TODO check for value range?
uint32_t now = millis();
motorRSpeedDesired = value;
motorREndTime = now + durationMillis;
}
void requestLeft(float value, uint16_t durationMillis = 1000)
{
uint32_t now = millis();
motorLSpeedDesired = value;
motorLEndTime = now + durationMillis;
}
void requestForward(float value, uint16_t durationMillis = 1000)
{
uint32_t desiredEndTime = millis() + durationMillis;
motorRSpeedDesired = value;
motorREndTime = desiredEndTime;
motorLSpeedDesired = value;
motorLEndTime = desiredEndTime;
}
void requestReverse(float value, uint16_t durationMillis = 1000)
{
uint32_t desiredEndTime = millis() + durationMillis;
motorRSpeedDesired = -value;
motorREndTime = desiredEndTime;
motorLSpeedDesired = -value;
motorLEndTime = desiredEndTime;
}
private:
void outputPin(int num)
{
digitalWrite(num, LOW);
pinMode(num, OUTPUT);
}
void setPidSetpoints(float desireR, float desireL)
{
double workingFactor = 1.15; // give PID something (a persistent error) to work with
double newSetpointRight = workingFactor * getNonDeadSpeed(desireR) * motorMaxTurns;
double newSetpointLeft = workingFactor * getNonDeadSpeed(desireL) * motorMaxTurns;
pidSetpointRight = newSetpointRight;
pidSetpointLeft = newSetpointLeft;
/*
Serial.print("SP "+String(pidSetpointRight)+" ");
if (++outCounter % 20 == 0)
Serial.println();*/
}
void setPidInputs(float currentR, float currentL)
{
// Rotation values are always positive
pidInputRight = currentR;
pidInputLeft = currentL;
}
void switchMotorR(double pwmValue)
{
if (pwmValue != currentPwmRight) {
/*
if (showDebug) {
Serial.print("RRa"+String(pwmValue)+" ");
if (++outCounter % 20 == 0)
Serial.println();
}*/
int16_t speedInt = round(pwmValue);
switchMotor(speedInt, 0, 1);
currentPwmRight = pwmValue;
}
}
void switchMotorL(double pwmValue)
{
if (pwmValue != currentPwmLeft) {
if (showDebug && random(5) == 4) {
Serial.print("LRa"+String(pwmValue)+" ");
if (++outCounter % 20 == 0)
Serial.println();
}
int16_t speedInt = round(pwmValue);
switchMotor(speedInt, 2, 3);
currentPwmLeft = pwmValue;
}
}
uint16_t outCounter = 0;
bool showDebug = true;
bool switchMotor(int16_t speedInt, uint8_t channelForward, uint8_t channelReverse)
{
uint16_t chan1Speed = _min(maxSpeedInt, speedInt >= 0 ? speedInt : 0);
uint16_t chan2Speed = _min(maxSpeedInt, speedInt < 0 ? -speedInt : 0);
/*
if (showDebug) {
Serial.print("SWM "+String(chan1Speed)+","+String(chan2Speed)+" ");
if (++outCounter % 20 == 0)
Serial.println();
}*/
ledcWrite(channelForward, chan1Speed);
ledcWrite(channelReverse, chan2Speed);
}
double getNonDeadSpeed(float speed)
{
double nonDeadSpeed = 0;
if (speed != 0) {
nonDeadSpeed = DEAD_ZONE_SPEED_LOW + LIVE_ZONE_RANGE * abs(speed);
}
return nonDeadSpeed;
}
float getCurrentlyDesiredTurns(float desiredSpeed)
{
return desiredSpeed * motorMaxTurns;
}
};
#endif