-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.cpp
283 lines (199 loc) · 7.16 KB
/
Robot.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
#include <time.h>
#include <GripPipeline.hpp>
#include "Robot.hpp"
#include "utils.hpp"
#include "GripPipeline.hpp"
Robot::Robot():
myRobot(0, 4, 1, 3),// drive train
xBox(0), climber(1),// xbox360 controller
winch(2), // climbing motor
sonar(1, 0), // ultrasonic range finder (not on robot)
gyro()
{
myRobot.SetExpiration(0.1);
}
void Robot::RobotInit() {
std::cout <<"Robot On" <<std::endl;
// autonomous chooser code
chooser.AddDefault(autoDriveForward, autoDriveForward); // drive past baseline
chooser.AddObject(autoGoMiddle, autoGoMiddle); // middle peg
chooser.AddObject(autoGoDist, autoGoDist);
chooser.AddObject(autoDoNothing, autoDoNothing); // don't do anything7
chooser.AddObject(autoLeftTurnRight, autoLeftTurnRight);
chooser.AddObject(autoRightTurnLeft, autoRightTurnLeft);
chooser.AddObject(autoVisionTest, autoVisionTest);
frc::SmartDashboard::PutData("Auto Modes", &chooser);
//get USB camera feed and post it to the Java SmartDashboard
cs::UsbCamera winchCam = CameraServer::GetInstance()->StartAutomaticCapture(0);
winchCam.SetResolution(320, 240);
// enable the ultrasonic sensor
sonar.SetAutomaticMode(true);
}
void Robot::AutonomousInit() {
// enable the motor controllers
myRobot.SetSafetyEnabled(false);
autoSelected = chooser.GetSelected();
// autoSelected = autoVisionTest;
// std::string autoSelected = SmartDashboard::GetString("Auto Selector", autoNameDefault);
std::cout << "Auto selected: " << autoSelected << std::endl;
if (autoSelected == autoDoNothing) {
// NOP
} else if (autoSelected == autoVisionTest) {
// set up the vision camera
cs::AxisCamera visionCam = CameraServer::GetInstance()->AddAxisCamera("69.254.10.224");
visionCam.SetResolution(640, 480);
visionCam.SetExposureAuto(); // will need to make manual...
// camera handle
cs::CvSink cvSink = CameraServer::GetInstance()->GetVideo(visionCam);
// take a picture
cv::Mat img;
if (cvSink.GrabFrame(img) == 0) {
std::cerr <<"Error: Vision code failed to get video from camera :/\n";
return;
}
// process the picture
grip::GripPipeline camPipe;
camPipe.Process(img);
// display evidence of functionality
std::cout <<"find contours output: " <<std::endl;
std::vector<std::vector<cv::Point> >& filteredContours = *camPipe.GetFilterContoursOutput();
for (std::vector<cv::Point> contour : filteredContours) {
for (cv::Point coord : contour) {
std::cout <<'(' <<coord.x <<", " <<coord.y <<") ";
}
std::cout <<std::endl;
}
std::cout <<"Total - " <<filteredContours.size() <<"contours\n";
// find center of contour
struct avg_t { // useful for taking averages of positive numbers
unsigned long long total; // sum of all values
unsigned count; // number to divide total by
} avgx, avgy;
for (size_t i = 0; i < filteredContours.size(); i++) {
avgx = avgy = { 0, 0 };
for (cv::Point coord : filteredContours[i]) {
avgx.total += coord.x;
avgx.count++;
avgy.total += coord.y;
avgy.count++;
}
std::cout <<"Center of contour#" <<i <<" is: ("
<<(float) avgx.total / avgx.count <<", "
<<(float) avgy.total / avgy.count <<")\n";
}
// go to middle peg and deposit the gear
} else if (autoSelected == autoGoMiddle) {
// drive straight 3.3 seconds @ 30% power
utils::driveStraight(gyro, myRobot, 3.3, 0.3);
myRobot.Drive(0.0, 0.0);
// at this point the human player would lift the gear
// out of the robots pocket and earn us a fuckton of
// points to start the game on a good footing
} else if (autoSelected == autoLeftTurnRight) {
// reset gyro to zero
gyro.Reset();
// drive forward 200in to the turning point
while (sonar.GetRangeInches() < 200)
myRobot.Drive(0.5, 0.0);
// stop moving
myRobot.Drive(0.0, 0.0);
// turn 315 degrees
double gyroAngle = gyro.GetAngle() + 315;
while (gyroAngle > gyro.GetAngle())
myRobot.Drive(0.0, 0.3);
// drive forward 6ft to the left peg
while (sonar.GetRangeInches() < 72)
myRobot.Drive(0.5, 0.0);
// stop moving
myRobot.Drive(0.0, 0.0);
// at this point the human player would lift the gear
// out of the robots pocket and earn us a fuckton of
// points to start the game on a good footing
} else if (autoSelected == autoLeftTurnRight) {
// reset gyro to zero
gyro.Reset();
// drive forward 200in to the turning point
while (sonar.GetRangeInches() < 200)
myRobot.Drive(0.5, 0.0);
// stop moving
myRobot.Drive(0.0, 0.0);
// turn 315 degrees
double gyroAngle = gyro.GetAngle() - 315;
while (gyroAngle < gyro.GetAngle())
myRobot.Drive(0.0, -0.3);
// drive forward 6ft to the left peg
while (sonar.GetRangeInches() < 72)
myRobot.Drive(0.5, 0.0);
// stop moving
myRobot.Drive(0.0, 0.0);
// at this point the human player would lift the gear
// out of the robots pocket and earn us a fuckton of
// points to start the game on a good footing
// drive forward for 2 seconds and stop
} else if (autoSelected == autoDriveForward) {
// drive straight for 2 seconds @ 80% pwr
utils::driveStraight(gyro, myRobot, 2, 0.75);
myRobot.Drive(0.0, 0.0); // stop
// drives until ultrasonic reads given value
} else if (autoSelected == autoGoDist) {
clock_t feedTimeout = clock();
// drive until given inches from peg
while (sonar.GetRangeInches() > frc::SmartDashboard::GetNumber("Distance: ", 0)
&& ((float)(clock() - feedTimeout) / CLOCKS_PER_SEC) < 4)
utils::driveStraight(gyro, myRobot, 0.06, 0.4);
myRobot.Drive(0.0, 0.0);
}
}
void Robot::AutonomousPeriodic() {
/* this isn't really applicable..
if (autoSelected == autoDoNothing) {
// NOP
} else {
}
*/
// put distance from ultrasonic in inches
//frc::SmartDashboard::PutNumber("Distance: ", sonar.GetRangeInches());
}
void Robot::TeleopInit() {
std::cout <<"Teleop has begun :))\n";
// enable the motor controllers
myRobot.SetSafetyEnabled(false);
}
void Robot::TeleopPeriodic() {
// Y switches directions
static bool dir_reversable = true, isForward = true;
if (dir_reversable && xBox.GetRawButton(4)) {
isForward = !isForward;
dir_reversable = false;
} else if (!dir_reversable && !xBox.GetRawButton(4))
dir_reversable = true;
// X toggles slow-mode
static bool slowable = true, isFast = true;
if (slowable && xBox.GetRawButton(6)) {
isFast = !isFast;
slowable = false;
} else if (!slowable && !xBox.GetRawButton(6))
slowable = true;
// joystick data from previous cycle
static double stickX = 0, stickY = 0; // `static` keeps this local variable in memory
// drive the robot
myRobot.ArcadeDrive(
utils::expReduceBrownout((isFast ? 1 : 1/*0.7*/) * (isForward ? -1 : 1)
* xBox.GetRawAxis(1), stickY),
-utils::expReduceBrownout(xBox.GetRawAxis(4), stickX) * 0.8
);
// control the winch for climbing
static bool climb = false;
// A starts climbing
if (xBox.GetRawButton(1)) {
climb = true;
std::cout <<"Do you even lift???" <<std::endl;
// B stops climbing
} else if (xBox.GetRawButton(2))
climb = false;
// set it on or off depending on the value of climber.leftTrigger
winch.Set(climb || (climber.GetRawAxis(3) > 0.60) ? 1 : 0);
// test ultrasonic
//frc::SmartDashboard::PutNumber("Dist (recieved): ", sonar.GetRangeInches());
}
START_ROBOT_CLASS(Robot);