-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomptition.cpp
More file actions
455 lines (394 loc) · 16.2 KB
/
comptition.cpp
File metadata and controls
455 lines (394 loc) · 16.2 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "vex.h"
// Test function to validate triple-down logic (for development testing)
void testTripleDownLogic()
{
logHandler("testTripleDownLogic", "Testing triple-down detection logic", Log::Level::Info);
// Test variables
int downPressCount = 0;
vex::timer downPressTimer;
const int TRIPLE_DOWN_WINDOW_MS = 1500;
// Test case 1: Quick triple press (should work)
downPressCount = 0;
downPressTimer.clear();
// Simulate first press
downPressCount++;
if (downPressCount < 3) downPressTimer.clear();
// Simulate second press after 300ms
vex::this_thread::sleep_for(300);
if (downPressTimer.time() <= TRIPLE_DOWN_WINDOW_MS) {
downPressCount++;
if (downPressCount < 3) downPressTimer.clear();
}
// Simulate third press after another 300ms
vex::this_thread::sleep_for(300);
if (downPressTimer.time() <= TRIPLE_DOWN_WINDOW_MS) {
downPressCount++;
if (downPressCount >= 3) {
logHandler("testTripleDownLogic", "Test case 1 PASSED: Quick triple press detected", Log::Level::Info);
}
}
// Test case 2: Slow triple press (should fail)
downPressCount = 0;
downPressTimer.clear();
downPressCount++;
vex::this_thread::sleep_for(800); // Wait longer
if (downPressTimer.time() <= TRIPLE_DOWN_WINDOW_MS) {
downPressCount++;
vex::this_thread::sleep_for(800); // Wait longer again
if (downPressTimer.time() <= TRIPLE_DOWN_WINDOW_MS) {
downPressCount++;
} else {
logHandler("testTripleDownLogic", "Test case 2 PASSED: Slow triple press correctly rejected", Log::Level::Info);
}
}
logHandler("testTripleDownLogic", "Triple-down logic testing completed", Log::Level::Info);
}
void autonomous()
{
logHandler("autonomous", "Test message.", Log::Level::Warn, 2);
return;
}
void collision(const vex::axisType axis, const double x, const double y, const double z)
{
printf("collision %d %6.2f %6.2f %6.2f\n", (int)axis, x, y, z);
}
// Drive system flags
bool tractionControlEnabled = true;
bool stabilityControlEnabled = true;
bool absEnabled = true;
/**
* @author @DVT7125
* @date 4/10/24
* @brief User control task.
* @return 0
*/
// Function to display system states on the Brain Screen
void displaySystemStates()
{
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Traction Control: %s\nStability Control: %s\nABS: %s",
tractionControlEnabled ? "ON" : "OFF",
stabilityControlEnabled ? "ON" : "OFF",
absEnabled ? "ON" : "OFF");
}
// Function to apply traction control
void applyTractionControl(double &forwardVolts)
{
double frontLeftMotorRPM = frontLeftMotor.velocity(vex::velocityUnits::rpm);
double rearLeftMotorRPM = rearLeftMotor.velocity(vex::velocityUnits::rpm);
double frontRightMotorRPM = frontRightMotor.velocity(vex::velocityUnits::rpm);
double rearRightMotorRPM = rearRightMotor.velocity(vex::velocityUnits::rpm);
double minSpeed = std::min({std::abs(frontLeftMotorRPM), std::abs(rearLeftMotorRPM), std::abs(frontRightMotorRPM), std::abs(rearRightMotorRPM)});
forwardVolts = minSpeed;
}
// Function to apply stability control
void applyStabilityControl(const double &forwardVolts)
{
double leftRPM = LeftDriveSmart.velocity(vex::velocityUnits::rpm);
double rightRPM = RightDriveSmart.velocity(vex::velocityUnits::rpm);
double minSpeed = std::min(std::abs(leftRPM), std::abs(rightRPM));
LeftDriveSmart.spin(vex::directionType::fwd, minSpeed, vex::voltageUnits::volt);
RightDriveSmart.spin(vex::directionType::fwd, minSpeed, vex::voltageUnits::volt);
}
// Function to apply ABS
void applyABS(double &brakeVolts)
{
double frontLeftMotorRPM = frontLeftMotor.velocity(vex::velocityUnits::rpm);
double rearLeftMotorRPM = rearLeftMotor.velocity(vex::velocityUnits::rpm);
double frontRightMotorRPM = frontRightMotor.velocity(vex::velocityUnits::rpm);
double rearRightMotorRPM = rearRightMotor.velocity(vex::velocityUnits::rpm);
double minSpeed = std::min({std::abs(frontLeftMotorRPM), std::abs(rearLeftMotorRPM), std::abs(frontRightMotorRPM), std::abs(rearRightMotorRPM)});
brakeVolts = minSpeed;
}
// Function to display drive mode menu
void displayDriveModeMenu()
{
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
getUserOption("Drive Mode", {"Left Arcade", "Right Arcade", "Split Arcade", "Tank"});
auto buttonPressDurations = controllerButtonsPressed(primaryController);
std::string buttonPressed;
if (buttonPressed == "A")
{
ConfigManager.setDriveMode(configManager::DriveMode::LeftArcade);
}
else if (buttonPressed == "B")
{
ConfigManager.setDriveMode(configManager::DriveMode::RightArcade);
}
else if (buttonPressed == "X")
{
ConfigManager.setDriveMode(configManager::DriveMode::SplitArcade);
}
else if (buttonPressed == "Y")
{
ConfigManager.setDriveMode(configManager::DriveMode::Tank);
}
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
primaryController.Screen.print("Drive Mode Selected");
}
// Function to display runtime settings menu
void displayRuntimeSettingsMenu()
{
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
primaryController.Screen.print("Runtime Settings");
// Simple menu with numbered options
primaryController.Screen.setCursor(2, 1);
primaryController.Screen.print("A: Drive Mode");
primaryController.Screen.setCursor(3, 1);
primaryController.Screen.print("B: Traction Ctrl");
primaryController.Screen.setCursor(4, 1);
primaryController.Screen.print("X: Stability Ctrl");
primaryController.Screen.setCursor(5, 1);
primaryController.Screen.print("Y: ABS Control");
vex::timer menuTimer;
bool menuActive = true;
bool buttonWasPressed = false; // Debounce flag
while (menuActive && Competition.isEnabled() && menuTimer.time() < 10000) // 10 second timeout
{
bool anyButtonPressed = primaryController.ButtonA.pressing() ||
primaryController.ButtonB.pressing() ||
primaryController.ButtonX.pressing() ||
primaryController.ButtonY.pressing() ||
primaryController.ButtonDown.pressing() ||
primaryController.ButtonUp.pressing();
if (!anyButtonPressed)
{
buttonWasPressed = false; // Reset debounce when no button is pressed
}
if (!buttonWasPressed && primaryController.ButtonA.pressing())
{
buttonWasPressed = true;
// Cycle through drive modes
configManager::DriveMode currentMode = ConfigManager.getDriveMode();
configManager::DriveMode newMode;
switch (currentMode)
{
case configManager::DriveMode::LeftArcade:
newMode = configManager::DriveMode::RightArcade;
break;
case configManager::DriveMode::RightArcade:
newMode = configManager::DriveMode::SplitArcade;
break;
case configManager::DriveMode::SplitArcade:
newMode = configManager::DriveMode::Tank;
break;
case configManager::DriveMode::Tank:
newMode = configManager::DriveMode::LeftArcade;
break;
}
ConfigManager.setDriveMode(newMode);
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
std::string modeText;
switch (newMode)
{
case configManager::DriveMode::LeftArcade:
modeText = "Left Arcade";
break;
case configManager::DriveMode::RightArcade:
modeText = "Right Arcade";
break;
case configManager::DriveMode::SplitArcade:
modeText = "Split Arcade";
break;
case configManager::DriveMode::Tank:
modeText = "Tank Drive";
break;
}
primaryController.Screen.print(("Mode: " + modeText).c_str());
vex::this_thread::sleep_for(1000);
menuActive = false;
}
else if (!buttonWasPressed && primaryController.ButtonB.pressing())
{
buttonWasPressed = true;
tractionControlEnabled = !tractionControlEnabled;
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
primaryController.Screen.print(tractionControlEnabled ? "Traction ON" : "Traction OFF");
vex::this_thread::sleep_for(1000);
menuActive = false;
}
else if (!buttonWasPressed && primaryController.ButtonX.pressing())
{
buttonWasPressed = true;
stabilityControlEnabled = !stabilityControlEnabled;
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
primaryController.Screen.print(stabilityControlEnabled ? "Stability ON" : "Stability OFF");
vex::this_thread::sleep_for(1000);
menuActive = false;
}
else if (!buttonWasPressed && primaryController.ButtonY.pressing())
{
buttonWasPressed = true;
absEnabled = !absEnabled;
primaryController.Screen.clearScreen();
primaryController.Screen.setCursor(1, 1);
primaryController.Screen.print(absEnabled ? "ABS ON" : "ABS OFF");
vex::this_thread::sleep_for(1000);
menuActive = false;
}
else if (!buttonWasPressed && (primaryController.ButtonDown.pressing() || primaryController.ButtonUp.pressing()))
{
buttonWasPressed = true;
// Exit menu if Down or Up is pressed
menuActive = false;
}
vex::this_thread::sleep_for(50);
}
primaryController.Screen.clearScreen();
}
// User control task
void userControl()
{
if (!Competition.isEnabled())
{
logHandler("drivetrain_main", "Ctrl1 is NOT in command mode!", Log::Level::Fatal);
}
vex::thread motortemp(motorMonitor);
InertialGyro.collision(collision);
double turnVolts, forwardVolts;
bool configMenuActive = false; // Tracks if options menu is active
// Triple-down detection variables
int downPressCount = 0;
vex::timer downPressTimer;
bool lastDownState = false;
const int TRIPLE_DOWN_WINDOW_MS = 1500; // 1.5 second window
// Load drive mode from config
configManager::DriveMode currentDriveMode = ConfigManager.getDriveMode();
int leftDeadzone = ConfigManager.getLeftDeadzone();
int rightDeadzone = ConfigManager.getRightDeadzone();
while (Competition.isEnabled())
{
// Triple-down detection for runtime settings
bool currentDownState = primaryController.ButtonDown.pressing();
if (currentDownState && !lastDownState) // Down button just pressed
{
if (downPressCount == 0 || downPressTimer.time() <= TRIPLE_DOWN_WINDOW_MS)
{
downPressCount++;
downPressTimer.clear();
if (downPressCount >= 3)
{
// Triple-down detected, open runtime settings
displayRuntimeSettingsMenu();
currentDriveMode = ConfigManager.getDriveMode(); // Update current mode
leftDeadzone = ConfigManager.getLeftDeadzone(); // Update deadzones if changed
rightDeadzone = ConfigManager.getRightDeadzone();
downPressCount = 0; // Reset counter
}
else if (downPressCount == 2)
{
// Give user feedback they're partway there
primaryController.rumble("-");
}
}
else
{
// Window expired, reset counter
downPressCount = 1;
downPressTimer.clear();
}
}
else if (downPressTimer.time() > TRIPLE_DOWN_WINDOW_MS && downPressCount > 0)
{
// Reset counter if window expires
downPressCount = 0;
}
lastDownState = currentDownState;
// Open configuration menu (existing UP button functionality)
if (primaryController.ButtonUp.pressing())
{
configMenuActive = !configMenuActive;
if (configMenuActive)
{
displayDriveModeMenu();
currentDriveMode = ConfigManager.getDriveMode(); // Update currentDriveMode after selection
}
}
switch (currentDriveMode)
{
case configManager::DriveMode::LeftArcade:
turnVolts = primaryController.Axis4.position() * 0.12; // -12 to 12
forwardVolts = primaryController.Axis3.position() * 0.12;
// Apply deadzones
if (std::abs(primaryController.Axis3.position()) < leftDeadzone)
{
forwardVolts = 0;
}
if (std::abs(primaryController.Axis4.position()) < rightDeadzone)
{
turnVolts = 0;
}
break;
case configManager::DriveMode::RightArcade:
turnVolts = primaryController.Axis1.position() * 0.12; // -12 to 12
forwardVolts = primaryController.Axis2.position() * 0.12;
// Apply deadzones
if (std::abs(primaryController.Axis2.position()) < leftDeadzone)
{
forwardVolts = 0;
}
if (std::abs(primaryController.Axis1.position()) < rightDeadzone)
{
turnVolts = 0;
}
break;
case configManager::DriveMode::SplitArcade:
turnVolts = primaryController.Axis1.position() * 0.12; // -12 to 12
forwardVolts = primaryController.Axis3.position() * 0.12;
// Apply deadzones
if (std::abs(primaryController.Axis3.position()) < leftDeadzone)
{
forwardVolts = 0;
}
if (std::abs(primaryController.Axis1.position()) < rightDeadzone)
{
turnVolts = 0;
}
break;
case configManager::DriveMode::Tank:
double leftVolts = primaryController.Axis3.position() * 0.12; // -12 to 12
double rightVolts = primaryController.Axis2.position() * 0.12; // -12 to 12
// Apply deadzones
if (std::abs(primaryController.Axis3.position()) < leftDeadzone)
{
leftVolts = 0;
}
if (std::abs(primaryController.Axis2.position()) < rightDeadzone)
{
rightVolts = 0;
}
LeftDriveSmart.spin(vex::directionType::fwd, leftVolts, vex::voltageUnits::volt);
RightDriveSmart.spin(vex::directionType::fwd, rightVolts, vex::voltageUnits::volt);
break;
}
if (currentDriveMode != configManager::DriveMode::Tank)
{
// Apply traction control if enabled
if (tractionControlEnabled)
{
applyTractionControl(forwardVolts);
}
// Apply stability control if enabled
if (stabilityControlEnabled)
{
applyStabilityControl(forwardVolts);
}
// Apply ABS if enabled
if (absEnabled)
{
applyABS(forwardVolts);
}
// Apply the calculated voltages to the motors
LeftDriveSmart.spin(vex::directionType::fwd, forwardVolts + turnVolts, vex::voltageUnits::volt);
RightDriveSmart.spin(vex::directionType::fwd, forwardVolts - turnVolts, vex::voltageUnits::volt);
}
vex::this_thread::sleep_for(ConfigManager.getCtrlr1PollingRate());
}
}