forked from jacob9706/HighTekerz2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot2012.cpp
320 lines (256 loc) · 7.93 KB
/
Robot2012.cpp
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
#include "WPILib.h"
#include "math.h"
#include "SubSystems/ElevatorSystem.h"
#include "Util/Switch.h"
#include "Util/XboxController.h"
/**
* This is a demo program showing the use of the RobotBase class.
* The SimpleRobot class is the base of a robot application that will automatically call your
* Autonomous and OperatorControl methods at the right time as controlled by the switches on
* the driver station or the field controls.
*/
class Robot2012 : public SimpleRobot
{
// Drive System /////////////////////
RobotDrive *myRobot;
// Outputs //////////////////////////
// Motors
Victor* bBallRotator;
Victor* bBallPitchMotor;
Victor* bBallShooterTop;
Victor* bBallShooterBottom;
Relay* bBallElevatorBottom;
Relay* bBallElevatorTop;
Relay* bBallCollector;
// Air
Compressor* airCompressor;
Solenoid* shooterArm;
Solenoid* rampArm;
// Inputs //////////////////////////
// On robot //
// Encoders
Encoder* encoderWheelsLeft;
Encoder* encoderWheelsRight;
Encoder* encoderTurretRotation;
Encoder* bBallShooterTopEncoder;
Encoder* bBallShooterBottomEncoder;
Encoder* bBallAngle;
// Switches
// DigitalInput *bBallCollectorSensor;
// DigitalInput *bBallElevatorSensor;
DigitalInput *bBallElevatorTopLimit;
DigitalInput *bBallElevatorBottomLimit;
// Other
ADXL345_I2C *accel;
Gyro *gyro;
// Driver Station //
DriverStation *driverStationControl; // driver station object
DriverStationLCD* dsLCD;
XboxController *xboxDrive;
XboxController *xboxShoot;
// Systems and Support ///////////////////
ElevatorSystem* robotElevator;
Switch shooterSwitch;
Switch switchAimTrigger;
bool isShootStick;
bool shooterState;
public:
/**
* Create an instance of a RobotDrive with left and right motors plugged into PWM
*/
Robot2012(void)
{
// Drive System /////////////////////
myRobot = new RobotDrive(3, 4, 1, 2); // create robot drive base
myRobot->SetExpiration(0.1);
// Outputs //////////////////////////
// Motors
bBallPitchMotor = new Victor(2,3);
bBallRotator = new Victor(2,4);
bBallShooterTop = new Victor(2,1);
bBallShooterBottom = new Victor(2,2);
bBallElevatorBottom = new Relay(2,1,Relay::kBothDirections);
bBallElevatorTop = new Relay(2,2,Relay::kBothDirections);
bBallCollector = new Relay(2,4,Relay::kBothDirections);
// Air
airCompressor = new Compressor(1,10,2,3);
airCompressor->Start();
shooterArm = new Solenoid(1);
// Inputs //////////////////////////
// On robot //
// Encoders
encoderWheelsLeft = new Encoder(1,1,1,2,false);
encoderWheelsRight = new Encoder(1,3,1,4,true);
encoderTurretRotation = new Encoder(2,3,2,4);
bBallAngle = new Encoder(2,11,2,12);
bBallShooterTopEncoder = new Encoder(2,5,2,6);
bBallShooterBottomEncoder = new Encoder(2,7,2,8);
encoderWheelsLeft->Start();
encoderWheelsRight->Start();
encoderTurretRotation->Start();
bBallAngle->Start();
bBallShooterTopEncoder->Start();
bBallShooterBottomEncoder->Start();
// Switches
// bBallCollectorSensor = new DigitalInput(2,3);
// bBallElevatorSensor = new DigitalInput(2,4);
// bBallBrushSensor = new DigitalInput(2,5);
bBallElevatorTopLimit = new DigitalInput(2,2);
bBallElevatorBottomLimit = new DigitalInput(2,1);
// Other
// accel = new ADXL345_I2C(1);
// gyro = new Gyro(1);
// Driver Station //
driverStationControl = DriverStation::GetInstance();
dsLCD = DriverStationLCD::GetInstance();
xboxDrive = new XboxController(1);
xboxShoot = new XboxController(2);
// Systems and Support ///////////////////
robotElevator = new ElevatorSystem(bBallElevatorBottom, bBallElevatorTop, bBallElevatorBottomLimit, bBallElevatorTopLimit);
shooterSwitch = Switch();
switchAimTrigger = Switch();
shooterState = false;
isShootStick = driverStationControl->GetDigitalIn(1);
// init
//random
dsLCD->Printf(DriverStationLCD::kUser_Line6, 1, "Hello World");
dsLCD->UpdateLCD();
}
/**
* Drive left & right motors for 2 seconds, enabled by a jumper (jumper
* must be in for autonomous to operate).
*/
void Autonomous(void)
{
}
/**
* Runs the motors under driver control with either tank or arcade steering selected
* by a jumper in DS Digin 0. Also an arm will operate based on a joystick Y-axis.
*/
void OperatorControl(void)
{
printf("hi");
while (IsOperatorControl())
{
if (isShootStick != driverStationControl->GetDigitalIn(1))
{
}
// Drive //////////////////////////////
myRobot->TankDrive(-xboxDrive->GetLeftY(), -xboxDrive->GetRightY()); // drive with tank style
// Aim ////////////////////////////////
bBallRotator->Set(-xboxShoot->GetRightX());
bBallPitchMotor->Set((xboxShoot->GetLeftY()/2.1));
// bBallShooterTop->Set(driverStationControl->GetAnalogIn(1)*-1);
// bBallShooterBottom->Set(driverStationControl->GetAnalogIn(1)*-1);
// initial plan ///////////////////////////////////////////////
// Driver
// -rb rt suck balls
// -lb lt spit balls
//
// Shooter
// -lt rt rotation
// -y activate elevator
// -a shoot
// Collect and Shoot bBalls///////////
if(xboxShoot->GetRB() || xboxShoot->GetRightTrigger() < -.1)
{
shooterState = true;
}
if(xboxShoot->GetLB() || xboxShoot->GetLeftTrigger() > .1)
{
shooterState = false;
}
if (shooterState)
{
bBallShooterTop->Set(driverStationControl->GetAnalogIn(1)*-1);
bBallShooterBottom->Set(driverStationControl->GetAnalogIn(2)*-1);
}
else
{
bBallShooterTop->Set(0.0);
bBallShooterBottom->Set(0.0);
}
shooterArm->Set(xboxShoot->GetA());
// collector ///////////////////////////////
if (xboxDrive->GetLB() || xboxDrive->GetLeftTrigger() > .1)
{
bBallCollector->Set(Relay::kOn);
bBallCollector->Set(Relay::kReverse);
}
else if (xboxDrive->GetRB() || xboxDrive->GetRightTrigger() < -.1)
{
bBallCollector->Set(Relay::kOn);
bBallCollector->Set(Relay::kForward);
}
else
{
bBallCollector->Set(Relay::kOff);
}
// ELEVATORS //////////////////////////////
// REVERSE IS UP ///////////
// bottom down
if (xboxShoot->GetSelect())
{
bBallElevatorBottom->Set(Relay::kOn);
bBallElevatorBottom->Set(Relay::kForward);
}
// bottom up
else if (xboxShoot->GetStart())
{
bBallElevatorBottom->Set(Relay::kOn);
bBallElevatorBottom->Set(Relay::kReverse);
}
else
{
bBallElevatorBottom->Set(Relay::kOff);
}
// top down
if (xboxShoot->GetX())
{
bBallElevatorTop->Set(Relay::kOn);
bBallElevatorTop->Set(Relay::kForward);
}
// top up
else if (xboxShoot->GetB())
{
bBallElevatorTop->Set(Relay::kOn);
bBallElevatorTop->Set(Relay::kReverse);
}
else
{
bBallElevatorTop->Set(Relay::kOff);
}
// Air compressor
// robotElevator->PeriodicSystem((switchAimTrigger.State(aimStick->GetTrigger())));
robotElevator->PeriodicSystem(xboxShoot->GetY());
// random output stuff!! ////////////
/* printf("lft: %d ", encoderWheelsLeft->Get());
printf("rt: %d ", encoderWheelsRight->Get());
printf("turret: %d ", encoderTurretRotation->Get());
printf("top enc: %d ", bBallShooterTopEncoder->Get());
printf("bottom enc: %d ", bBallShooterBottomEncoder->Get());
printf("tilt: %d ", bBallAngle->Get());
printf("stick y: %d /r", xboxDrive->GetLeftY());
*/
Wait(0.01);
}
}
float degrees(float rads)
{
//180/pi = 57.296
return rads*57.296;
}
void CalculateIdealAngleAndSpeed(double distance, double height, double* computedAngle, double* computedSpeed)
{
*computedAngle = 0.0;
*computedSpeed = 0.0;
// =45+((DEGREES(ATAN(height/distance)))/2)
*computedAngle = 45.0+((degrees(atan(height/distance)))/2.0);
printf("a: %f/n",*computedAngle);
//32.2 ft/s/s
// =SQRT(32.2*(height+(SQRT(height^2+distance^2))))
*computedSpeed = sqrt(32.2*(height+(sqrt(pow(height,2.0)+pow(distance,2.0)))));
printf("S: %f/n",*computedSpeed);
}
};
START_ROBOT_CLASS(Robot2012);