blob: 1e5343e6a3b838b0ba26aacf6a8dbc86e53c713c [file] [log] [blame]
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07001#include <libusb-1.0/libusb.h>
2#include <memory>
3
4#include "aos/common/inttypes.h"
5#include "aos/atom_code/init.h"
6#include "aos/common/logging/logging.h"
7#include "aos/common/time.h"
8#include "aos/common/sensors/sensor_unpacker.h"
9#include "aos/common/sensors/sensor_receiver.h"
10
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/control_loops/wrist/wrist_motor.q.h"
13#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
14#include "frc971/control_loops/index/index_motor.q.h"
15#include "frc971/control_loops/shooter/shooter_motor.q.h"
16#include "frc971/input/gyro_board_data.h"
17#include "gyro_board/src/libusb-driver/libusb_wrap.h"
18#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!"
87 " dip switches are %x\n", data->robot_id, data->dip_switches);
88 return;
89 } else {
90 LOG(DEBUG, "processing a packet dip switches %x\n", data->dip_switches);
91 }
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070092
93 static ::aos::time::Time last_time = ::aos::time::Time::Now();
94 if ((last_time - ::aos::time::Time::Now()) >
95 ::aos::time::Time::InMS(0.00205)) {
96 LOG(INFO, "missed one\n");
97 }
98
99 gyro.MakeWithBuilder()
100 .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
101 .Send();
102
Brian Silvermanf92396c2013-09-12 20:13:13 -0700103 UpdateWrappingCounter(data->main.top_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700104 &last_top_rise_count_, &top_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700105 UpdateWrappingCounter(data->main.top_fall_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700106 &last_top_fall_count_, &top_fall_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700107 UpdateWrappingCounter(data->main.bottom_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700108 &last_bottom_rise_count_, &bottom_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700109 UpdateWrappingCounter(data->main.bottom_fall_delay_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700110 &last_bottom_fall_delay_count_, &bottom_fall_delay_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700111 UpdateWrappingCounter(data->main.bottom_fall_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700112 &last_bottom_fall_count_, &bottom_fall_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700113 UpdateWrappingCounter(data->main.wrist_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700114 &last_wrist_rise_count_, &wrist_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700115 UpdateWrappingCounter(data->main.shooter_angle_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700116 &last_shooter_angle_rise_count_, &shooter_angle_rise_count_);
117
118 drivetrain.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700119 .right_encoder(drivetrain_translate(data->main.right_drive))
120 .left_encoder(-drivetrain_translate(data->main.left_drive))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700121 .Send();
122
123 wrist.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700124 .pos(wrist_translate(data->main.wrist))
125 .hall_effect(!data->main.wrist_hall_effect)
126 .calibration(wrist_translate(data->main.capture_wrist_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700127 .Send();
128
129 angle_adjust.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700130 .angle(angle_adjust_translate(data->main.shooter_angle))
131 .bottom_hall_effect(!data->main.angle_adjust_bottom_hall_effect)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700132 .middle_hall_effect(false)
133 .bottom_calibration(angle_adjust_translate(
Brian Silvermanf92396c2013-09-12 20:13:13 -0700134 data->main.capture_shooter_angle_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700135 .middle_calibration(angle_adjust_translate(
136 0))
137 .Send();
138
139 shooter.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700140 .position(shooter_translate(data->main.shooter))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700141 .Send();
142
143 index_loop.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700144 .index_position(index_translate(data->main.indexer))
145 .top_disc_detect(!data->main.top_disc)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700146 .top_disc_posedge_count(top_rise_count_)
Brian Silvermanf92396c2013-09-12 20:13:13 -0700147 .top_disc_posedge_position(index_translate(data->main.capture_top_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700148 .top_disc_negedge_count(top_fall_count_)
Brian Silvermanf92396c2013-09-12 20:13:13 -0700149 .top_disc_negedge_position(index_translate(data->main.capture_top_fall))
150 .bottom_disc_detect(!data->main.bottom_disc)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700151 .bottom_disc_posedge_count(bottom_rise_count_)
152 .bottom_disc_negedge_count(bottom_fall_count_)
153 .bottom_disc_negedge_wait_position(index_translate(
Brian Silvermanf92396c2013-09-12 20:13:13 -0700154 data->main.capture_bottom_fall_delay))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700155 .bottom_disc_negedge_wait_count(bottom_fall_delay_count_)
156 .Send();
157 }
158
159 private:
160 void UpdateWrappingCounter(
161 uint8_t current, uint8_t *last, int32_t *counter) {
162 if (*last > current) {
163 *counter += 0x100;
164 }
165 *counter = (*counter & 0xffffff00) | current;
166 *last = current;
167 }
168
169 int32_t top_rise_count_;
170 uint8_t last_top_rise_count_;
171 int32_t top_fall_count_;
172 uint8_t last_top_fall_count_;
173 int32_t bottom_rise_count_;
174 uint8_t last_bottom_rise_count_;
175 int32_t bottom_fall_delay_count_;
176 uint8_t last_bottom_fall_delay_count_;
177 int32_t bottom_fall_count_;
178 uint8_t last_bottom_fall_count_;
179 int32_t wrist_rise_count_;
180 uint8_t last_wrist_rise_count_;
181 int32_t shooter_angle_rise_count_;
182 uint8_t last_shooter_angle_rise_count_;
183};
184
185class GyroSensorReceiver :
186 public ::aos::sensors::SensorReceiver<GyroBoardData> {
187 public:
188 GyroSensorReceiver(
189 ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker)
190 : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker),
191 start_time_(0, 0) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700192 static_assert(sizeof(GyroBoardData) <= kDataLength,
193 "the buffer will be too small");
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700194 }
195
196 private:
197 static const unsigned char kEndpoint = 0x81;
198 // in ms
199 // 0 is unlimited
200 static const unsigned int kReadTimeout = 1000;
201
202 // vendor ID
203 static const int32_t kVid = 0x1424;
204 // product ID
205 static const int32_t kPid = 0xd243;
206
Brian Silverman9a574d52013-03-31 00:53:53 -0700207 // How big of a buffer to give the function.
208 static const size_t kDataLength = 64;
209
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700210 virtual void DoReceiveData() {
211 // Loop and then return once we get a good one.
212 while (true) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700213 completed_transfer_ = NULL;
214 while (completed_transfer_ == NULL) {
215 libusb_.HandleEvents();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700216 }
Brian Silverman9a574d52013-03-31 00:53:53 -0700217 LOG(DEBUG, "processing transfer %p\n", completed_transfer_);
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700218
Brian Silverman9a574d52013-03-31 00:53:53 -0700219 if (completed_transfer_->read_bytes() <
220 static_cast<ssize_t>(sizeof(GyroBoardData))) {
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700221 LOG(ERROR, "read %d bytes instead of at least %zd\n",
Brian Silverman9a574d52013-03-31 00:53:53 -0700222 completed_transfer_->read_bytes(), sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700223 continue;
224 }
225
Brian Silverman9a574d52013-03-31 00:53:53 -0700226 memcpy(&data()->values, completed_transfer_->data(),
227 sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700228 if (data()->count == 0) {
229 start_time_ = ::aos::time::Time::Now();
230 data()->count = 1;
231 } else {
232 ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_;
233 data()->count = static_cast<int32_t>(
234 (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5);
235 }
236 return;
237 }
238 }
239
240 virtual void Reset() {
Brian Silverman9a574d52013-03-31 00:53:53 -0700241 transfer1_.reset();
242 transfer2_.reset();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700243 dev_handle_ = ::std::unique_ptr<LibUSBDeviceHandle>(
244 libusb_.FindDeviceWithVIDPID(kVid, kPid));
245 if (!dev_handle_) {
Brian Silverman63ad1312013-08-29 15:20:09 -0700246 LOG(ERROR, "couldn't find device. exiting\n");
247 exit(1);
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700248 }
Brian Silverman9a574d52013-03-31 00:53:53 -0700249 transfer1_ = ::std::unique_ptr<libusb::Transfer>(
250 new libusb::Transfer(kDataLength, StaticTransferCallback, this));
251 transfer2_ = ::std::unique_ptr<libusb::Transfer>(
252 new libusb::Transfer(kDataLength, StaticTransferCallback, this));
253 transfer1_->FillInterrupt(dev_handle_.get(), kEndpoint, kReadTimeout);
254 transfer2_->FillInterrupt(dev_handle_.get(), kEndpoint, kReadTimeout);
255 transfer1_->Submit();
256 transfer2_->Submit();
257
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700258 data()->count = 0;
259 }
260
Brian Silverman9a574d52013-03-31 00:53:53 -0700261 static void StaticTransferCallback(libusb::Transfer *transfer, void *self) {
262 static_cast<GyroSensorReceiver *>(self)->TransferCallback(transfer);
263 }
264 void TransferCallback(libusb::Transfer *transfer) {
265 if (transfer->status() == LIBUSB_TRANSFER_COMPLETED) {
266 LOG(DEBUG, "transfer %p completed\n", transfer);
267 completed_transfer_ = transfer;
268 } else if (transfer->status() == LIBUSB_TRANSFER_TIMED_OUT) {
269 LOG(WARNING, "transfer %p timed out\n", transfer);
270 } else if (transfer->status() == LIBUSB_TRANSFER_CANCELLED) {
271 LOG(DEBUG, "transfer %p cancelled\n", transfer);
272 } else {
273 LOG(FATAL, "transfer %p has status %d\n", transfer, transfer->status());
274 }
275 transfer->Submit();
276 }
277
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700278 virtual void Synchronized(::aos::time::Time start_time) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700279 // Subtract off how many packets it read while synchronizing from the time.
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700280 start_time_ = start_time -
281 ::aos::sensors::kSensorSendFrequency * data()->count;
282 }
283
284 ::std::unique_ptr<LibUSBDeviceHandle> dev_handle_;
Brian Silverman9a574d52013-03-31 00:53:53 -0700285 ::std::unique_ptr<libusb::Transfer> transfer1_, transfer2_;
286 // Temporary variable for holding a completed transfer to communicate that
287 // information from the callback to the code that wants it.
288 libusb::Transfer *completed_transfer_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700289
290 ::aos::time::Time start_time_;
291
292 LibUSB libusb_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700293};
294
295} // namespace frc971
296
297int main() {
298 ::aos::Init();
299 ::frc971::GyroSensorUnpacker unpacker;
300 ::frc971::GyroSensorReceiver receiver(&unpacker);
301 while (true) {
302 receiver.RunIteration();
303 }
304 ::aos::Cleanup();
305}