blob: 411fb5e4221f843fa2b2d50a60f2f78f4a0590de [file] [log] [blame]
Brian Silverman1ba46c72013-10-31 16:05:57 -07001#include <inttypes.h>
2
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07003#include "aos/atom_code/init.h"
4#include "aos/common/logging/logging.h"
Brian Silvermanf4937f62013-10-16 10:32:00 -07005#include "aos/common/util/wrapping_counter.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -07006
7#include "frc971/control_loops/drivetrain/drivetrain.q.h"
8#include "frc971/control_loops/wrist/wrist_motor.q.h"
9#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
10#include "frc971/control_loops/index/index_motor.q.h"
11#include "frc971/control_loops/shooter/shooter_motor.q.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070012#include "frc971/queues/GyroAngle.q.h"
Brian Silverman1e869f32013-10-25 18:00:20 -070013#include "frc971/input/usb_receiver.h"
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070014
15#ifndef M_PI
16#define M_PI 3.14159265358979323846
17#endif
18
19using ::frc971::control_loops::drivetrain;
20using ::frc971::control_loops::wrist;
21using ::frc971::control_loops::angle_adjust;
22using ::frc971::control_loops::shooter;
23using ::frc971::control_loops::index_loop;
24using ::frc971::sensors::gyro;
Brian Silvermanf4937f62013-10-16 10:32:00 -070025using ::aos::util::WrappingCounter;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070026
27namespace frc971 {
28namespace {
29
30inline double drivetrain_translate(int32_t in) {
31 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
32 (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ *
33 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
34}
35
36inline double wrist_translate(int32_t in) {
37 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
38 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
39}
40
41inline double angle_adjust_translate(int32_t in) {
42 static const double kCableDiameter = 0.060;
43 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
44 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
45 (2 * M_PI);
46}
47
48inline double shooter_translate(int32_t in) {
49 return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
50 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
51}
52
53inline double index_translate(int32_t in) {
54 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) *
55 (1.0) /*gears*/ * (2 * M_PI);
56}
57
Brian Silverman74acd622013-10-26 14:47:14 -070058// Translates values from the ADC into voltage.
59inline double adc_translate(uint16_t in) {
60 static const double kVRefN = 0;
61 static const double kVRefP = 3.3;
62 static const int kMaximumValue = 0x3FF;
63 return kVRefN +
64 (static_cast<double>(in) / static_cast<double>(kMaximumValue) *
65 (kVRefP - kVRefN));
66}
67
Brian Silvermana280ae02013-10-28 18:21:15 -070068inline double gyro_translate(int64_t in) {
69 return in / 16.0 / 1000.0 / (180.0 / M_PI);
70}
71
Brian Silverman1ba46c72013-10-31 16:05:57 -070072inline double battery_translate(uint16_t in) {
73 return adc_translate(in) * 80.8 / 17.8;
74}
75
Brian Silverman2e0dcfd2013-03-30 22:44:40 -070076} // namespace
77
Brian Silverman1e869f32013-10-25 18:00:20 -070078class GyroSensorReceiver : public USBReceiver {
Brian Silvermana280ae02013-10-28 18:21:15 -070079 public:
80 GyroSensorReceiver() : USBReceiver(2) {}
Brian Silvermanf4937f62013-10-16 10:32:00 -070081
Brian Silvermana280ae02013-10-28 18:21:15 -070082 virtual void ProcessData(const ::aos::time::Time &/*timestamp*/) override {
Brian Silvermanf4937f62013-10-16 10:32:00 -070083 gyro.MakeWithBuilder()
Brian Silvermana280ae02013-10-28 18:21:15 -070084 .angle(gyro_translate(data()->gyro_angle))
Brian Silvermanf4937f62013-10-16 10:32:00 -070085 .Send();
86
87 drivetrain.position.MakeWithBuilder()
88 .right_encoder(drivetrain_translate(data()->main.right_drive))
89 .left_encoder(-drivetrain_translate(data()->main.left_drive))
90 .Send();
91
92 wrist.position.MakeWithBuilder()
93 .pos(wrist_translate(data()->main.wrist))
Brian Silvermancaeed862013-10-25 17:32:19 -070094 .hall_effect(data()->main.wrist_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -070095 .calibration(wrist_translate(data()->main.capture_wrist_rise))
96 .Send();
97
98 angle_adjust.position.MakeWithBuilder()
99 .angle(angle_adjust_translate(data()->main.shooter_angle))
Brian Silvermancaeed862013-10-25 17:32:19 -0700100 .bottom_hall_effect(data()->main.angle_adjust_bottom_hall_effect)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700101 .middle_hall_effect(false)
102 .bottom_calibration(angle_adjust_translate(
103 data()->main.capture_shooter_angle_rise))
104 .middle_calibration(angle_adjust_translate(
105 0))
106 .Send();
107
108 shooter.position.MakeWithBuilder()
109 .position(shooter_translate(data()->main.shooter))
110 .Send();
111
112 index_loop.position.MakeWithBuilder()
113 .index_position(index_translate(data()->main.indexer))
Brian Silvermancaeed862013-10-25 17:32:19 -0700114 .top_disc_detect(data()->main.top_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700115 .top_disc_posedge_count(top_rise_.Update(data()->main.top_rise_count))
116 .top_disc_posedge_position(
117 index_translate(data()->main.capture_top_rise))
118 .top_disc_negedge_count(top_fall_.Update(data()->main.top_fall_count))
119 .top_disc_negedge_position(
120 index_translate(data()->main.capture_top_fall))
Brian Silvermancaeed862013-10-25 17:32:19 -0700121 .bottom_disc_detect(data()->main.bottom_disc)
Brian Silvermanf4937f62013-10-16 10:32:00 -0700122 .bottom_disc_posedge_count(
123 bottom_rise_.Update(data()->main.bottom_rise_count))
124 .bottom_disc_negedge_count(
125 bottom_fall_.Update(data()->main.bottom_fall_count))
126 .bottom_disc_negedge_wait_position(index_translate(
127 data()->main.capture_bottom_fall_delay))
128 .bottom_disc_negedge_wait_count(
129 bottom_fall_delay_.Update(data()->main.bottom_fall_delay_count))
130 .loader_top(data()->main.loader_top)
131 .loader_bottom(data()->main.loader_bottom)
132 .Send();
Brian Silverman1ba46c72013-10-31 16:05:57 -0700133
134 LOG(DEBUG, "battery %f %f %" PRIu16 "\n",
135 battery_translate(data()->main.battery_voltage),
136 adc_translate(data()->main.battery_voltage),
137 data()->main.battery_voltage);
138 LOG(DEBUG, "halls l=%f r=%f\n",
139 adc_translate(data()->main.left_drive_hall),
140 adc_translate(data()->main.right_drive_hall));
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700141 }
142
Brian Silvermanf4937f62013-10-16 10:32:00 -0700143 WrappingCounter top_rise_;
144 WrappingCounter top_fall_;
145 WrappingCounter bottom_rise_;
146 WrappingCounter bottom_fall_delay_;
147 WrappingCounter bottom_fall_;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700148};
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700149
150} // namespace frc971
151
152int main() {
Brian Silvermanf3cfbd72013-10-28 16:26:09 -0700153 ::aos::Init(frc971::USBReceiver::kRelativePriority);
Brian Silvermanf4937f62013-10-16 10:32:00 -0700154 ::frc971::GyroSensorReceiver receiver;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700155 while (true) {
156 receiver.RunIteration();
157 }
158 ::aos::Cleanup();
159}