This repository has been archived by the owner on Feb 18, 2019. It is now read-only.
forked from aerhoespace/lemonade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleftPID.c
403 lines (338 loc) · 10.2 KB
/
leftPID.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
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
#pragma platform(VEX)
#pragma competitionControl(Competition)
#pragma autonomousDuration(15)
#pragma userControlDuration(105)
// PID using optical shaft encoder
#define PID_INTEGRAL_LIMIT 50
#define PD_INTEGRAL_LIMIT 50
#define LEFT_SENSOR_INDEX leftEncoder
#define RIGHT_SENSOR_INDEX rightEncoder
#define PID_SENSOR_SCALE -1
#define LEFT_MOTOR_INDEX left1
#define RIGHT_MOTOR_INDEX right1
#define PID_MOTOR_SCALE -1
#define PID_DRIVE_MAX 80
#define PID_DRIVE_MIN (-80)
// These could be constants but leaving
// as variables allows them to be modified in the debugger "live"
static int pidRunning = 1;
static float pidRequestedValue;
static int pdRunning = 1;
static float pdRequestedValue;
bool taskRunning=false;
/*-----------------------------------------------------------------------------*/
/* */
/* PID control task */
/* */
/*-----------------------------------------------------------------------------*/
task leftPIDController()
{
taskRunning=true;
float pidSensorCurrentValue;
float pidError;
float pidLastError;
float pidIntegral;
float pidDerivative;
float pidDrive;
float pdSensorCurrentValue;
float pdError;
float pdLastError;
float pdIntegral;
float pdDerivative;
float pidDrive2;
// If we are using an encoder then clear it
if( SensorType[ LEFT_SENSOR_INDEX ] == sensorQuadEncoder )
SensorValue[ LEFT_SENSOR_INDEX ] = 0;
if( SensorType[ RIGHT_SENSOR_INDEX ] == sensorQuadEncoder )
SensorValue[ RIGHT_SENSOR_INDEX ] = 0;
// Init the variables - thanks Glenn :)
pidLastError = 0;
pidIntegral = 0;
pdLastError = 0;
pdIntegral = 0;
while( true ){
if( pidRunning ){
if(pidSensorCurrentValue==pidRequestedValue){
taskRunning = false;
stopTask(leftPIDController);
}
// Is PID control active ?
if( pdRunning )
{
// Read the sensor value and scale
pdSensorCurrentValue = SensorValue[ RIGHT_SENSOR_INDEX ] * PID_SENSOR_SCALE;
// Read the sensor value and scale
pidSensorCurrentValue = SensorValue[ LEFT_SENSOR_INDEX ] * PID_SENSOR_SCALE;
// Calculate error
pidError = pidSensorCurrentValue - pidRequestedValue;
// Calculate error
pdError = pdSensorCurrentValue - pdRequestedValue;
// Integral - if Ki is not 0
if( pd_Ki != 0 )
{
// If we are inside controlable window then integrate the error
if( abs(pdError) < PD_INTEGRAL_LIMIT )
pdIntegral = pdIntegral + pdError;
else
pdIntegral = 0;
}
else
pdIntegral = 0;
// Integral - if Ki is not 0
if( pid_Ki != 0 )
{
// If we are inside controlable window then integrate the error
if( abs(pidError) < PID_INTEGRAL_LIMIT )
pidIntegral = pidIntegral + pidError;
else
pidIntegral = 0;
}
else
pidIntegral = 0;
// Calculate the derivative
pidDerivative = pidError - pidLastError;
pidLastError = pidError;
// calculate the derivative
pdDerivative = pdError - pdLastError;
pdLastError = pdError;
// calculate drive
pidDrive = (pid_Kp * pidError) + (pid_Ki * pidIntegral) + (pid_Kd * pidDerivative);
pidDrive2 = (pd_Kp * pidError) + (pd_Ki * pidIntegral) + (pd_Kd * pidDerivative);
// Limit drive
if( pidDrive > PID_DRIVE_MAX )
pidDrive = PID_DRIVE_MAX;
if( pidDrive < PID_DRIVE_MIN )
pidDrive = PID_DRIVE_MIN;
if( pidDrive2 > PID_DRIVE_MAX )
pidDrive2 = PID_DRIVE_MAX;
if( pidDrive2 < PID_DRIVE_MIN )
pidDrive2 = PID_DRIVE_MIN;
leftDrive(pidDrive);
rightDrive(pidDrive2);
}
}else{
// Clear all
pidError = 0;
pidLastError = 0;
pidIntegral = 0;
pidDerivative = 0;
// Clear all
pdError = 0;
pdLastError = 0;
pdIntegral = 0;
pdDerivative = 0;
leftDrive(0);
rightDrive(0);
}
// Run at 50Hz
wait1Msec( 25 );
}
}
/*-----------------------------------------------------------------------------*/
/*
*/
/* Main task
/*
*/
/*-----------------------------------------------------------------------------*/
void drivePID(int clicks, int clicks2){
// Send the motor off somewhere
pidRequestedValue= clicks*-1;
pdRequestedValue= clicks2*-1;
// Start the PID task
startTask( leftPIDController );
taskRunning=true;
// startTask( rightPIDController );
// Use joystick to modify the requested position
while(taskRunning){
// Maximum change for pidRequestedValue will be 127/4*20, around 640 counts per second
// Free spinning motor is 100rmp so 1.67 rotations per second
// 1.67 * 360 counts is 600
wait1Msec(20);
}
stopTask(leftPIDController);
}
/*-----------------------------------------------------------------------------*/
/*
*/
/* Auton functions
/*
*/
/*-----------------------------------------------------------------------------*/
// Puncher
void autoShoot(int time){
SetMotor(puncher1,127);
SetMotor(puncher2,127);
wait1Msec(time);
}
void puncherStop(int time){
SetMotor(puncher1,0);
SetMotor(puncher2,0);
wait1Msec(time);
}
task shoot(){
while(true){
autoShoot(2000);
}
}
task dontShoot(){
while(true){
puncherStop(10);
}
}
// Intake
void autoTake(int time){
SetMotor(intake1,127);
SetMotor(intake2,127);
wait1Msec(time);
}
void intakeStop(int time){
SetMotor(intake1,0);
SetMotor(intake2,0);
wait1Msec(time);
}
task halfway(){
while(true){
autoTake(300);
}
}
task up(){
while(true){
autoTake(450);
}
}
task urdone(){
while(true){
intakeStop(10);
}
}
// Wait to start next task
void wait(){
wait1Msec(10);
}
/*----------------------------------------------------------------------------------------------------*\
|* - Point Turns with Encoders - *|
|* ROBOTC on VEX 2.0 CORTEX *|
|* *|
|* This program instructs the robot to turn left, and then right, using feedback from the encoders *|
|* to determine how much. There is a 2 second pause at the beginning of the program. *|
|* *|
|* ROBOT CONFIGURATION *|
|* NOTES: *|
|* 1) Reversing 'rightMotor' (port 2) in the "Motors and Sensors Setup" is needed with the *|
|* "Squarebot" model, but may not be needed for all robot configurations. *|
|* 2) Whichever encoder is being used for feedback should be cleared just before it starts *|
|* counting by using the "SensorValue(encoder) = 0;". This helps ensure consistancy. *|
|* *|
|* MOTORS & SENSORS: *|
|* [I/O Port] [Name] [Type] [Description] *|
|* Motor - Port 2 rightMotor VEX 3-wire module Right side motor *|
|* Motor - Port 3 leftMotor VEX 3-wire module Left side motor *|
|* Digital - Port 1,2 rightEncoder VEX Shaft Encoder Right side *|
|* Digital - Port 3,4 leftEncoder VEX Shaft Encoder Left side *|
\*----------------------------------------------------------------------------------------------------*/
// Drive
void resetEncoders(){
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
}
// Drive
void autoDrive(int speed1, int speed2){
SetMotor(left1,speed1);
SetMotor(right1,speed2);
SetMotor(left2,speed1);
SetMotor(right2,speed2);
SetMotor(left3,speed1);
SetMotor(right3,speed2);
}
void driveStop(int time){
SetMotor(left1,0);
SetMotor(right1,0);
SetMotor(left2,0);
SetMotor(right2,0);
SetMotor(left3,0);
SetMotor(right3,0);
wait1Msec(time);
}
// Turn left
void turnLeft(int leftVal){
resetEncoders();
while(SensorValue(rightEncoder) < leftVal)
{
rightDrive(127);
leftDrive(-127);
}
}
// Turn right
void turnRight(int rightVal){
resetEncoders();
while(SensorValue(leftEncoder) < rightVal)
{
rightDrive(-127);
leftDrive(127);
}
}
/*-----------------------------------------------------------------------------*/
/*
*/
/* Auton modes
/*
*/
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Blue alliance auton
/*-----------------------------------------------------------------------------*/
// Flag side (+6)
void auton1(){
// Reset encoders
resetEncoders();
// Drive forward to hit cap (+1)
drivePID(1150,1150);
// Intake ball halfway
startTask(halfway);
stopTask(halfway);
startTask(urdone);
stopTask(urdone);
// Drive back
resetEncoders();
drivePID(-1150,-1150);
// Turn left to face flags
resetEncoders();
turnLeft(360);
// Drive forward to top flag position
//resetEncoders();
//drivePID(300,300);
// Shoot top flag (+2)
//autoShoot(2000);
//puncherStop(50);
// Drive forward to middle flag position
//resetEncoders();
//drivePID(300,300);
// Intake ball rest of the way
//autoTake(600);
//intakeStop(50);
// Shoot middle flag (+2)
//autoShoot(2000);
//puncherStop(50);
// Drive forward to hit bottom flag (+1)
//resetEncoders();
//drivePID(600,600);
}
/*-----------------------------------------------------------------------------*/
/* Red alliance auton
/*-----------------------------------------------------------------------------*/
// Simple auton
void auton(){
// Start facing flags
// Drive forward to middle flag position
autoDrive(127,127);
wait1Msec(600);
driveStop(10);
// Shoot flag
autoShoot(2500);
puncherStop(10);
// Drive forward to hit bottom flag
autoDrive(127,127);
wait1Msec(600);
driveStop(10);
}