-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTesting
318 lines (249 loc) · 8.27 KB
/
Testing
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
#include "robot-config.h" //< Code for this is after the main function
#define current_rpm mFlywheel.velocity(velocityUnits::rpm)
// Note: voltage passed to the motor ranges from -12 V to +12 V //
void pdPID(int target) {
double power {0};
double error {target - current_rpm};
double last_error {0};
double kp {0};
double kd {0};
double ki {0};
while(fabs(error) > 5){
error = current_rpm;
double calc_P {kp * error};
double calc_D {kd * (error - last_error)};
double calc_I {0};
if(error < 20){
calc_I = ki * (error + last_error);
}
power = calc_P + calc_D + calc_I;
mFlywheel.spin(directionType::fwd, power, voltageUnits::volt); //yea this really needs to be checked before we run so we don't trash smth
this_thread::sleep_for(10);
}
}
static bool reverse {false};
static bool toggle {false};
static int mode {1};
void Toggle()
{
toggle = !toggle;
if (toggle)
mIntake.spin(directionType::fwd, 100, velocityUnits::pct);
else
mIntake.spin(directionType::fwd, 0, velocityUnits::pct);
}
void puIntake()
{mIntake.spin(directionType::fwd, 100, velocityUnits::pct);}
void pdIntake()
{
mIntake.spin(directionType::fwd, 0, velocityUnits::pct);
reverse = false;
}
void rIntake()
{
mIntake.spin(directionType::fwd, -100, velocityUnits::pct);
reverse = true;
}
void puIndexer()
{mIndexer.spin(directionType::fwd, 100, velocityUnits::pct);}
void pdIndexer()
{mIndexer.spin(directionType::fwd, 0, velocityUnits::pct);}
void rIndexer()
{mIndexer.spin(directionType::fwd, -100, velocityUnits::pct);}
void nlDrive_Tank_l()
{
int speed {joystick.Axis3.value()};
if (abs(speed) < 5)
speed = 0;
else
speed = ( (speed^2/100) * speed ) / 100;
if (joystick.Axis3.value() < 0)
speed = speed * -1;
else
speed = speed * 1;
mDrive_l1.spin(directionType::fwd, speed, velocityUnits::pct);
mDrive_l2.spin(directionType::fwd, speed, velocityUnits::pct);
}
void nlDrive_Tank_r()
{
int speed {joystick.Axis2.value()};
if (abs(speed) < 5)
speed = 0;
else
speed = ( (speed^2/100) * speed ) / 100;
if (joystick.Axis2.value() < 0)
speed = speed * -1;
else
speed = speed * 1;
mDrive_r1.spin(directionType::fwd, speed, velocityUnits::pct);
mDrive_r2.spin(directionType::fwd, speed, velocityUnits::pct);
}
void nlDrive_Arcade_l()
{
int speed {( joystick.Axis3.value() + joystick.Axis4.value() ) / 2};
if (abs(speed) < 5)
speed = 0;
else
speed = ( (speed^2/100) * speed ) / 100;
if ((( joystick.Axis3.value() + joystick.Axis4.value() ) / 2) < 0)
speed = speed * -1;
else
speed = speed * 1;
mDrive_l1.spin(directionType::fwd, speed, velocityUnits::pct);
mDrive_l2.spin(directionType::fwd, speed, velocityUnits::pct);
}
void nlDrive_Arcade_r()
{
int speed {( joystick.Axis3.value() - joystick.Axis4.value() ) / 2};
if (abs(speed) < 5)
speed = 0;
else
speed = ( (speed^2/100) * speed ) / 100;
if ((( joystick.Axis3.value() - joystick.Axis4.value() ) / 2) < 0)
speed = speed * -1;
else
speed = speed * 1;
mDrive_r1.spin(directionType::fwd, speed, velocityUnits::pct);
mDrive_r2.spin(directionType::fwd, speed, velocityUnits::pct);
}
void nlDrive_SplitArcade_l()
{
int speed {( joystick.Axis3.value() + joystick.Axis1.value() ) / 2};
if (abs(speed) < 5)
speed = 0;
else
speed = ( (speed^2/100) * speed ) / 100;
if ((( joystick.Axis3.value() + joystick.Axis1.value() ) / 2) < 0)
speed = speed * -1;
else
speed = speed * 1;
mDrive_l1.spin(directionType::fwd, speed, velocityUnits::pct);
mDrive_l2.spin(directionType::fwd, speed, velocityUnits::pct);
}
void nlDrive_SplitArcade_r()
{
int speed {( joystick.Axis3.value() - joystick.Axis1.value() ) / 2};
if (abs(speed) < 5)
speed = 0;
else
speed = ( (speed^2/100) * speed ) / 100;
if ((( joystick.Axis3.value() - joystick.Axis1.value() ) / 2) < 0)
speed = speed * -1;
else
speed = speed * 1;
mDrive_r1.spin(directionType::fwd, speed, velocityUnits::pct);
mDrive_r2.spin(directionType::fwd, speed, velocityUnits::pct);
}
void park()
{
mDrive_l1.stop();
mDrive_l2.stop();
mDrive_r1.stop();
mDrive_r2.stop();
}
void controlSwitch()
{
if (mode == 1)
{
joystick.Axis3.changed(nlDrive_Tank_l);
joystick.Axis2.changed(nlDrive_Tank_r);
}
else if (mode == 2)
{
joystick.Axis3.changed(nlDrive_Arcade_l);
joystick.Axis2.changed(nlDrive_Arcade_r);
}
else if (mode == 3)
{
joystick.Axis3.changed(nlDrive_SplitArcade_l);
joystick.Axis2.changed(nlDrive_SplitArcade_r);
}
if (mode >= 4)
mode = 1;
}
void pFlywheel()
{
int target {170};
int lamp {( sLight_top.value(analogUnits::pct) + sLight_bottom.value(analogUnits::pct) ) / 2};
if (60 <= lamp && lamp <= 70) //> will probiblly need to callibrate
mFlywheel.spin(directionType::fwd, target, velocityUnits::rpm);
else
//mFlywheel.stop();
mFlywheel.spin(directionType::fwd, 50, velocityUnits::rpm);
if(current_rpm < (target-10))
{
pdPID(target-20);
mFlywheel.spin(directionType::fwd, target-20, velocityUnits::rpm);
this_thread::sleep_for(1000);
}
}
int main()
{
while( 0==0 )
{
Brain.Screen.clearScreen();
Brain.Screen.print(mDrive_l1.temperature(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.print(mDrive_l2.temperature(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.print(mDrive_r1.temperature(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.print(mDrive_r2.temperature(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.print(mIntake.temperature(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.print(mIndexer.temperature(percentUnits::pct));
Brain.Screen.newLine();
}
/************** Flywheel Controller **************/
sLight_top.changed(pFlywheel);
sLight_bottom.changed(pFlywheel);
/***************** Drive Control *****************/
joystick.ButtonL1.pressed(controlSwitch);
joystick.ButtonA.pressed(park);
/************* Ball Intake & Indexer *************/
joystick.ButtonR1.pressed(puIndexer); //> indexer
joystick.ButtonR1.released(pdIndexer);
joystick.ButtonR2.pressed(Toggle); //> intake toggle
joystick.ButtonL2.pressed(rIntake); //> reverse
joystick.ButtonL2.pressed(rIndexer);
joystick.ButtonL2.released(pdIntake);
joystick.ButtonL2.released(pdIndexer);
}
/*
using namespace vex;
class motor_special : public motor {
private:
uint32_t _local_index;
public:
motor_special( int32_t index ) : motor( index ), _local_index(index) {};
~motor_special() {};
// Allow overloading of base class methods
using motor::spin;
// This is similar, not quite the same, as new a method in the next (Nov ?) SDK release
// voltage can be +/-12.0 volta or +/-12000 mV
void spin( directionType dir, double voltage, voltageUnits units ) {
// convert volts to mV is necessary
int32_t voltage_mv = (units == voltageUnits::volt ? voltage * 1000.0 : voltage );
// flip based on direction flag
voltage_mv = (dir == directionType::fwd ? voltage_mv : -(voltage_mv) );
if( voltage_mv == 0 ) {
stop();
} else {
// send mV value to control motor open loop
vexMotorVoltageSet( _local_index, voltage_mv );
}
}
};
brain Brain;
controller joystick = controller();
motor mDrive_l1 = motor(PORT1);
motor mDrive_l2 = motor(PORT2);
motor mDrive_r1 = motor(PORT3);
motor mDrive_r2 = motor(PORT4);
motor_special mFlywheel(PORT5);
light sLight_top = light(Brain.ThreeWirePort.A);
light sLight_bottom = light(Brain.ThreeWirePort.B);
motor mIntake = motor(PORT6);
motor mIndexer = motor(PORT7);
*/