Repository navigation
Expand file tree
/
Copy pathmain_with_PID.cpp
More file actions
193 lines (140 loc) · 3.75 KB
/
Copy pathmain_with_PID.cpp
File metadata and controls
193 lines (140 loc) · 3.75 KB
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
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: C:\Users\USER */
/* Created: Sat Jan 11 2020 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// motor1 motor 1
// motor2 motor 2
// motor3 motor 3
// motor4 motor 4
// ramp1 motor 5
// ramp2 motor 6
// Controller1 controller
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
using namespace vex;
void PIDBaseControl (motor r, float target, float waitTime, float maxPower = 1);
class ramp {
private:
motor r = motor(PORT1, ratio18_1, false);
public:
bool run;
ramp(int32_t port){
r = motor(port, ratio18_1, false);
}
void raise(){
r.setReversed(false);
//set target revolution and wait time
PIDBaseControl(r, 20, .1);
}
void lower(){
r.setReversed(true);
// r.rotateFor(/*how many revolution*/, rev, /*how fast*/, rpm, true);
}
};
int fixTimerValue (float rawSeconds)
{
int miliseconds;
miliseconds = rawSeconds*1000;
if (miliseconds < 250)
{
miliseconds = 250;
}
return miliseconds;
}
void PIDBaseControl (motor r, float target, float waitTime, float maxPower)
{
float Kp = 0.34; // change
float Ki = 0.01;
float Kd = 0.5;
int error;
//needchanging
float proportion;
int integralRaw;
float integral;
int lastError;
int derivative;
// might need this or not
float integralActiveZone = 50;
float integralPowerLimit = 50/Ki;
int finalPower;
bool timerBool = true;
// reset ramp rotation
r.resetRotation();
Brain.resetTimer();
while (Brain.Timer.value() < fixTimerValue(waitTime))
{
error = target-r.rotation(rev);
proportion = Kp*error;
if (abs(error) < integralActiveZone && error != 0)
{
integralRaw = integralRaw+error;
}
else
{
integralRaw = 0;
}
if (integralRaw > integralPowerLimit)
{
integralRaw = integralPowerLimit;
}
if (integralRaw < -integralPowerLimit)
{
integralRaw = -integralPowerLimit;
}
integral = Ki*integralRaw;
derivative = Kd*(error-lastError);
lastError = error;
if (error == 0)
{
derivative = 0;
}
finalPower = proportion+integral+derivative;
if (finalPower > maxPower*127)
{
finalPower = maxPower*127;
}
else if (finalPower < -maxPower*127)
{
finalPower = -maxPower*127;
}
r.setVelocity(finalPower,rpm);
r.spin(forward);
vex::task::sleep(40);
if (error < 30)
{
timerBool = false;
}
if (timerBool)
{
Brain.resetTimer();
}
}
r.setVelocity(0,rpm);
r.spin(forward);
}
void run(int vertical, int horizontal){
motor1.spin(forward);
motor2.spin(forward);
motor3.spin(forward);
motor4.spin(forward);
motor1.setVelocity(vertical,rpm);
motor2.setVelocity(vertical,rpm);
motor3.setVelocity(horizontal,rpm);
motor4.setVelocity(horizontal,rpm);
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while(true){
run(Controller1.Axis3.position(percent),
Controller1.Axis4.position(percent)
);
}
}