-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.c
83 lines (71 loc) · 1.8 KB
/
drive.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
#pragma systemFile
int drivekp = 10;
int drivekd = 0;
int driveki = 0;
int driveLSetPt = 0;
int driveRSetPt = 0;
void setDriveRight(int power) {
SetMotor(driveRF, power);
SetMotor(driveRB, power);
}
void setDriveLeft(int power) {
SetMotor(driveLF, -power);
SetMotor(driveLB, -power);
}
void setDrive(int power) {
setDriveRight(power);
setDriveLeft(power);
}
float leftDrive() { return -SensorValue[lDriveEncoder]; }
float rightDrive() { return SensorValue[rDriveEncoder]; }
task drivePID() {
float lError = 0;
float lPrevError = 0;
float lIntegral = 0;
float lDerivative = 0;
float rError = 0;
float rPrevError = 0;
float rIntegral = 0;
float rDerivative = 0;
while(true) {
lError = driveLSetPt - leftDrive();
rError = driveRSetPt - rightDrive();
lIntegral += lError;
rIntegral += rError;
lDerivative = lError - lPrevError;
rDerivative = rError - rPrevError;
setDriveLeft(1*((drivekp*lError)+(driveki*lIntegral)+(drivekd*lDerivative)));
setDriveRight(1*((drivekp*rError)+(driveki*rIntegral)+(drivekd*rDerivative)));
lPrevError = lError;
rPrevError = rError;
writeDebugStreamLine("drive: %f, %f", lError, rError);
}
}
void startDrivePID(int kp, int kd = 0, int ki = 0) {
drivekp = kp;
drivekd = kd;
driveki = ki;
startTask(drivePID);
}
void changeLDrive(int change) {
driveLSetPt += change;
}
void changeRDrive(int change) {
driveRSetPt += change;
}
void changeDrive(int change) {
changeLDrive(change);
changeRDrive(change);
}
void changeDrive(int changeLeft, int changeRight) {
changeLDrive(changeLeft);
changeRDrive(changeRight);
}
void arcade() {
motor[driveRB] = motor[driveRF] = (vexRT[Ch3] - vexRT[Ch1]);
motor[driveLB] = motor[driveLF] = (vexRT[Ch3] + vexRT[Ch1]);
}
void tank() {
motor[driveRB] = motor[driveRF] = vexRT[Ch2];
motor[driveLB] = motor[driveLF] = vexRT[Ch3];
}