blob: 5f0ce5738815fea7beb1ccb266e103958213ea7d [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 Silverman2e0dcfd2013-03-30 22:44:40 -070066} // namespace
67
Brian Silverman1e869f32013-10-25 18:00:20 -070068class GyroSensorReceiver : public USBReceiver {
69 virtual void ProcessData() override {
Brian Silverman74acd622013-10-26 14:47:14 -070070 if (data()->robot_id != 2) {
Brian Silvermanf4937f62013-10-16 10:32:00 -070071 LOG(ERROR, "gyro board sent data for robot id %hhd!"
72 " dip switches are %x\n",
73 data()->robot_id, data()->base_status & 0xF);
74 return;
75 } else {
76 LOG(DEBUG, "processing a packet dip switches %x\n",
77 data()->base_status & 0xF);
78 }
79
Brian Silvermanf4937f62013-10-16 10:32:00 -070080 gyro.MakeWithBuilder()
81 .angle(data()->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
82 .Send();
83
84 drivetrain.position.MakeWithBuilder()
85 .right_encoder(drivetrain_translate(data()->main.right_drive))
86 .left_encoder(-drivetrain_translate(data()->main.left_drive))
87 .Send();
88
89 wrist.position.MakeWithBuilder()
90 .pos(wrist_translate(data()->main.wrist))
Brian Silvermancaeed862013-10-25 17:32:19 -070091 .hall_effect(data()->main.wrist_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070092 .calibration(wrist_translate(data()->main.capture_wrist_rise))
93 .Send();
94
95 angle_adjust.position.MakeWithBuilder()
96 .angle(angle_adjust_translate(data()->main.shooter_angle))
Brian Silvermancaeed862013-10-25 17:32:19 -070097 .bottom_hall_effect(data()->main.angle_adjust_bottom_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070098 .middle_hall_effect(false)
99 .bottom_calibration(angle_adjust_translate(
100 data()->main.capture_shooter_angle_rise))
101 .middle_calibration(angle_adjust_translate(
102 0))
103 .Send();
104
105 shooter.position.MakeWithBuilder()
106 .position(shooter_translate(data()->main.shooter))
107 .Send();
108
109 index_loop.position.MakeWithBuilder()
110 .index_position(index_translate(data()->main.indexer))
Brian Silvermancaeed862013-10-25 17:32:19 -0700111 .top_disc_detect(data()->main.top_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700112 .top_disc_posedge_count(top_rise_.Update(data()->main.top_rise_count))
113 .top_disc_posedge_position(
114 index_translate(data()->main.capture_top_rise))
115 .top_disc_negedge_count(top_fall_.Update(data()->main.top_fall_count))
116 .top_disc_negedge_position(
117 index_translate(data()->main.capture_top_fall))
Brian Silvermancaeed862013-10-25 17:32:19 -0700118 .bottom_disc_detect(data()->main.bottom_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700119 .bottom_disc_posedge_count(
120 bottom_rise_.Update(data()->main.bottom_rise_count))
121 .bottom_disc_negedge_count(
122 bottom_fall_.Update(data()->main.bottom_fall_count))
123 .bottom_disc_negedge_wait_position(index_translate(
124 data()->main.capture_bottom_fall_delay))
125 .bottom_disc_negedge_wait_count(
126 bottom_fall_delay_.Update(data()->main.bottom_fall_delay_count))
127 .loader_top(data()->main.loader_top)
128 .loader_bottom(data()->main.loader_bottom)
129 .Send();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700130 }
131
Brian Silvermanf4937f62013-10-16 10:32:00 -0700132 WrappingCounter top_rise_;
133 WrappingCounter top_fall_;
134 WrappingCounter bottom_rise_;
135 WrappingCounter bottom_fall_delay_;
136 WrappingCounter bottom_fall_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700137};
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700138
139} // namespace frc971
140
141int main() {
142 ::aos::Init();
Brian Silvermanf4937f62013-10-16 10:32:00 -0700143 ::frc971::GyroSensorReceiver receiver;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700144 while (true) {
145 receiver.RunIteration();
146 }
147 ::aos::Cleanup();
148}