Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 6 | #include <chrono> |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 7 | #include <thread> |
| 8 | #include <mutex> |
| 9 | #include <functional> |
| 10 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 11 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 12 | #include "frc971/wpilib/ahal/Compressor.h" |
| 13 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 14 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 15 | #include "frc971/wpilib/ahal/Encoder.h" |
| 16 | #include "frc971/wpilib/ahal/PowerDistributionPanel.h" |
| 17 | #include "frc971/wpilib/ahal/Relay.h" |
| 18 | #include "frc971/wpilib/ahal/Talon.h" |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 19 | #include "frc971/wpilib/wpilib_robot_base.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 20 | #undef ERROR |
| 21 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 22 | #include "aos/events/shm-event-loop.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 23 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 24 | #include "aos/logging/logging.h" |
| 25 | #include "aos/logging/queue_logging.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 26 | #include "aos/make_unique.h" |
| 27 | #include "aos/robot_state/robot_state.q.h" |
| 28 | #include "aos/stl_mutex/stl_mutex.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 29 | #include "aos/time/time.h" |
| 30 | #include "aos/util/log_interval.h" |
| 31 | #include "aos/util/phased_loop.h" |
| 32 | #include "aos/util/wrapping_counter.h" |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 33 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 34 | #include "frc971/wpilib/buffered_pcm.h" |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 35 | #include "frc971/wpilib/buffered_solenoid.h" |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 36 | #include "frc971/wpilib/dma.h" |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 37 | #include "frc971/wpilib/dma_edge_counting.h" |
Sabina Davis | e33f59a | 2019-02-03 01:17:45 -0800 | [diff] [blame] | 38 | #include "frc971/wpilib/drivetrain_writer.h" |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 39 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 41 | #include "frc971/wpilib/joystick_sender.h" |
| 42 | #include "frc971/wpilib/logging.q.h" |
| 43 | #include "frc971/wpilib/loop_output_handler.h" |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 44 | #include "frc971/wpilib/sensor_reader.h" |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 45 | #include "y2012/control_loops/accessories/accessories.q.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 46 | |
| 47 | #ifndef M_PI |
| 48 | #define M_PI 3.14159265358979323846 |
| 49 | #endif |
| 50 | |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 51 | using ::frc971::control_loops::drivetrain_queue; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 52 | using ::y2012::control_loops::AccessoriesQueue; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 53 | using namespace frc; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 54 | using aos::make_unique; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 55 | |
| 56 | namespace y2012 { |
| 57 | namespace wpilib { |
| 58 | |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 59 | double drivetrain_translate(int32_t in) { |
| 60 | return -static_cast<double>(in) / |
| 61 | (256.0 /*cpr*/ * 4.0 /*4x*/) * |
| 62 | 1 * |
| 63 | (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0; |
| 64 | } |
| 65 | |
| 66 | double drivetrain_velocity_translate(double in) { |
| 67 | return (1.0 / in) / 256.0 /*cpr*/ * |
| 68 | 1 * |
| 69 | (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0; |
| 70 | } |
| 71 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 72 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 73 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 74 | SensorReader(::aos::EventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 75 | : ::frc971::wpilib::SensorReader(event_loop), |
| 76 | accessories_position_sender_( |
| 77 | event_loop->MakeSender<::aos::control_loops::Position>( |
| 78 | ".y2012.control_loops.accessories_queue.position")) {} |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 79 | |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 80 | void RunIteration() { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 81 | { |
| 82 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 83 | drivetrain_message->right_encoder = |
| 84 | drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 85 | drivetrain_message->left_encoder = |
| 86 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 87 | drivetrain_message->left_speed = |
| 88 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 89 | drivetrain_message->right_speed = |
| 90 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 91 | |
| 92 | drivetrain_message.Send(); |
| 93 | } |
| 94 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 95 | accessories_position_sender_.MakeMessage().Send(); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 96 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 97 | |
| 98 | private: |
| 99 | ::aos::Sender<::aos::control_loops::Position> accessories_position_sender_; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | class SolenoidWriter { |
| 103 | public: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 104 | SolenoidWriter(::aos::EventLoop *event_loop, |
| 105 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 106 | : event_loop_(event_loop), |
| 107 | pcm_(pcm), |
| 108 | drivetrain_fetcher_( |
| 109 | event_loop_ |
| 110 | ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>( |
| 111 | ".y2012.control_loops.drivetrain_queue.output")), |
| 112 | accessories_fetcher_(event_loop_->MakeFetcher< |
| 113 | ::y2012::control_loops::AccessoriesQueue::Message>( |
| 114 | ".y2012.control_loops.accessories_queue.output")) { |
| 115 | event_loop_->set_name("Solenoids"); |
| 116 | event_loop_->SetRuntimeRealtimePriority(27); |
| 117 | |
| 118 | event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 119 | ::std::chrono::milliseconds(20), |
| 120 | ::std::chrono::milliseconds(1)); |
| 121 | } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 122 | |
| 123 | void set_drivetrain_high( |
| 124 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 125 | drivetrain_high_ = ::std::move(s); |
| 126 | } |
| 127 | |
| 128 | void set_drivetrain_low( |
| 129 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 130 | drivetrain_low_ = ::std::move(s); |
| 131 | } |
| 132 | |
| 133 | void set_s1(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 134 | s1_ = ::std::move(s); |
| 135 | } |
| 136 | |
| 137 | void set_s2(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 138 | s2_ = ::std::move(s); |
| 139 | } |
| 140 | |
| 141 | void set_s3(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 142 | s3_ = ::std::move(s); |
| 143 | } |
| 144 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 145 | void Loop(const int iterations) { |
| 146 | if (iterations != 1) { |
| 147 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 148 | } |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 149 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 150 | { |
| 151 | accessories_fetcher_.Fetch(); |
| 152 | if (accessories_fetcher_.get()) { |
| 153 | LOG_STRUCT(DEBUG, "solenoids", *accessories_fetcher_); |
| 154 | s1_->Set(accessories_fetcher_->solenoids[0]); |
| 155 | s2_->Set(accessories_fetcher_->solenoids[1]); |
| 156 | s3_->Set(accessories_fetcher_->solenoids[2]); |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 157 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 158 | } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 159 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 160 | { |
| 161 | drivetrain_fetcher_.Fetch(); |
| 162 | if (drivetrain_fetcher_.get()) { |
| 163 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_fetcher_); |
| 164 | const bool high = |
| 165 | drivetrain_fetcher_->left_high || drivetrain_fetcher_->right_high; |
| 166 | drivetrain_high_->Set(high); |
| 167 | drivetrain_low_->Set(!high); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 168 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 169 | } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 170 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 171 | { |
| 172 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 173 | pcm_->Flush(); |
| 174 | to_log.read_solenoids = pcm_->GetAll(); |
| 175 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 179 | private: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 180 | ::aos::EventLoop *event_loop_; |
| 181 | |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 182 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
| 183 | |
| 184 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_high_; |
| 185 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_low_; |
| 186 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s1_, s2_, s3_; |
| 187 | |
| 188 | ::std::unique_ptr<Compressor> compressor_; |
| 189 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 190 | ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> |
| 191 | drivetrain_fetcher_; |
| 192 | ::aos::Fetcher<::y2012::control_loops::AccessoriesQueue::Message> |
| 193 | accessories_fetcher_; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 194 | }; |
| 195 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 196 | class AccessoriesWriter |
| 197 | : public ::frc971::wpilib::LoopOutputHandler<AccessoriesQueue::Message> { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 198 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 199 | AccessoriesWriter(::aos::EventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 200 | : ::frc971::wpilib::LoopOutputHandler<AccessoriesQueue::Message>( |
| 201 | event_loop, ".y2012.control_loops.accessories_queue.output") {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 202 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 203 | void set_talon1(::std::unique_ptr<Talon> t) { talon1_ = ::std::move(t); } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 204 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 205 | void set_talon2(::std::unique_ptr<Talon> t) { talon2_ = ::std::move(t); } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 206 | |
| 207 | private: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 208 | virtual void Write(const AccessoriesQueue::Message &output) override { |
| 209 | talon1_->SetSpeed(output.sticks[0]); |
| 210 | talon2_->SetSpeed(output.sticks[1]); |
| 211 | LOG_STRUCT(DEBUG, "will output", output); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | virtual void Stop() override { |
| 215 | LOG(WARNING, "shooter output too old\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 216 | talon1_->SetDisabled(); |
| 217 | talon2_->SetDisabled(); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | ::std::unique_ptr<Talon> talon1_, talon2_; |
| 221 | }; |
| 222 | |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 223 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 224 | public: |
| 225 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 226 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 227 | Encoder::k4X); |
| 228 | } |
| 229 | |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 230 | void Run() override { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 231 | // Thread 1. |
| 232 | ::aos::ShmEventLoop joystick_sender_event_loop; |
| 233 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 234 | &joystick_sender_event_loop); |
| 235 | AddLoop(&joystick_sender_event_loop); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 236 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 237 | // Thread 2. |
| 238 | ::aos::ShmEventLoop sensor_reader_event_loop; |
| 239 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 240 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 241 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 242 | AddLoop(&sensor_reader_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 243 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 244 | // Thread 3. |
| 245 | ::aos::ShmEventLoop output_event_loop; |
| 246 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | e33f59a | 2019-02-03 01:17:45 -0800 | [diff] [blame] | 247 | drivetrain_writer.set_left_controller0( |
| 248 | ::std::unique_ptr<Talon>(new Talon(3)), true); |
| 249 | drivetrain_writer.set_right_controller0( |
| 250 | ::std::unique_ptr<Talon>(new Talon(4)), false); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 251 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 252 | ::y2012::wpilib::AccessoriesWriter accessories_writer(&output_event_loop); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 253 | accessories_writer.set_talon1(::std::unique_ptr<Talon>(new Talon(5))); |
| 254 | accessories_writer.set_talon2(::std::unique_ptr<Talon>(new Talon(6))); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 255 | AddLoop(&output_event_loop); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 256 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 257 | // Thread 4. |
| 258 | ::aos::ShmEventLoop solenoid_writer_event_loop; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 259 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 260 | new ::frc971::wpilib::BufferedPcm()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 261 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 262 | solenoid_writer.set_drivetrain_high(pcm->MakeSolenoid(0)); |
| 263 | solenoid_writer.set_drivetrain_low(pcm->MakeSolenoid(2)); |
| 264 | solenoid_writer.set_s1(pcm->MakeSolenoid(1)); |
| 265 | solenoid_writer.set_s2(pcm->MakeSolenoid(3)); |
| 266 | solenoid_writer.set_s3(pcm->MakeSolenoid(4)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 267 | AddLoop(&solenoid_writer_event_loop); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 268 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 269 | RunLoops(); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 270 | } |
| 271 | }; |
| 272 | |
| 273 | } // namespace wpilib |
| 274 | } // namespace y2012 |
| 275 | |
| 276 | |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 277 | AOS_ROBOT_CLASS(::y2012::wpilib::WPILibRobot); |