-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.cpp
More file actions
349 lines (317 loc) · 8.9 KB
/
robot.cpp
File metadata and controls
349 lines (317 loc) · 8.9 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
#include <WPILib.h>
#include "robot.h"
#include "portnums.h"
#include "arduinoI2C.h"
robot::robot(void) : DS(DriverStation::GetInstance()),DSEIO(DS->GetEnhancedIO())
{
I2C = new ArduinoI2C();
joystick = new Joystick(joystickPort);
theChassis = new chassis(leftFrontTalonPort, leftRearTalonPort, rightFrontTalonPort, rightRearTalonPort, leftUltrasonicPort, rightUltrasonicPort);
theCatapult = new catapult(catapultTalonOnePort, catapultTalonTwoPort, catapultEncoderPortA, catapultEncoderPortB, catapultLimitSwitchPort);
theCollector = new collector(liftingTalonPort, rollerTalonPort, I2C, upperLimitSensorPort, lowerLimitSensorPort);
AutonomousTimer = new Timer();
UnfoldingTimer = new Timer();
AutonomousServo = new Servo(autonomousServoPort);
}
void robot::RobotInit()
{
theCatapult->SetMotorPower(1.0);
// insight_encoder_count.setHeader("Count:");
// insight.registerData(insight_encoder_count, 1);
// insight.startDisplay();
SmartDashboard::init();
}
void robot::DisabledInit()
{
theCatapult->ReInit();
AutonomousTimer->Stop();
}
void robot::AutonomousInit()
{
SmartDashboard::PutNumber("Potential Targets:", -666); //so if the pandaboard doesn't send data it's obvious
// SmartDashboard::PutNumber("Target Range:", 0);
auto_fired = false;
auto_shots = 0;
AutonomousTimer->Start();
AutonomousTimer->Reset();
unfoldingState = collectorLower;
//movementState = waiting;
autonomousState = waiting;
unfolding_done = false;
collector_started = false;
//old_error = 999; //old_error needs a value because it is compared to the current error, but we won't have an actual old_error value until the second loop
correct_range_loops = 0;
error = 0;
cummulative_error = 0;
theCatapult->ResetEncoder();
theCatapult->SetMotorPower(1.0);
theCollector->SetAutomaticRollerPower(0.65);
theCollector->Run();
}
void robot::TeleopInit()
{
AutonomousTimer->Stop();
theCollector->SetAutomaticRollerPower(1.0);
theCatapult->SetMotorPower(1.0);
}
void robot::TestInit()
{
}
void robot::DisabledPeriodic()
{
// float leftVoltage = theUltrasonics->GetLeftUltrasonicVoltage();
// float rightVoltage = theUltrasonics->GetRightUltrasonicVoltage();
// insight_ballDistance.setData(0);
// theUltrasonics->Idle();
// SmartDashboard::PutNumber("leftVoltage", leftVoltage);
// SmartDashboard::PutNumber("rightVoltage", rightVoltage);
int left = 0, right = 0, ball = 0;
static int loops = 0;
loops++;
if(loops % 250)
{
I2C->GetValues(left, right,ball);
}
I2C->Idle();
theCatapult->SetStoppingPoint(140);
theCollector->DisableProtectedMode();
// insight_encoder_count.setData(theCatapult->GetEncoderCount());
}
void robot::AutonomousPeriodic() {
I2C->Idle(); //updates the data acquired from I2C
int num_targets = static_cast<int>(SmartDashboard::GetNumber("Potential Targets:")); //gets how many targets we can see from pandaboard -666 = no data sent, 0 = no targest, 1 = cold, 2 = hot
// float target_range = SmartDashboard::GetNumber("Target Range:");
// float optimal_range = 120;
// float range_tolerance = 30; //inches
if(DS->GetDigitalIn(2)){
switch(unfoldingState){
case collectorLower:
if(AutonomousTimer->Get() > .87){
theCollector->Disable();
theCollector->ManualRoller(0.169);
}
if(AutonomousTimer->Get() < 1.5){ //waiting for collector to lower
initial_num_targets = num_targets;
theCatapult->SetMotorPower(0.2);
theCatapult->SetStoppingPoint(250);
theCatapult->Fire();
}
else{
theCatapult->ResetLoweringTimer();
theCatapult->ReInit();
unfoldingState = catapultLower;
}
break;
case catapultLower:
theCatapult->AutonomousLower();
unfoldingState = waitingForCatapult;
break;
case waitingForCatapult:
if(theCatapult->GetState() == 4){
unfoldingState = done;
}
break;
case done:
unfolding_done = true;
break;
}
switch(autonomousState){
case waiting:
if(AutonomousTimer->Get() < 1){
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
}
else{
autonomousState = running;
}
break;
case running:
if(AutonomousTimer->Get() > 6 && auto_shots == 0 && unfolding_done){ //Well it doesn't matter if the goal is hot, if we haven't fired yet, we need to fire
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
theCatapult->SetMotorPower(1.0);
theCatapult->SetStoppingPoint(135); //old 140
theCatapult->Fire();
auto_shots++;
}
else if(AutonomousTimer->Get() > 6.25 && !collector_started){
theCollector->Run();
collector_started = true;
}
else if(AutonomousTimer->Get() > 9.25 && auto_shots == 1 && unfolding_done){
theCollector->Disable();
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
theCatapult->SetMotorPower(1.0);
theCatapult->SetStoppingPoint(140); //old 135
theCatapult->Fire();
auto_shots++;
}
else if(I2C->GetRight() > 80){ //Moving to shooting range
error = I2C->GetRight() - 80;
cummulative_error += error;
float x = 0;
float y = -(((error)*(0.006))+(cummulative_error*0.00005)); //PI control
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
}
else{
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
}
break;
case stopped: //For if something goes horribly wrong
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
break;
}
theChassis->Idle();
theCatapult->Idle();
theCollector->Idle();
}
else{
switch(unfoldingState){
case collectorLower:
if(AutonomousTimer->Get() < 1.5){ //waiting for collector to lower
initial_num_targets = num_targets;
}
else{
theCatapult->ResetLoweringTimer();
unfoldingState = catapultLower;
}
break;
case catapultLower:
theCatapult->AutonomousLower();
unfoldingState = waitingForCatapult;
break;
case waitingForCatapult:
if(theCatapult->GetState() == 4){
unfoldingState = done;
}
break;
case done:
if(AutonomousTimer->Get() < 3){ //giving collector time to pick up ball
}
else{
theCollector->Disable();
unfolding_done = true;
}
break;
}
switch(autonomousState){
case waiting:
if(AutonomousTimer->Get() < 1){
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
}
else{
autonomousState = running;
}
break;
case running:
if(AutonomousTimer->Get() > 7 && !auto_fired && unfolding_done){ //Well it doesn't matter if the goal is hot, if we haven't fired yet, we need to fire
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
theCatapult->SetMotorPower(1.0);
theCatapult->SetStoppingPoint(135); //old 140
theCatapult->Fire();
auto_fired = true;
}
else if(I2C->GetRight() > 80){ //Moving to shooting range
error = I2C->GetRight() - 80;
cummulative_error += error;
float x = 0;
float y = -(((error)*(0.006))+(cummulative_error*0.00005)); //PI control
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
}
//else if(!auto_fired && initial_num_targets > 1 && unfolding_done){ //Have we not fired and is the goal hot? If so, fire!
// float x = 0;
// float y = 0;
// float twist = 0;
// theChassis->SetJoystickData(x, y, twist);
// theCatapult->SetMotorPower(1.0);
// theCatapult->SetStoppingPoint(140); //old 135
// theCatapult->Fire();
// auto_fired = true;
//}
else{
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
}
break;
case stopped: //For if something goes horribly wrong
float x = 0;
float y = 0;
float twist = 0;
theChassis->SetJoystickData(x, y, twist);
break;
}
}
theChassis->Idle();
theCatapult->Idle();
theCollector->Idle();
}
void robot::TeleopPeriodic() {
I2C->Idle();
float x = joystick->GetRawAxis(1);
float y = joystick->GetRawAxis(2);
float twist = joystick->GetRawAxis(3);
theChassis->SetJoystickData(x, y, twist);
if(joystick->GetRawButton(1)){
theCatapult->Fire();
}
if(joystick->GetRawButton(2)){
theCollector->Run();
}
if(joystick->GetRawButton(3)){
theCollector->Disable();
}
if(joystick->GetRawButton(4)){
theCatapult->ResetEncoder();
}
if(joystick->GetRawButton(5)){
theCatapult->SetStoppingPoint(80);
}
if(joystick->GetRawButton(6)){
theCatapult->SetStoppingPoint(135);
}
if(joystick->GetRawButton(7)){
theCollector->EnableProtectedMode();
}
if(joystick->GetRawButton(8)){
theCollector->DisableProtectedMode();
}
if(joystick->GetRawButton(9)){
theCatapult->SetStoppingPoint(100);
}
theCollector->ManualRaise(static_cast<int>(joystick->GetRawAxis(6)));
theCollector->ManualRoller(static_cast<int>(joystick->GetRawAxis(5)));
theCatapult->Idle();
theChassis->Idle();
theCollector->Idle();
}
void robot::TestPeriodic(){
theCollector->AssistedManualRaise();
theCatapult->ReInit();
theChassis->SetJoystickData(0, 0, 0);
theCatapult->Idle();
theChassis->Idle();
}
START_ROBOT_CLASS(robot);