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