-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathShooter.cpp
390 lines (352 loc) · 9.59 KB
/
Shooter.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
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
#include "Shooter.h"
#include "612.h"
#include "SmoothJoystick.h"
#include "main.h"
#include "ADXL345_I2C_612.h"
const double Shooter::SPEED_AXISPOWER_TELEOP = 0.80;
const double Shooter::SPEED_AXISPOWER_AUTO_SLOW = 0.40;
const double Shooter::SPEED_AXISPOWER_AUTO_VERY_SLOW = 0.25;
const double Shooter::SPEED_AXISPOWER_AUTO_FAST = 0.80;
const double Shooter::SPEED_ATTRACTOR = 1.0;
const double Shooter::SPEED_WORM = 1.0;
Shooter::Shooter(main_robot* r,uint8_t axisCan,
uint8_t attractMod, uint32_t attractChan,
uint8_t clampMod, uint32_t clampFChan, uint32_t clampRChan,
uint8_t wormCan,
uint8_t punchMod,uint32_t punchFChan,uint32_t punchRChan,
uint8_t bobMod)
:isPickingUp(false),isPitchingUp(false),
isPitchingDown(false),wormIsPulling(false),winching(false),
hasTilted(false),isPickingUpStopping(false),autoPulling(false),
smartFiring(false),accelWorking(true),smartFireTimer(new Timer())
{
robot = r;
axis = new CANJaguar(axisCan);
attractor = new Talon(attractMod, attractChan);
clamper = new DoubleSolenoid(clampMod, clampFChan, clampRChan);
wormGear = new CANJaguar(wormCan);
puncher = new DoubleSolenoid(punchMod,punchFChan,punchRChan);
bobTheAccelerometer = new ADXL345_I2C_612(bobMod);
// bobThePot = new AnalogChannel(1,5);
isPickingUp = false;
shooterJoy = robot -> gunnerJoy;
shooterJoy -> addJoyFunctions(&buttonHelper,(void*)this,CLAMP);
shooterJoy -> addJoyFunctions(&buttonHelper,(void*)this,ENERGIZE);
shooterJoy -> addJoyFunctions(&buttonHelper,(void*)this,FIRE);
shooterJoy -> addJoyFunctions(&buttonHelper,(void*)this,AUTO_HIGHGOAL);
shooterJoy -> addJoyFunctions(&buttonHelper,(void*)this,AUTO_VERTICAL);
shooterJoy -> addJoyFunctions(&setPickupHelper,(void*)this,PICKUP);
robot -> update -> addFunctions(&updateHelper, (void*)this);
smartFireTimer->Stop();
}
Shooter::~Shooter()
{
delete axis;
delete attractor;
delete clamper;
delete bobTheAccelerometer;
delete wormGear;
delete puncher;
}
double Shooter::getPitchSpeed()
{
if(isPitchingUp || isPitchingDown)
{
if(fabs(originPitch - destinationPitch) > AXIS_SPEED_FAR_THRESH)
{
return SPEED_AXISPOWER_AUTO_FAST;
}
else
{
if(fabs(currentPitch - destinationPitch) < AXIS_SPEED_CLOSE_THRESH)
{
return SPEED_AXISPOWER_AUTO_VERY_SLOW;
}
else
{
return SPEED_AXISPOWER_AUTO_SLOW;
}
}
}
else
{
return SPEED_AXISPOWER_TELEOP;
}
}
void Shooter::pitchUp()
{
axis->Set(-getPitchSpeed());
}
void Shooter::pitchDown()
{
axis->Set(getPitchSpeed());
}
void Shooter::pitchStop()
{
isPitchingUp = false;
isPitchingDown = false;
axis->Set(0);
}
void Shooter::pitchAngle(double newPitch)
{
if(!accelWorking)
{
return;
}
originPitch = currentPitch;
destinationPitch = newPitch;
isPickingUp = false;
isPitchingDown = false;
hasTilted = false;
if (newPitch < originPitch)
{
isPitchingDown = true;
}
if (newPitch > originPitch)
{
isPitchingUp = true;
}
}
void Shooter::rollerPull()
{
attractor->Set(SPEED_ATTRACTOR); // apparently its supposed to be negative to pull
}
void Shooter::rollerStop()
{
attractor->Set(0);
}
void Shooter::rollerRepel()
{
attractor->Set(-SPEED_ATTRACTOR);
}
//@param goClamp moves clamper, off says to stop clamper
void Shooter::autoClamp()
{
if(clamp == up)
clampDown();
else
clampUp();
}
void Shooter::clampDown()
{
robot -> pnum->setVectorValues(TIME, clamper, DoubleSolenoid::kForward);
clamp = down;
}
void Shooter::clampUp()
{
robot -> pnum->setVectorValues(TIME, clamper, DoubleSolenoid::kReverse);
clamp = up;
}
void Shooter::wormPull()
{
if(wormDone())
{
return;
}
if(!wormIsPulling)
{
robot -> pnum -> setVectorValues(PUNCH_TIME, puncher, DoubleSolenoid::kForward);
}
wormIsPulling = true;
}
void Shooter::wormStop()
{
wormGear -> Set(0);
wormIsPulling = false;
//currentSpeed = SPEED_WORM;
}
bool Shooter::wormDone()
{
if (shooterJoy->GetSmoothButton(BUTTON_Y))
{
std::printf("Worm Drive OFF\n");
return true;
}
else
{
bool off = !(wormGear -> GetForwardLimitOK());
if (off == true)
{
std::printf("Worm Drive OFF\n");
}
return off;
}
}
void Shooter::punch()
{
robot -> pnum -> setVectorValues(PUNCH_TIME, puncher, DoubleSolenoid::kReverse);
}
void Shooter::smartFire()
{
if(!smartFiring)
{
clampUp();
smartFiring = true;
smartFireTimer->Reset();
smartFireTimer->Start();
}
}
bool Shooter::doubleEqual(double a,double b)
{
return fabs(a-b) < FLOAT_THRESH;
}
void Shooter::buttonHelper(void* objPtr, uint32_t button)
{
Shooter* shooterObj=(Shooter*)objPtr;
if(button==CLAMP)
{
shooterObj->autoClamp();
}
if(button==ENERGIZE)
{
shooterObj->wormPull();
}
if(button==FIRE)
{
shooterObj->smartFire();
}
if(button==AUTO_HIGHGOAL)
{
shooterObj->pitchAngle(HIGHGOAL_POSITION);
}
if(button==AUTO_VERTICAL)
{
shooterObj->pitchAngle(VERTICAL_POSITION);
}
}
double Shooter::getAngle() {
double bobX = bobTheAccelerometer->GetAcceleration(ADXL345_I2C_612::kAxis_X);
double bobY = bobTheAccelerometer->GetAcceleration(ADXL345_I2C_612::kAxis_Y);
double bobZ = bobTheAccelerometer->GetAcceleration(ADXL345_I2C_612::kAxis_Z);
accelWorking = !(doubleEqual(bobX,0.0) && doubleEqual(bobY,0.0) && doubleEqual(bobZ,0.0));
if(!accelWorking)
{
isPitchingUp = false;
isPitchingDown = false;
}
double newPitch = (atan2(bobX, sqrt(bobY*bobY + bobZ*bobZ))*180.0)/PI;
// if(fabs(newPitch-currentPitch) < 10)
currentPitch = newPitch;
/* double volts = bobThePot->GetVoltage();
currentPitch = bobThePot; // TODO get pot conversion */
return currentPitch;
}
/*
*
* The following code is a shortcut of sorts for Ben so that he can press one button to do a bunch of stuff
*
*/
void Shooter::setPickup() {
clampDown();
pitchAngle(PICKUP_POSITION);
}
void Shooter::setPickupHelper(void* instName, uint32_t button) {
Shooter* shooterObj = (Shooter*)instName;
shooterObj -> setPickup();
}
// TODO IMPORTANT: What if we are pitching down to pickup angle, but we never reach
// there because we hit the frame? Somehow we need to detect that
// and set isPitchingDown to false if it hard stops
void Shooter::update()
{
getAngle();
static int output = 0;
if(output%20 == 0)
{
printf("Tilt Angle: %f\n",currentPitch);
}
output++;
// angle presets, triggers
if(shooterJoy -> GetTriggerState() == AUTO_LOWGOAL && !doubleEqual(destinationPitch, LOWGOAL_POSITION))
{
pitchAngle(LOWGOAL_POSITION);
}
else if(shooterJoy -> GetTriggerState() == AUTO_PICKUP && !doubleEqual(destinationPitch, PICKUP_POSITION))
{
pitchAngle(PICKUP_POSITION);
}
// manual controls
if(!isPitchingUp && !isPitchingDown)
{
if(shooterJoy -> IsAxisZero(TILT))
pitchStop();
else if(shooterJoy -> GetRawAxis(TILT) < 0) // push up = negative values = tilt down
pitchDown();
else
pitchUp();
}
if(shooterJoy -> IsAxisZero(ROLLERS))
rollerStop();
else if(shooterJoy -> GetRawAxis(ROLLERS) < 0) // push down = pull, push up = repel
rollerPull();
else
rollerRepel();
if (isPitchingUp)
{
pitchUp();
if (currentPitch >= destinationPitch || !(axis->GetReverseLimitOK()))
{
printf("angle tilting STOP at %f\n",currentPitch);
pitchStop();
isPitchingUp = false;
}
}
if (isPitchingDown)
{
pitchDown();
if (currentPitch <= destinationPitch || !(axis->GetForwardLimitOK()))
{
printf("angle tilting STOP at %f\n",currentPitch);
pitchStop();
isPitchingDown = false;
}
}
if (!isPitchingDown && !isPitchingUp)
hasTilted = true;
/*if(shooterJoy -> GetSmoothButton(PICKUP)) // holding down button
{
if (!isPickingUp) // hasn't started sequence yet
{
isPickingUp = true;
pitchAngle(PICKUP_POSITION);
clampUp();
}
if(!isPitchingDown) // tilt at pickup position
{
clampDown();
rollerPull();
}
}
else // let go of button
{
if(isPickingUp) // was picking up, so now should stop picking up
{
pitchAngle(SHOOTING_POSITION);
isPickingUp = false;
isPickingUpStopping = true;
}
if (!isPitchingUp && isPickingUpStopping) // tilt at shooting position
{
clampUp();
rollerStop();
isPickingUpStopping = false;
}
}*/
if(smartFiring && smartFireTimer->HasPeriodPassed(SMARTFIRE_TIME))
{
punch();
smartFiring = false;
smartFireTimer->Stop();
}
if(wormIsPulling)
{
wormGear->Set(SPEED_WORM);
if (wormDone()) //checks if loader has reached farthest position
wormStop();
}
}
void Shooter::updateHelper(void* instName)
{
Shooter* shooterObj = (Shooter*)instName;
shooterObj -> update();
}