-
Notifications
You must be signed in to change notification settings - Fork 3
/
turningFunctions.c
248 lines (194 loc) · 5.94 KB
/
turningFunctions.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifndef TURNINGFUNCTIONS_C_INCLUDED
#define TURNINGFUNCTIONS_C_INCLUDED
/**
* Turns clockwise for an angle in degrees
* @param angle Angle to turn for (deg)
*/
void turn(long angle)
{
//Save left and right quad values instead of setting them to zero
const long encoderLeft = nMotorEncoder[driveLFY], encoderRight = nMotorEncoder[driveRFY];
//Total angle change since start
float angleChange = 0, lastAngle = 0;
//Conversion between encoder degrees and base_link degrees
const float conv = 12.88361;
//Fix angle
while(angle>180)
angle-=360;
while(angle<=-180)
angle+=360;
int targetAngle = angle * conv;
pos_PID anglePID;
pos_PID_InitController(&anglePID, &angleChange, 0.61, 0.12, 0.07); //Ku = 1.4, Tu = 0.45
pos_PID_SetTargetPosition(&anglePID, targetAngle);
//If angle PID controller is at target
bool atTarget = false;
//Angle that is "close enough" to target
const int atTargetAngle = 10;
//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 angleOutput;
while (!atTarget)
{
//Calculate distance displacement
currentLeft = nMotorEncoder[driveLFY] - encoderLeft;
currentRight = nMotorEncoder[driveRFY] - encoderRight;
angleChange = currentRight - currentLeft;
//Get output from PID
angleOutput = pos_PID_StepController(&anglePID);
//Set motors to angle PID output
setLeftMotors(-1 * angleOutput);
setRightMotors(angleOutput);
//Place mark if we're close enough to the target angle
if (fabs(targetAngle - angleChange) <= atTargetAngle)
{
timer_PlaceHardMarker(&atTargetTimer);
}
//Place mark if we haven't moved much
else if (fabs(angleChange - lastAngle) <= threshold)
{
timer_PlaceHardMarker(&atTargetTimer);
}
else
{
timer_ClearHardMarker(&atTargetTimer);
}
lastAngle = angleChange;
//If we've been close enough for long enough, we're there
if (timer_GetDTFromHardMarker(&atTargetTimer) >= timeoutPeriod)
{
atTarget = true;
}
wait1Msec(15);
}
setAllDriveMotors(0);
}
/**
* Turns clockwise for an angle in degrees
* @param angle Angle to turn for (deg)
*/
void turn_SBallsy(long angle){
//Save left and right quad values instead of setting them to zero
const long encoderLeft = nMotorEncoder[driveLFY], encoderRight = nMotorEncoder[driveRFY];
//Total angle change since start
float angleChange = 0, lastAngle = 0;
//Conversion between encoder degrees and base_link degrees
const float conv = 12.88361;
//Fix angle
while(angle>180)
angle-=360;
while(angle<=-180)
angle+=360;
int targetAngle = angle * conv;
//If angle PID controller is at target
bool atTarget = false;
long currentLeft, currentRight;
//Angle that is "close enough" to target
const int atTargetAngle = 10;
while(!atTarget){
//Calculate distance displacement
currentLeft = nMotorEncoder[driveLFY] - encoderLeft;
currentRight = nMotorEncoder[driveRFY] - encoderRight;
angleChange = currentRight - currentLeft;
setLeftMotors(-1 * sgn(targetAngle) * 127);
setRightMotors(sgn(targetAngle) * 127);
atTarget = angleChange > targetAngle - atTargetAngle && angleChange < targetAngle + atTargetAngle;
}
setLeftMotors(sgn(targetAngle) * 127);
setRightMotors(-1 * sgn(targetAngle) * 127);
wait1Msec(20);
setAllDriveMotors(0);
}
void turn_Ballsy(long angle)
{
//Save left and right quad values instead of setting them to zero
const long encoderLeft = nMotorEncoder[driveLFY], encoderRight = nMotorEncoder[driveRFY];
//Total angle change since start
float angleChange = 0, lastAngle = 0;
//Conversion between encoder degrees and base_link degrees
const float conv = 12.88361;
//Fix angle
while(angle>180)
angle-=360;
while(angle<=-180)
angle+=360;
int targetAngle = angle * conv;
pos_PID anglePID;
pos_PID_InitController(&anglePID, &angleChange, 0.61, 0.12, 0.07); //Ku = 1.4, Tu = 0.45
pos_PID_SetTargetPosition(&anglePID, targetAngle);
//If angle PID controller is at target
bool atTarget = false;
//Angle that is "close enough" to target
const int atTargetAngle = 10;
//Threshold for not moving
const int threshold = 2;
//Timer for being at target
timer atTargetTimer, exitTimer;
timer_Initialize(&atTargetTimer);
timer_Initialize(&exitTimer);
//Timeout period (ms)
const int timeoutPeriod = 250;
//Current left and right quad displacements
long currentLeft, currentRight;
//Distance and angle PID output
int angleOutput;
while (!atTarget)
{
//Calculate distance displacement
currentLeft = nMotorEncoder[driveLFY] - encoderLeft;
currentRight = nMotorEncoder[driveRFY] - encoderRight;
angleChange = currentRight - currentLeft;
//Get output from PID
angleOutput = pos_PID_StepController(&anglePID);
//Set motors to angle PID output
setLeftMotors(-1 * angleOutput);
setRightMotors(angleOutput);
//Place mark if we're close enough to the target angle
if (fabs(targetAngle - angleChange) <= atTargetAngle)
{
timer_PlaceHardMarker(&atTargetTimer);
timer_PlaceHardMarker(&exitTimer);
}
//Place mark if we haven't moved much
else if (fabs(angleChange - lastAngle) <= threshold)
{
timer_PlaceHardMarker(&atTargetTimer);
}
else
{
timer_ClearHardMarker(&atTargetTimer);
}
lastAngle = angleChange;
//If we've been close enough for long enough, we're there
if (timer_GetDTFromHardMarker(&atTargetTimer) >= timeoutPeriod ||
timer_GetDTFromHardMarker(&exitTimer) >= 50)
{
break;
}
wait1Msec(15);
}
setAllDriveMotors(0);
}
/**
* Turns to an angle in the field frame
* @param deg Degrees to turn to
*/
void turnToAbsAngle(const long deg)
{
long theta = 0;
BCI_lockSem(std_msgSem, "turnToAbsAngle")
{
theta = std_msg[STD_MSG_EST_THETA];
BCI_unlockSem(std_msgSem, "turnToAbsAngle")
}
turn_SBallsy(deg - theta);
}
#endif //TURNINGFUNCTIONS_C_INCLUDED