blob: db8ce0a9044b4728a5eb7ff269ad0236b74e656e [file] [log] [blame]
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07001#include <memory>
2
3#include "aos/common/inttypes.h"
4#include "aos/atom_code/init.h"
5#include "aos/common/logging/logging.h"
6#include "aos/common/time.h"
7#include "aos/common/sensors/sensor_unpacker.h"
8#include "aos/common/sensors/sensor_receiver.h"
Brian Silverman93871ee2013-09-14 18:15:28 -07009#include "aos/common/glibusb/glibusb.h"
10#include "aos/common/glibusb/gbuffer.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070011
12#include "frc971/control_loops/drivetrain/drivetrain.q.h"
13#include "frc971/control_loops/wrist/wrist_motor.q.h"
14#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
15#include "frc971/control_loops/index/index_motor.q.h"
16#include "frc971/control_loops/shooter/shooter_motor.q.h"
17#include "frc971/input/gyro_board_data.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070018#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
Brian Silverman93871ee2013-09-14 18:15:28 -0700197 static constexpr glibusb::VendorProductId kDeviceId =
198 glibusb::VendorProductId(0x1424 /* vendor ID */,
199 0xd243 /* product ID */);
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700200
Brian Silverman9a574d52013-03-31 00:53:53 -0700201 // How big of a buffer to give the function.
202 static const size_t kDataLength = 64;
203
Brian Silverman93871ee2013-09-14 18:15:28 -0700204 virtual bool DoReceiveData() {
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700205 // Loop and then return once we get a good one.
206 while (true) {
Brian Silverman93871ee2013-09-14 18:15:28 -0700207 using glibusb::UsbEndpoint;
208 UsbEndpoint::IoStatus result =
209 endpoint_->ReadAtMostWithTimeout(sizeof(GyroBoardData),
210 kReadTimeout,
211 &buffer_);
212 switch (result) {
213 case UsbEndpoint::kSuccess:
214 break;
215 case UsbEndpoint::kTimeout:
216 LOG(WARNING, "read timed out\n");
217 continue;
218 case UsbEndpoint::kNoDevice:
219 LOG(ERROR, "no device\n");
220 return true;
221 case UsbEndpoint::kUnknown:
222 case UsbEndpoint::kFail:
223 case UsbEndpoint::kAbort:
224 LOG(ERROR, "read failed\n");
225 continue;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700226 }
Brian Silverman93871ee2013-09-14 18:15:28 -0700227 if (buffer_.Length() < sizeof(GyroBoardData)) {
228 LOG(ERROR, "read %zd bytes instead of at least %zd\n",
229 buffer_.Length(), sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700230 continue;
231 }
232
Brian Silverman93871ee2013-09-14 18:15:28 -0700233 memcpy(&data()->values, buffer_.GetBufferPointer(sizeof(GyroBoardData)),
Brian Silverman9a574d52013-03-31 00:53:53 -0700234 sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700235 if (data()->count == 0) {
236 start_time_ = ::aos::time::Time::Now();
237 data()->count = 1;
238 } else {
239 ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_;
240 data()->count = static_cast<int32_t>(
241 (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5);
242 }
Brian Silverman93871ee2013-09-14 18:15:28 -0700243 return false;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700244 }
245 }
246
247 virtual void Reset() {
Brian Silverman93871ee2013-09-14 18:15:28 -0700248 device_ = ::std::unique_ptr<glibusb::UsbDevice>(
249 libusb_.FindSingleMatchingDeviceOrLose(kDeviceId));
250 CHECK(device_);
251 endpoint_ = ::std::unique_ptr<glibusb::UsbInEndpoint>(
252 device_->InEndpoint(kEndpoint));
Brian Silverman9a574d52013-03-31 00:53:53 -0700253
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700254 data()->count = 0;
255 }
256
257 virtual void Synchronized(::aos::time::Time start_time) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700258 // Subtract off how many packets it read while synchronizing from the time.
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700259 start_time_ = start_time -
260 ::aos::sensors::kSensorSendFrequency * data()->count;
261 }
262
Brian Silverman93871ee2013-09-14 18:15:28 -0700263 ::std::unique_ptr<glibusb::UsbDevice> device_;
264 ::std::unique_ptr<glibusb::UsbInEndpoint> endpoint_;
265 glibusb::Buffer buffer_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700266
267 ::aos::time::Time start_time_;
268
Brian Silverman93871ee2013-09-14 18:15:28 -0700269 glibusb::Libusb libusb_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700270};
Brian Silverman93871ee2013-09-14 18:15:28 -0700271constexpr glibusb::VendorProductId GyroSensorReceiver::kDeviceId;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700272
273} // namespace frc971
274
275int main() {
276 ::aos::Init();
277 ::frc971::GyroSensorUnpacker unpacker;
278 ::frc971::GyroSensorReceiver receiver(&unpacker);
279 while (true) {
280 receiver.RunIteration();
281 }
282 ::aos::Cleanup();
283}