blob: 06e760ce34df8f9341e510aa0456f550d8235bfc [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/control_loop/Timing.h"
8#include "aos/common/time.h"
9
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/control_loops/wrist/wrist_motor.q.h"
12#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
13#include "frc971/control_loops/index/index_motor.q.h"
14#include "frc971/control_loops/shooter/shooter_motor.q.h"
15#include "frc971/input/gyro_board_data.h"
16#include "gyro_board/src/libusb-driver/libusb_wrap.h"
17#include "frc971/queues/GyroAngle.q.h"
18
19#ifndef M_PI
20#define M_PI 3.14159265358979323846
21#endif
22
23using ::frc971::control_loops::drivetrain;
24using ::frc971::control_loops::wrist;
25using ::frc971::control_loops::angle_adjust;
26using ::frc971::control_loops::shooter;
27using ::frc971::control_loops::index_loop;
28using ::frc971::sensors::gyro;
29
30namespace frc971 {
31namespace {
32
33inline double drivetrain_translate(int32_t in) {
34 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
35 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
36 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
37}
38
39inline double wrist_translate(int32_t in) {
40 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
41 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
42}
43
44inline double angle_adjust_translate(int32_t in) {
45 static const double kCableDiameter = 0.060;
46 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
47 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
48 (2 * M_PI);
49}
50
51inline double shooter_translate(int32_t in) {
52 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
53 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
54}
55
56inline double index_translate(int32_t in) {
57 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) *
58 (1.0) /*gears*/ * (2 * M_PI);
59}
60
61} // namespace
62
63class GyroBoardReader {
64 public:
65 GyroBoardReader()
66 : top_rise_count_(0),
67 last_top_rise_count_(0),
68 top_fall_count_(0),
69 last_top_fall_count_(0),
70 bottom_rise_count_(0),
71 last_bottom_rise_count_(0),
72 bottom_fall_delay_count_(0),
73 last_bottom_fall_delay_count_(0),
74 bottom_fall_count_(0),
75 last_bottom_fall_count_(0),
76 wrist_rise_count_(0),
77 last_wrist_rise_count_(0),
78 shooter_angle_rise_count_(0),
79 last_shooter_angle_rise_count_(0) {
80 }
81
82 void Run() {
83 LibUSB libusb;
84
85 dev_handle_ = ::std::unique_ptr<LibUSBDeviceHandle>(
86 libusb.FindDeviceWithVIDPID(kVid, kPid));
87 if (!dev_handle_) {
88 LOG(ERROR, "couldn't find device. exiting\n");
89 exit(1);
90 }
91
92 uint8_t data[64];
93 GyroBoardData *real_data;
94 static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit");
95
96 uint8_t *data_pointer = data;
97 memcpy(&real_data, &data_pointer, sizeof(data_pointer));
98 while (true) {
99 if (false) {
100 // Theoretically need -3ms of offset. Using a slightly larger one to avoid
101 // missing the first control loop in the worst case.
102 ::aos::time::PhasedLoop10MS(
103 ::aos::time::Time::InSeconds(-0.0031).ToUSec());
104 LOG(DEBUG, "starting now\n");
105
106 // Read 2 to make sure that we get fresh data.
107 if (!ReadPacket(data, sizeof(data))) continue;
108 //LOG(DEBUG, "in between\n");
109 if (!ReadPacket(data, sizeof(data))) continue;
110 } else {
111 if (!ReadPacket(data, sizeof(data))) continue;
112
113 ProcessData(real_data);
114 }
115 }
116 }
117
118 private:
119 static const unsigned char kEndpoint = 0x81;
120 // in ms
121 // 0 is unlimited
122 static const unsigned int kReadTimeout = 1000;
123
124 // vendor ID
125 static const int32_t kVid = 0x1424;
126 // product ID
127 static const int32_t kPid = 0xd243;
128
129 // Returns whether it read a good packet.
130 bool ReadPacket(uint8_t *data, size_t data_size) {
131 int read_bytes;
132 int r = dev_handle_->interrupt_transfer(
133 kEndpoint, data, data_size, &read_bytes, kReadTimeout);
134
135 if (r != 0) {
136 if (r == LIBUSB_ERROR_TIMEOUT) {
137 LOG(ERROR, "read timed out\n");
138 return false;
139 }
140 LOG(FATAL, "libusb gave error %d\n", r);
141 }
142
143 if (read_bytes < static_cast<ssize_t>(sizeof(GyroBoardData))) {
144 LOG(ERROR, "read %d bytes instead of at least %zd\n",
145 read_bytes, sizeof(GyroBoardData));
146 return false;
147 }
148
149 return true;
150 }
151
152 void UpdateWrappingCounter(
153 uint8_t current, uint8_t *last, int32_t *counter) {
154 if (*last > current) {
155 *counter += 0x100;
156 }
157 *counter = (*counter & 0xffffff00) | current;
158 *last = current;
159 }
160
161 void ProcessData(GyroBoardData *data) {
162 data->NetworkToHost();
163 LOG(DEBUG, "processing a packet\n");
164 static ::aos::time::Time last_time = ::aos::time::Time::Now();
165 if ((last_time - ::aos::time::Time::Now()) >
166 ::aos::time::Time::InMS(0.00205)) {
167 LOG(INFO, "missed one\n");
168 }
169
170 gyro.MakeWithBuilder()
171 .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
172 .Send();
173
174 UpdateWrappingCounter(data->top_rise_count,
175 &last_top_rise_count_, &top_rise_count_);
176 UpdateWrappingCounter(data->top_fall_count,
177 &last_top_fall_count_, &top_fall_count_);
178 UpdateWrappingCounter(data->bottom_rise_count,
179 &last_bottom_rise_count_, &bottom_rise_count_);
180 UpdateWrappingCounter(data->bottom_fall_delay_count,
181 &last_bottom_fall_delay_count_, &bottom_fall_delay_count_);
182 UpdateWrappingCounter(data->bottom_fall_count,
183 &last_bottom_fall_count_, &bottom_fall_count_);
184 UpdateWrappingCounter(data->wrist_rise_count,
185 &last_wrist_rise_count_, &wrist_rise_count_);
186 UpdateWrappingCounter(data->shooter_angle_rise_count,
187 &last_shooter_angle_rise_count_, &shooter_angle_rise_count_);
188
189 drivetrain.position.MakeWithBuilder()
190 .right_encoder(drivetrain_translate(data->right_drive))
191 .left_encoder(-drivetrain_translate(data->left_drive))
192 .Send();
193
194 wrist.position.MakeWithBuilder()
195 .pos(wrist_translate(data->wrist))
196 .hall_effect(data->wrist_hall_effect)
197 .calibration(wrist_translate(data->capture_wrist_rise))
198 .Send();
199
200 angle_adjust.position.MakeWithBuilder()
201 .angle(angle_adjust_translate(data->shooter_angle))
202 .bottom_hall_effect(data->angle_adjust_bottom_hall_effect)
203 .middle_hall_effect(false)
204 .bottom_calibration(angle_adjust_translate(
205 data->capture_shooter_angle_rise))
206 .middle_calibration(angle_adjust_translate(
207 0))
208 .Send();
209
210 shooter.position.MakeWithBuilder()
211 .position(shooter_translate(data->shooter))
212 .Send();
213
214 index_loop.position.MakeWithBuilder()
215 .index_position(index_translate(data->indexer))
216 .top_disc_detect(data->top_disc)
217 .top_disc_posedge_count(top_rise_count_)
218 .top_disc_posedge_position(index_translate(data->capture_top_rise))
219 .top_disc_negedge_count(top_fall_count_)
220 .top_disc_negedge_position(index_translate(data->capture_top_fall))
221 .bottom_disc_detect(data->bottom_disc)
222 .bottom_disc_posedge_count(bottom_rise_count_)
223 .bottom_disc_negedge_count(bottom_fall_count_)
224 .bottom_disc_negedge_wait_position(index_translate(
225 data->capture_bottom_fall_delay))
226 .bottom_disc_negedge_wait_count(bottom_fall_delay_count_)
227 .Send();
228 }
229
230 ::std::unique_ptr<LibUSBDeviceHandle> dev_handle_;
231
232 int32_t top_rise_count_;
233 uint8_t last_top_rise_count_;
234 int32_t top_fall_count_;
235 uint8_t last_top_fall_count_;
236 int32_t bottom_rise_count_;
237 uint8_t last_bottom_rise_count_;
238 int32_t bottom_fall_delay_count_;
239 uint8_t last_bottom_fall_delay_count_;
240 int32_t bottom_fall_count_;
241 uint8_t last_bottom_fall_count_;
242 int32_t wrist_rise_count_;
243 uint8_t last_wrist_rise_count_;
244 int32_t shooter_angle_rise_count_;
245 uint8_t last_shooter_angle_rise_count_;
246};
247
248} // namespace frc971
249
250int main() {
251 ::aos::Init();
252 ::frc971::GyroBoardReader reader;
253 reader.Run();
254 ::aos::Cleanup();
255 return 0;
256}