-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrivingFunctions.c
224 lines (175 loc) · 6.32 KB
/
drivingFunctions.c
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
#ifndef DRIVINGFUNCTIONS_C_INCLUDED
#define DRIVINGFUNCTIONS_C_INCLUDED
/**
* Drives in a straight line for a distance in mm
* @param distance Distance to drive for (mm)
*/
void driveStraight(const long distance)
{
//Save left and right quad values instead of setting them to zero
const long encoderLeft = nMotorEncoder[driveLFY], encoderRight = nMotorEncoder[driveRFY];
//Total distance elapsed since start and total angle change since start
float distanceElapsed = 0, angleChange = 0;
float lastDistance = 0;
//Conversion between encoder degrees and base_link mm
const float conv = 1.345;
//Target distance for the distance PID controller
//Angle PID controller's target is 0
int targetDistance = distance * conv;
pos_PID distancePID, anglePID;
if (distance <= 800)
{
//pos_PID_InitController(&distancePID, &distanceElapsed, 0.2, 0.2, 0.1);
pos_PID_InitController(&distancePID, &distanceElapsed, 0.4, 0.25, 0.167);
pos_PID_InitController(&anglePID, &angleChange, 0.5, 0.25, 0);
}
else
{
pos_PID_InitController(&distancePID, &distanceElapsed, 0.3, 0.2, 0.2);
pos_PID_InitController(&anglePID, &angleChange, 0.5, 0.7, 0);
}
pos_PID_SetTargetPosition(&distancePID, targetDistance);
pos_PID_SetTargetPosition(&anglePID, 0);
//If distance PID controller is at target
bool atTarget = false;
//Distance that is "close enough" to target
const int atTargetDistance = 15;
//Threshold for not moving
const int threshold = 2;
//Timer for being at target
timer atTargetTimer;
timer_Initialize(&atTargetTimer);
//Timeout period (ms)
const int timeoutPeriod = 250;
//Current left and right quad displacements
long currentLeft, currentRight;
//Distance and angle PID output
int distOutput, angleOutput;
while (!atTarget)
{
//Calculate distance displacement
currentLeft = nMotorEncoder[driveLFY] - encoderLeft;
currentRight = nMotorEncoder[driveRFY] - encoderRight;
//Overall displacement is the average of left and right displacements
distanceElapsed = (currentLeft + currentRight) / 2.0;
//Angle change doesn't need to be a real angle, just the difference in
//displacements
angleChange = currentLeft - currentRight;
//Get output from both PID's
distOutput = pos_PID_StepController(&distancePID);
angleOutput = pos_PID_StepController(&anglePID);
//Set motors to distance PID output with correction from angle PID
setLeftMotors(distOutput + angleOutput);
setRightMotors(distOutput - angleOutput);
//Place mark if we're close enough to the target distance
if (fabs(targetDistance - distanceElapsed) <= atTargetDistance)
{
timer_PlaceHardMarker(&atTargetTimer);
}
//Place mark if we haven't moved much
else if (fabs(distanceElapsed - lastDistance) <= threshold)
{
timer_PlaceHardMarker(&atTargetTimer);
}
else
{
timer_ClearHardMarker(&atTargetTimer);
}
lastDistance = distanceElapsed;
//If we've been close enough for long enough, we're there
if (timer_GetDTFromHardMarker(&atTargetTimer) >= timeoutPeriod)
{
atTarget = true;
}
wait1Msec(15);
}
setAllDriveMotors(0);
//writeDebugStreamLine("target: %1.2f", targetDistance);
}
/**
* Drives in a straight line for a distance in mm
* @param distance Distance to drive for (mm)
*/
void driveStraight_Ballsy(const long distance)
{
//Save left and right quad values instead of setting them to zero
const long encoderLeft = nMotorEncoder[driveLFY], encoderRight = nMotorEncoder[driveRFY];
//Total distance elapsed since start and total angle change since start
float distanceElapsed = 0, angleChange = 0;
float lastDistance = 0;
//Conversion between encoder degrees and base_link mm
const float conv = 1.345;
//Target distance for the distance PID controller
//Angle PID controller's target is 0
int targetDistance = distance * conv;
pos_PID distancePID, anglePID;
if (distance <= 800)
{
//pos_PID_InitController(&distancePID, &distanceElapsed, 0.2, 0.2, 0.1);
pos_PID_InitController(&distancePID, &distanceElapsed, 0.4, 0.25, 0);
pos_PID_InitController(&anglePID, &angleChange, 0.5, 0.25, 0);
}
else
{
pos_PID_InitController(&distancePID, &distanceElapsed, 0.3, 0.2, 0);
pos_PID_InitController(&anglePID, &angleChange, 0.5, 0.7, 0);
}
pos_PID_SetTargetPosition(&distancePID, targetDistance);
pos_PID_SetTargetPosition(&anglePID, 0);
//If distance PID controller is at target
bool atTarget = false;
//Distance that is "close enough" to target
const int atTargetDistance = 15;
//Threshold for not moving
const int threshold = 2;
//Timer for being at target
timer atTargetTimer;
timer_Initialize(&atTargetTimer);
//Timeout period (ms)
const int timeoutPeriod = 250;
//Current left and right quad displacements
long currentLeft, currentRight;
//Distance and angle PID output
int distOutput, angleOutput;
while (!atTarget)
{
//Calculate distance displacement
currentLeft = nMotorEncoder[driveLFY] - encoderLeft;
currentRight = nMotorEncoder[driveRFY] - encoderRight;
//Overall displacement is the average of left and right displacements
distanceElapsed = (currentLeft + currentRight) / 2.0;
//Angle change doesn't need to be a real angle, just the difference in
//displacements
angleChange = currentLeft - currentRight;
//Get output from both PID's
distOutput = pos_PID_StepController(&distancePID);
angleOutput = pos_PID_StepController(&anglePID);
//Set motors to distance PID output with correction from angle PID
setLeftMotors(distOutput + angleOutput);
setRightMotors(distOutput - angleOutput);
//Place mark if we're close enough to the target distance
if (fabs(targetDistance - distanceElapsed) <= atTargetDistance)
{
break;
}
//Place mark if we haven't moved much
else if (fabs(distanceElapsed - lastDistance) <= threshold)
{
timer_PlaceHardMarker(&atTargetTimer);
}
else
{
timer_ClearHardMarker(&atTargetTimer);
}
lastDistance = distanceElapsed;
//If we've been close enough for long enough, we're there
if (timer_GetDTFromHardMarker(&atTargetTimer) >= timeoutPeriod)
{
break;
}
wait1Msec(15);
}
setAllDriveMotors(0);
//writeDebugStreamLine("target: %1.2f", targetDistance);
}
#endif //DRIVINGFUNCTIONS_C_INCLUDED