blob: 1e435563f28cb082e3e2650db7044aa0fa8cddd9 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "DriverStation.h"
9
10#include <boost/mem_fn.hpp>
11
12#include "HAL/cpp/Log.h"
13#include "Timer.h"
14#include "Utility.h"
15#include "WPIErrors.h"
16#include "simulation/MainNode.h"
17
18using namespace frc;
19
20const int DriverStation::kBatteryChannel;
21const int DriverStation::kJoystickPorts;
22const int DriverStation::kJoystickAxes;
23const double DriverStation::kUpdatePeriod = 0.02;
24int DriverStation::m_updateNumber = 0;
25
26/**
27 * DriverStation contructor.
28 *
29 * This is only called once the first time GetInstance() is called
30 */
31DriverStation::DriverStation() {
32 state = gazebo::msgs::DriverStationPtr(new gazebo::msgs::DriverStation());
33 stateSub =
34 MainNode::Subscribe("~/ds/state", &DriverStation::stateCallback, this);
35 // TODO: for loop + boost bind
36 joysticks[0] = gazebo::msgs::FRCJoystickPtr(new gazebo::msgs::FRCJoystick());
37 joysticksSub[0] = MainNode::Subscribe(
38 "~/ds/joysticks/0", &DriverStation::joystickCallback0, this);
39 joysticks[1] = gazebo::msgs::FRCJoystickPtr(new gazebo::msgs::FRCJoystick());
40 joysticksSub[1] = MainNode::Subscribe(
41 "~/ds/joysticks/1", &DriverStation::joystickCallback1, this);
42 joysticks[2] = gazebo::msgs::FRCJoystickPtr(new gazebo::msgs::FRCJoystick());
43 joysticksSub[2] = MainNode::Subscribe(
44 "~/ds/joysticks/2", &DriverStation::joystickCallback2, this);
45 joysticks[3] = gazebo::msgs::FRCJoystickPtr(new gazebo::msgs::FRCJoystick());
46 joysticksSub[3] = MainNode::Subscribe(
47 "~/ds/joysticks/5", &DriverStation::joystickCallback3, this);
48 joysticks[4] = gazebo::msgs::FRCJoystickPtr(new gazebo::msgs::FRCJoystick());
49 joysticksSub[4] = MainNode::Subscribe(
50 "~/ds/joysticks/4", &DriverStation::joystickCallback4, this);
51 joysticks[5] = gazebo::msgs::FRCJoystickPtr(new gazebo::msgs::FRCJoystick());
52 joysticksSub[5] = MainNode::Subscribe(
53 "~/ds/joysticks/5", &DriverStation::joystickCallback5, this);
54}
55
56/**
57 * Return a pointer to the singleton DriverStation.
58 */
59DriverStation& DriverStation::GetInstance() {
60 static DriverStation instance;
61 return instance;
62}
63
64/**
65 * Read the battery voltage. Hardcoded to 12 volts for Simulation.
66 *
67 * @return The battery voltage.
68 */
69double DriverStation::GetBatteryVoltage() const {
70 return 12.0; // 12 volts all the time!
71}
72
73/**
74 * Get the value of the axis on a joystick.
75 * This depends on the mapping of the joystick connected to the specified port.
76 *
77 * @param stick The joystick to read.
78 * @param axis The analog axis value to read from the joystick.
79 * @return The value of the axis on the joystick.
80 */
81double DriverStation::GetStickAxis(int stick, int axis) {
82 if (axis < 0 || axis > (kJoystickAxes - 1)) {
83 wpi_setWPIError(BadJoystickAxis);
84 return 0.0;
85 }
86 if (stick < 0 || stick > 5) {
87 wpi_setWPIError(BadJoystickIndex);
88 return 0.0;
89 }
90
91 std::unique_lock<std::recursive_mutex> lock(m_joystickMutex);
92 if (joysticks[stick] == nullptr || axis >= joysticks[stick]->axes().size()) {
93 return 0.0;
94 }
95 return joysticks[stick]->axes(axis);
96}
97
98/**
99 * The state of a specific button (1 - 12) on the joystick.
100 *
101 * This method only works in simulation, but is more efficient than
102 * GetStickButtons.
103 *
104 * @param stick The joystick to read.
105 * @param button The button number to check.
106 * @return If the button is pressed.
107 */
108bool DriverStation::GetStickButton(int stick, int button) {
109 if (stick < 0 || stick >= 6) {
110 wpi_setWPIErrorWithContext(ParameterOutOfRange,
111 "stick must be between 0 and 5");
112 return false;
113 }
114
115 std::unique_lock<std::recursive_mutex> lock(m_joystickMutex);
116 if (joysticks[stick] == nullptr ||
117 button >= joysticks[stick]->buttons().size()) {
118 return false;
119 }
120 return joysticks[stick]->buttons(button - 1);
121}
122
123/**
124 * The state of the buttons on the joystick.
125 *
126 * 12 buttons (4 msb are unused) from the joystick.
127 *
128 * @param stick The joystick to read.
129 * @return The state of the buttons on the joystick.
130 */
131int16_t DriverStation::GetStickButtons(int stick) {
132 if (stick < 0 || stick >= 6) {
133 wpi_setWPIErrorWithContext(ParameterOutOfRange,
134 "stick must be between 0 and 5");
135 return false;
136 }
137 int16_t btns = 0, btnid;
138
139 std::unique_lock<std::recursive_mutex> lock(m_joystickMutex);
140 gazebo::msgs::FRCJoystickPtr joy = joysticks[stick];
141 for (btnid = 0; btnid < joy->buttons().size() && btnid < 12; btnid++) {
142 if (joysticks[stick]->buttons(btnid)) {
143 btns |= (1 << btnid);
144 }
145 }
146 return btns;
147}
148
149// 5V divided by 10 bits
150#define kDSAnalogInScaling (5.0 / 1023.0)
151
152/**
153 * Get an analog voltage from the Driver Station.
154 *
155 * The analog values are returned as voltage values for the Driver Station
156 * analog inputs. These inputs are typically used for advanced operator
157 * interfaces consisting of potentiometers or resistor networks representing
158 * values on a rotary switch.
159 *
160 * @param channel The analog input channel on the driver station to read from.
161 * Valid range is 1 - 4.
162 * @return The analog voltage on the input.
163 */
164double DriverStation::GetAnalogIn(int channel) {
165 wpi_setWPIErrorWithContext(UnsupportedInSimulation, "GetAnalogIn");
166 return 0.0;
167}
168
169/**
170 * Get values from the digital inputs on the Driver Station.
171 *
172 * Return digital values from the Drivers Station. These values are typically
173 * used for buttons and switches on advanced operator interfaces.
174 *
175 * @param channel The digital input to get. Valid range is 1 - 8.
176 */
177bool DriverStation::GetDigitalIn(int channel) {
178 wpi_setWPIErrorWithContext(UnsupportedInSimulation, "GetDigitalIn");
179 return false;
180}
181
182/**
183 * Set a value for the digital outputs on the Driver Station.
184 *
185 * Control digital outputs on the Drivers Station. These values are typically
186 * used for giving feedback on a custom operator station such as LEDs.
187 *
188 * @param channel The digital output to set. Valid range is 1 - 8.
189 * @param value The state to set the digital output.
190 */
191void DriverStation::SetDigitalOut(int channel, bool value) {
192 wpi_setWPIErrorWithContext(UnsupportedInSimulation, "SetDigitalOut");
193}
194
195/**
196 * Get a value that was set for the digital outputs on the Driver Station.
197 *
198 * @param channel The digital ouput to monitor. Valid range is 1 through 8.
199 * @return A digital value being output on the Drivers Station.
200 */
201bool DriverStation::GetDigitalOut(int channel) {
202 wpi_setWPIErrorWithContext(UnsupportedInSimulation, "GetDigitalOut");
203 return false;
204}
205
206bool DriverStation::IsEnabled() const {
207 std::unique_lock<std::recursive_mutex> lock(m_stateMutex);
208 return state != nullptr ? state->enabled() : false;
209}
210
211bool DriverStation::IsDisabled() const { return !IsEnabled(); }
212
213bool DriverStation::IsAutonomous() const {
214 std::unique_lock<std::recursive_mutex> lock(m_stateMutex);
215 return state != nullptr
216 ? state->state() == gazebo::msgs::DriverStation_State_AUTO
217 : false;
218}
219
220bool DriverStation::IsOperatorControl() const {
221 return !(IsAutonomous() || IsTest());
222}
223
224bool DriverStation::IsTest() const {
225 std::unique_lock<std::recursive_mutex> lock(m_stateMutex);
226 return state != nullptr
227 ? state->state() == gazebo::msgs::DriverStation_State_TEST
228 : false;
229}
230
231/**
232 * Is the driver station attached to a Field Management System?
233 * @return True if the robot is competing on a field being controlled by a Field
234 * Management System
235 */
236bool DriverStation::IsFMSAttached() const {
237 return false; // No FMS in simulation
238}
239
240/**
241 * Return the alliance that the driver station says it is on.
242 * This could return kRed or kBlue.
243 * @return The Alliance enum
244 */
245DriverStation::Alliance DriverStation::GetAlliance() const {
246 // if (m_controlData->dsID_Alliance == 'R') return kRed;
247 // if (m_controlData->dsID_Alliance == 'B') return kBlue;
248 // wpi_assert(false);
249 return kInvalid; // TODO: Support alliance colors
250}
251
252/**
253 * Return the driver station location on the field.
254 * This could return 1, 2, or 3.
255 * @return The location of the driver station
256 */
257int DriverStation::GetLocation() const {
258 return -1; // TODO: Support locations
259}
260
261/**
262 * Wait until a new packet comes from the driver station.
263 *
264 * This blocks on a semaphore, so the waiting is efficient.
265 *
266 * This is a good way to delay processing until there is new driver station data
267 * to act on.
268 */
269void DriverStation::WaitForData() { WaitForData(0); }
270
271/**
272 * Wait until a new packet comes from the driver station, or wait for a timeout.
273 *
274 * If the timeout is less then or equal to 0, wait indefinitely.
275 *
276 * Timeout is in milliseconds
277 *
278 * This blocks on a semaphore, so the waiting is efficient.
279 *
280 * This is a good way to delay processing until there is new driver station data
281 * to act on.
282 *
283 * @param timeout Timeout time in seconds
284 *
285 * @return true if new data, otherwise false
286 */
287bool DriverStation::WaitForData(double timeout) {
288#if defined(_MSC_VER) && _MSC_VER < 1900
289 auto timeoutTime = std::chrono::steady_clock::now() +
290 std::chrono::duration<int64_t, std::nano>(
291 static_cast<int64_t>(timeout * 1e9));
292#else
293 auto timeoutTime =
294 std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
295#endif
296
297 std::unique_lock<priority_mutex> lock(m_waitForDataMutex);
298 while (!m_updatedControlLoopData) {
299 if (timeout > 0) {
300 auto timedOut = m_waitForDataCond.wait_until(lock, timeoutTime);
301 if (timedOut == std::cv_status::timeout) {
302 return false;
303 }
304 } else {
305 m_waitForDataCond.wait(lock);
306 }
307 }
308 m_updatedControlLoopData = false;
309 return true;
310}
311
312/**
313 * Return the approximate match time.
314 * The FMS does not currently send the official match time to the robots
315 * This returns the time since the enable signal sent from the Driver Station
316 * At the beginning of autonomous, the time is reset to 0.0 seconds
317 * At the beginning of teleop, the time is reset to +15.0 seconds
318 * If the robot is disabled, this returns 0.0 seconds
319 * Warning: This is not an official time (so it cannot be used to argue with
320 * referees)
321 * @return Match time in seconds since the beginning of autonomous
322 */
323double DriverStation::GetMatchTime() const {
324 if (m_approxMatchTimeOffset < 0.0) return 0.0;
325 return Timer::GetFPGATimestamp() - m_approxMatchTimeOffset;
326}
327
328/**
329 * Report an error to the DriverStation messages window.
330 * The error is also printed to the program console.
331 */
332void DriverStation::ReportError(llvm::StringRef error) {
333 std::cout << error << std::endl;
334}
335
336/**
337 * Report a warning to the DriverStation messages window.
338 * The warning is also printed to the program console.
339 */
340void DriverStation::ReportWarning(llvm::StringRef error) {
341 std::cout << error << std::endl;
342}
343
344/**
345 * Report an error to the DriverStation messages window.
346 * The error is also printed to the program console.
347 */
348void DriverStation::ReportError(bool is_error, int code, llvm::StringRef error,
349 llvm::StringRef location,
350 llvm::StringRef stack) {
351 if (!location.empty())
352 std::cout << (is_error ? "Error" : "Warning") << " at " << location << ": ";
353 std::cout << error << std::endl;
354 if (!stack.empty()) std::cout << stack << std::endl;
355}
356
357/**
358 * Return the team number that the Driver Station is configured for.
359 * @return The team number
360 */
361uint16_t DriverStation::GetTeamNumber() const { return 348; }
362
363void DriverStation::stateCallback(
364 const gazebo::msgs::ConstDriverStationPtr& msg) {
365 {
366 std::unique_lock<std::recursive_mutex> lock(m_stateMutex);
367 *state = *msg;
368 }
369 {
370 std::lock_guard<priority_mutex> lock(m_waitForDataMutex);
371 m_updatedControlLoopData = true;
372 }
373 m_waitForDataCond.notify_all();
374}
375
376void DriverStation::joystickCallback(
377 const gazebo::msgs::ConstFRCJoystickPtr& msg, int i) {
378 std::unique_lock<std::recursive_mutex> lock(m_joystickMutex);
379 *(joysticks[i]) = *msg;
380}
381
382void DriverStation::joystickCallback0(
383 const gazebo::msgs::ConstFRCJoystickPtr& msg) {
384 joystickCallback(msg, 0);
385}
386
387void DriverStation::joystickCallback1(
388 const gazebo::msgs::ConstFRCJoystickPtr& msg) {
389 joystickCallback(msg, 1);
390}
391
392void DriverStation::joystickCallback2(
393 const gazebo::msgs::ConstFRCJoystickPtr& msg) {
394 joystickCallback(msg, 2);
395}
396
397void DriverStation::joystickCallback3(
398 const gazebo::msgs::ConstFRCJoystickPtr& msg) {
399 joystickCallback(msg, 3);
400}
401
402void DriverStation::joystickCallback4(
403 const gazebo::msgs::ConstFRCJoystickPtr& msg) {
404 joystickCallback(msg, 4);
405}
406
407void DriverStation::joystickCallback5(
408 const gazebo::msgs::ConstFRCJoystickPtr& msg) {
409 joystickCallback(msg, 5);
410}