-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAlfredo_NoU2.cpp
421 lines (374 loc) · 12.4 KB
/
Alfredo_NoU2.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "esp32-hal-ledc.h"
#include "Arduino.h"
#include "Alfredo_NoU2.h"
uint8_t RSL::state = RSL_OFF;
float fmap(float val, float in_min, float in_max, float out_min, float out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
NoU_Motor::NoU_Motor(uint8_t motorPort)
{
switch (motorPort) {
case 1:
aPin = MOTOR1_A;
bPin = MOTOR1_B;
channel = MOTOR1_CHANNEL;
break;
case 2:
aPin = MOTOR2_A;
bPin = MOTOR2_B;
channel = MOTOR2_CHANNEL;
break;
case 3:
aPin = MOTOR3_A;
bPin = MOTOR3_B;
channel = MOTOR3_CHANNEL;
break;
case 4:
aPin = MOTOR4_A;
bPin = MOTOR4_B;
channel = MOTOR4_CHANNEL;
break;
case 5:
aPin = MOTOR5_A;
bPin = MOTOR5_B;
channel = MOTOR5_CHANNEL;
break;
case 6:
aPin = MOTOR6_A;
bPin = MOTOR6_B;
channel = MOTOR6_CHANNEL;
break;
}
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
// Code for version 3.x
#else
ledcSetup(channel, MOTOR_PWM_FREQ, MOTOR_PWM_RES);
#endif
pinMode(aPin, OUTPUT);
pinMode(bPin, OUTPUT);
setState(RELEASE);
setPower(0);
}
void NoU_Motor::setPower(uint16_t power) {
power = min(power, (uint16_t)((1 << MOTOR_PWM_RES) - 1));
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
switch (this->state) {
case FORWARD:
ledcWrite(aPin, power);
break;
case BACKWARD:
ledcWrite(bPin, power);
break;
case BRAKE:
ledcWrite(aPin, power);
ledcWrite(bPin, power);
break;
case RELEASE:
break;
}
#else
ledcWrite(channel, power);
#endif
this->output = (state == BACKWARD ? -1 : 1) * ((float)power / ((1 << MOTOR_PWM_RES) - 1));
}
void NoU_Motor::setState(uint8_t state) {
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
switch (state) {
case FORWARD:
ledcAttachChannel(aPin, MOTOR_PWM_FREQ, MOTOR_PWM_RES, channel);
ledcDetach(bPin);
break;
case BACKWARD:
ledcDetach(aPin);
ledcAttachChannel(bPin, MOTOR_PWM_FREQ, MOTOR_PWM_RES, channel);
break;
case BRAKE:
ledcAttachChannel(aPin, MOTOR_PWM_FREQ, MOTOR_PWM_RES, channel);
ledcAttachChannel(bPin, MOTOR_PWM_FREQ, MOTOR_PWM_RES, channel);
break;
case RELEASE:
ledcDetach(aPin);
ledcDetach(bPin);
break;
}
this->state = state;
#else
switch (state) {
case FORWARD:
ledcAttachPin(aPin, channel);
ledcDetachPin(bPin);
break;
case BACKWARD:
ledcDetachPin(aPin);
ledcAttachPin(bPin, channel);
break;
case BRAKE:
ledcAttachPin(aPin, channel);
ledcAttachPin(bPin, channel);
break;
case RELEASE:
ledcDetachPin(aPin);
ledcDetachPin(bPin);
break;
}
this->state = state;
#endif
}
void NoU_Motor::set(float output) {
output = applyCurve(output);
setState(output > 0 ? FORWARD : BACKWARD);
setPower(fabs(output) * ((1 << MOTOR_PWM_RES) - 1));
}
float NoU_Motor::applyCurve(float input) {
return fmap((fabs(input) < deadband ? 0 : 1) // apply deadband
* pow(max(fmap(constrain(fabs(input), -1, 1), deadband, 1, 0, 1), 0.0f), exponent), // account for deadband, apply exponent
0, 1, minimumOutput, maximumOutput) // apply minimum and maximum output limits
* (input == 0 ? 0 : input > 0 ? 1 : -1) // apply original sign
* (inverted ? -1 : 1); // account for inversion
}
void NoU_Motor::setMinimumOutput(float minimumOutput) {
minimumOutput = constrain(minimumOutput, 0, maximumOutput);
this->minimumOutput = minimumOutput;
}
void NoU_Motor::setMaximumOutput(float maximumOutput) {
maximumOutput = constrain(maximumOutput, minimumOutput, 1);
this->maximumOutput = maximumOutput;
}
void NoU_Motor::setDeadband(float deadband) {
deadband = constrain(deadband, 0, 1);
this->deadband = deadband;
}
void NoU_Motor::setExponent(float exponent) {
exponent = max(0.0f, exponent);
this->exponent = exponent;
}
void NoU_Motor::setInverted(boolean inverted) {
this->inverted = inverted;
}
boolean NoU_Motor::isInverted() {
return inverted;
}
float NoU_Motor::getOutput() {
return output;
}
NoU_Servo::NoU_Servo(uint8_t servoPort, uint16_t minPulse, uint16_t maxPulse) {
switch (servoPort) {
case 1:
pin = SERVO1_PIN;
channel = SERVO1_CHANNEL;
break;
case 2:
pin = SERVO2_PIN;
channel = SERVO2_CHANNEL;
break;
case 3:
pin = SERVO3_PIN;
channel = SERVO3_CHANNEL;
break;
case 4:
pin = SERVO4_PIN;
channel = SERVO4_CHANNEL;
break;
}
this->minPulse = minPulse;
this->maxPulse = maxPulse;
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcAttachChannel(pin, SERVO_PWM_FREQ, SERVO_PWM_RES, channel);
#else
ledcSetup(channel, SERVO_PWM_FREQ, SERVO_PWM_RES);
ledcAttachPin(pin, channel);
#endif
}
void NoU_Servo::write(float degrees) {
writeMicroseconds(fmap(degrees, 0, 180, minPulse, maxPulse));
}
void NoU_Servo::writeMicroseconds(uint16_t pulseLength) {
this->pulse = pulseLength;
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcWrite(pin, fmap(pulseLength, 0, 20000, 0, (1 << SERVO_PWM_RES) - 1));
#else
ledcWrite(channel, fmap(pulseLength, 0, 20000, 0, (1 << SERVO_PWM_RES) - 1));
#endif
}
void NoU_Servo::setMinimumPulse(uint16_t minPulse) {
this->minPulse = minPulse;
}
void NoU_Servo::setMaximumPulse(uint16_t maxPulse) {
this->maxPulse = maxPulse;
}
uint16_t NoU_Servo::getMicroseconds() {
return pulse;
}
float NoU_Servo::getDegrees() {
return fmap(pulse, minPulse, maxPulse, 0, 180);
}
NoU_Drivetrain::NoU_Drivetrain(NoU_Motor* leftMotor, NoU_Motor* rightMotor)
: frontLeftMotor(leftMotor), frontRightMotor(rightMotor),
rearLeftMotor(), rearRightMotor(), drivetrainType(TWO_MOTORS)
{ }
NoU_Drivetrain::NoU_Drivetrain(NoU_Motor* frontLeftMotor, NoU_Motor* frontRightMotor,
NoU_Motor* rearLeftMotor, NoU_Motor* rearRightMotor)
: frontLeftMotor(frontLeftMotor), frontRightMotor(frontRightMotor),
rearLeftMotor(rearLeftMotor), rearRightMotor(rearRightMotor), drivetrainType(FOUR_MOTORS)
{ }
float NoU_Drivetrain::applyInputCurve(float input) {
return (fabs(input) < inputDeadband ? 0 : 1) // apply deadband
* pow(max(fmap(constrain(fabs(input), -1, 1), inputDeadband, 1, 0, 1), 0.0f), inputExponent) // account for deadband, apply exponent
* (input > 0 ? 1 : -1); // apply original sign
}
void NoU_Drivetrain::setMotors(float frontLeftPower, float frontRightPower, float rearLeftPower, float rearRightPower) {
switch (drivetrainType) {
case FOUR_MOTORS:
rearLeftMotor->set(rearLeftPower);
rearRightMotor->set(rearRightPower);
case TWO_MOTORS:
frontLeftMotor->set(frontLeftPower);
frontRightMotor->set(frontRightPower);
}
}
void NoU_Drivetrain::tankDrive(float leftPower, float rightPower) {
leftPower = applyInputCurve(leftPower);
rightPower = applyInputCurve(rightPower);
setMotors(leftPower, rightPower, leftPower, rightPower);
}
void NoU_Drivetrain::arcadeDrive(float throttle, float rotation, boolean invertedReverse) {
throttle = applyInputCurve(throttle);
rotation = applyInputCurve(rotation);
float leftPower = 0;
float rightPower = 0;
float maxInput = (throttle > 0 ? 1 : -1) * max(fabs(throttle), fabs(rotation));
if (throttle > 0) {
if (rotation > 0) {
leftPower = maxInput;
rightPower = throttle - rotation;
}
else {
leftPower = throttle + rotation;
rightPower = maxInput;
}
} else {
if (rotation > 0) {
leftPower = invertedReverse ? maxInput : throttle + rotation;
rightPower = invertedReverse ? throttle + rotation : maxInput;
}
else {
leftPower = invertedReverse ? throttle - rotation : maxInput;
rightPower = invertedReverse ? maxInput : throttle - rotation;
}
}
setMotors(leftPower, rightPower, leftPower, rightPower);
}
void NoU_Drivetrain::curvatureDrive(float throttle, float rotation, boolean isQuickTurn) {
throttle = applyInputCurve(throttle);
rotation = applyInputCurve(rotation);
float angularPower;
boolean overPower;
if (isQuickTurn) {
if (fabs(throttle) < quickStopThreshold) {
quickStopAccumulator = (1 - quickStopAlpha) * quickStopAccumulator + quickStopAlpha * rotation * 2;
}
overPower = true;
angularPower = rotation;
}
else {
overPower = false;
angularPower = fabs(throttle) * rotation - quickStopAccumulator;
if (quickStopAccumulator > 1) quickStopAccumulator--;
else if (quickStopAccumulator < -1) quickStopAccumulator++;
else quickStopAccumulator = 0;
}
float leftPower;
float rightPower;
leftPower = throttle + angularPower;
rightPower = throttle - angularPower;
if (overPower) {
if (leftPower > 1) {
rightPower -= leftPower - 1;
leftPower = 1;
} else if (rightPower > 1) {
leftPower -= rightPower - 1;
rightPower = 1;
} else if (leftPower < -1) {
rightPower -= leftPower + 1;
leftPower = -1;
} else if (rightPower < -1) {
leftPower -= rightPower + 1;
rightPower = -1;
}
}
float maxMagnitude = max(fabs(leftPower), fabs(rightPower));
if (maxMagnitude > 1) {
leftPower /= maxMagnitude;
rightPower /= maxMagnitude;
}
setMotors(leftPower, rightPower, leftPower, rightPower);
}
void NoU_Drivetrain::holonomicDrive(float xVelocity, float yVelocity, float rotation) {
if (drivetrainType == TWO_MOTORS) return;
xVelocity = applyInputCurve(xVelocity);
yVelocity = applyInputCurve(yVelocity);
rotation = applyInputCurve(rotation);
float frontLeftPower = xVelocity + yVelocity + rotation;
float frontRightPower = -xVelocity + yVelocity - rotation;
float rearLeftPower = -xVelocity + yVelocity + rotation;
float rearRightPower = xVelocity + yVelocity - rotation;
float maxMagnitude = max(fabs(frontLeftPower), max(fabs(frontRightPower), max(fabs(rearLeftPower), fabs(rearRightPower))));
if (maxMagnitude > 1) {
frontLeftPower /= maxMagnitude;
frontRightPower /= maxMagnitude;
rearLeftPower /= maxMagnitude;
rearRightPower /= maxMagnitude;
}
setMotors(frontLeftPower, frontRightPower, rearLeftPower, rearRightPower);
}
void NoU_Drivetrain::setMinimumOutput(float minimumOutput) {
switch (drivetrainType) {
case FOUR_MOTORS:
rearLeftMotor->setMinimumOutput(minimumOutput);
rearRightMotor->setMinimumOutput(minimumOutput);
case TWO_MOTORS:
frontLeftMotor->setMinimumOutput(minimumOutput);
frontRightMotor->setMinimumOutput(minimumOutput);
}
}
void NoU_Drivetrain::setInputExponent(float inputExponent) {
inputExponent = max(0.0f, inputExponent);
this->inputExponent = inputExponent;
}
void NoU_Drivetrain::setInputDeadband(float inputDeadband) {
inputDeadband = constrain(inputDeadband, 0, 1);
this->inputDeadband = inputDeadband;
}
void RSL::initialize() {
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcAttachChannel(RSL_PIN, RSL_PWM_FREQ, RSL_PWM_RES, RSL_CHANNEL);
#else
ledcSetup(RSL_CHANNEL, RSL_PWM_FREQ, RSL_PWM_RES);
ledcAttachPin(RSL_PIN, RSL_CHANNEL);
#endif
}
void RSL::setState(uint8_t state) {
RSL::state = state;
}
void RSL::update() {
uint32_t rsl_duty = 0;
switch (state) {
case RSL_OFF:
rsl_duty = 1;
break;
case RSL_ON:
rsl_duty = (1 << RSL_PWM_RES) - 1;
break;
case RSL_ENABLED:
rsl_duty = millis() % 1000 < 500 ? (millis() % 500) * 2 : (500 - (millis() % 500)) * 2;
break;
case RSL_DISABLED:
rsl_duty = (1 << RSL_PWM_RES) - 1;
break;
}
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcWrite(RSL_PIN, rsl_duty);
#else
ledcWrite(RSL_CHANNEL, rsl_duty);
#endif
}