blob: 023bc5f677aa9a2bbb04d870dce2fbdb9b00645a [file] [log] [blame]
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07001#include <memory>
2
3#include "aos/common/inttypes.h"
4#include "aos/atom_code/init.h"
5#include "aos/common/logging/logging.h"
6#include "aos/common/time.h"
7#include "aos/common/sensors/sensor_unpacker.h"
8#include "aos/common/sensors/sensor_receiver.h"
Brian Silverman93871ee2013-09-14 18:15:28 -07009#include "aos/common/glibusb/glibusb.h"
10#include "aos/common/glibusb/gbuffer.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070011
12#include "frc971/control_loops/drivetrain/drivetrain.q.h"
13#include "frc971/control_loops/wrist/wrist_motor.q.h"
14#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
15#include "frc971/control_loops/index/index_motor.q.h"
16#include "frc971/control_loops/shooter/shooter_motor.q.h"
17#include "frc971/input/gyro_board_data.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070018#include "frc971/queues/GyroAngle.q.h"
19
20#ifndef M_PI
21#define M_PI 3.14159265358979323846
22#endif
23
24using ::frc971::control_loops::drivetrain;
25using ::frc971::control_loops::wrist;
26using ::frc971::control_loops::angle_adjust;
27using ::frc971::control_loops::shooter;
28using ::frc971::control_loops::index_loop;
29using ::frc971::sensors::gyro;
30
31namespace frc971 {
32namespace {
33
34inline double drivetrain_translate(int32_t in) {
35 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
36 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
37 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
38}
39
40inline double wrist_translate(int32_t in) {
41 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
42 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
43}
44
45inline double angle_adjust_translate(int32_t in) {
46 static const double kCableDiameter = 0.060;
47 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
48 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
49 (2 * M_PI);
50}
51
52inline double shooter_translate(int32_t in) {
53 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
54 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
55}
56
57inline double index_translate(int32_t in) {
58 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) *
59 (1.0) /*gears*/ * (2 * M_PI);
60}
61
62} // namespace
63
64class GyroSensorUnpacker :
65 public ::aos::sensors::SensorUnpackerInterface<GyroBoardData> {
66 public:
67 GyroSensorUnpacker()
68 : top_rise_count_(0),
69 last_top_rise_count_(0),
70 top_fall_count_(0),
71 last_top_fall_count_(0),
72 bottom_rise_count_(0),
73 last_bottom_rise_count_(0),
74 bottom_fall_delay_count_(0),
75 last_bottom_fall_delay_count_(0),
76 bottom_fall_count_(0),
77 last_bottom_fall_count_(0),
78 wrist_rise_count_(0),
79 last_wrist_rise_count_(0),
80 shooter_angle_rise_count_(0),
81 last_shooter_angle_rise_count_(0) {
82 }
83
84 void UnpackFrom(GyroBoardData *data) {
Brian Silvermanf92396c2013-09-12 20:13:13 -070085 if (data->robot_id != 0) {
86 LOG(ERROR, "gyro board sent data for robot id %hhd!"
Brian Silvermanb2451272013-10-11 21:42:22 -070087 " dip switches are %x\n", data->robot_id, data->base_status & 0xF);
Brian Silvermanf92396c2013-09-12 20:13:13 -070088 return;
89 } else {
Brian Silvermanb2451272013-10-11 21:42:22 -070090 LOG(DEBUG, "processing a packet dip switches %x\n",
91 data->base_status & 0xF);
Brian Silvermanf92396c2013-09-12 20:13:13 -070092 }
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070093
94 static ::aos::time::Time last_time = ::aos::time::Time::Now();
95 if ((last_time - ::aos::time::Time::Now()) >
96 ::aos::time::Time::InMS(0.00205)) {
97 LOG(INFO, "missed one\n");
98 }
99
100 gyro.MakeWithBuilder()
101 .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
102 .Send();
103
Brian Silvermanf92396c2013-09-12 20:13:13 -0700104 UpdateWrappingCounter(data->main.top_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700105 &last_top_rise_count_, &top_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700106 UpdateWrappingCounter(data->main.top_fall_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700107 &last_top_fall_count_, &top_fall_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700108 UpdateWrappingCounter(data->main.bottom_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700109 &last_bottom_rise_count_, &bottom_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700110 UpdateWrappingCounter(data->main.bottom_fall_delay_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700111 &last_bottom_fall_delay_count_, &bottom_fall_delay_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700112 UpdateWrappingCounter(data->main.bottom_fall_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700113 &last_bottom_fall_count_, &bottom_fall_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700114 UpdateWrappingCounter(data->main.wrist_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700115 &last_wrist_rise_count_, &wrist_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700116 UpdateWrappingCounter(data->main.shooter_angle_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700117 &last_shooter_angle_rise_count_, &shooter_angle_rise_count_);
118
119 drivetrain.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700120 .right_encoder(drivetrain_translate(data->main.right_drive))
121 .left_encoder(-drivetrain_translate(data->main.left_drive))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700122 .Send();
123
124 wrist.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700125 .pos(wrist_translate(data->main.wrist))
126 .hall_effect(!data->main.wrist_hall_effect)
127 .calibration(wrist_translate(data->main.capture_wrist_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700128 .Send();
129
130 angle_adjust.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700131 .angle(angle_adjust_translate(data->main.shooter_angle))
132 .bottom_hall_effect(!data->main.angle_adjust_bottom_hall_effect)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700133 .middle_hall_effect(false)
134 .bottom_calibration(angle_adjust_translate(
Brian Silvermanf92396c2013-09-12 20:13:13 -0700135 data->main.capture_shooter_angle_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700136 .middle_calibration(angle_adjust_translate(
137 0))
138 .Send();
139
140 shooter.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700141 .position(shooter_translate(data->main.shooter))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700142 .Send();
143
144 index_loop.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700145 .index_position(index_translate(data->main.indexer))
146 .top_disc_detect(!data->main.top_disc)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700147 .top_disc_posedge_count(top_rise_count_)
Brian Silvermanf92396c2013-09-12 20:13:13 -0700148 .top_disc_posedge_position(index_translate(data->main.capture_top_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700149 .top_disc_negedge_count(top_fall_count_)
Brian Silvermanf92396c2013-09-12 20:13:13 -0700150 .top_disc_negedge_position(index_translate(data->main.capture_top_fall))
151 .bottom_disc_detect(!data->main.bottom_disc)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700152 .bottom_disc_posedge_count(bottom_rise_count_)
153 .bottom_disc_negedge_count(bottom_fall_count_)
154 .bottom_disc_negedge_wait_position(index_translate(
Brian Silvermanf92396c2013-09-12 20:13:13 -0700155 data->main.capture_bottom_fall_delay))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700156 .bottom_disc_negedge_wait_count(bottom_fall_delay_count_)
Brian Silvermanb2451272013-10-11 21:42:22 -0700157 .loader_top(data->main.loader_top)
158 .loader_bottom(data->main.loader_bottom)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700159 .Send();
160 }
161
162 private:
163 void UpdateWrappingCounter(
164 uint8_t current, uint8_t *last, int32_t *counter) {
165 if (*last > current) {
166 *counter += 0x100;
167 }
168 *counter = (*counter & 0xffffff00) | current;
169 *last = current;
170 }
171
172 int32_t top_rise_count_;
173 uint8_t last_top_rise_count_;
174 int32_t top_fall_count_;
175 uint8_t last_top_fall_count_;
176 int32_t bottom_rise_count_;
177 uint8_t last_bottom_rise_count_;
178 int32_t bottom_fall_delay_count_;
179 uint8_t last_bottom_fall_delay_count_;
180 int32_t bottom_fall_count_;
181 uint8_t last_bottom_fall_count_;
182 int32_t wrist_rise_count_;
183 uint8_t last_wrist_rise_count_;
184 int32_t shooter_angle_rise_count_;
185 uint8_t last_shooter_angle_rise_count_;
186};
187
188class GyroSensorReceiver :
189 public ::aos::sensors::SensorReceiver<GyroBoardData> {
190 public:
191 GyroSensorReceiver(
192 ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker)
193 : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker),
194 start_time_(0, 0) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700195 static_assert(sizeof(GyroBoardData) <= kDataLength,
196 "the buffer will be too small");
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700197 }
198
199 private:
200 static const unsigned char kEndpoint = 0x81;
201 // in ms
202 // 0 is unlimited
203 static const unsigned int kReadTimeout = 1000;
204
Brian Silverman93871ee2013-09-14 18:15:28 -0700205 static constexpr glibusb::VendorProductId kDeviceId =
206 glibusb::VendorProductId(0x1424 /* vendor ID */,
207 0xd243 /* product ID */);
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700208
Brian Silverman9a574d52013-03-31 00:53:53 -0700209 // How big of a buffer to give the function.
210 static const size_t kDataLength = 64;
211
Brian Silverman93871ee2013-09-14 18:15:28 -0700212 virtual bool DoReceiveData() {
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700213 // Loop and then return once we get a good one.
214 while (true) {
Brian Silverman93871ee2013-09-14 18:15:28 -0700215 using glibusb::UsbEndpoint;
216 UsbEndpoint::IoStatus result =
217 endpoint_->ReadAtMostWithTimeout(sizeof(GyroBoardData),
218 kReadTimeout,
219 &buffer_);
220 switch (result) {
221 case UsbEndpoint::kSuccess:
222 break;
223 case UsbEndpoint::kTimeout:
224 LOG(WARNING, "read timed out\n");
225 continue;
226 case UsbEndpoint::kNoDevice:
227 LOG(ERROR, "no device\n");
228 return true;
229 case UsbEndpoint::kUnknown:
230 case UsbEndpoint::kFail:
231 case UsbEndpoint::kAbort:
232 LOG(ERROR, "read failed\n");
233 continue;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700234 }
Brian Silverman93871ee2013-09-14 18:15:28 -0700235 if (buffer_.Length() < sizeof(GyroBoardData)) {
236 LOG(ERROR, "read %zd bytes instead of at least %zd\n",
237 buffer_.Length(), sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700238 continue;
239 }
240
Brian Silverman93871ee2013-09-14 18:15:28 -0700241 memcpy(&data()->values, buffer_.GetBufferPointer(sizeof(GyroBoardData)),
Brian Silverman9a574d52013-03-31 00:53:53 -0700242 sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700243 if (data()->count == 0) {
244 start_time_ = ::aos::time::Time::Now();
245 data()->count = 1;
246 } else {
247 ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_;
248 data()->count = static_cast<int32_t>(
249 (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5);
250 }
Brian Silverman93871ee2013-09-14 18:15:28 -0700251 return false;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700252 }
253 }
254
255 virtual void Reset() {
Brian Silverman93871ee2013-09-14 18:15:28 -0700256 device_ = ::std::unique_ptr<glibusb::UsbDevice>(
257 libusb_.FindSingleMatchingDeviceOrLose(kDeviceId));
258 CHECK(device_);
259 endpoint_ = ::std::unique_ptr<glibusb::UsbInEndpoint>(
260 device_->InEndpoint(kEndpoint));
Brian Silverman9a574d52013-03-31 00:53:53 -0700261
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700262 data()->count = 0;
263 }
264
265 virtual void Synchronized(::aos::time::Time start_time) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700266 // Subtract off how many packets it read while synchronizing from the time.
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700267 start_time_ = start_time -
268 ::aos::sensors::kSensorSendFrequency * data()->count;
269 }
270
Brian Silverman93871ee2013-09-14 18:15:28 -0700271 ::std::unique_ptr<glibusb::UsbDevice> device_;
272 ::std::unique_ptr<glibusb::UsbInEndpoint> endpoint_;
273 glibusb::Buffer buffer_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700274
275 ::aos::time::Time start_time_;
276
Brian Silverman93871ee2013-09-14 18:15:28 -0700277 glibusb::Libusb libusb_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700278};
Brian Silverman93871ee2013-09-14 18:15:28 -0700279constexpr glibusb::VendorProductId GyroSensorReceiver::kDeviceId;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700280
281} // namespace frc971
282
283int main() {
284 ::aos::Init();
285 ::frc971::GyroSensorUnpacker unpacker;
286 ::frc971::GyroSensorReceiver receiver(&unpacker);
287 while (true) {
288 receiver.RunIteration();
289 }
290 ::aos::Cleanup();
291}