-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrobot.cpp
More file actions
521 lines (485 loc) · 13.4 KB
/
Copy pathrobot.cpp
File metadata and controls
521 lines (485 loc) · 13.4 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include "WPILib.h"
#include <ctime>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sys/time.h>
#include <vector>
#include <cmath>
#define AUTO_TARGET 2.2 //TODO: get a real value for this
#define RAMP_RATE 0.05
#define LOG(X) logA << X; logB << X; logC << X
class Robot : public IterativeRobot
{
private:
RobotDrive drivetrain;
Joystick driveStick, lifterStick;
CANTalon frontLeft, frontRight,
rearLeft, rearRight;
DigitalInput winchTension;
CANTalon winch, gripper;
std::vector<DigitalInput*> winchLimits;
DigitalInput a,b,c,d,e,f,g;
bool IgnoreLimits, autoMode;
short stage;
bool getLimit(int num)
{
//invert everything other than top and bottom because the roboRIO reports open as 1
//top and bottom are not inverted becuase they they are NC (whereas the rest are NO)
return (num == 0 || num == 5) ? winchLimits[num]->Get() : !winchLimits[num]->Get();
}
bool updateWinch(int t)
{
static int target = -1;
static int lastLimit = -1;
float out = 0;
//iterate through all of the limits, and save the last one
for(int ii = 0;ii <= 5;ii++)
{
if(getLimit(ii)) lastLimit = ii;
}
//check if we have a new target
if(t > -1) target = t;
//clear target if -2 passed (for disable)
if(t == -2) target = -1;
//check for down
if(t == -3) target = -3;
//if the stick is not being used, and we have a target, turn on the motor
if(std::abs(lifterStick.GetY()) < 0.2 && target != -1)
{
if(target == -3)
{
if(winchTension.Get() && !getLimit(0)) //check for tension
out = 1; //go down
else
{
out = 0;
target = -1;
}
}
else if(!getLimit(target))
//if the target is above the lastLimit, go up (-1), else go down (1)
out = target < lastLimit ? 1 : -1;
else
{
out = 0;
target = -1; //clear the target
}
}
else
{
out = lifterStick.GetY();
target = -1; //clear the target
}
//check if we have hit an end stop
if(getLimit(0) || getLimit(5) && !IgnoreLimits)
{
if((getLimit(0) && (out < 0)) || (getLimit(5) && (out > 0)))
{
winch.Set(out);
std::cout << "Winch limit hit, limiting movement" << std::endl;
}
else
{
winch.Set(0);
std::cout << "Winch limit hit, stopping" << std::endl;
}
}
else
winch.Set(out);
return (target == -1);
}
void updateDashboard()
{
LiveWindow::GetInstance()->SetEnabled(false);
for(int ii = 0;ii <= 5;ii++)
{
SmartDashboard::PutBoolean("Winch " + std::to_string(ii), getLimit(ii));
}
SmartDashboard::PutBoolean("Winch Tension", winchTension.Get());
IgnoreLimits = SmartDashboard::GetBoolean("Ignore Limits", false);
SmartDashboard::PutBoolean("Ignore Limits", IgnoreLimits);
autoMode = SmartDashboard::GetBoolean("1519 Mode", false);
SmartDashboard::PutBoolean("1519 Mode", autoMode);
SmartDashboard::PutBoolean("Gripper inside", gripper.IsFwdLimitSwitchClosed());
SmartDashboard::PutBoolean("Gripper outside", gripper.IsRevLimitSwitchClosed());
}
void LogData()
{
static PowerDistributionPanel pdp; // preparing to read from the pdp
static DriverStation* ds = DriverStation::GetInstance();
static std::vector<CANTalon*> motors;
static std::ofstream logA, logB, logC;
timeval tm;
SmartDashboard::PutBoolean("log A", logA.is_open());
SmartDashboard::PutBoolean("log B", logB.is_open());
SmartDashboard::PutBoolean("log C", logC.is_open());
if (!logA.is_open() && !logB.is_open() && !logC.is_open())
{
std::fstream logNumFile;
int logNum;
logNumFile.open("/home/lvuser/logNum");
logNumFile >> logNum;
logNum++;
logNumFile.seekp(0);
logNumFile << logNum;
// writing to /home/lvuser/logs/[unixtime].log
logA.open("/media/sda1/logs/log" + std::to_string(logNum) + ".csv");
std::cerr << (logA.is_open() ? "Opened" : "Failed to open") << "log A." << std::endl;
logB.open("/media/sdb1/logs/log" + std::to_string(logNum) + ".csv");
std::cerr << (logB.is_open() ? "Opened" : "Failed to open") << "log B." << std::endl;
logC.open("/home/lvuser/logs/log" + std::to_string(logNum) + ".csv");
std::cerr << (logC.is_open() ? "Opened" : "Failed to open") << "log C." << std::endl;
LOG("Time\tpdpInput voltage\tpdpTemperature\tpdpTotal Current\t");
for (int ii = 0; ii < 16; ii++)
{
LOG("pdpChannel " << ii << " current\t");
}
LOG("FrontLeft Bus Voltage\tFrontLeft Output Current\tFrontLeft Output Voltage\tFrontLeft Temperature");
motors.push_back(&frontLeft);
LOG("\tFrontRight Bus Voltage\tFrontRight Output Current\tFrontRight Output Voltage\tFrontRight Temperature");
motors.push_back(&frontRight);
LOG("\tRearLeft Bus Voltage\tRearLeft Output Current\tRearLeft Output Voltage\tRearLeft Temperature");
motors.push_back(&rearLeft);
LOG("\tRearRight Bus Voltage\tRearRight Output Current\tRearRight Output Voltage\tRearRight Temperature");
motors.push_back(&rearRight);
LOG("\tWinch Bus Voltage\tWinch Output Current\tWinch Output Voltage\tWinch Temperature");
motors.push_back(&winch);
LOG("\tGripper Bus Voltage\tGripper Output Current\tGripper Output Voltage\tGripper Temperature");
motors.push_back(&gripper);
LOG("\tJoystick X\tJoystick Y\tJoystick Twist");
LOG("\tWinch Tension");
for(int ii = 0; ii <= 5; ii++)
{
LOG("\t Winch Limit " << ii);
}
LOG("\tAlliance\tLocation\tMatch Time\tFMS Attached\tBrowned Out");
LOG("\tTestStage");
LOG(std::endl);
}
gettimeofday(&tm, NULL);
LOG(time(0) << '.' << std::setfill('0') << std::setw(3) << tm.tv_usec/1000);
// Some general information
LOG("\t" << pdp.GetVoltage());
LOG("\t" << pdp.GetTemperature());
LOG("\t" << pdp.GetTotalCurrent());
// current on each channel
for (int ii = 0; ii < 16; ii++)
{
LOG("\t" << pdp.GetCurrent(ii));
}
//Talon Data
for(int ii = 0; ii < motors.size(); ii++)
{
LOG("\t" << motors[ii]->GetBusVoltage());
LOG("\t" << motors[ii]->GetOutputVoltage());
LOG("\t" << motors[ii]->GetOutputCurrent());
LOG("\t" << motors[ii]->GetTemperature());
}
//control data
LOG("\t" << driveStick.GetX());
LOG("\t" << driveStick.GetY());
LOG("\t" << driveStick.GetTwist());
//Winch Limits
LOG("\t" << winchTension.Get());
for(int ii = 0; ii <= 5; ii++)
{
LOG("\t" << getLimit(ii));
}
//DriverStation Data
LOG("\t" << ds->GetAlliance());
LOG("\t" << ds->GetLocation());
LOG("\t" << ds->GetMatchTime());
LOG("\t" << ds->IsFMSAttached());
LOG("\t" << ds->IsSysBrownedOut());
//Test stage
LOG("\t" << stage);
LOG(std::endl);
}
public:
Robot():
frontLeft(0), frontRight(1),
rearLeft(2), rearRight(3),
drivetrain(frontLeft, rearLeft,
frontRight, rearRight),
driveStick(0), lifterStick(1),
winchTension(0),
winch(4), gripper(5),
a(1), b(2), c(3), d(4), e(5), f(6), g(7)
{
winchLimits.push_back(&a);
winchLimits.push_back(&b);
winchLimits.push_back(&c);
winchLimits.push_back(&d);
winchLimits.push_back(&e);
winchLimits.push_back(&f);
winchLimits.push_back(&g);
}
void DisabledInit()
{
stage=0;
drivetrain.SetSafetyEnabled(false); //disable watchdog
//clear winch target
updateWinch(-2);
}
void DisabledPeriodic()
{
updateDashboard();
LogData();
}
void AutonomousPeriodic()
{
static Timer t;
static short autoStage = 0;
if(!autoMode)
{
switch(autoStage)
{
case 0:
//TODO: grip grippers
autoStage++;
break;
case 1:
autoStage += updateWinch(1);
break;
case 2:
if(!t.Get())
t.Start();
//check if we are at our destination distance
if(t.Get() < AUTO_TARGET)
{
drivetrain.MecanumDrive_Cartesian(0, -0.5, 0); //drive forwards
std::cout << "time: " << t.Get() << std::endl;
}
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
autoStage++;
}
break;
case 3:
autoStage += updateWinch(-3);
break;
case 4:
gripper.Set(-1);
break;
}
}
else
{
switch(autoStage)
{
case 0:
if(!t.Get()) t.Start();
if(t.Get() < 3) drivetrain.MecanumDrive_Cartesian(0, 0, 0);
else
{
autoStage++;
t.Stop();
t.Reset();
}
break;
case 1:
if(!t.Get()) t.Start();
if(t.Get() < 1.2) drivetrain.MecanumDrive_Cartesian(0, -0.5, 0);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
autoStage++;
}
break;
}
}
updateDashboard();
LogData();
}
void TestPeriodic()
{
static Timer t;
static short oldStage = -1;
int timePerAction = 5;
float testSpeed = 0.3;
if(oldStage != stage){
if(driveStick.GetRawButton(1)) oldStage = stage;
}
else
{
if(!t.Get()) t.Start();
switch(stage)
{
case 0:
//drive forwards for 5 seconds
SmartDashboard::PutString("Test Status", "driving forwards for 5 seconds");
if(t.Get() < timePerAction) drivetrain.MecanumDrive_Cartesian(0, -testSpeed, 0);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
t.Stop();
t.Reset();
stage++;
}
break;
case 1:
//drive backwards for 5 seconds
SmartDashboard::PutString("Test Status", "driving backwards for 5 seconds");
if(t.Get() < timePerAction) drivetrain.MecanumDrive_Cartesian(0, testSpeed, 0);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
t.Stop();
t.Reset();
stage++;
}
break;
case 2:
//drive left for 5 seconds
SmartDashboard::PutString("Test Status", "strafing left for 5 seconds");
if(t.Get() < timePerAction) drivetrain.MecanumDrive_Cartesian(-testSpeed, 0, 0);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
t.Stop();
t.Reset();
stage++;
}
break;
case 3:
//drive right for 5 seconds
SmartDashboard::PutString("Test Status", "strafing right for 5 seconds");
if(t.Get() < timePerAction) drivetrain.MecanumDrive_Cartesian(testSpeed, 0, 0);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
t.Stop();
t.Reset();
stage++;
}
break;
case 4:
SmartDashboard::PutString("Test Status", "rotating left (CC) for 5 seconds");
//rotate left (CC) for 5 seconds
if(t.Get() < timePerAction) drivetrain.MecanumDrive_Cartesian(0, 0, -testSpeed);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
t.Stop();
t.Reset();
stage++;
}
break;
case 5:
SmartDashboard::PutString("Test Status", "rotate right (C) for 5 seconds");
//rotate right (C) for 5 seconds
if(t.Get() < timePerAction) drivetrain.MecanumDrive_Cartesian(0, 0, testSpeed);
else
{
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
t.Stop();
t.Reset();
stage++;
}
break;
case 6:
SmartDashboard::PutString("Test Status", "winch level 0");
//winch down to level 0
stage += updateWinch(0);
break;
case 7:
SmartDashboard::PutString("Test Status", "winch level 1");
//winch up to level 1
stage += updateWinch(1);
break;
case 8:
SmartDashboard::PutString("Test Status", "winch level 2");
//winch up to level 2
stage += updateWinch(2);
break;
case 9:
SmartDashboard::PutString("Test Status", "winch level 3");
//winch up to level 3
stage += updateWinch(3);
break;
case 10:
SmartDashboard::PutString("Test Status", "winch level 4");
//winch up to level 4
stage += updateWinch(4);
break;
case 11:
SmartDashboard::PutString("Test Status", "winch level 5");
//winch up to level 5
stage += updateWinch(5);
break;
case 12:
SmartDashboard::PutString("Test Status", "winch tension");
//test winch set down
stage += updateWinch(-3);
break;
case 13:
SmartDashboard::PutString("Test Status", "winch level 0");
//winch down to level 0
stage += updateWinch(0);
break;
case 14:
SmartDashboard::PutString("Test Status", "gripper inner limit");
//grip to inner limit
//if(gripper.GetForwardLimitOK()) gripper.Set(testSpeed);
if(true);
else
{
gripper.Set(0);
stage++;
}
break;
case 15:
SmartDashboard::PutString("Test Status", "gripper outer limit");
//grip to outer limit
if(gripper.GetReverseLimitOK()) gripper.Set(-testSpeed);
else
{
gripper.Set(0);
stage++;
}
break;
default:
drivetrain.MecanumDrive_Cartesian(0, 0, 0);
winch.Set(0);
gripper.Set(0);
break;
}
}
updateDashboard();
LogData();
}
void TeleopInit()
{
printf("Starting Teleop mode");
drivetrain.SetExpiration(2); //set the timeout for the watchdog
drivetrain.SetSafetyEnabled(true); //enable watchdog
}
void TeleopPeriodic()
{
//Driving
static double scaled[3];
float throttleScale = ((1 - driveStick.GetThrottle()) / 2);
float gripperScale = ((1 - lifterStick.GetThrottle()) / 2);
scaled[0] = (driveStick.GetX() + -(driveStick.GetPOV() == 90) + (driveStick.GetPOV() == 270)) * throttleScale;
scaled[1] = driveStick.GetY() * throttleScale;
scaled[2] = driveStick.GetTwist() * throttleScale * (driveStick.GetRawButton(2) || driveStick.GetRawButton(3)) * (1 - (0.5 * !driveStick.GetRawButton(3)));
drivetrain.MecanumDrive_Cartesian(scaled[0], scaled[1], scaled[2]);
//Winching
int winchButton = -1;
for(int jj = 7;jj <= 12;jj++)
{
if(lifterStick.GetRawButton(jj)) winchButton = (jj-7);
}
if(lifterStick.GetRawButton(3)) winchButton = 2;
winchButton = lifterStick.GetRawButton(1) ? -3 : winchButton;
updateWinch(winchButton);
gripper.Set((-(lifterStick.GetPOV() == 90) + (lifterStick.GetPOV() == 270)) * gripperScale);
updateDashboard();
//Data logging
LogData();
}
};
START_ROBOT_CLASS(Robot);