Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 1 | #include <inttypes.h> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 5 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 6 | #include <chrono> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 7 | #include <functional> |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 8 | #include <mutex> |
| 9 | #include <thread> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 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/DigitalInput.h" |
| 14 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 15 | #include "frc971/wpilib/ahal/Encoder.h" |
| 16 | #include "frc971/wpilib/ahal/Relay.h" |
| 17 | #include "frc971/wpilib/ahal/Talon.h" |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 18 | #include "frc971/wpilib/wpilib_robot_base.h" |
Austin Schuh | bc9a2ab | 2015-11-26 16:20:17 -0800 | [diff] [blame] | 19 | #undef ERROR |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 20 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "aos/events/shm_event_loop.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 22 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 23 | #include "aos/logging/logging.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 24 | #include "aos/make_unique.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | #include "aos/robot_state/robot_state_generated.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 26 | #include "aos/stl_mutex/stl_mutex.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 27 | #include "aos/time/time.h" |
| 28 | #include "aos/util/log_interval.h" |
| 29 | #include "aos/util/phased_loop.h" |
| 30 | #include "aos/util/wrapping_counter.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 32 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 33 | #include "frc971/wpilib/buffered_pcm.h" |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 34 | #include "frc971/wpilib/buffered_solenoid.h" |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 35 | #include "frc971/wpilib/dma.h" |
Sabina Davis | 5ba275b | 2019-02-03 01:18:04 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/drivetrain_writer.h" |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 37 | #include "frc971/wpilib/gyro_sender.h" |
| 38 | #include "frc971/wpilib/joystick_sender.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | #include "frc971/wpilib/logging_generated.h" |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/loop_output_handler.h" |
| 41 | #include "frc971/wpilib/pdp_fetcher.h" |
| 42 | #include "frc971/wpilib/sensor_reader.h" |
| 43 | #include "y2014_bot3/control_loops/drivetrain/drivetrain_base.h" |
| 44 | #include "y2014_bot3/control_loops/rollers/rollers.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | #include "y2014_bot3/control_loops/rollers/rollers_output_generated.h" |
| 46 | #include "y2014_bot3/control_loops/rollers/rollers_position_generated.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 47 | |
| 48 | #ifndef M_PI |
| 49 | #define M_PI 3.14159265358979323846 |
| 50 | #endif |
| 51 | |
| 52 | using ::aos::util::SimpleLogInterval; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 53 | using ::frc971::wpilib::BufferedPcm; |
| 54 | using ::frc971::wpilib::BufferedSolenoid; |
| 55 | using ::frc971::wpilib::LoopOutputHandler; |
| 56 | using ::frc971::wpilib::JoystickSender; |
| 57 | using ::frc971::wpilib::GyroSender; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 58 | using aos::make_unique; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 59 | |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 60 | namespace y2014_bot3 { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 61 | namespace wpilib { |
| 62 | |
| 63 | double drivetrain_translate(int32_t in) { |
| 64 | return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) * |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 65 | ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio * |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 66 | (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 67 | } |
| 68 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 69 | double drivetrain_velocity_translate(double in) { |
| 70 | return (1.0 / in) / 256.0 /*cpr*/ * |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 71 | ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio * |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 72 | (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 73 | } |
| 74 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 75 | // Reads in our inputs. (sensors, voltages, etc.) |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 76 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 77 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 78 | SensorReader(::aos::ShmEventLoop *event_loop) |
Austin Schuh | 30020d9 | 2019-05-27 13:07:02 -0700 | [diff] [blame] | 79 | : ::frc971::wpilib::SensorReader(event_loop), |
| 80 | rollers_position_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | event_loop |
| 82 | ->MakeSender<::y2014_bot3::control_loops::rollers::Position>( |
| 83 | "/rollers")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 84 | drivetrain_position_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | event_loop |
| 86 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 87 | "/drivetrain")) {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 88 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 89 | void RunIteration() { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 90 | // Drivetrain |
| 91 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | frc971::control_loops::drivetrain::Position::Builder position_builder = |
| 95 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 96 | position_builder.add_right_encoder( |
| 97 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw())); |
| 98 | position_builder.add_left_encoder( |
| 99 | drivetrain_translate(drivetrain_left_encoder_->GetRaw())); |
| 100 | position_builder.add_left_speed( |
| 101 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 102 | position_builder.add_right_speed(drivetrain_velocity_translate( |
| 103 | drivetrain_right_encoder_->GetPeriod())); |
| 104 | |
| 105 | builder.Send(position_builder.Finish()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Rollers |
| 109 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 110 | auto builder = rollers_position_sender_.MakeBuilder(); |
| 111 | builder.Send( |
| 112 | builder.MakeBuilder<control_loops::rollers::Position>().Finish()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
Austin Schuh | 30020d9 | 2019-05-27 13:07:02 -0700 | [diff] [blame] | 115 | |
| 116 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 117 | ::aos::Sender<::y2014_bot3::control_loops::rollers::Position> |
Austin Schuh | 30020d9 | 2019-05-27 13:07:02 -0700 | [diff] [blame] | 118 | rollers_position_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 120 | drivetrain_position_sender_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | // Writes out our pneumatic outputs. |
| 124 | class SolenoidWriter { |
| 125 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 126 | SolenoidWriter(::aos::ShmEventLoop *event_loop, |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 127 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 128 | : pcm_(pcm), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 129 | drivetrain_( |
| 130 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 131 | ->MakeFetcher<::frc971::control_loops::drivetrain::Output>( |
| 132 | "/drivetrain")), |
| 133 | rollers_( |
| 134 | event_loop |
| 135 | ->MakeFetcher<::y2014_bot3::control_loops::rollers::Output>( |
| 136 | "/rollers")), |
| 137 | pneumatics_to_log_sender_( |
| 138 | event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 139 | event_loop->set_name("Solenoids"); |
| 140 | event_loop->SetRuntimeRealtimePriority(27); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 141 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 142 | event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 143 | ::std::chrono::milliseconds(20), |
| 144 | ::std::chrono::milliseconds(1)); |
| 145 | } |
| 146 | |
| 147 | void set_pressure_switch( |
| 148 | ::std::unique_ptr<::frc::DigitalInput> pressure_switch) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 149 | pressure_switch_ = ::std::move(pressure_switch); |
| 150 | } |
| 151 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 152 | void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 153 | compressor_relay_ = ::std::move(compressor_relay); |
| 154 | } |
| 155 | |
| 156 | void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) { |
| 157 | drivetrain_left_ = ::std::move(s); |
| 158 | } |
| 159 | |
| 160 | void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) { |
| 161 | drivetrain_right_ = ::std::move(s); |
| 162 | } |
| 163 | |
| 164 | void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) { |
| 165 | rollers_front_ = ::std::move(s); |
| 166 | } |
| 167 | |
| 168 | void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) { |
| 169 | rollers_back_ = ::std::move(s); |
| 170 | } |
| 171 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 172 | void Loop(const int iterations) { |
| 173 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 174 | AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 175 | } |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 176 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 177 | // Drivetrain |
| 178 | { |
| 179 | drivetrain_.Fetch(); |
| 180 | if (drivetrain_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | drivetrain_left_->Set(drivetrain_->left_high()); |
| 182 | drivetrain_right_->Set(drivetrain_->right_high()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 185 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 186 | // Intake |
| 187 | { |
| 188 | rollers_.Fetch(); |
| 189 | if (rollers_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 190 | rollers_front_->Set(rollers_->front_extended()); |
| 191 | rollers_back_->Set(rollers_->back_extended()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | // Compressor |
| 196 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | auto builder = pneumatics_to_log_sender_.MakeBuilder(); |
| 198 | |
| 199 | ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder = |
| 200 | builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>(); |
| 201 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 202 | { |
| 203 | // Refill if pneumatic pressure goes too low. |
| 204 | const bool compressor_on = !pressure_switch_->Get(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 205 | to_log_builder.add_compressor_on(compressor_on); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 206 | if (compressor_on) { |
| 207 | compressor_relay_->Set(::frc::Relay::kForward); |
| 208 | } else { |
| 209 | compressor_relay_->Set(::frc::Relay::kOff); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | pcm_->Flush(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 214 | to_log_builder.add_read_solenoids(pcm_->GetAll()); |
| 215 | builder.Send(to_log_builder.Finish()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 218 | |
| 219 | private: |
| 220 | const ::std::unique_ptr<BufferedPcm> &pcm_; |
| 221 | |
| 222 | ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_; |
| 223 | ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_; |
| 224 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 225 | ::std::unique_ptr<::frc::DigitalInput> pressure_switch_; |
| 226 | ::std::unique_ptr<::frc::Relay> compressor_relay_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 227 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 228 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_; |
| 229 | ::aos::Fetcher<::y2014_bot3::control_loops::rollers::Output> rollers_; |
| 230 | aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 233 | // Writes out rollers voltages. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 234 | class RollersWriter : public LoopOutputHandler< |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 235 | ::y2014_bot3::control_loops::rollers::Output> { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 236 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 237 | RollersWriter(::aos::EventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 238 | : ::frc971::wpilib::LoopOutputHandler< |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | ::y2014_bot3::control_loops::rollers::Output>(event_loop, |
| 240 | "/rollers") {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 241 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 242 | void set_rollers_front_intake_talon(::std::unique_ptr<::frc::Talon> t_left, |
| 243 | ::std::unique_ptr<::frc::Talon> t_right) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 244 | rollers_front_left_intake_talon_ = ::std::move(t_left); |
| 245 | rollers_front_right_intake_talon_ = ::std::move(t_right); |
| 246 | } |
| 247 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 248 | void set_rollers_back_intake_talon(::std::unique_ptr<::frc::Talon> t_left, |
| 249 | ::std::unique_ptr<::frc::Talon> t_right) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 250 | rollers_back_left_intake_talon_ = ::std::move(t_left); |
| 251 | rollers_back_right_intake_talon_ = ::std::move(t_right); |
| 252 | } |
| 253 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 254 | void set_rollers_low_goal_talon(::std::unique_ptr<::frc::Talon> t) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 255 | rollers_low_goal_talon_ = ::std::move(t); |
| 256 | } |
| 257 | |
| 258 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 259 | virtual void Write(const ::y2014_bot3::control_loops::rollers::Output |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 260 | &output) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 261 | rollers_front_left_intake_talon_->SetSpeed(output.front_intake_voltage() / |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 262 | 12.0); |
| 263 | rollers_front_right_intake_talon_->SetSpeed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 264 | -(output.front_intake_voltage() / 12.0)); |
| 265 | rollers_back_left_intake_talon_->SetSpeed(output.back_intake_voltage() / |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 266 | 12.0); |
| 267 | rollers_back_right_intake_talon_->SetSpeed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 268 | -(output.back_intake_voltage() / 12.0)); |
| 269 | rollers_low_goal_talon_->SetSpeed(output.low_goal_voltage() / 12.0); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | virtual void Stop() override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 273 | AOS_LOG(WARNING, "Intake output too old\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 274 | rollers_front_left_intake_talon_->SetDisabled(); |
| 275 | rollers_front_right_intake_talon_->SetDisabled(); |
| 276 | rollers_back_left_intake_talon_->SetDisabled(); |
| 277 | rollers_back_right_intake_talon_->SetDisabled(); |
| 278 | rollers_low_goal_talon_->SetDisabled(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 281 | ::std::unique_ptr<::frc::Talon> rollers_front_left_intake_talon_, |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 282 | rollers_back_left_intake_talon_, rollers_front_right_intake_talon_, |
| 283 | rollers_back_right_intake_talon_, rollers_low_goal_talon_; |
| 284 | }; |
| 285 | |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 286 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 287 | public: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 288 | ::std::unique_ptr<::frc::Encoder> make_encoder(int index) { |
| 289 | return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 290 | ::frc::Encoder::k4X); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 291 | } |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 292 | void Run() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 294 | aos::configuration::ReadConfig("config.json"); |
| 295 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 296 | // Thread 1. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 297 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 298 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 299 | &joystick_sender_event_loop); |
| 300 | AddLoop(&joystick_sender_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 301 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 302 | // Thread 2. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 303 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 304 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 305 | AddLoop(&pdp_fetcher_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 306 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 307 | // Thread 3. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 308 | // Sensors |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 309 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 310 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 311 | sensor_reader.set_drivetrain_left_encoder(make_encoder(4)); |
| 312 | sensor_reader.set_drivetrain_right_encoder(make_encoder(5)); |
| 313 | AddLoop(&sensor_reader_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 314 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 315 | // Thread 4. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 316 | ::aos::ShmEventLoop gyro_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 317 | GyroSender gyro_sender(&gyro_event_loop); |
| 318 | AddLoop(&gyro_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 319 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 320 | // Thread 5. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 321 | // Outputs |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 322 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 323 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | 5ba275b | 2019-02-03 01:18:04 -0800 | [diff] [blame] | 324 | drivetrain_writer.set_left_controller0( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 325 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), true); |
Sabina Davis | 5ba275b | 2019-02-03 01:18:04 -0800 | [diff] [blame] | 326 | drivetrain_writer.set_right_controller0( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 327 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)), false); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 328 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 329 | RollersWriter rollers_writer(&output_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 330 | rollers_writer.set_rollers_front_intake_talon( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 331 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)), |
| 332 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7))); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 333 | rollers_writer.set_rollers_back_intake_talon( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 334 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)), |
| 335 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6))); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 336 | |
| 337 | rollers_writer.set_rollers_low_goal_talon( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 338 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4))); |
| 339 | AddLoop(&output_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 340 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 341 | // Thread 6. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 342 | ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 343 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 344 | new ::frc971::wpilib::BufferedPcm()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 345 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 346 | solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6)); |
| 347 | solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5)); |
| 348 | solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2)); |
| 349 | solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4)); |
| 350 | |
| 351 | // Don't change the following IDs. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 352 | solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(9)); |
| 353 | solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0)); |
| 354 | AddLoop(&solenoid_writer_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 355 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 356 | RunLoops(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 357 | } |
| 358 | }; |
| 359 | |
| 360 | } // namespace wpilib |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 361 | } // namespace y2014_bot3 |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 362 | |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 363 | AOS_ROBOT_CLASS(::y2014_bot3::wpilib::WPILibRobot); |