-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
98 lines (72 loc) · 2.22 KB
/
example.cpp
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
void turn(double theta, bool isPossessingItem){
double turn_speed = 0;
double inaccuracyRange = 0.1;
double kp = 1.2; //proportional constant
double ki = 0.00005; //integration constant
double kd = 0.9; //derivative constant
if(isPossessingItem){ //center of mass impacted by certain game elements, retuning constants accordingly
kp = 1.0;
ki = 0.00002;
kd = 0.9;
}
int loop_count = 0;
double error = 0;
double previouserror = 0;
double derivative = 0;
double pwr = 0;
double area = 0;
double dT = 15;
double integral = 0;
double setpoint = theta;
int count = 0;
/* Do the following if using inertial
imu.resetRotation();
imu.resetHeading();
replace robotPos.heading with imu.heading(rotationUnits::deg) * scalingFactor if using inertial
can also create a field-centric system and not reset
*/
//assuming GPS/odom:
autonomous_func::position_info robotPos = odom::getRobotPosition();
while(fabs(robotPos.heading - setpoint) > inaccuracyRange) {
loop_count++;
error = setpoint - robotPos.heading;
pwr = error*kp;
turn_speed = pwr;
area = error * dT;
integral = integral + area;
if (fabs(error) <= 0.5){
integral = 0;
}
if (fabs(error) >= 100){
integral = 0;
}
derivative = error - previouserror;
previouserror = error;
pwr = error*kp + integral*ki + derivative*kd;
turn_speed = (pwr*12)/100;
if(pwr<0) {
turn_speed = fabs(turn_speed);
leftDrive.spin(directionType::fwd, turn_speed, voltageUnits::volt);
rightDrive.spin(directionType::reverse, turn_speed, voltageUnits::volt);
}
else {
turn_speed = fabs(turn_speed);
rightDrive.spin(directionType::fwd, turn_speed, voltageUnits::volt);
leftDrive.spin(directionType::reverse, turn_speed, voltageUnits::volt);
}
if(fabs(derivative) <= 0.01){
count+=1;
}
else if(fabs(derivative) > 0.01){
count = 0;
}
if(loop_count>10 && count>=10){
break;
}
vex::task::sleep(dT);
robotPos = odom::getRobotPosition(); //updates robotPos with current position; only necessary if using odom
}
leftDrive.stop();
rightDrive.stop();
}
}