blob: 3d95758221a49de55314c4dacfcd2800826aa576 [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) {
85 data->NetworkToHost();
86 LOG(DEBUG, "processing a packet\n");
87
88 static ::aos::time::Time last_time = ::aos::time::Time::Now();
89 if ((last_time - ::aos::time::Time::Now()) >
90 ::aos::time::Time::InMS(0.00205)) {
91 LOG(INFO, "missed one\n");
92 }
93
94 gyro.MakeWithBuilder()
95 .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
96 .Send();
97
98 UpdateWrappingCounter(data->top_rise_count,
99 &last_top_rise_count_, &top_rise_count_);
100 UpdateWrappingCounter(data->top_fall_count,
101 &last_top_fall_count_, &top_fall_count_);
102 UpdateWrappingCounter(data->bottom_rise_count,
103 &last_bottom_rise_count_, &bottom_rise_count_);
104 UpdateWrappingCounter(data->bottom_fall_delay_count,
105 &last_bottom_fall_delay_count_, &bottom_fall_delay_count_);
106 UpdateWrappingCounter(data->bottom_fall_count,
107 &last_bottom_fall_count_, &bottom_fall_count_);
108 UpdateWrappingCounter(data->wrist_rise_count,
109 &last_wrist_rise_count_, &wrist_rise_count_);
110 UpdateWrappingCounter(data->shooter_angle_rise_count,
111 &last_shooter_angle_rise_count_, &shooter_angle_rise_count_);
112
113 drivetrain.position.MakeWithBuilder()
Brian Silverman87cd6222013-03-31 01:56:26 -0700114 .right_encoder(drivetrain_translate(data->right_drive))
115 .left_encoder(-drivetrain_translate(data->left_drive))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700116 .Send();
117
118 wrist.position.MakeWithBuilder()
119 .pos(wrist_translate(data->wrist))
120 .hall_effect(!data->wrist_hall_effect)
121 .calibration(wrist_translate(data->capture_wrist_rise))
122 .Send();
123
124 angle_adjust.position.MakeWithBuilder()
125 .angle(angle_adjust_translate(data->shooter_angle))
126 .bottom_hall_effect(!data->angle_adjust_bottom_hall_effect)
127 .middle_hall_effect(false)
128 .bottom_calibration(angle_adjust_translate(
129 data->capture_shooter_angle_rise))
130 .middle_calibration(angle_adjust_translate(
131 0))
132 .Send();
133
134 shooter.position.MakeWithBuilder()
135 .position(shooter_translate(data->shooter))
136 .Send();
137
138 index_loop.position.MakeWithBuilder()
139 .index_position(index_translate(data->indexer))
140 .top_disc_detect(!data->top_disc)
141 .top_disc_posedge_count(top_rise_count_)
142 .top_disc_posedge_position(index_translate(data->capture_top_rise))
143 .top_disc_negedge_count(top_fall_count_)
144 .top_disc_negedge_position(index_translate(data->capture_top_fall))
145 .bottom_disc_detect(!data->bottom_disc)
146 .bottom_disc_posedge_count(bottom_rise_count_)
147 .bottom_disc_negedge_count(bottom_fall_count_)
148 .bottom_disc_negedge_wait_position(index_translate(
149 data->capture_bottom_fall_delay))
150 .bottom_disc_negedge_wait_count(bottom_fall_delay_count_)
151 .Send();
152 }
153
154 private:
155 void UpdateWrappingCounter(
156 uint8_t current, uint8_t *last, int32_t *counter) {
157 if (*last > current) {
158 *counter += 0x100;
159 }
160 *counter = (*counter & 0xffffff00) | current;
161 *last = current;
162 }
163
164 int32_t top_rise_count_;
165 uint8_t last_top_rise_count_;
166 int32_t top_fall_count_;
167 uint8_t last_top_fall_count_;
168 int32_t bottom_rise_count_;
169 uint8_t last_bottom_rise_count_;
170 int32_t bottom_fall_delay_count_;
171 uint8_t last_bottom_fall_delay_count_;
172 int32_t bottom_fall_count_;
173 uint8_t last_bottom_fall_count_;
174 int32_t wrist_rise_count_;
175 uint8_t last_wrist_rise_count_;
176 int32_t shooter_angle_rise_count_;
177 uint8_t last_shooter_angle_rise_count_;
178};
179
180class GyroSensorReceiver :
181 public ::aos::sensors::SensorReceiver<GyroBoardData> {
182 public:
183 GyroSensorReceiver(
184 ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker)
185 : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker),
186 start_time_(0, 0) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700187 static_assert(sizeof(GyroBoardData) <= kDataLength,
188 "the buffer will be too small");
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700189 }
190
191 private:
192 static const unsigned char kEndpoint = 0x81;
193 // in ms
194 // 0 is unlimited
195 static const unsigned int kReadTimeout = 1000;
196
197 // vendor ID
198 static const int32_t kVid = 0x1424;
199 // product ID
200 static const int32_t kPid = 0xd243;
201
Brian Silverman9a574d52013-03-31 00:53:53 -0700202 // How big of a buffer to give the function.
203 static const size_t kDataLength = 64;
204
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700205 virtual void DoReceiveData() {
206 // Loop and then return once we get a good one.
207 while (true) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700208 completed_transfer_ = NULL;
209 while (completed_transfer_ == NULL) {
210 libusb_.HandleEvents();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700211 }
Brian Silverman9a574d52013-03-31 00:53:53 -0700212 LOG(DEBUG, "processing transfer %p\n", completed_transfer_);
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700213
Brian Silverman9a574d52013-03-31 00:53:53 -0700214 if (completed_transfer_->read_bytes() <
215 static_cast<ssize_t>(sizeof(GyroBoardData))) {
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700216 LOG(ERROR, "read %d bytes instead of at least %zd\n",
Brian Silverman9a574d52013-03-31 00:53:53 -0700217 completed_transfer_->read_bytes(), sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700218 continue;
219 }
220
Brian Silverman9a574d52013-03-31 00:53:53 -0700221 memcpy(&data()->values, completed_transfer_->data(),
222 sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700223 if (data()->count == 0) {
224 start_time_ = ::aos::time::Time::Now();
225 data()->count = 1;
226 } else {
227 ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_;
228 data()->count = static_cast<int32_t>(
229 (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5);
230 }
231 return;
232 }
233 }
234
235 virtual void Reset() {
Brian Silverman9a574d52013-03-31 00:53:53 -0700236 transfer1_.reset();
237 transfer2_.reset();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700238 dev_handle_ = ::std::unique_ptr<LibUSBDeviceHandle>(
239 libusb_.FindDeviceWithVIDPID(kVid, kPid));
240 if (!dev_handle_) {
241 LOG(FATAL, "couldn't find device\n");
242 }
Brian Silverman9a574d52013-03-31 00:53:53 -0700243 transfer1_ = ::std::unique_ptr<libusb::Transfer>(
244 new libusb::Transfer(kDataLength, StaticTransferCallback, this));
245 transfer2_ = ::std::unique_ptr<libusb::Transfer>(
246 new libusb::Transfer(kDataLength, StaticTransferCallback, this));
247 transfer1_->FillInterrupt(dev_handle_.get(), kEndpoint, kReadTimeout);
248 transfer2_->FillInterrupt(dev_handle_.get(), kEndpoint, kReadTimeout);
249 transfer1_->Submit();
250 transfer2_->Submit();
251
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700252 data()->count = 0;
253 }
254
Brian Silverman9a574d52013-03-31 00:53:53 -0700255 static void StaticTransferCallback(libusb::Transfer *transfer, void *self) {
256 static_cast<GyroSensorReceiver *>(self)->TransferCallback(transfer);
257 }
258 void TransferCallback(libusb::Transfer *transfer) {
259 if (transfer->status() == LIBUSB_TRANSFER_COMPLETED) {
260 LOG(DEBUG, "transfer %p completed\n", transfer);
261 completed_transfer_ = transfer;
262 } else if (transfer->status() == LIBUSB_TRANSFER_TIMED_OUT) {
263 LOG(WARNING, "transfer %p timed out\n", transfer);
264 } else if (transfer->status() == LIBUSB_TRANSFER_CANCELLED) {
265 LOG(DEBUG, "transfer %p cancelled\n", transfer);
266 } else {
267 LOG(FATAL, "transfer %p has status %d\n", transfer, transfer->status());
268 }
269 transfer->Submit();
270 }
271
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700272 virtual void Synchronized(::aos::time::Time start_time) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700273 // Subtract off how many packets it read while synchronizing from the time.
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700274 start_time_ = start_time -
275 ::aos::sensors::kSensorSendFrequency * data()->count;
276 }
277
278 ::std::unique_ptr<LibUSBDeviceHandle> dev_handle_;
Brian Silverman9a574d52013-03-31 00:53:53 -0700279 ::std::unique_ptr<libusb::Transfer> transfer1_, transfer2_;
280 // Temporary variable for holding a completed transfer to communicate that
281 // information from the callback to the code that wants it.
282 libusb::Transfer *completed_transfer_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700283
284 ::aos::time::Time start_time_;
285
286 LibUSB libusb_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700287};
288
289} // namespace frc971
290
291int main() {
292 ::aos::Init();
293 ::frc971::GyroSensorUnpacker unpacker;
294 ::frc971::GyroSensorReceiver receiver(&unpacker);
295 while (true) {
296 receiver.RunIteration();
297 }
298 ::aos::Cleanup();
299}