Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
| 6 | #include <thread> |
| 7 | #include <mutex> |
| 8 | #include <functional> |
| 9 | |
Austin Schuh | 6d5d9ae | 2015-10-31 19:39:57 -0700 | [diff] [blame] | 10 | #include "Encoder.h" |
| 11 | #include "Talon.h" |
| 12 | #include "DriverStation.h" |
| 13 | #include "AnalogInput.h" |
| 14 | #include "Compressor.h" |
| 15 | #include "Relay.h" |
| 16 | #include "RobotBase.h" |
| 17 | #include "dma.h" |
| 18 | #include "ControllerPower.h" |
| 19 | #ifndef WPILIB2015 |
| 20 | #include "DigitalGlitchFilter.h" |
| 21 | #endif |
| 22 | #include "DigitalInput.h" |
| 23 | #undef ERROR |
| 24 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 25 | #include "aos/common/logging/logging.h" |
| 26 | #include "aos/common/logging/queue_logging.h" |
| 27 | #include "aos/common/time.h" |
| 28 | #include "aos/common/util/log_interval.h" |
| 29 | #include "aos/common/util/phased_loop.h" |
| 30 | #include "aos/common/util/wrapping_counter.h" |
| 31 | #include "aos/common/stl_mutex.h" |
| 32 | #include "aos/linux_code/init.h" |
| 33 | #include "aos/common/messages/robot_state.q.h" |
| 34 | |
| 35 | #include "frc971/control_loops/control_loops.q.h" |
| 36 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 37 | #include "bot3/control_loops/elevator/elevator.q.h" |
| 38 | #include "bot3/control_loops/intake/intake.q.h" |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 39 | #include "bot3/autonomous/auto.q.h" |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 40 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 41 | #include "frc971/wpilib/joystick_sender.h" |
| 42 | #include "frc971/wpilib/loop_output_handler.h" |
| 43 | #include "frc971/wpilib/buffered_solenoid.h" |
| 44 | #include "frc971/wpilib/buffered_pcm.h" |
| 45 | #include "frc971/wpilib/gyro_sender.h" |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 46 | #include "frc971/wpilib/logging.q.h" |
| 47 | #include "bot3/control_loops/drivetrain/drivetrain.h" |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 48 | #include "bot3/control_loops/elevator/elevator.h" |
| 49 | #include "bot3/control_loops/intake/intake.h" |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 50 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 51 | |
| 52 | #ifndef M_PI |
| 53 | #define M_PI 3.14159265358979323846 |
| 54 | #endif |
| 55 | |
| 56 | using ::aos::util::SimpleLogInterval; |
| 57 | using ::bot3::control_loops::drivetrain_queue; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 58 | using ::bot3::control_loops::elevator_queue; |
| 59 | using ::bot3::control_loops::intake_queue; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 60 | using ::frc971::wpilib::BufferedPcm; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 61 | using ::frc971::wpilib::BufferedSolenoid; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 62 | using ::frc971::wpilib::LoopOutputHandler; |
| 63 | using ::frc971::wpilib::JoystickSender; |
| 64 | using ::frc971::wpilib::GyroSender; |
| 65 | |
| 66 | namespace bot3 { |
| 67 | namespace wpilib { |
| 68 | |
| 69 | double drivetrain_translate(int32_t in) { |
Austin Schuh | 316ab46 | 2015-09-13 04:44:40 +0000 | [diff] [blame] | 70 | return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) * |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 71 | ::bot3::control_loops::kDrivetrainEncoderRatio * |
| 72 | (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 73 | } |
| 74 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 75 | double elevator_translate(int32_t in) { |
| 76 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 77 | ::bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) * |
Austin Schuh | 316ab46 | 2015-09-13 04:44:40 +0000 | [diff] [blame] | 78 | ::bot3::control_loops::kElevChainReduction * |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 79 | ::bot3::control_loops::kElevGearboxOutputRadianDistance; |
| 80 | } |
| 81 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 82 | static const double kMaximumEncoderPulsesPerSecond = |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 83 | 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ / |
| 84 | 60.0 /* seconds / minute */ * 512.0 /* CPR */ * |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 85 | 4.0 /* index pulse = 1/4 cycle */; |
| 86 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 87 | // Reads in our inputs. (sensors, voltages, etc.) |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 88 | class SensorReader { |
| 89 | public: |
| 90 | SensorReader() { |
| 91 | // Set it to filter out anything shorter than 1/4 of the minimum pulse width |
| 92 | // we should ever see. |
| 93 | filter_.SetPeriodNanoSeconds( |
| 94 | static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5)); |
| 95 | } |
| 96 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 97 | // Drivetrain setters. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 98 | void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) { |
| 99 | left_encoder_ = ::std::move(left_encoder); |
| 100 | } |
| 101 | |
| 102 | void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) { |
| 103 | right_encoder_ = ::std::move(right_encoder); |
| 104 | } |
| 105 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 106 | // Elevator setters. |
| 107 | void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) { |
| 108 | filter_.Add(encoder.get()); |
| 109 | elevator_encoder_ = ::std::move(encoder); |
| 110 | } |
| 111 | |
Austin Schuh | 39e5abc | 2015-10-31 18:51:20 -0700 | [diff] [blame] | 112 | void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) { |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 113 | zeroing_hall_effect_ = ::std::move(hall); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Austin Schuh | d6bc8ba | 2015-09-13 19:39:38 +0000 | [diff] [blame] | 116 | void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) { |
| 117 | tote_sensor_ = ::std::move(tote_sensor); |
| 118 | } |
| 119 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 120 | void operator()() { |
| 121 | LOG(INFO, "In sensor reader thread\n"); |
| 122 | ::aos::SetCurrentThreadName("SensorReader"); |
| 123 | |
| 124 | my_pid_ = getpid(); |
Austin Schuh | 3b5b69b | 2015-10-31 18:55:47 -0700 | [diff] [blame] | 125 | ds_ = |
| 126 | #ifdef WPILIB2015 |
| 127 | DriverStation::GetInstance(); |
| 128 | #else |
| 129 | &DriverStation::GetInstance(); |
| 130 | #endif |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 131 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 132 | LOG(INFO, "Things are now started\n"); |
| 133 | |
| 134 | ::aos::SetCurrentThreadRealtimePriority(kPriority); |
| 135 | while (run_) { |
| 136 | ::aos::time::PhasedLoopXMS(5, 4000); |
| 137 | RunIteration(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void RunIteration() { |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 142 | // General |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 143 | { |
| 144 | auto new_state = ::aos::robot_state.MakeMessage(); |
| 145 | |
| 146 | new_state->reader_pid = my_pid_; |
| 147 | new_state->outputs_enabled = ds_->IsSysActive(); |
| 148 | new_state->browned_out = ds_->IsSysBrownedOut(); |
| 149 | |
| 150 | new_state->is_3v3_active = ControllerPower::GetEnabled3V3(); |
| 151 | new_state->is_5v_active = ControllerPower::GetEnabled5V(); |
| 152 | new_state->voltage_3v3 = ControllerPower::GetVoltage3V3(); |
| 153 | new_state->voltage_5v = ControllerPower::GetVoltage5V(); |
| 154 | |
| 155 | new_state->voltage_roborio_in = ControllerPower::GetInputVoltage(); |
| 156 | new_state->voltage_battery = ds_->GetBatteryVoltage(); |
| 157 | |
| 158 | LOG_STRUCT(DEBUG, "robot_state", *new_state); |
| 159 | |
| 160 | new_state.Send(); |
| 161 | } |
| 162 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 163 | // Drivetrain |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 164 | { |
| 165 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 166 | drivetrain_message->right_encoder = |
| 167 | -drivetrain_translate(right_encoder_->GetRaw()); |
| 168 | drivetrain_message->left_encoder = |
| 169 | drivetrain_translate(left_encoder_->GetRaw()); |
| 170 | |
| 171 | drivetrain_message.Send(); |
| 172 | } |
| 173 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 174 | // Elevator |
| 175 | { |
| 176 | // Update control loop positions. |
| 177 | auto elevator_message = elevator_queue.position.MakeMessage(); |
| 178 | elevator_message->encoder = |
| 179 | elevator_translate(elevator_encoder_->GetRaw()); |
Austin Schuh | 39e5abc | 2015-10-31 18:51:20 -0700 | [diff] [blame] | 180 | elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get(); |
Austin Schuh | d6bc8ba | 2015-09-13 19:39:38 +0000 | [diff] [blame] | 181 | elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 182 | |
| 183 | elevator_message.Send(); |
| 184 | } |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 185 | |
| 186 | // Intake |
| 187 | { |
| 188 | auto intake_message = intake_queue.position.MakeMessage(); |
| 189 | intake_message.Send(); |
| 190 | } |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void Quit() { run_ = false; } |
| 194 | |
| 195 | private: |
| 196 | static const int kPriority = 30; |
| 197 | static const int kInterruptPriority = 55; |
| 198 | |
| 199 | int32_t my_pid_; |
| 200 | DriverStation *ds_; |
| 201 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 202 | ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_; |
Austin Schuh | 39e5abc | 2015-10-31 18:51:20 -0700 | [diff] [blame] | 203 | ::std::unique_ptr<DigitalInput> zeroing_hall_effect_; |
Austin Schuh | d6bc8ba | 2015-09-13 19:39:38 +0000 | [diff] [blame] | 204 | ::std::unique_ptr<AnalogInput> tote_sensor_; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 205 | |
| 206 | ::std::atomic<bool> run_{true}; |
| 207 | DigitalGlitchFilter filter_; |
| 208 | }; |
| 209 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 210 | // Writes out our pneumatic outputs. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 211 | class SolenoidWriter { |
| 212 | public: |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 213 | SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm) |
Jasmine Zhou | da77c5f | 2015-09-12 15:16:10 -0700 | [diff] [blame] | 214 | : pcm_(pcm), |
| 215 | elevator_(".bot3.control_loops.elevator_queue.output"), |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 216 | intake_(".bot3.control_loops.intake_queue.output"), |
| 217 | can_grabber_control_(".bot3.autonomous.can_grabber_control") {} |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 218 | |
Austin Schuh | 39e5abc | 2015-10-31 18:51:20 -0700 | [diff] [blame] | 219 | void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) { |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 220 | pressure_switch_ = ::std::move(pressure_switch); |
| 221 | } |
| 222 | |
| 223 | void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) { |
| 224 | compressor_relay_ = ::std::move(compressor_relay); |
| 225 | } |
| 226 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 227 | void set_elevator_passive_support( |
| 228 | ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) { |
| 229 | elevator_passive_support_ = ::std::move(elevator_passive_support); |
| 230 | } |
| 231 | |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 232 | void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) { |
| 233 | can_grabber_ = ::std::move(can_grabber); |
| 234 | } |
| 235 | |
Jasmine Zhou | da77c5f | 2015-09-12 15:16:10 -0700 | [diff] [blame] | 236 | void set_elevator_can_support( |
| 237 | ::std::unique_ptr<BufferedSolenoid> elevator_can_support) { |
| 238 | elevator_can_support_ = ::std::move(elevator_can_support); |
| 239 | } |
| 240 | |
| 241 | void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) { |
| 242 | intake_claw_ = ::std::move(intake_claw); |
| 243 | } |
| 244 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 245 | void operator()() { |
| 246 | ::aos::SetCurrentThreadName("Solenoids"); |
| 247 | ::aos::SetCurrentThreadRealtimePriority(30); |
| 248 | |
| 249 | while (run_) { |
| 250 | ::aos::time::PhasedLoopXMS(20, 1000); |
| 251 | |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 252 | // Can Grabber |
| 253 | { |
| 254 | can_grabber_control_.FetchLatest(); |
| 255 | if (can_grabber_control_.get()) { |
| 256 | LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_); |
| 257 | can_grabber_->Set(can_grabber_control_->can_grabbers); |
| 258 | } |
| 259 | } |
| 260 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 261 | // Elevator |
| 262 | { |
| 263 | elevator_.FetchLatest(); |
| 264 | if (elevator_.get()) { |
| 265 | LOG_STRUCT(DEBUG, "solenoids", *elevator_); |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 266 | elevator_passive_support_->Set(!elevator_->passive_support); |
| 267 | elevator_can_support_->Set(!elevator_->can_support); |
Jasmine Zhou | da77c5f | 2015-09-12 15:16:10 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | // Intake |
| 272 | { |
| 273 | intake_.FetchLatest(); |
| 274 | if (intake_.get()) { |
| 275 | LOG_STRUCT(DEBUG, "solenoids", *intake_); |
| 276 | intake_claw_->Set(intake_->claw_closed); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 279 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 280 | // Compressor |
| 281 | ::aos::joystick_state.FetchLatest(); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 282 | { |
| 283 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 284 | { |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 285 | // Refill if pneumatic pressure goes too low. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 286 | const bool compressor_on = !pressure_switch_->Get(); |
| 287 | to_log.compressor_on = compressor_on; |
| 288 | if (compressor_on) { |
| 289 | compressor_relay_->Set(Relay::kForward); |
| 290 | } else { |
| 291 | compressor_relay_->Set(Relay::kOff); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | pcm_->Flush(); |
| 296 | to_log.read_solenoids = pcm_->GetAll(); |
| 297 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void Quit() { run_ = false; } |
| 303 | |
| 304 | private: |
| 305 | const ::std::unique_ptr<BufferedPcm> &pcm_; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 306 | |
| 307 | ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_; |
Jasmine Zhou | da77c5f | 2015-09-12 15:16:10 -0700 | [diff] [blame] | 308 | ::std::unique_ptr<BufferedSolenoid> elevator_can_support_; |
| 309 | ::std::unique_ptr<BufferedSolenoid> intake_claw_; |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 310 | ::std::unique_ptr<BufferedSolenoid> can_grabber_; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 311 | |
Austin Schuh | 39e5abc | 2015-10-31 18:51:20 -0700 | [diff] [blame] | 312 | ::std::unique_ptr<DigitalInput> pressure_switch_; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 313 | ::std::unique_ptr<Relay> compressor_relay_; |
| 314 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 315 | ::aos::Queue<::bot3::control_loops::ElevatorQueue::Output> elevator_; |
Jasmine Zhou | da77c5f | 2015-09-12 15:16:10 -0700 | [diff] [blame] | 316 | ::aos::Queue<::bot3::control_loops::IntakeQueue::Output> intake_; |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 317 | ::aos::Queue<::bot3::autonomous::CanGrabberControl> can_grabber_control_; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 318 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 319 | ::std::atomic<bool> run_{true}; |
| 320 | }; |
| 321 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 322 | // Writes out drivetrain voltages. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 323 | class DrivetrainWriter : public LoopOutputHandler { |
| 324 | public: |
| 325 | void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) { |
| 326 | left_drivetrain_talon_ = ::std::move(t); |
| 327 | } |
| 328 | |
| 329 | void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) { |
| 330 | right_drivetrain_talon_ = ::std::move(t); |
| 331 | } |
| 332 | |
| 333 | private: |
| 334 | virtual void Read() override { |
| 335 | ::bot3::control_loops::drivetrain_queue.output.FetchAnother(); |
| 336 | } |
| 337 | |
| 338 | virtual void Write() override { |
| 339 | auto &queue = ::bot3::control_loops::drivetrain_queue.output; |
| 340 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 341 | left_drivetrain_talon_->Set(queue->left_voltage / 12.0); |
| 342 | right_drivetrain_talon_->Set(-queue->right_voltage / 12.0); |
| 343 | } |
| 344 | |
| 345 | virtual void Stop() override { |
| 346 | LOG(WARNING, "drivetrain output too old\n"); |
| 347 | left_drivetrain_talon_->Disable(); |
| 348 | right_drivetrain_talon_->Disable(); |
| 349 | } |
| 350 | |
| 351 | ::std::unique_ptr<Talon> left_drivetrain_talon_; |
| 352 | ::std::unique_ptr<Talon> right_drivetrain_talon_; |
| 353 | }; |
| 354 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 355 | // Writes out elevator voltages. |
| 356 | class ElevatorWriter : public LoopOutputHandler { |
| 357 | public: |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 358 | void set_elevator_talon1(::std::unique_ptr<Talon> t) { |
| 359 | elevator_talon1_ = ::std::move(t); |
| 360 | } |
| 361 | void set_elevator_talon2(::std::unique_ptr<Talon> t) { |
| 362 | elevator_talon2_ = ::std::move(t); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | private: |
| 366 | virtual void Read() override { |
| 367 | ::bot3::control_loops::elevator_queue.output.FetchAnother(); |
| 368 | } |
| 369 | |
| 370 | virtual void Write() override { |
| 371 | auto &queue = ::bot3::control_loops::elevator_queue.output; |
| 372 | LOG_STRUCT(DEBUG, "will output", *queue); |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 373 | elevator_talon1_->Set(queue->elevator / 12.0); |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 374 | elevator_talon2_->Set(-queue->elevator / 12.0); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | virtual void Stop() override { |
| 378 | LOG(WARNING, "Elevator output too old.\n"); |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 379 | elevator_talon1_->Disable(); |
| 380 | elevator_talon2_->Disable(); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 383 | ::std::unique_ptr<Talon> elevator_talon1_; |
| 384 | ::std::unique_ptr<Talon> elevator_talon2_; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | // Writes out intake voltages. |
| 388 | class IntakeWriter : public LoopOutputHandler { |
| 389 | public: |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 390 | void set_intake_talon1(::std::unique_ptr<Talon> t) { |
| 391 | intake_talon1_ = ::std::move(t); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 392 | } |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 393 | void set_intake_talon2(::std::unique_ptr<Talon> t) { |
| 394 | intake_talon2_ = ::std::move(t); |
| 395 | } |
| 396 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 397 | private: |
| 398 | virtual void Read() override { |
| 399 | ::bot3::control_loops::intake_queue.output.FetchAnother(); |
| 400 | } |
| 401 | |
| 402 | virtual void Write() override { |
| 403 | auto &queue = ::bot3::control_loops::intake_queue.output; |
| 404 | LOG_STRUCT(DEBUG, "will output", *queue); |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 405 | intake_talon1_->Set(queue->intake / 12.0); |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 406 | intake_talon2_->Set(-queue->intake / 12.0); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | virtual void Stop() override { |
| 410 | LOG(WARNING, "Intake output too old.\n"); |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 411 | intake_talon1_->Disable(); |
| 412 | intake_talon2_->Disable(); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 415 | ::std::unique_ptr<Talon> intake_talon1_; |
| 416 | ::std::unique_ptr<Talon> intake_talon2_; |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 419 | // Writes out can grabber voltages. |
| 420 | class CanGrabberWriter : public LoopOutputHandler { |
| 421 | public: |
| 422 | CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {} |
| 423 | |
| 424 | void set_can_grabber_talon1(::std::unique_ptr<Talon> t) { |
| 425 | can_grabber_talon1_ = ::std::move(t); |
| 426 | } |
| 427 | |
| 428 | void set_can_grabber_talon2(::std::unique_ptr<Talon> t) { |
| 429 | can_grabber_talon2_ = ::std::move(t); |
| 430 | } |
| 431 | |
| 432 | private: |
| 433 | virtual void Read() override { |
| 434 | ::bot3::autonomous::can_grabber_control.FetchAnother(); |
| 435 | } |
| 436 | |
| 437 | virtual void Write() override { |
| 438 | auto &queue = ::bot3::autonomous::can_grabber_control; |
| 439 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 440 | can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0); |
| 441 | can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0); |
| 442 | } |
| 443 | |
| 444 | virtual void Stop() override { |
| 445 | LOG(WARNING, "Can grabber output too old\n"); |
| 446 | can_grabber_talon1_->Disable(); |
| 447 | can_grabber_talon2_->Disable(); |
| 448 | } |
| 449 | |
| 450 | ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_; |
| 451 | }; |
| 452 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 453 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 454 | // have support. |
| 455 | template <class T, class... U> |
| 456 | std::unique_ptr<T> make_unique(U &&... u) { |
| 457 | return std::unique_ptr<T>(new T(std::forward<U>(u)...)); |
| 458 | } |
| 459 | |
| 460 | class WPILibRobot : public RobotBase { |
| 461 | public: |
| 462 | ::std::unique_ptr<Encoder> encoder(int index) { |
| 463 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 464 | Encoder::k4X); |
| 465 | } |
| 466 | virtual void StartCompetition() { |
| 467 | ::aos::InitNRT(); |
| 468 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 469 | |
| 470 | JoystickSender joystick_sender; |
| 471 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 472 | |
| 473 | SensorReader reader; |
| 474 | LOG(INFO, "Creating the reader\n"); |
| 475 | |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 476 | reader.set_elevator_encoder(encoder(6)); |
Austin Schuh | 39e5abc | 2015-10-31 18:51:20 -0700 | [diff] [blame] | 477 | reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6)); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 478 | |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 479 | reader.set_left_encoder(encoder(0)); |
| 480 | reader.set_right_encoder(encoder(1)); |
Austin Schuh | d6bc8ba | 2015-09-13 19:39:38 +0000 | [diff] [blame] | 481 | reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0)); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 482 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 483 | ::std::thread reader_thread(::std::ref(reader)); |
| 484 | GyroSender gyro_sender; |
| 485 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 486 | |
| 487 | DrivetrainWriter drivetrain_writer; |
| 488 | drivetrain_writer.set_left_drivetrain_talon( |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 489 | ::std::unique_ptr<Talon>(new Talon(0))); |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 490 | drivetrain_writer.set_right_drivetrain_talon( |
| 491 | ::std::unique_ptr<Talon>(new Talon(7))); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 492 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 493 | |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 494 | ElevatorWriter elevator_writer; |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 495 | elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1))); |
| 496 | elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6))); |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 497 | ::std::thread elevator_writer_thread(::std::ref(elevator_writer)); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 498 | |
| 499 | IntakeWriter intake_writer; |
Jasmine Zhou | 954419a | 2015-09-12 18:31:31 -0700 | [diff] [blame] | 500 | intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2))); |
| 501 | intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5))); |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 502 | ::std::thread intake_writer_thread(::std::ref(intake_writer)); |
Comran Morshed | e140e38 | 2015-08-19 20:35:25 +0000 | [diff] [blame] | 503 | |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 504 | CanGrabberWriter can_grabber_writer; |
| 505 | can_grabber_writer.set_can_grabber_talon1( |
| 506 | ::std::unique_ptr<Talon>(new Talon(3))); |
| 507 | can_grabber_writer.set_can_grabber_talon2( |
| 508 | ::std::unique_ptr<Talon>(new Talon(4))); |
| 509 | ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer)); |
| 510 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 511 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 512 | new ::frc971::wpilib::BufferedPcm()); |
| 513 | SolenoidWriter solenoid_writer(pcm); |
| 514 | solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9)); |
| 515 | solenoid_writer.set_compressor_relay(make_unique<Relay>(0)); |
Jasmine Zhou | da77c5f | 2015-09-12 15:16:10 -0700 | [diff] [blame] | 516 | solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0)); |
| 517 | solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1)); |
| 518 | solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2)); |
Austin Schuh | bd01a58 | 2015-09-21 00:05:31 +0000 | [diff] [blame] | 519 | solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3)); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 520 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 521 | |
| 522 | // Wait forever. Not much else to do... |
| 523 | PCHECK(select(0, nullptr, nullptr, nullptr, nullptr)); |
| 524 | |
| 525 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 526 | |
| 527 | joystick_sender.Quit(); |
| 528 | joystick_thread.join(); |
| 529 | reader.Quit(); |
| 530 | reader_thread.join(); |
| 531 | gyro_sender.Quit(); |
| 532 | gyro_thread.join(); |
| 533 | |
| 534 | drivetrain_writer.Quit(); |
| 535 | drivetrain_writer_thread.join(); |
Austin Schuh | a3b2eef | 2015-09-13 07:52:02 +0000 | [diff] [blame] | 536 | |
| 537 | elevator_writer.Quit(); |
| 538 | elevator_writer_thread.join(); |
| 539 | |
| 540 | intake_writer.Quit(); |
| 541 | intake_writer_thread.join(); |
| 542 | |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 543 | can_grabber_writer.Quit(); |
| 544 | can_grabber_writer_thread.join(); |
| 545 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 546 | solenoid_writer.Quit(); |
| 547 | solenoid_thread.join(); |
| 548 | |
| 549 | ::aos::Cleanup(); |
| 550 | } |
| 551 | }; |
| 552 | |
| 553 | } // namespace wpilib |
| 554 | } // namespace bot3 |
| 555 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 556 | START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot); |