blob: 2a522c5115399837e39833809c17bb6a00c3889e [file] [log] [blame]
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07001#include "aos/atom_code/init.h"
2#include "aos/common/logging/logging.h"
Brian Silvermanf4937f62013-10-16 10:32:00 -07003#include "aos/common/util/wrapping_counter.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07004
5#include "frc971/control_loops/drivetrain/drivetrain.q.h"
6#include "frc971/control_loops/wrist/wrist_motor.q.h"
7#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
8#include "frc971/control_loops/index/index_motor.q.h"
9#include "frc971/control_loops/shooter/shooter_motor.q.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070010#include "frc971/queues/GyroAngle.q.h"
Brian Silverman1e869f32013-10-25 18:00:20 -070011#include "frc971/input/usb_receiver.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070012
13#ifndef M_PI
14#define M_PI 3.14159265358979323846
15#endif
16
17using ::frc971::control_loops::drivetrain;
18using ::frc971::control_loops::wrist;
19using ::frc971::control_loops::angle_adjust;
20using ::frc971::control_loops::shooter;
21using ::frc971::control_loops::index_loop;
22using ::frc971::sensors::gyro;
Brian Silvermanf4937f62013-10-16 10:32:00 -070023using ::aos::util::WrappingCounter;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070024
25namespace frc971 {
26namespace {
27
28inline double drivetrain_translate(int32_t in) {
29 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
30 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
31 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
32}
33
34inline double wrist_translate(int32_t in) {
35 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
36 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
37}
38
39inline double angle_adjust_translate(int32_t in) {
40 static const double kCableDiameter = 0.060;
41 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
42 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
43 (2 * M_PI);
44}
45
46inline double shooter_translate(int32_t in) {
47 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
48 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
49}
50
51inline double index_translate(int32_t in) {
52 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) *
53 (1.0) /*gears*/ * (2 * M_PI);
54}
55
Brian Silverman74acd622013-10-26 14:47:14 -070056// Translates values from the ADC into voltage.
57inline double adc_translate(uint16_t in) {
58 static const double kVRefN = 0;
59 static const double kVRefP = 3.3;
60 static const int kMaximumValue = 0x3FF;
61 return kVRefN +
62 (static_cast<double>(in) / static_cast<double>(kMaximumValue) *
63 (kVRefP - kVRefN));
64}
65
Brian Silvermana280ae02013-10-28 18:21:15 -070066inline double gyro_translate(int64_t in) {
67 return in / 16.0 / 1000.0 / (180.0 / M_PI);
68}
69
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070070} // namespace
71
Brian Silverman1e869f32013-10-25 18:00:20 -070072class GyroSensorReceiver : public USBReceiver {
Brian Silvermana280ae02013-10-28 18:21:15 -070073 public:
74 GyroSensorReceiver() : USBReceiver(2) {}
Brian Silvermanf4937f62013-10-16 10:32:00 -070075
Brian Silvermana280ae02013-10-28 18:21:15 -070076 virtual void ProcessData(const ::aos::time::Time &/*timestamp*/) override {
Brian Silvermanf4937f62013-10-16 10:32:00 -070077 gyro.MakeWithBuilder()
Brian Silvermana280ae02013-10-28 18:21:15 -070078 .angle(gyro_translate(data()->gyro_angle))
Brian Silvermanf4937f62013-10-16 10:32:00 -070079 .Send();
80
81 drivetrain.position.MakeWithBuilder()
82 .right_encoder(drivetrain_translate(data()->main.right_drive))
83 .left_encoder(-drivetrain_translate(data()->main.left_drive))
84 .Send();
85
86 wrist.position.MakeWithBuilder()
87 .pos(wrist_translate(data()->main.wrist))
Brian Silvermancaeed862013-10-25 17:32:19 -070088 .hall_effect(data()->main.wrist_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070089 .calibration(wrist_translate(data()->main.capture_wrist_rise))
90 .Send();
91
92 angle_adjust.position.MakeWithBuilder()
93 .angle(angle_adjust_translate(data()->main.shooter_angle))
Brian Silvermancaeed862013-10-25 17:32:19 -070094 .bottom_hall_effect(data()->main.angle_adjust_bottom_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070095 .middle_hall_effect(false)
96 .bottom_calibration(angle_adjust_translate(
97 data()->main.capture_shooter_angle_rise))
98 .middle_calibration(angle_adjust_translate(
99 0))
100 .Send();
101
102 shooter.position.MakeWithBuilder()
103 .position(shooter_translate(data()->main.shooter))
104 .Send();
105
106 index_loop.position.MakeWithBuilder()
107 .index_position(index_translate(data()->main.indexer))
Brian Silvermancaeed862013-10-25 17:32:19 -0700108 .top_disc_detect(data()->main.top_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700109 .top_disc_posedge_count(top_rise_.Update(data()->main.top_rise_count))
110 .top_disc_posedge_position(
111 index_translate(data()->main.capture_top_rise))
112 .top_disc_negedge_count(top_fall_.Update(data()->main.top_fall_count))
113 .top_disc_negedge_position(
114 index_translate(data()->main.capture_top_fall))
Brian Silvermancaeed862013-10-25 17:32:19 -0700115 .bottom_disc_detect(data()->main.bottom_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700116 .bottom_disc_posedge_count(
117 bottom_rise_.Update(data()->main.bottom_rise_count))
118 .bottom_disc_negedge_count(
119 bottom_fall_.Update(data()->main.bottom_fall_count))
120 .bottom_disc_negedge_wait_position(index_translate(
121 data()->main.capture_bottom_fall_delay))
122 .bottom_disc_negedge_wait_count(
123 bottom_fall_delay_.Update(data()->main.bottom_fall_delay_count))
124 .loader_top(data()->main.loader_top)
125 .loader_bottom(data()->main.loader_bottom)
126 .Send();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700127 }
128
Brian Silvermanf4937f62013-10-16 10:32:00 -0700129 WrappingCounter top_rise_;
130 WrappingCounter top_fall_;
131 WrappingCounter bottom_rise_;
132 WrappingCounter bottom_fall_delay_;
133 WrappingCounter bottom_fall_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700134};
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700135
136} // namespace frc971
137
138int main() {
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700139 ::aos::Init(frc971::USBReceiver::kRelativePriority);
Brian Silvermanf4937f62013-10-16 10:32:00 -0700140 ::frc971::GyroSensorReceiver receiver;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700141 while (true) {
142 receiver.RunIteration();
143 }
144 ::aos::Cleanup();
145}