Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
| 6 | #include <chrono> |
| 7 | #include <thread> |
| 8 | #include <mutex> |
| 9 | #include <functional> |
| 10 | #include <array> |
| 11 | |
| 12 | #include "Encoder.h" |
| 13 | #include "VictorSP.h" |
| 14 | #include "Relay.h" |
| 15 | #include "DriverStation.h" |
| 16 | #include "AnalogInput.h" |
| 17 | #include "Compressor.h" |
| 18 | #include "DigitalGlitchFilter.h" |
| 19 | #undef ERROR |
| 20 | |
| 21 | #include "aos/common/logging/logging.h" |
| 22 | #include "aos/common/logging/queue_logging.h" |
| 23 | #include "aos/common/time.h" |
| 24 | #include "aos/common/util/log_interval.h" |
| 25 | #include "aos/common/util/phased_loop.h" |
| 26 | #include "aos/common/util/wrapping_counter.h" |
| 27 | #include "aos/common/stl_mutex.h" |
| 28 | #include "aos/linux_code/init.h" |
| 29 | #include "aos/common/messages/robot_state.q.h" |
| 30 | #include "aos/common/commonmath.h" |
| 31 | |
| 32 | #include "frc971/control_loops/control_loops.q.h" |
| 33 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 34 | #include "y2017/constants.h" |
| 35 | #include "y2017/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 36 | #include "y2017/control_loops/superstructure/superstructure.q.h" |
| 37 | #include "y2017/actors/autonomous_action.q.h" |
| 38 | |
| 39 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 40 | #include "frc971/wpilib/joystick_sender.h" |
| 41 | #include "frc971/wpilib/loop_output_handler.h" |
| 42 | #include "frc971/wpilib/buffered_solenoid.h" |
| 43 | #include "frc971/wpilib/buffered_pcm.h" |
| 44 | #include "frc971/wpilib/gyro_sender.h" |
| 45 | #include "frc971/wpilib/dma_edge_counting.h" |
| 46 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 47 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 48 | #include "frc971/wpilib/logging.q.h" |
| 49 | #include "frc971/wpilib/wpilib_interface.h" |
| 50 | #include "frc971/wpilib/pdp_fetcher.h" |
| 51 | #include "frc971/wpilib/ADIS16448.h" |
| 52 | #include "frc971/wpilib/dma.h" |
| 53 | |
| 54 | #ifndef M_PI |
| 55 | #define M_PI 3.14159265358979323846 |
| 56 | #endif |
| 57 | |
| 58 | using ::frc971::control_loops::drivetrain_queue; |
| 59 | using ::y2017::control_loops::superstructure_queue; |
| 60 | |
| 61 | namespace y2017 { |
| 62 | namespace wpilib { |
| 63 | namespace { |
| 64 | constexpr double kMaxBringupPower = 12.0; |
| 65 | } // namespace |
| 66 | |
| 67 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 68 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 69 | // The low bit is direction. |
| 70 | |
| 71 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 72 | // have support. |
| 73 | template <class T, class... U> |
| 74 | std::unique_ptr<T> make_unique(U &&... u) { |
| 75 | return std::unique_ptr<T>(new T(std::forward<U>(u)...)); |
| 76 | } |
| 77 | |
| 78 | // Translates for the sensor values to convert raw index pulses into something |
| 79 | // with proper units. |
| 80 | // TODO(campbell): Update everything below to match sensors on the robot. |
| 81 | |
| 82 | // TODO(comran): Template these methods since there is a lot of repetition here. |
| 83 | double drivetrain_translate(int32_t in) { |
| 84 | return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) * |
| 85 | constants::Values::kDrivetrainEncoderRatio * |
| 86 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
| 87 | } |
| 88 | |
| 89 | double drivetrain_velocity_translate(double in) { |
| 90 | return (1.0 / in) / 256.0 /*cpr*/ * |
| 91 | constants::Values::kDrivetrainEncoderRatio * |
| 92 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
| 93 | } |
| 94 | |
| 95 | double shooter_translate(int32_t in) { |
| 96 | return static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) * |
| 97 | constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/); |
| 98 | } |
| 99 | |
| 100 | double intake_translate(int32_t in) { |
| 101 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 102 | constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/); |
| 103 | } |
| 104 | |
| 105 | double intake_pot_translate(double voltage) { |
| 106 | return voltage * constants::Values::kIntakePotRatio * |
| 107 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 108 | } |
| 109 | |
| 110 | double turret_translate(int32_t in) { |
| 111 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 112 | constants::Values::kTurretEncoderRatio * (2 * M_PI /*radians*/); |
| 113 | } |
| 114 | |
| 115 | double turret_pot_translate(double voltage) { |
| 116 | return voltage * constants::Values::kTurretPotRatio * |
| 117 | (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 118 | } |
| 119 | |
| 120 | double serializer_translate(int32_t in) { |
| 121 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 122 | constants::Values::kSerializerEncoderRatio * |
| 123 | (2 * M_PI /*radians*/); |
| 124 | } |
| 125 | |
| 126 | double hood_translate(int32_t in) { |
| 127 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 128 | constants::Values::kHoodEncoderRatio * (2 * M_PI /*radians*/); |
| 129 | } |
| 130 | |
| 131 | // TODO(campbell): Update all gear ratios below. |
| 132 | |
| 133 | constexpr double kMaxDrivetrainEncoderPulsesPerSecond = |
| 134 | 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 / |
| 135 | 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ / |
| 136 | 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */; |
| 137 | |
| 138 | constexpr double kMaxIntakeEncoderPulsesPerSecond = |
| 139 | 18700.0 /* 775pro free speed RPM */ * 12.0 / |
| 140 | 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ * |
| 141 | 128.0 /* CPR */ * 4 /* edges per cycle */; |
| 142 | |
| 143 | constexpr double kMaxShooterEncoderPulsesPerSecond = |
| 144 | 18700.0 /* 775pro free speed RPM */ * 12.0 / |
| 145 | 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ * |
| 146 | 128.0 /* CPR */ * 4 /* edges per cycle */; |
| 147 | |
| 148 | constexpr double kMaxSerializerEncoderPulsesPerSecond = |
| 149 | 18700.0 /* 775pro free speed RPM */ * 12.0 / |
| 150 | 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ * |
| 151 | 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */; |
| 152 | |
| 153 | double kMaxEncoderPulsesPerSecond = |
| 154 | ::std::max(kMaxSerializerEncoderPulsesPerSecond, |
| 155 | ::std::max(kMaxIntakeEncoderPulsesPerSecond, |
| 156 | ::std::max(kMaxDrivetrainEncoderPulsesPerSecond, |
| 157 | kMaxShooterEncoderPulsesPerSecond))); |
| 158 | |
| 159 | // Class to send position messages with sensor readings to our loops. |
| 160 | class SensorReader { |
| 161 | public: |
| 162 | SensorReader() { |
| 163 | // Set it to filter out anything shorter than 1/4 of the minimum pulse width |
| 164 | // we should ever see. |
| 165 | drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds( |
| 166 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 167 | kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 + |
| 168 | 0.5)); |
| 169 | superstructure_encoder_filter_.SetPeriodNanoSeconds( |
| 170 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 171 | kMaxSuperstructureEncoderPulsesPerSecond * 1e9 + |
| 172 | 0.5)); |
| 173 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 174 | } |
| 175 | |
| 176 | // Drivetrain setters. |
| 177 | void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) { |
| 178 | drivetrain_shooter_encoder_filter_.Add(encoder.get()); |
| 179 | drivetrain_left_encoder_ = ::std::move(encoder); |
| 180 | } |
| 181 | |
| 182 | void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) { |
| 183 | drivetrain_shooter_encoder_filter_.Add(encoder.get()); |
| 184 | drivetrain_right_encoder_ = ::std::move(encoder); |
| 185 | } |
| 186 | |
| 187 | // Shooter setter. |
| 188 | void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) { |
| 189 | drivetrain_shooter_encoder_filter_.Add(encoder.get()); |
| 190 | shooter_encoder_ = ::std::move(encoder); |
| 191 | } |
| 192 | |
| 193 | // Intake setters. |
| 194 | void set_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
| 195 | superstructure_encoder_filter_.Add(encoder.get()); |
| 196 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 197 | } |
| 198 | |
| 199 | void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 200 | intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 201 | } |
| 202 | |
| 203 | void set_intake_index(::std::unique_ptr<DigitalInput> index) { |
| 204 | superstructure_encoder_filter_.Add(index.get()); |
| 205 | intake_encoder_.set_index(::std::move(index)); |
| 206 | } |
| 207 | |
| 208 | // Serializer setters. |
| 209 | void set_serializer_encoder(::std::unique_ptr<Encoder> encoder) { |
| 210 | serializer_encoder_ = ::std::move(encoder); |
| 211 | } |
| 212 | |
| 213 | // Turret setters. |
| 214 | void set_turret_encoder(::std::unique_ptr<Encoder> encoder) { |
| 215 | superstructure_encoder_filter_.Add(encoder.get()); |
| 216 | turret_encoder_.set_encoder(::std::move(encoder)); |
| 217 | } |
| 218 | |
| 219 | void set_turret_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 220 | turret_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 221 | } |
| 222 | |
| 223 | void set_turret_index(::std::unique_ptr<DigitalInput> index) { |
| 224 | superstructure_encoder_filter_.Add(index.get()); |
| 225 | turret_encoder_.set_index(::std::move(index)); |
| 226 | } |
| 227 | |
| 228 | // Shooter hood setter. |
| 229 | void set_hood_encoder(::std::unique_ptr<Encoder> encoder) { |
| 230 | superstructure_encoder_filter_.Add(encoder.get()); |
| 231 | hood_encoder_.set_encoder(::std::move(encoder)); |
| 232 | } |
| 233 | |
| 234 | void set_hood_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 235 | hood_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 236 | } |
| 237 | |
| 238 | void set_hood_index(::std::unique_ptr<DigitalInput> index) { |
| 239 | superstructure_encoder_filter_.Add(index.get()); |
| 240 | hood_encoder_.set_index(::std::move(index)); |
| 241 | } |
| 242 | |
| 243 | // Autonomous mode switch setter. |
| 244 | void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) { |
| 245 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | // All of the DMA-related set_* calls must be made before this, and it doesn't |
| 250 | // hurt to do all of them. |
| 251 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 252 | dma_synchronizer_.reset( |
| 253 | new ::frc971::wpilib::DMASynchronizer(::std::move(dma))); |
| 254 | dma_synchronizer_->Add(&intake_encoder_); |
| 255 | dma_synchronizer_->Add(&turret_encoder_); |
| 256 | dma_synchronizer_->Add(&hood_encoder_); |
| 257 | } |
| 258 | |
| 259 | void operator()() { |
| 260 | ::aos::SetCurrentThreadName("SensorReader"); |
| 261 | |
| 262 | my_pid_ = getpid(); |
| 263 | ds_ = |
| 264 | &DriverStation::GetInstance(); |
| 265 | |
| 266 | dma_synchronizer_->Start(); |
| 267 | |
| 268 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 269 | ::std::chrono::milliseconds(4)); |
| 270 | |
| 271 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 272 | while (run_) { |
| 273 | { |
| 274 | const int iterations = phased_loop.SleepUntilNext(); |
| 275 | if (iterations != 1) { |
| 276 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 277 | } |
| 278 | } |
| 279 | RunIteration(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void RunIteration() { |
| 284 | ::frc971::wpilib::SendRobotState(my_pid_, ds_); |
| 285 | |
| 286 | const auto values = constants::GetValues(); |
| 287 | |
| 288 | { |
| 289 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 290 | drivetrain_message->right_encoder = |
| 291 | drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 292 | drivetrain_message->left_encoder = |
| 293 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 294 | drivetrain_message->left_speed = |
| 295 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 296 | drivetrain_message->right_speed = |
| 297 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 298 | |
| 299 | drivetrain_message.Send(); |
| 300 | } |
| 301 | |
| 302 | dma_synchronizer_->RunIteration(); |
| 303 | |
| 304 | { |
| 305 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 306 | CopyPotAndAbsolutePosition( |
| 307 | intake_encoder_, &superstructure_message->intake, intake_translate, |
| 308 | intake_pot_translate, false, values.intake_pot_offset); |
| 309 | |
| 310 | superstructure_message->theta_serializer = |
| 311 | serializer_translate(serializer_encoder_->GetRaw()); |
| 312 | |
| 313 | superstructure_message->theta_shooter= |
| 314 | shooter_translate(shooter_encoder_->GetRaw()); |
| 315 | |
| 316 | CopyPotAndAbsolutePosition(hood_encoder_, &superstructure_message->hood, |
| 317 | hood_translate, hood_pot_translate, false, |
| 318 | values.hood_pot_offset); |
| 319 | |
| 320 | CopyPotAndAbsolutePosition(turret_encoder_, |
| 321 | &superstructure_message->turret, |
| 322 | turret_translate, turret_pot_translate, false, |
| 323 | values.turret_pot_offset); |
| 324 | |
| 325 | superstructure_message.Send(); |
| 326 | } |
| 327 | |
| 328 | { |
| 329 | auto auto_mode_message = ::y2017::actors::auto_mode.MakeMessage(); |
| 330 | auto_mode_message->mode = 0; |
| 331 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 332 | if (autonomous_modes_[i]->Get()) { |
| 333 | auto_mode_message->mode |= 1 << i; |
| 334 | } |
| 335 | } |
| 336 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 337 | auto_mode_message.Send(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void Quit() { run_ = false; } |
| 342 | |
| 343 | private: |
| 344 | void CopyPotAndIndexPosition( |
| 345 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 346 | ::frc971::PotAndIndexPosition *position, |
| 347 | ::std::function<double(int32_t)> encoder_translate, |
| 348 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 349 | double pot_offset) { |
| 350 | const double multiplier = reverse ? -1.0 : 1.0; |
| 351 | position->encoder = |
| 352 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 353 | position->pot = multiplier * potentiometer_translate( |
| 354 | encoder.polled_potentiometer_voltage()) + |
| 355 | pot_offset; |
| 356 | position->latched_encoder = |
| 357 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 358 | position->latched_pot = |
| 359 | multiplier * |
| 360 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 361 | pot_offset; |
| 362 | position->index_pulses = encoder.index_posedge_count(); |
| 363 | } |
| 364 | |
| 365 | // TODO(campbell): Fix this stuff. It is all wrong. |
| 366 | void CopyPotAndAbsolutePosition( |
| 367 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 368 | ::frc971::PotAndAbsolutePosition *position, |
| 369 | ::std::function<double(int32_t)> encoder_translate, |
| 370 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 371 | double pot_offset) { |
| 372 | const double multiplier = reverse ? -1.0 : 1.0; |
| 373 | position->pot = multiplier * potentiometer_translate( |
| 374 | encoder.polled_potentiometer_voltage()) + |
| 375 | pot_offset; |
| 376 | position->relative_encoder = |
| 377 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 378 | position->absolute_encoder = |
| 379 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 380 | } |
| 381 | |
| 382 | // TODO(campbell): Fix this stuff. It is all wrong. |
| 383 | void CopyAbsoluteAndIndexPosition( |
| 384 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 385 | ::frc971::EncoderAndIndexPosition *position, |
| 386 | ::std::function<double(int32_t)> encoder_translate, bool reverse) { |
| 387 | const double multiplier = reverse ? -1.0 : 1.0; |
| 388 | position->encoder = |
| 389 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 390 | position->latched_encoder = |
| 391 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 392 | position->index_pulses = encoder.index_posedge_count(); |
| 393 | } |
| 394 | |
| 395 | int32_t my_pid_; |
| 396 | DriverStation *ds_; |
| 397 | |
| 398 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 399 | |
| 400 | ::std::unique_ptr<Encoder> drivetrain_left_encoder_, |
| 401 | drivetrain_right_encoder_; |
| 402 | |
| 403 | ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_; |
| 404 | |
| 405 | ::std::unique_ptr<Encoder> serializer_encoder_; |
| 406 | ::std::unique_ptr<AnalogInput> serializer_hall_; |
| 407 | |
| 408 | ::frc971::wpilib::DMAEncoderAndPotentiometer turret_encoder_; |
| 409 | ::frc971::wpilib::DMAEncoderAndPotentiometer hood_encoder_; |
| 410 | ::std::unique_ptr<Encoder> shooter_encoder_; |
| 411 | |
| 412 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
| 413 | |
| 414 | ::std::atomic<bool> run_{true}; |
| 415 | DigitalGlitchFilter drivetrain_shooter_encoder_filter_, |
| 416 | superstructure_encoder_filter_, hall_filter_; |
| 417 | }; |
| 418 | |
| 419 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 420 | public: |
| 421 | void set_drivetrain_left_victor(::std::unique_ptr<VictorSP> t) { |
| 422 | drivetrain_left_victor_ = ::std::move(t); |
| 423 | } |
| 424 | |
| 425 | void set_drivetrain_right_victor(::std::unique_ptr<VictorSP> t) { |
| 426 | drivetrain_right_victor_ = ::std::move(t); |
| 427 | } |
| 428 | |
| 429 | private: |
| 430 | virtual void Read() override { |
| 431 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 432 | } |
| 433 | |
| 434 | virtual void Write() override { |
| 435 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 436 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 437 | drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0); |
| 438 | drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0); |
| 439 | } |
| 440 | |
| 441 | virtual void Stop() override { |
| 442 | LOG(WARNING, "drivetrain output too old\n"); |
| 443 | drivetrain_left_victor_->SetDisabled(); |
| 444 | drivetrain_right_victor_->SetDisabled(); |
| 445 | } |
| 446 | |
| 447 | ::std::unique_ptr<VictorSP> drivetrain_left_victor_, drivetrain_right_victor_; |
| 448 | }; |
| 449 | |
| 450 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 451 | public: |
| 452 | void set_intake_victor(::std::unique_ptr<VictorSP> t) { |
| 453 | intake_victor_ = ::std::move(t); |
| 454 | } |
| 455 | void set_intake_rollers_victor(::std::unique_ptr<VictorSP> t) { |
| 456 | intake_rollers_victor_ = ::std::move(t); |
| 457 | } |
| 458 | |
| 459 | void set_serializer_victor(::std::unique_ptr<VictorSP> t) { |
| 460 | serializer_victor_ = ::std::move(t); |
| 461 | } |
| 462 | void set_serializer_roller_victor(::std::unique_ptr<VictorSP> t) { |
| 463 | serializer_roller_victor_ = ::std::move(t); |
| 464 | } |
| 465 | |
| 466 | void set_shooter_victor(::std::unique_ptr<VictorSP> t) { |
| 467 | shooter_victor_ = ::std::move(t); |
| 468 | } |
| 469 | void set_turret_victor(::std::unique_ptr<VictorSP> t) { |
| 470 | turret_victor_ = ::std::move(t); |
| 471 | } |
| 472 | void set_hood_victor(::std::unique_ptr<VictorSP> t) { |
| 473 | hood_victor_ = ::std::move(t); |
| 474 | } |
| 475 | |
| 476 | private: |
| 477 | virtual void Read() override { |
| 478 | ::y2017::control_loops::superstructure_queue.output.FetchAnother(); |
| 479 | } |
| 480 | |
| 481 | virtual void Write() override { |
| 482 | auto &queue = ::y2017::control_loops::superstructure_queue.output; |
| 483 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 484 | intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake, |
| 485 | -kMaxBringupPower, kMaxBringupPower) / |
| 486 | 12.0); |
| 487 | intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0); |
| 488 | serializer_victor_->SetSpeed(queue->voltage_serializer / 12.0); |
| 489 | serializer_roller_victor_->SetSpeed(queue->voltage_serializer_rollers / |
| 490 | 12.0); |
| 491 | turret_victor_->SetSpeed(::aos::Clip(queue->voltage_turret, |
| 492 | -kMaxBringupPower, kMaxBringupPower) / |
| 493 | 12.0); |
| 494 | hood_victor_->SetSpeed( |
| 495 | ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) / |
| 496 | 12.0); |
| 497 | shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0); |
| 498 | } |
| 499 | |
| 500 | virtual void Stop() override { |
| 501 | LOG(WARNING, "Superstructure output too old.\n"); |
| 502 | intake_victor_->SetDisabled(); |
| 503 | intake_rollers_victor_->SetDisabled(); |
| 504 | serializer_victor_->SetDisabled(); |
| 505 | serializer_roller_victor_->SetDisabled(); |
| 506 | turret_victor_->SetDisabled(); |
| 507 | hood_victor_->SetDisabled(); |
| 508 | shooter_victor_->SetDisabled(); |
| 509 | } |
| 510 | |
| 511 | ::std::unique_ptr<VictorSP> intake_victor_, intake_rollers_victor_, |
| 512 | serializer_victor_, serializer_roller_victor_, shooter_victor_, |
| 513 | turret_victor_, hood_victor_; |
| 514 | }; |
| 515 | |
| 516 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 517 | public: |
| 518 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 519 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 520 | Encoder::k4X); |
| 521 | } |
| 522 | |
| 523 | void Run() override { |
| 524 | ::aos::InitNRT(); |
| 525 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 526 | |
| 527 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 528 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 529 | |
| 530 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 531 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 532 | SensorReader reader; |
| 533 | |
| 534 | // TODO(campbell): Update port numbers |
| 535 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 536 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 537 | |
| 538 | reader.set_intake_encoder(make_encoder(2)); |
| 539 | reader.set_intake_index(make_unique<DigitalInput>(0)); |
| 540 | reader.set_intake_potentiometer(make_unique<AnalogInput>(0)); |
| 541 | |
| 542 | reader.set_serializer_encoder(make_encoder(3)); |
| 543 | reader.set_serializer_hall(make_unique<AnalogInput>(1)); |
| 544 | |
| 545 | reader.set_turret_encoder(make_encoder(5)); |
| 546 | reader.set_turret_index(make_unique<DigitalInput>(1)); |
| 547 | reader.set_turret_potentiometer(make_unique<AnalogInput>(3)); |
| 548 | |
| 549 | reader.set_hood_encoder(make_encoder(6)); |
| 550 | reader.set_hood_index(make_unique<DigitalInput>(2)); |
| 551 | |
| 552 | reader.set_shooter_encoder(make_encoder(7)); |
| 553 | |
| 554 | reader.set_autonomous_mode(0, make_unique<DigitalInput>(6)); |
| 555 | reader.set_autonomous_mode(1, make_unique<DigitalInput>(5)); |
| 556 | reader.set_autonomous_mode(2, make_unique<DigitalInput>(4)); |
| 557 | reader.set_autonomous_mode(3, make_unique<DigitalInput>(3)); |
| 558 | |
| 559 | reader.set_dma(make_unique<DMA>()); |
| 560 | ::std::thread reader_thread(::std::ref(reader)); |
| 561 | |
| 562 | ::frc971::wpilib::GyroSender gyro_sender; |
| 563 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 564 | |
| 565 | auto imu_trigger = make_unique<DigitalInput>(5); |
| 566 | ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get()); |
| 567 | ::std::thread imu_thread(::std::ref(imu)); |
| 568 | |
| 569 | DrivetrainWriter drivetrain_writer; |
| 570 | drivetrain_writer.set_drivetrain_left_victor( |
| 571 | ::std::unique_ptr<VictorSP>(new VictorSP(0))); |
| 572 | drivetrain_writer.set_drivetrain_right_victor( |
| 573 | ::std::unique_ptr<VictorSP>(new VictorSP(1))); |
| 574 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 575 | |
| 576 | SuperstructureWriter superstructure_writer; |
| 577 | superstructure_writer.set_intake_victor( |
| 578 | ::std::unique_ptr<VictorSP>(new VictorSP(2))); |
| 579 | superstructure_writer.set_intake_rollers_victor( |
| 580 | ::std::unique_ptr<VictorSP>(new VictorSP(3))); |
| 581 | superstructure_writer.set_serializer_victor( |
| 582 | ::std::unique_ptr<VictorSP>(new VictorSP(4))); |
| 583 | superstructure_writer.set_serializer_roller_victor( |
| 584 | ::std::unique_ptr<VictorSP>(new VictorSP(5))); |
| 585 | superstructure_writer.set_turret_victor( |
| 586 | ::std::unique_ptr<VictorSP>(new VictorSP(6))); |
| 587 | superstructure_writer.set_hood_victor( |
| 588 | ::std::unique_ptr<VictorSP>(new VictorSP(7))); |
| 589 | superstructure_writer.set_shooter_victor( |
| 590 | ::std::unique_ptr<VictorSP>(new VictorSP(8))); |
| 591 | ::std::thread superstructure_writer_thread( |
| 592 | ::std::ref(superstructure_writer)); |
| 593 | |
| 594 | // Wait forever. Not much else to do... |
| 595 | while (true) { |
| 596 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 597 | if (r != 0) { |
| 598 | PLOG(WARNING, "infinite select failed"); |
| 599 | } else { |
| 600 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 605 | |
| 606 | joystick_sender.Quit(); |
| 607 | joystick_thread.join(); |
| 608 | pdp_fetcher.Quit(); |
| 609 | pdp_fetcher_thread.join(); |
| 610 | reader.Quit(); |
| 611 | reader_thread.join(); |
| 612 | gyro_sender.Quit(); |
| 613 | gyro_thread.join(); |
| 614 | imu.Quit(); |
| 615 | imu_thread.join(); |
| 616 | |
| 617 | drivetrain_writer.Quit(); |
| 618 | drivetrain_writer_thread.join(); |
| 619 | superstructure_writer.Quit(); |
| 620 | superstructure_writer_thread.join(); |
| 621 | |
| 622 | ::aos::Cleanup(); |
| 623 | } |
| 624 | }; |
| 625 | |
| 626 | } // namespace wpilib |
| 627 | } // namespace y2017 |
| 628 | |
| 629 | AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot); |