blob: 42b50c87f63b46c3dc56c80ed6f584d96497a719 [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
56} // namespace
57
Brian Silverman1e869f32013-10-25 18:00:20 -070058class GyroSensorReceiver : public USBReceiver {
59 virtual void ProcessData() override {
Brian Silvermanf4937f62013-10-16 10:32:00 -070060 if (data()->robot_id != 0) {
61 LOG(ERROR, "gyro board sent data for robot id %hhd!"
62 " dip switches are %x\n",
63 data()->robot_id, data()->base_status & 0xF);
64 return;
65 } else {
66 LOG(DEBUG, "processing a packet dip switches %x\n",
67 data()->base_status & 0xF);
68 }
69
Brian Silvermanf4937f62013-10-16 10:32:00 -070070 gyro.MakeWithBuilder()
71 .angle(data()->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI)
72 .Send();
73
74 drivetrain.position.MakeWithBuilder()
75 .right_encoder(drivetrain_translate(data()->main.right_drive))
76 .left_encoder(-drivetrain_translate(data()->main.left_drive))
77 .Send();
78
79 wrist.position.MakeWithBuilder()
80 .pos(wrist_translate(data()->main.wrist))
Brian Silvermancaeed862013-10-25 17:32:19 -070081 .hall_effect(data()->main.wrist_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070082 .calibration(wrist_translate(data()->main.capture_wrist_rise))
83 .Send();
84
85 angle_adjust.position.MakeWithBuilder()
86 .angle(angle_adjust_translate(data()->main.shooter_angle))
Brian Silvermancaeed862013-10-25 17:32:19 -070087 .bottom_hall_effect(data()->main.angle_adjust_bottom_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070088 .middle_hall_effect(false)
89 .bottom_calibration(angle_adjust_translate(
90 data()->main.capture_shooter_angle_rise))
91 .middle_calibration(angle_adjust_translate(
92 0))
93 .Send();
94
95 shooter.position.MakeWithBuilder()
96 .position(shooter_translate(data()->main.shooter))
97 .Send();
98
99 index_loop.position.MakeWithBuilder()
100 .index_position(index_translate(data()->main.indexer))
Brian Silvermancaeed862013-10-25 17:32:19 -0700101 .top_disc_detect(data()->main.top_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700102 .top_disc_posedge_count(top_rise_.Update(data()->main.top_rise_count))
103 .top_disc_posedge_position(
104 index_translate(data()->main.capture_top_rise))
105 .top_disc_negedge_count(top_fall_.Update(data()->main.top_fall_count))
106 .top_disc_negedge_position(
107 index_translate(data()->main.capture_top_fall))
Brian Silvermancaeed862013-10-25 17:32:19 -0700108 .bottom_disc_detect(data()->main.bottom_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700109 .bottom_disc_posedge_count(
110 bottom_rise_.Update(data()->main.bottom_rise_count))
111 .bottom_disc_negedge_count(
112 bottom_fall_.Update(data()->main.bottom_fall_count))
113 .bottom_disc_negedge_wait_position(index_translate(
114 data()->main.capture_bottom_fall_delay))
115 .bottom_disc_negedge_wait_count(
116 bottom_fall_delay_.Update(data()->main.bottom_fall_delay_count))
117 .loader_top(data()->main.loader_top)
118 .loader_bottom(data()->main.loader_bottom)
119 .Send();
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700120 }
121
Brian Silvermanf4937f62013-10-16 10:32:00 -0700122 WrappingCounter top_rise_;
123 WrappingCounter top_fall_;
124 WrappingCounter bottom_rise_;
125 WrappingCounter bottom_fall_delay_;
126 WrappingCounter bottom_fall_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700127};
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700128
129} // namespace frc971
130
131int main() {
132 ::aos::Init();
Brian Silvermanf4937f62013-10-16 10:32:00 -0700133 ::frc971::GyroSensorReceiver receiver;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700134 while (true) {
135 receiver.RunIteration();
136 }
137 ::aos::Cleanup();
138}