blob: e15f603f20036733e4f072d5920471b76abe16ac [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) {
Brian Silvermanf92396c2013-09-12 20:13:13 -070085 if (data->robot_id != 0) {
86 LOG(ERROR, "gyro board sent data for robot id %hhd!"
87 " dip switches are %x\n", data->robot_id, data->dip_switches);
88 return;
89 } else {
90 LOG(DEBUG, "processing a packet dip switches %x\n", data->dip_switches);
91 }
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070092
93 static ::aos::time::Time last_time = ::aos::time::Time::Now();
94 if ((last_time - ::aos::time::Time::Now()) >
95 ::aos::time::Time::InMS(0.00205)) {
96 LOG(INFO, "missed one\n");
97 }
98
99 gyro.MakeWithBuilder()
100 .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
101 .Send();
102
Brian Silvermanf92396c2013-09-12 20:13:13 -0700103 UpdateWrappingCounter(data->main.top_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700104 &last_top_rise_count_, &top_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700105 UpdateWrappingCounter(data->main.top_fall_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700106 &last_top_fall_count_, &top_fall_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700107 UpdateWrappingCounter(data->main.bottom_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700108 &last_bottom_rise_count_, &bottom_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700109 UpdateWrappingCounter(data->main.bottom_fall_delay_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700110 &last_bottom_fall_delay_count_, &bottom_fall_delay_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700111 UpdateWrappingCounter(data->main.bottom_fall_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700112 &last_bottom_fall_count_, &bottom_fall_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700113 UpdateWrappingCounter(data->main.wrist_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700114 &last_wrist_rise_count_, &wrist_rise_count_);
Brian Silvermanf92396c2013-09-12 20:13:13 -0700115 UpdateWrappingCounter(data->main.shooter_angle_rise_count,
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700116 &last_shooter_angle_rise_count_, &shooter_angle_rise_count_);
117
118 drivetrain.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700119 .right_encoder(drivetrain_translate(data->main.right_drive))
120 .left_encoder(-drivetrain_translate(data->main.left_drive))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700121 .Send();
122
123 wrist.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700124 .pos(wrist_translate(data->main.wrist))
125 .hall_effect(!data->main.wrist_hall_effect)
126 .calibration(wrist_translate(data->main.capture_wrist_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700127 .Send();
128
129 angle_adjust.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700130 .angle(angle_adjust_translate(data->main.shooter_angle))
131 .bottom_hall_effect(!data->main.angle_adjust_bottom_hall_effect)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700132 .middle_hall_effect(false)
133 .bottom_calibration(angle_adjust_translate(
Brian Silvermanf92396c2013-09-12 20:13:13 -0700134 data->main.capture_shooter_angle_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700135 .middle_calibration(angle_adjust_translate(
136 0))
137 .Send();
138
139 shooter.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700140 .position(shooter_translate(data->main.shooter))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700141 .Send();
142
143 index_loop.position.MakeWithBuilder()
Brian Silvermanf92396c2013-09-12 20:13:13 -0700144 .index_position(index_translate(data->main.indexer))
145 .top_disc_detect(!data->main.top_disc)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700146 .top_disc_posedge_count(top_rise_count_)
Brian Silvermanf92396c2013-09-12 20:13:13 -0700147 .top_disc_posedge_position(index_translate(data->main.capture_top_rise))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700148 .top_disc_negedge_count(top_fall_count_)
Brian Silvermanf92396c2013-09-12 20:13:13 -0700149 .top_disc_negedge_position(index_translate(data->main.capture_top_fall))
150 .bottom_disc_detect(!data->main.bottom_disc)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700151 .bottom_disc_posedge_count(bottom_rise_count_)
152 .bottom_disc_negedge_count(bottom_fall_count_)
153 .bottom_disc_negedge_wait_position(index_translate(
Brian Silvermanf92396c2013-09-12 20:13:13 -0700154 data->main.capture_bottom_fall_delay))
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700155 .bottom_disc_negedge_wait_count(bottom_fall_delay_count_)
Brian Silverman004ec812013-09-26 15:47:58 -0700156 .loader_top(data->loader_top)
157 .loader_bottom(data->loader_bottom)
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700158 .Send();
159 }
160
161 private:
162 void UpdateWrappingCounter(
163 uint8_t current, uint8_t *last, int32_t *counter) {
164 if (*last > current) {
165 *counter += 0x100;
166 }
167 *counter = (*counter & 0xffffff00) | current;
168 *last = current;
169 }
170
171 int32_t top_rise_count_;
172 uint8_t last_top_rise_count_;
173 int32_t top_fall_count_;
174 uint8_t last_top_fall_count_;
175 int32_t bottom_rise_count_;
176 uint8_t last_bottom_rise_count_;
177 int32_t bottom_fall_delay_count_;
178 uint8_t last_bottom_fall_delay_count_;
179 int32_t bottom_fall_count_;
180 uint8_t last_bottom_fall_count_;
181 int32_t wrist_rise_count_;
182 uint8_t last_wrist_rise_count_;
183 int32_t shooter_angle_rise_count_;
184 uint8_t last_shooter_angle_rise_count_;
185};
186
187class GyroSensorReceiver :
188 public ::aos::sensors::SensorReceiver<GyroBoardData> {
189 public:
190 GyroSensorReceiver(
191 ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker)
192 : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker),
193 start_time_(0, 0) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700194 static_assert(sizeof(GyroBoardData) <= kDataLength,
195 "the buffer will be too small");
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700196 }
197
198 private:
199 static const unsigned char kEndpoint = 0x81;
200 // in ms
201 // 0 is unlimited
202 static const unsigned int kReadTimeout = 1000;
203
Brian Silverman93871ee2013-09-14 18:15:28 -0700204 static constexpr glibusb::VendorProductId kDeviceId =
205 glibusb::VendorProductId(0x1424 /* vendor ID */,
206 0xd243 /* product ID */);
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700207
Brian Silverman9a574d52013-03-31 00:53:53 -0700208 // How big of a buffer to give the function.
209 static const size_t kDataLength = 64;
210
Brian Silverman93871ee2013-09-14 18:15:28 -0700211 virtual bool DoReceiveData() {
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700212 // Loop and then return once we get a good one.
213 while (true) {
Brian Silverman93871ee2013-09-14 18:15:28 -0700214 using glibusb::UsbEndpoint;
215 UsbEndpoint::IoStatus result =
216 endpoint_->ReadAtMostWithTimeout(sizeof(GyroBoardData),
217 kReadTimeout,
218 &buffer_);
219 switch (result) {
220 case UsbEndpoint::kSuccess:
221 break;
222 case UsbEndpoint::kTimeout:
223 LOG(WARNING, "read timed out\n");
224 continue;
225 case UsbEndpoint::kNoDevice:
226 LOG(ERROR, "no device\n");
227 return true;
228 case UsbEndpoint::kUnknown:
229 case UsbEndpoint::kFail:
230 case UsbEndpoint::kAbort:
231 LOG(ERROR, "read failed\n");
232 continue;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700233 }
Brian Silverman93871ee2013-09-14 18:15:28 -0700234 if (buffer_.Length() < sizeof(GyroBoardData)) {
235 LOG(ERROR, "read %zd bytes instead of at least %zd\n",
236 buffer_.Length(), sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700237 continue;
238 }
239
Brian Silverman93871ee2013-09-14 18:15:28 -0700240 memcpy(&data()->values, buffer_.GetBufferPointer(sizeof(GyroBoardData)),
Brian Silverman9a574d52013-03-31 00:53:53 -0700241 sizeof(GyroBoardData));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700242 if (data()->count == 0) {
243 start_time_ = ::aos::time::Time::Now();
244 data()->count = 1;
245 } else {
246 ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_;
247 data()->count = static_cast<int32_t>(
248 (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5);
249 }
Brian Silverman93871ee2013-09-14 18:15:28 -0700250 return false;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700251 }
252 }
253
254 virtual void Reset() {
Brian Silverman93871ee2013-09-14 18:15:28 -0700255 device_ = ::std::unique_ptr<glibusb::UsbDevice>(
256 libusb_.FindSingleMatchingDeviceOrLose(kDeviceId));
257 CHECK(device_);
258 endpoint_ = ::std::unique_ptr<glibusb::UsbInEndpoint>(
259 device_->InEndpoint(kEndpoint));
Brian Silverman9a574d52013-03-31 00:53:53 -0700260
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700261 data()->count = 0;
262 }
263
264 virtual void Synchronized(::aos::time::Time start_time) {
Brian Silverman9a574d52013-03-31 00:53:53 -0700265 // Subtract off how many packets it read while synchronizing from the time.
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700266 start_time_ = start_time -
267 ::aos::sensors::kSensorSendFrequency * data()->count;
268 }
269
Brian Silverman93871ee2013-09-14 18:15:28 -0700270 ::std::unique_ptr<glibusb::UsbDevice> device_;
271 ::std::unique_ptr<glibusb::UsbInEndpoint> endpoint_;
272 glibusb::Buffer buffer_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700273
274 ::aos::time::Time start_time_;
275
Brian Silverman93871ee2013-09-14 18:15:28 -0700276 glibusb::Libusb libusb_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700277};
Brian Silverman93871ee2013-09-14 18:15:28 -0700278constexpr glibusb::VendorProductId GyroSensorReceiver::kDeviceId;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700279
280} // namespace frc971
281
282int main() {
283 ::aos::Init();
284 ::frc971::GyroSensorUnpacker unpacker;
285 ::frc971::GyroSensorReceiver receiver(&unpacker);
286 while (true) {
287 receiver.RunIteration();
288 }
289 ::aos::Cleanup();
290}