-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed.cpp
More file actions
88 lines (67 loc) · 2.46 KB
/
speed.cpp
File metadata and controls
88 lines (67 loc) · 2.46 KB
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
/*
* speed.cpp
*
* Contains funcitons reponsible for speed method selection and calculation
*/
#include "settings.h"
/** Calculates speed output in relation to position error
*
* For low error values, the speed is set to the maximum value defined in settings.h,
* otherwise it is adjusted proportionally to the error value.
*
* @param LeftWheelSpeed pointer
* @param RightWheelSpeed pointer
* @param Error passed by value to avoid double pointer and possible memory access issues
*/
void updateSpeedRelative(float* SpeedOutputLeft, float* SpeedOutputRight, float positionLineError ) {
// Car turns left, set right wheel to be faster and right slower
// to improve turning angle with additional wheelpower
// position error should be lower than negative relative error threshold
if (positionLineError < (RELATIVE_MIN_ERR * -1.0)) {
*SpeedOutputLeft = SPEED_MAX - (SPEED_MAX - SPEED_MIN) * ( (float) positionLineError * 2 / CAM_DATA_LEN );
*SpeedOutputRight = SPEED_MIN;
}
// Car turns right, set left wheel to be slower and right faster
// to improve turning angle with additional wheelpower
// position error should be higher than positive relative error threshold
else if (positionLineError > RELATIVE_MIN_ERR) {
*SpeedOutputLeft = SPEED_MIN;
*SpeedOutputRight = SPEED_MAX - (SPEED_MAX - SPEED_MIN) * ( (float) positionLineError * 2 / CAM_DATA_LEN );
}
// If error is withing -/+ range of relative error threshold, full speed to improve
// lap time on the track
else {
*SpeedOutputLeft = SPEED_MAX;
*SpeedOutputRight = SPEED_MAX;
}
}
/** Selects desired calculation method
* for cars' speed output(SPEED_MODE)
*
* @param LeftWheelSpeed pointer
* @param RightWheelSpeed pointer
* @param Error variable pointer
*/
void updateSpeed(float positionLineError) {
float SpeedOutputLeft(0); // Left motor speed
float SpeedOutputRight(0); // Right motor speed
// Update speed
switch (SPEED_MODE) {
// Crazy fast mode - risky
case 0:
SpeedOutputLeft = SPEED_MAX;
SpeedOutputRight = SPEED_MAX;
break;
// Slow mode
case 1:
SpeedOutputLeft = SPEED_MIN;
SpeedOutputRight = SPEED_MIN;
break;
// Relative speed mode
case 2:
updateSpeedRelative(&SpeedOutputLeft, &SpeedOutputRight, positionLineError );
break;
}
// Set values
TFC_SetMotorPWM(SpeedOutputLeft, SpeedOutputRight);
}