blob: 6553e3ae2e2ac16301e0c8029f917a0f881f1be3 [file] [log] [blame]
Daniel Petti3fe36542013-09-25 04:18:24 +00001#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
Daniel Petti1f448512013-10-19 19:35:55 +000011#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Daniel Petti3fe36542013-09-25 04:18:24 +000012#include "frc971/input/gyro_board_data.h"
13#include "gyro_board/src/libusb-driver/libusb_wrap.h"
14#include "frc971/queues/GyroAngle.q.h"
15
16#ifndef M_PI
17#define M_PI 3.14159265358979323846
18#endif
19
Daniel Petti1f448512013-10-19 19:35:55 +000020using ::bot3::control_loops::drivetrain;
Daniel Petti3fe36542013-09-25 04:18:24 +000021using ::frc971::sensors::gyro;
Daniel Petti1f448512013-10-19 19:35:55 +000022using ::frc971::GyroBoardData;
Daniel Petti3fe36542013-09-25 04:18:24 +000023
Daniel Petti1f448512013-10-19 19:35:55 +000024namespace bot3 {
Daniel Petti3fe36542013-09-25 04:18:24 +000025namespace {
26
27inline double drivetrain_translate(int32_t in) {
28 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
29 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
30 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
31}
32
33inline double wrist_translate(int32_t in) {
34 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
35 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
36}
37
38inline double angle_adjust_translate(int32_t in) {
39 static const double kCableDiameter = 0.060;
40 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
41 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
42 (2 * M_PI);
43}
44
45inline double shooter_translate(int32_t in) {
46 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
47 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
48}
49
50inline double index_translate(int32_t in) {
51 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) *
52 (1.0) /*gears*/ * (2 * M_PI);
53}
54
55} // namespace
56
57class GyroSensorUnpacker :
58 public ::aos::sensors::SensorUnpackerInterface<GyroBoardData> {
59 public:
60 GyroSensorUnpacker()
61 : top_rise_count_(0),
62 last_top_rise_count_(0),
63 top_fall_count_(0),
64 last_top_fall_count_(0),
65 bottom_rise_count_(0),
66 last_bottom_rise_count_(0),
67 bottom_fall_delay_count_(0),
68 last_bottom_fall_delay_count_(0),
69 bottom_fall_count_(0),
70 last_bottom_fall_count_(0),
71 wrist_rise_count_(0),
72 last_wrist_rise_count_(0),
73 shooter_angle_rise_count_(0),
74 last_shooter_angle_rise_count_(0) {
75 }
76
77 void UnpackFrom(GyroBoardData *data) {
78 data->NetworkToHost();
79 LOG(DEBUG, "processing a packet\n");
80
81 static ::aos::time::Time last_time = ::aos::time::Time::Now();
82 if ((last_time - ::aos::time::Time::Now()) >
83 ::aos::time::Time::InMS(0.00205)) {
84 LOG(INFO, "missed one\n");
85 }
86
87 gyro.MakeWithBuilder()
88 .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
89 .Send();
90
91 UpdateWrappingCounter(data->top_rise_count,
92 &last_top_rise_count_, &top_rise_count_);
93 UpdateWrappingCounter(data->top_fall_count,
94 &last_top_fall_count_, &top_fall_count_);
95 UpdateWrappingCounter(data->bottom_rise_count,
96 &last_bottom_rise_count_, &bottom_rise_count_);
97 UpdateWrappingCounter(data->bottom_fall_delay_count,
98 &last_bottom_fall_delay_count_, &bottom_fall_delay_count_);
99 UpdateWrappingCounter(data->bottom_fall_count,
100 &last_bottom_fall_count_, &bottom_fall_count_);
101 UpdateWrappingCounter(data->wrist_rise_count,
102 &last_wrist_rise_count_, &wrist_rise_count_);
103 UpdateWrappingCounter(data->shooter_angle_rise_count,
104 &last_shooter_angle_rise_count_, &shooter_angle_rise_count_);
105
106 drivetrain.position.MakeWithBuilder()
107 .right_encoder(drivetrain_translate(data->right_drive))
108 .left_encoder(-drivetrain_translate(data->left_drive))
109 .Send();
Daniel Petti3fe36542013-09-25 04:18:24 +0000110 }
111
112 private:
113 void UpdateWrappingCounter(
114 uint8_t current, uint8_t *last, int32_t *counter) {
115 if (*last > current) {
116 *counter += 0x100;
117 }
118 *counter = (*counter & 0xffffff00) | current;
119 *last = current;
120 }
121
122 int32_t top_rise_count_;
123 uint8_t last_top_rise_count_;
124 int32_t top_fall_count_;
125 uint8_t last_top_fall_count_;
126 int32_t bottom_rise_count_;
127 uint8_t last_bottom_rise_count_;
128 int32_t bottom_fall_delay_count_;
129 uint8_t last_bottom_fall_delay_count_;
130 int32_t bottom_fall_count_;
131 uint8_t last_bottom_fall_count_;
132 int32_t wrist_rise_count_;
133 uint8_t last_wrist_rise_count_;
134 int32_t shooter_angle_rise_count_;
135 uint8_t last_shooter_angle_rise_count_;
136};
137
138class GyroSensorReceiver :
139 public ::aos::sensors::SensorReceiver<GyroBoardData> {
140 public:
141 GyroSensorReceiver(
142 ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker)
143 : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker),
144 start_time_(0, 0) {
145 static_assert(sizeof(GyroBoardData) <= kDataLength,
146 "the buffer will be too small");
147 }
148
149 private:
150 static const unsigned char kEndpoint = 0x81;
151 // in ms
152 // 0 is unlimited
153 static const unsigned int kReadTimeout = 1000;
154
155 // vendor ID
156 static const int32_t kVid = 0x1424;
157 // product ID
158 static const int32_t kPid = 0xd243;
159
160 // How big of a buffer to give the function.
161 static const size_t kDataLength = 64;
162
163 virtual void DoReceiveData() {
164 // Loop and then return once we get a good one.
165 while (true) {
166 completed_transfer_ = NULL;
167 while (completed_transfer_ == NULL) {
168 libusb_.HandleEvents();
169 }
170 LOG(DEBUG, "processing transfer %p\n", completed_transfer_);
171
172 if (completed_transfer_->read_bytes() <
173 static_cast<ssize_t>(sizeof(GyroBoardData))) {
174 LOG(ERROR, "read %d bytes instead of at least %zd\n",
175 completed_transfer_->read_bytes(), sizeof(GyroBoardData));
176 continue;
177 }
178
179 memcpy(&data()->values, completed_transfer_->data(),
180 sizeof(GyroBoardData));
181 if (data()->count == 0) {
182 start_time_ = ::aos::time::Time::Now();
183 data()->count = 1;
184 } else {
185 ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_;
186 data()->count = static_cast<int32_t>(
187 (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5);
188 }
189 return;
190 }
191 }
192
193 virtual void Reset() {
194 transfer1_.reset();
195 transfer2_.reset();
196 dev_handle_ = ::std::unique_ptr<LibUSBDeviceHandle>(
197 libusb_.FindDeviceWithVIDPID(kVid, kPid));
198 if (!dev_handle_) {
199 LOG(ERROR, "couldn't find device. exiting\n");
200 exit(1);
201 }
202 transfer1_ = ::std::unique_ptr<libusb::Transfer>(
203 new libusb::Transfer(kDataLength, StaticTransferCallback, this));
204 transfer2_ = ::std::unique_ptr<libusb::Transfer>(
205 new libusb::Transfer(kDataLength, StaticTransferCallback, this));
206 transfer1_->FillInterrupt(dev_handle_.get(), kEndpoint, kReadTimeout);
207 transfer2_->FillInterrupt(dev_handle_.get(), kEndpoint, kReadTimeout);
208 transfer1_->Submit();
209 transfer2_->Submit();
210
211 data()->count = 0;
212 }
213
214 static void StaticTransferCallback(libusb::Transfer *transfer, void *self) {
215 static_cast<GyroSensorReceiver *>(self)->TransferCallback(transfer);
216 }
217 void TransferCallback(libusb::Transfer *transfer) {
218 if (transfer->status() == LIBUSB_TRANSFER_COMPLETED) {
219 LOG(DEBUG, "transfer %p completed\n", transfer);
220 completed_transfer_ = transfer;
221 } else if (transfer->status() == LIBUSB_TRANSFER_TIMED_OUT) {
222 LOG(WARNING, "transfer %p timed out\n", transfer);
223 } else if (transfer->status() == LIBUSB_TRANSFER_CANCELLED) {
224 LOG(DEBUG, "transfer %p cancelled\n", transfer);
225 } else {
226 LOG(FATAL, "transfer %p has status %d\n", transfer, transfer->status());
227 }
228 transfer->Submit();
229 }
230
231 virtual void Synchronized(::aos::time::Time start_time) {
232 // Subtract off how many packets it read while synchronizing from the time.
233 start_time_ = start_time -
234 ::aos::sensors::kSensorSendFrequency * data()->count;
235 }
236
237 ::std::unique_ptr<LibUSBDeviceHandle> dev_handle_;
238 ::std::unique_ptr<libusb::Transfer> transfer1_, transfer2_;
239 // Temporary variable for holding a completed transfer to communicate that
240 // information from the callback to the code that wants it.
241 libusb::Transfer *completed_transfer_;
242
243 ::aos::time::Time start_time_;
244
245 LibUSB libusb_;
246};
247
Daniel Petti1f448512013-10-19 19:35:55 +0000248} // namespace bot3
Daniel Petti3fe36542013-09-25 04:18:24 +0000249
250int main() {
251 ::aos::Init();
Daniel Petti1f448512013-10-19 19:35:55 +0000252 ::bot3::GyroSensorUnpacker unpacker;
253 ::bot3::GyroSensorReceiver receiver(&unpacker);
Daniel Petti3fe36542013-09-25 04:18:24 +0000254 while (true) {
255 receiver.RunIteration();
256 }
257 ::aos::Cleanup();
258}