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