-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineControlProject.cpp
More file actions
305 lines (253 loc) · 8.56 KB
/
Copy pathMachineControlProject.cpp
File metadata and controls
305 lines (253 loc) · 8.56 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
#pragma region VEXcode Generated Robot Configuration
// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "vex.h"
using namespace vex;
// Brain should be defined by default
brain Brain;
// START V5 MACROS
#define waitUntil(condition) \
do { \
wait(5, msec); \
} while (!(condition))
#define repeat(iterations) \
for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS
// Robot configuration code.
motor elevatorMotor = motor(PORT10, ratio18_1, false);
distance elevatorDistance = distance(PORT2);
motor floorOneDoor = motor(PORT11, ratio18_1, false);
motor floorTwoDoor = motor(PORT12, ratio18_1, false);
motor floorThreeDoor = motor(PORT13, ratio18_1, false);
bumper floorOneBumper = bumper(Brain.ThreeWirePort.A);
bumper floorTwoBumper = bumper(Brain.ThreeWirePort.B);
bumper floorThreeBumper = bumper(Brain.ThreeWirePort.C);
led floorThreeLED = led(Brain.ThreeWirePort.H);
limit floorOneLS = limit(Brain.ThreeWirePort.D);
limit floorTwoLS = limit(Brain.ThreeWirePort.E);
limit floorThreeLS = limit(Brain.ThreeWirePort.F);
led floorTwoLED = led(Brain.ThreeWirePort.G);
// generating and setting random seed
void initializeRandomSeed(){
int systemTime = Brain.Timer.systemHighResolution();
double batteryCurrent = Brain.Battery.current();
double batteryVoltage = Brain.Battery.voltage(voltageUnits::mV);
// Combine these values into a single integer
int seed = int(batteryVoltage + batteryCurrent * 100) + systemTime;
// Set the seed
srand(seed);
}
void vexcodeInit() {
//Initializing random seed.
initializeRandomSeed();
}
// Helper to make playing sounds from the V5 in VEXcode easier and
// keeps the code cleaner by making it clear what is happening.
void playVexcodeSound(const char *soundName) {
printf("VEXPlaySound:%s\n", soundName);
wait(5, msec);
}
#pragma endregion VEXcode Generated Robot Configuration
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: {Abdullah Khaled} */
/* Created: {3/3/2025} */
/* Description: Code for elevator project for */
/* FISD PLTW Machine Control Project */
/*----------------------------------------------------------------------------*/
// Include the V5 Library
#include "vex.h"
// Allows for easier use of the VEX Library
using namespace vex;
// Calculations for elevator motor speed
double calculateElevatorSpeed(double kP, double curr, double setpoint) {
return (setpoint - curr) * kP;
}
// MathUtil functions
double max(double a, double max) {
if (a > max) {
return max;
} else {
return a;
}
}
double abs(double a) {
if (a > 0) {
return a;
} else {
return -a;
}
}
double getDistance() {
return elevatorDistance.objectDistance(mm);
}
double getClosestFloor() {
return (round((getDistance() - 22) / 55) + 1);
}
// Set the elevator speed in rpm based on the distance away from the setpoint
// With a max speed of 5000 rpm
int goToHeight(double floorDist) {
double kP = 3.0;
elevatorMotor.setVelocity(
max(
calculateElevatorSpeed(kP, getDistance(), floorDist),
5000
),
rpm
);
elevatorMotor.spin(reverse);
return 0;
}
int openDoor(int floor) {
floorOneDoor.setVelocity(50, percent);
floorTwoDoor.setVelocity(50, percent);
floorThreeDoor.setVelocity(50, percent);
if (floor == 1) {
floorOneDoor.spinToPosition(60, degrees);
return 1;
}
if (floor == 2) {
floorTwoDoor.spinToPosition(60, degrees);
return 2;
}
if (floor == 3) {
floorThreeDoor.spinToPosition(60, degrees);
return 3;
}
return 0;
}
int toggleLEDs(bool toggle) {
if (toggle) {
floorTwoLED.on();
floorThreeLED.on();
} else {
floorTwoLED.off();
floorThreeLED.off();
}
return 0;
}
// During maintenance mode, print to screen, open doors, and turn on LEDs
int maintenanceMode() {
// double initTime = Brain.Timer.time(seconds);
// double addTime = 2;
Brain.Screen.setCursor(5, 5);
Brain.Screen.print("MAINTENANCE MODE");
floorOneDoor.spinToPosition(60, degrees);
floorTwoDoor.spinToPosition(60, degrees);
floorThreeDoor.spinToPosition(60, degrees);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 5);
Brain.Screen.setFillColor(red);
Brain.Screen.setPenColor(black);
Brain.Screen.print("MAINTENANCE MODE");
Brain.Screen.setPenWidth(10);
while (floorOneLS.pressing() && floorTwoLS.pressing() && floorThreeLS.pressing()) {
toggleLEDs(true);
// Brain.Screen.newLine();
Brain.Screen.setPenColor(red);
Brain.Screen.setFillColor(red);
Brain.Screen.drawRectangle(1, 30, 1000, 1000);
wait(.5, seconds);
Brain.Screen.setPenColor(yellow);
Brain.Screen.setFillColor(yellow);
Brain.Screen.drawRectangle(1, 30, 1000, 1000);
wait(.5, seconds);
}
Brain.Screen.setPenColor(black);
Brain.Screen.setFillColor(black);
Brain.Screen.drawRectangle(1, 30, 1000, 1000);
return 0;
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// After 10 seconds (converted to ms) floor one downtime will kick in
Brain.Timer.clear();
int kUnusedTime = 10 * 1000;
bool maintenanceLocked = false;
// Store constants for floors and set the floor to floor one
double kFloorTolerance = 5.0;
double kFloorOneDist = 22.0;
double kFloorTwoDist = 75.0;
double kFloorThreeDist = 130.0;
double distanceSetpoint = kFloorOneDist;
double currentFloor = 1;
int floorReachedFloor = 0;
color printColor = orange;
// Initialize doors
floorOneDoor.setPosition(0, degrees);
floorTwoDoor.setPosition(0, degrees);
floorThreeDoor.setPosition(0, degrees);
// Initialize screen settings
Brain.Screen.setCursor(1, 1);
Brain.Screen.setFont(mono30);
Brain.Screen.setPenColor(white);
while (true) {
// If all three limit switches are pressed, toggle on maintenance mode
if (floorOneLS.pressing() && floorTwoLS.pressing() && floorThreeLS.pressing()) {
if (!maintenanceLocked) {
maintenanceLocked = true;
maintenanceMode();
}
} else {
Brain.Screen.setPenColor(black);
toggleLEDs(false);
maintenanceLocked = false;
Brain.Screen.clearScreen();
// Logic for floor selection
if (floorOneBumper.pressing() || floorOneLS.pressing()) {
Brain.Timer.clear();
currentFloor = 1;
distanceSetpoint = kFloorOneDist;
}
if (floorTwoBumper.pressing() || floorTwoLS.pressing()) {
Brain.Timer.clear();
currentFloor = 2;
distanceSetpoint = kFloorTwoDist;
}
if (floorThreeBumper.pressing() || floorThreeLS.pressing()) {
Brain.Timer.clear();
currentFloor = 3;
distanceSetpoint = kFloorThreeDist;
}
// Logic for floor one default
if (Brain.Timer.time(msec) >= kUnusedTime) {
currentFloor = 1;
distanceSetpoint = kFloorOneDist;
}
goToHeight(distanceSetpoint);
// Telemetry for tuning P gain
// Brain.Screen.print(distanceSetpoint);
// Brain.Screen.newLine();
// Brain.Screen.print(getDistance());
// Telemetry for displaying the current floor
Brain.Screen.setCursor(1, 10);
Brain.Screen.print("Floor: ");
Brain.Screen.setCursor(1, 17);
Brain.Screen.print(floorReachedFloor);
Brain.Screen.setPenColor(printColor);
Brain.Screen.setFillColor(printColor);
Brain.Screen.drawRectangle(30, 30, 1000, 1000);
// Logic for opening doors (must be within tolerances)
if (abs(getDistance() - distanceSetpoint) <= kFloorTolerance) {
floorReachedFloor = openDoor(currentFloor);
// floorReachedFloor = currentFloor;
Brain.Screen.newLine();
Brain.Screen.print("FLOOR REACHED");
printColor = green;
} else {
floorOneDoor.spinToPosition(0, degrees);
floorTwoDoor.spinToPosition(0, degrees);
floorThreeDoor.spinToPosition(0, degrees);
printColor = orange;
Brain.Screen.drawRectangle(30, 30, 1000, 1000);
}
}
}
}