-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
187 lines (148 loc) · 5.88 KB
/
main.cpp
File metadata and controls
187 lines (148 loc) · 5.88 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
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: C:\Users\127-alee */
/* Created: Fri Nov 18 2022 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;
vex::controller master(controllerType::primary);
// define your global instances of motors and other devices here
//wheels
//front == flywheel side (true means going backwards)
vex::motor backleft(PORT1);
vex::motor backright(PORT2, true);
vex::motor frontleft(PORT9);
vex::motor frontright(PORT10, true);
//flywheel, 1 is closer to the front
vex::motor flywheel1(PORT6, gearSetting::ratio6_1);
vex::motor flywheel2(PORT8, gearSetting::ratio6_1, true);
//other
vex::motor intake(PORT13, true);
vex::motor indexer(PORT19, true);
vex::motor test(PORT12, true);
double wheelWidth = 4.125;
double wheelCirc = wheelWidth * 3.14;
void pdrivedistance(double distance, double kP) {
double revolutions = distance / wheelCirc;
bool done = false;
while(!done) {
Brain.Screen.printAt( 10, 50, "Hello V5 %f", (float)frontleft.rotation(rotationUnits::rev));
double error = revolutions - frontleft.rotation(rotationUnits::rev);
double power = error * kP;
frontleft.spin(directionType::fwd, power, velocityUnits::pct);
frontright.spin(directionType::fwd, power, velocityUnits::pct);
backleft.spin(directionType::fwd, power, velocityUnits::pct);
backright.spin(directionType::fwd, power, velocityUnits::pct);
}
}
void drivedistance(double distance, double velocity) { //inches
double revolutions = distance / wheelCirc;
frontleft.rotateFor(directionType::fwd, revolutions, rotationUnits::rev, velocity, velocityUnits::pct, false);
backleft.rotateFor(directionType::fwd, revolutions, rotationUnits::rev, velocity, velocityUnits::pct, false);
frontright.rotateFor(directionType::fwd, revolutions, rotationUnits::rev, velocity, velocityUnits::pct, false);
backright.rotateFor(directionType::fwd, revolutions, rotationUnits::rev, velocity, velocityUnits::pct, true);
}
int indexercontroller() {
while(1){
//Indexer
if (master.ButtonL2.pressing()){
indexer.spinFor(120, rotationUnits::deg, 100, velocityUnits::pct);
indexer.spinFor(-130, rotationUnits::deg, 50, velocityUnits::pct);
}
else {
indexer.stop();
}
//Reset indexer position (accomodating for the shitty spin mech)
if (master.ButtonA.pressing()){
indexer.spinFor(-2, rotationUnits::deg, 100, velocityUnits::pct);
}
else {
indexer.stop();
}
this_thread::sleep_for(10);
}
}
int sgn(float num) {
if(num >= 0) {
return 1;
}
return 0;
}
bool flywheel_on = false;
void toggle_fw() {
flywheel_on = !flywheel_on;
}
int main() {
//int count = 0;
//double flywheelRatio = 60/8;
task test = task(indexercontroller);
float previous_error = 0;
float gain = .002;
float tbh = 0;
float target = 480;
float output = 0;
//master.ButtonL1.pressed(toggle_fw);
while(1) {
Brain.Screen.printAt( 10, 50, "%f", flywheel1.velocity(velocityUnits::rpm)); //%f//, (float)frontleft.rotation(rotationUnits::rev));
// Allow other tasks to run
//Wheels and movement
backright.spin(directionType::fwd, master.Axis3.value() + master.Axis1.value() * .7, velocityUnits::pct);
backleft.spin(directionType::fwd, master.Axis3.value() - master.Axis1.value() * .7, velocityUnits::pct);
frontright.spin(directionType::fwd, master.Axis3.value() + master.Axis1.value() * .7, velocityUnits::pct);
frontleft.spin(directionType::fwd, master.Axis3.value() - master.Axis1.value() * .7, velocityUnits::pct);
//Intake
if (master.ButtonR1.pressing()){
intake.spin(directionType::fwd, 100, velocityUnits::pct);
}
else if (master.ButtonR2.pressing()){
intake.spin(directionType::rev, 100, velocityUnits::pct);
}
else {
intake.stop();
}
//Flywheel
if(master.ButtonL1.pressing()) {
flywheel1.spin(fwd, 100, velocityUnits::pct);
flywheel2.spin(fwd, 100, velocityUnits::pct);
}
else {
flywheel1.stop();
flywheel2.stop();
}
if(master.ButtonA.pressing()) {
target += 5;
master.Screen.print("%f", target);
}
if(master.ButtonB.pressing()) {
target -= 5;
master.Screen.print("%f", target);
}
//Brain.Screen.printAt( 10, 50, "Hello V5 %f", flywheel1.velocity(velocityUnits::rpm) * flywheelRatio );
//Brain.Screen.printAt( 10, 80, "hi angel");
//master.Screen.print("%f", flywheel1.velocity(velocityUnits::rpm));
Brain.Screen.printAt( 10, 80, "play omori today");
Brain.Screen.printAt( 10, 130, "add me on osu");
//if(flywheel_on) {
//tbh
// float error = target - flywheel1.velocity(velocityUnits::rpm);
// output += gain * error;
// if(sgn(error) != sgn(previous_error)) {
// output = .5 * (output + tbh);
// tbh = output;
// previous_error = error;
// }
// flywheel1.spin(directionType::fwd, output, voltageUnits::volt);
// flywheel2.spin(directionType::fwd, output, voltageUnits::volt);
//}
//else{
// flywheel1.stop();
// flywheel2.stop();
//}
this_thread::sleep_for(10);
}
}