Brian Silverman | 1ba46c7 | 2013-10-31 16:05:57 -0700 | [diff] [blame^] | 1 | #include <inttypes.h> |
| 2 | |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 3 | #include "aos/atom_code/init.h" |
| 4 | #include "aos/common/logging/logging.h" |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 5 | #include "aos/common/util/wrapping_counter.h" |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 6 | |
| 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 Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 12 | #include "frc971/queues/GyroAngle.q.h" |
Brian Silverman | 1e869f3 | 2013-10-25 18:00:20 -0700 | [diff] [blame] | 13 | #include "frc971/input/usb_receiver.h" |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 14 | |
| 15 | #ifndef M_PI |
| 16 | #define M_PI 3.14159265358979323846 |
| 17 | #endif |
| 18 | |
| 19 | using ::frc971::control_loops::drivetrain; |
| 20 | using ::frc971::control_loops::wrist; |
| 21 | using ::frc971::control_loops::angle_adjust; |
| 22 | using ::frc971::control_loops::shooter; |
| 23 | using ::frc971::control_loops::index_loop; |
| 24 | using ::frc971::sensors::gyro; |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 25 | using ::aos::util::WrappingCounter; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 26 | |
| 27 | namespace frc971 { |
| 28 | namespace { |
| 29 | |
| 30 | inline 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 | |
| 36 | inline 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 | |
| 41 | inline 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 | |
| 48 | inline 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 | |
| 53 | inline 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 Silverman | 74acd62 | 2013-10-26 14:47:14 -0700 | [diff] [blame] | 58 | // Translates values from the ADC into voltage. |
| 59 | inline 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 Silverman | a280ae0 | 2013-10-28 18:21:15 -0700 | [diff] [blame] | 68 | inline double gyro_translate(int64_t in) { |
| 69 | return in / 16.0 / 1000.0 / (180.0 / M_PI); |
| 70 | } |
| 71 | |
Brian Silverman | 1ba46c7 | 2013-10-31 16:05:57 -0700 | [diff] [blame^] | 72 | inline double battery_translate(uint16_t in) { |
| 73 | return adc_translate(in) * 80.8 / 17.8; |
| 74 | } |
| 75 | |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 76 | } // namespace |
| 77 | |
Brian Silverman | 1e869f3 | 2013-10-25 18:00:20 -0700 | [diff] [blame] | 78 | class GyroSensorReceiver : public USBReceiver { |
Brian Silverman | a280ae0 | 2013-10-28 18:21:15 -0700 | [diff] [blame] | 79 | public: |
| 80 | GyroSensorReceiver() : USBReceiver(2) {} |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 81 | |
Brian Silverman | a280ae0 | 2013-10-28 18:21:15 -0700 | [diff] [blame] | 82 | virtual void ProcessData(const ::aos::time::Time &/*timestamp*/) override { |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 83 | gyro.MakeWithBuilder() |
Brian Silverman | a280ae0 | 2013-10-28 18:21:15 -0700 | [diff] [blame] | 84 | .angle(gyro_translate(data()->gyro_angle)) |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 85 | .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 Silverman | caeed86 | 2013-10-25 17:32:19 -0700 | [diff] [blame] | 94 | .hall_effect(data()->main.wrist_hall_effect) |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 95 | .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 Silverman | caeed86 | 2013-10-25 17:32:19 -0700 | [diff] [blame] | 100 | .bottom_hall_effect(data()->main.angle_adjust_bottom_hall_effect) |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 101 | .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 Silverman | caeed86 | 2013-10-25 17:32:19 -0700 | [diff] [blame] | 114 | .top_disc_detect(data()->main.top_disc) |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 115 | .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 Silverman | caeed86 | 2013-10-25 17:32:19 -0700 | [diff] [blame] | 121 | .bottom_disc_detect(data()->main.bottom_disc) |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 122 | .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 Silverman | 1ba46c7 | 2013-10-31 16:05:57 -0700 | [diff] [blame^] | 133 | |
| 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 Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 143 | WrappingCounter top_rise_; |
| 144 | WrappingCounter top_fall_; |
| 145 | WrappingCounter bottom_rise_; |
| 146 | WrappingCounter bottom_fall_delay_; |
| 147 | WrappingCounter bottom_fall_; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 148 | }; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 149 | |
| 150 | } // namespace frc971 |
| 151 | |
| 152 | int main() { |
Brian Silverman | f3cfbd7 | 2013-10-28 16:26:09 -0700 | [diff] [blame] | 153 | ::aos::Init(frc971::USBReceiver::kRelativePriority); |
Brian Silverman | f4937f6 | 2013-10-16 10:32:00 -0700 | [diff] [blame] | 154 | ::frc971::GyroSensorReceiver receiver; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 155 | while (true) { |
| 156 | receiver.RunIteration(); |
| 157 | } |
| 158 | ::aos::Cleanup(); |
| 159 | } |