blob: f53f15b152f71538f73667f8d65afad8c9d7c63c [file] [log] [blame]
Daniel Petti3fe36542013-09-25 04:18:24 +00001#include "aos/atom_code/init.h"
2#include "aos/common/logging/logging.h"
Daniel Petti0663d302013-10-27 05:39:39 +00003#include "aos/common/util/wrapping_counter.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00004
Daniel Petti1f448512013-10-19 19:35:55 +00005#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00006#include "frc971/queues/GyroAngle.q.h"
Daniel Petti0663d302013-10-27 05:39:39 +00007#include "frc971/input/usb_receiver.h"
Daniel Petti3fe36542013-09-25 04:18:24 +00008
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
Daniel Petti1f448512013-10-19 19:35:55 +000013using ::bot3::control_loops::drivetrain;
Daniel Petti3fe36542013-09-25 04:18:24 +000014using ::frc971::sensors::gyro;
Daniel Petti0663d302013-10-27 05:39:39 +000015using ::aos::util::WrappingCounter;
16using ::frc971::USBReceiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000017
Daniel Petti1f448512013-10-19 19:35:55 +000018namespace bot3 {
Daniel Petti3fe36542013-09-25 04:18:24 +000019namespace {
20
21inline double drivetrain_translate(int32_t in) {
22 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
23 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
24 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
25}
26
27inline double wrist_translate(int32_t in) {
28 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
29 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
30}
31
32inline double angle_adjust_translate(int32_t in) {
33 static const double kCableDiameter = 0.060;
34 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
35 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
36 (2 * M_PI);
37}
38
39inline double shooter_translate(int32_t in) {
40 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
41 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
42}
43
44inline double index_translate(int32_t in) {
45 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) *
46 (1.0) /*gears*/ * (2 * M_PI);
47}
48
49} // namespace
50
Daniel Petti0663d302013-10-27 05:39:39 +000051class GyroSensorReceiver : public USBReceiver {
52 virtual void ProcessData() override {
53 if (data()->robot_id != 0) {
54 LOG(ERROR, "gyro board sent data for robot id %hhd!"
55 " dip switches are %x\n",
56 data()->robot_id, data()->base_status & 0xF);
57 return;
58 } else {
59 LOG(DEBUG, "processing a packet dip switches %x\n",
60 data()->base_status & 0xF);
Daniel Petti3fe36542013-09-25 04:18:24 +000061 }
62
63 gyro.MakeWithBuilder()
Daniel Petti0663d302013-10-27 05:39:39 +000064 .angle(data()->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
Daniel Petti3fe36542013-09-25 04:18:24 +000065 .Send();
66
Daniel Petti3fe36542013-09-25 04:18:24 +000067 drivetrain.position.MakeWithBuilder()
Daniel Petti0663d302013-10-27 05:39:39 +000068 .right_encoder(drivetrain_translate(data()->main.right_drive))
69 .left_encoder(-drivetrain_translate(data()->main.left_drive))
Daniel Petti3fe36542013-09-25 04:18:24 +000070 .Send();
Daniel Petti3fe36542013-09-25 04:18:24 +000071 }
72
Daniel Petti0663d302013-10-27 05:39:39 +000073 WrappingCounter top_rise_;
74 WrappingCounter top_fall_;
75 WrappingCounter bottom_rise_;
76 WrappingCounter bottom_fall_delay_;
77 WrappingCounter bottom_fall_;
Daniel Petti3fe36542013-09-25 04:18:24 +000078};
79
Daniel Petti1f448512013-10-19 19:35:55 +000080} // namespace bot3
Daniel Petti3fe36542013-09-25 04:18:24 +000081
82int main() {
83 ::aos::Init();
Daniel Petti0663d302013-10-27 05:39:39 +000084 ::bot3::GyroSensorReceiver receiver;
Daniel Petti3fe36542013-09-25 04:18:24 +000085 while (true) {
86 receiver.RunIteration();
87 }
88 ::aos::Cleanup();
89}