blob: f123b8e09fdaaf5eac249e90dcec248c6beef194 [file] [log] [blame]
Brian Silverman59685152013-03-29 21:37:43 -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
8#include "frc971/control_loops/drivetrain/drivetrain.q.h"
9#include "frc971/control_loops/wrist/wrist_motor.q.h"
10#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
11#include "frc971/control_loops/index/index_motor.q.h"
12#include "frc971/control_loops/shooter/shooter_motor.q.h"
13#include "frc971/input/gyro_board_data.h"
14#include "gyro_board/src/libusb-driver/libusb_wrap.h"
15
16#ifndef M_PI
17#define M_PI 3.14159265358979323846
18#endif
19
20using ::frc971::control_loops::drivetrain;
21using ::frc971::control_loops::wrist;
22using ::frc971::control_loops::angle_adjust;
23using ::frc971::control_loops::shooter;
24using ::frc971::control_loops::index_loop;
25
26namespace frc971 {
27namespace {
28
29inline double drivetrain_translate(int32_t in) {
30 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
31 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
32 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
33}
34
35inline double wrist_translate(int32_t in) {
36 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
37 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
38}
39
40inline double angle_adjust_translate(int32_t in) {
41 static const double kCableDiameter = 0.060;
42 return -static_cast<double>(in) / (256.0 /*cpr*/ * 2.0 /*2x*/) *
43 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
44 (2 * M_PI);
45}
46
47inline double shooter_translate(int32_t in) {
48 return -static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
49 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
50}
51
52inline double index_translate(int32_t in) {
53 return -static_cast<double>(in) / (128.0 /*cpr*/ * 2.0 /*2x*/) *
54 (1.0) /*gears*/ * (2 * M_PI);
55}
56
57} // namespace
58
59class GyroBoardReader {
60 public:
61 void Run() {
62 LibUSB libusb;
63
64 ::std::unique_ptr<LibUSBDeviceHandle> dev_handle(
65 libusb.FindDeviceWithVIDPID(kVid, kPid));
66 if (!dev_handle) {
67 LOG(FATAL, "couldn't find device\n");
68 }
69
70 uint8_t data[64];
71 GyroBoardData *real_data;
72 static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit");
73
74 uint8_t *data_pointer = data;
75 memcpy(&real_data, &data_pointer, sizeof(data_pointer));
76 while (true) {
77 int read_bytes;
78 int r = dev_handle->interrupt_transfer(
79 kEndpoint, data, sizeof(data), &read_bytes, kReadTimeout);
80
81 if (r != 0) {
82 if (r == LIBUSB_ERROR_TIMEOUT) {
83 LOG(ERROR, "read timed out\n");
84 continue;
85 }
86 LOG(FATAL, "libusb gave error %d\n", r);
87 }
88
89 if (read_bytes != sizeof(data)) {
90 LOG(ERROR, "read %d bytes instead of %zu\n",
91 read_bytes, sizeof(data));
92 continue;
93 }
94
95 ProcessData(real_data);
96 }
97 }
98
99 private:
100 static const unsigned char kEndpoint = 0x81;
101 // in ms
102 // 0 is unlimited
103 static const unsigned int kReadTimeout = 1000;
104
105 // vendor ID
106 static const int32_t kVid = 0x1424;
107 // product ID
108 static const int32_t kPid = 0xd243;
109
110 void ProcessData(GyroBoardData *data) {
111 data->NetworkToHost();
112
113 drivetrain.position.MakeWithBuilder()
114 .right_encoder(data->right_drive)
115 .left_encoder(data->left_drive)
116 .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(data->top_rise_count)
142 .top_disc_posedge_position(index_translate(data->capture_top_rise))
143 .top_disc_negedge_count(data->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(data->bottom_rise_count)
147 .bottom_disc_negedge_count(data->bottom_fall_count)
148 .bottom_disc_negedge_wait_position(index_translate(
149 data->capture_bottom_fall_delay))
150 .bottom_disc_negedge_wait_count(data->bottom_fall_delay_count)
151 .Send();
152 }
153};
154
155} // namespace frc971
156
157int main() {
158 ::aos::Init();
159 ::frc971::GyroBoardReader reader;
160 reader.Run();
161 ::aos::Cleanup();
162 return 0;
163}