blob: fbec2ef177048398ddf8414fa823efe84ee0a381 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2011. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "KinectStick.h"
8
9#include "DriverStation.h"
10#include "Joystick.h"
11#include "NetworkCommunication/FRCComm.h"
12#include "NetworkCommunication/UsageReporting.h"
13#include "Utility.h"
14#include "WPIErrors.h"
15
16UINT32 KinectStick::_recentPacketNumber = 0;
17KinectStick::KinectStickData KinectStick::_sticks;
18
19#define kJoystickBundleID kFRC_NetworkCommunication_DynamicType_Kinect_Joystick
20#define kTriggerMask 1
21#define kTopMask 2
22
23/**
24 * Kinect joystick constructor
25 * @param id value is either 1 or 2 for the left or right joystick decoded from
26 * gestures interpreted by the Kinect server on the Driver Station computer.
27 */
28KinectStick::KinectStick(int id)
29{
30 if (id != 1 && id != 2)
31 {
32 wpi_setWPIErrorWithContext(ParameterOutOfRange, "KinectStick ID must be 1 or 2");
33 return;
34 }
35 m_id = id;
36
37 nUsageReporting::report(nUsageReporting::kResourceType_KinectStick, id);
38}
39
40/**
41 * Get the X value of the KinectStick. This axis
42 * is unimplemented in the default gestures but can
43 * be populated by teams editing the Kinect Server.
44 * @param hand Unused
45 * @return The X value of the KinectStick
46 */
47float KinectStick::GetX(JoystickHand hand)
48{
49 return GetRawAxis(Joystick::kDefaultXAxis);
50}
51
52/**
53 * Get the Y value of the KinectStick. This axis
54 * represents arm angle in the default gestures
55 * @param hand Unused
56 * @return The Y value of the KinectStick
57 */
58float KinectStick::GetY(JoystickHand hand)
59{
60 return GetRawAxis(Joystick::kDefaultYAxis);
61}
62
63/**
64 * Get the Z value of the KinectStick. This axis
65 * is unimplemented in the default gestures but can
66 * be populated by teams editing the Kinect Server.
67 * @param hand Unused
68 * @return The Z value of the KinectStick
69 */
70float KinectStick::GetZ()
71{
72 return GetRawAxis(Joystick::kDefaultZAxis);
73}
74
75/**
76 * Get the Twist value of the KinectStick. This axis
77 * is unimplemented in the default gestures but can
78 * be populated by teams editing the Kinect Server.
79 * @return The Twist value of the KinectStick
80 */
81float KinectStick::GetTwist()
82{
83 return GetRawAxis(Joystick::kDefaultTwistAxis);
84}
85
86/**
87 * Get the Throttle value of the KinectStick. This axis
88 * is unimplemented in the default gestures but can
89 * be populated by teams editing the Kinect Server.
90 * @return The Throttle value of the KinectStick
91 */
92float KinectStick::GetThrottle()
93{
94 return GetRawAxis(Joystick::kDefaultThrottleAxis);
95}
96
97/**
98 * Get the value of the KinectStick axis.
99 *
100 * @param axis The axis to read [1-6].
101 * @return The value of the axis
102 */
103float KinectStick::GetRawAxis(UINT32 axis)
104{
105 if (StatusIsFatal()) return 0.0;
106
107 GetData();
108 float value = ConvertRawToFloat(_sticks.formatted.rawSticks[m_id - 1].axis[axis-1]);
109 return value;
110}
111
112/**
113 * Get the button value for the button set as the default trigger
114 *
115 * @param hand Unused
116 * @return The state of the button.
117 */
118bool KinectStick::GetTrigger(JoystickHand hand)
119{
120 return GetRawButton(kTriggerMask);
121}
122
123/**
124 * Get the button value for the button set as the default top
125 *
126 * @param hand Unused
127 * @return The state of the button.
128 */
129bool KinectStick::GetTop(JoystickHand hand)
130{
131 return GetRawButton(kTopMask);
132}
133
134/**
135 * Get the button value for the button set as the default bumper (button 4)
136 *
137 * @param hand Unused
138 * @return The state of the button.
139 */
140bool KinectStick::GetBumper(JoystickHand hand)
141{
142 // TODO: Should this even be in GenericHID? Is 4 an appropriate mask value (button 3)?
143 return GetRawButton(4);
144}
145
146/**
147 * Get the button value for buttons 1 through 12. The default gestures
148 * implement only 9 buttons.
149 *
150 * The appropriate button is returned as a boolean value.
151 *
152 * @param button The button number to be read.
153 * @return The state of the button.
154 */
155bool KinectStick::GetRawButton(UINT32 button)
156{
157 if (StatusIsFatal()) return false;
158
159 GetData();
160 return (_sticks.formatted.rawSticks[m_id - 1].buttons & (1 << button)) != 0;
161}
162
163/**
164 * Get dynamic data from the driver station buffer
165 */
166void KinectStick::GetData()
167{
168 UINT32 packetNumber = DriverStation::GetInstance()->GetPacketNumber();
169 if (_recentPacketNumber != packetNumber)
170 {
171 _recentPacketNumber = packetNumber;
172 int retVal = getDynamicControlData(kJoystickBundleID, _sticks.data, sizeof(_sticks.data), 5);
173 if (retVal == 0)
174 {
175 wpi_assert(_sticks.formatted.size == sizeof(_sticks.data) - 1);
176 }
177 }
178}
179
180/**
181 * Convert an 8 bit joystick value to a floating point (-1,1) value
182 * @param value The 8 bit raw joystick value returned from the driver station
183 */
184float KinectStick::ConvertRawToFloat(INT8 value)
185{
186 float result;
187
188 if (value < 0)
189 result = ((float) value) / 128.0;
190 else
191 result = ((float) value) / 127.0;
192
193 wpi_assert(result <= 1.0 && result >= -1.0);
194
195 if (result > 1.0)
196 result = 1.0;
197 else if (result < -1.0)
198 result = -1.0;
199
200 return result;
201}