Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <array> |
| 7 | #include <chrono> |
| 8 | #include <cmath> |
| 9 | #include <functional> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
| 13 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 14 | #include "frc971/wpilib/ahal/Counter.h" |
| 15 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 16 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 17 | #include "frc971/wpilib/ahal/Encoder.h" |
| 18 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 19 | #undef ERROR |
| 20 | |
| 21 | #include "aos/commonmath.h" |
| 22 | #include "aos/init.h" |
| 23 | #include "aos/logging/logging.h" |
| 24 | #include "aos/logging/queue_logging.h" |
| 25 | #include "aos/make_unique.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 26 | #include "aos/time/time.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 27 | #include "aos/util/log_interval.h" |
| 28 | #include "aos/util/phased_loop.h" |
| 29 | #include "aos/util/wrapping_counter.h" |
| 30 | |
| 31 | #include "frc971/autonomous/auto.q.h" |
| 32 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 33 | #include "frc971/wpilib/ADIS16448.h" |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame^] | 34 | #include "frc971/wpilib/buffered_pcm.h" |
| 35 | #include "frc971/wpilib/buffered_solenoid.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/dma.h" |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 37 | #include "frc971/wpilib/drivetrain_writer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 38 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 39 | #include "frc971/wpilib/joystick_sender.h" |
| 40 | #include "frc971/wpilib/logging.q.h" |
| 41 | #include "frc971/wpilib/loop_output_handler.h" |
| 42 | #include "frc971/wpilib/pdp_fetcher.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 43 | #include "frc971/wpilib/sensor_reader.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 44 | #include "frc971/wpilib/wpilib_robot_base.h" |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame^] | 45 | #include "third_party/Phoenix-frc-lib/cpp/include/ctre/phoenix/MotorControl/CAN/TalonSRX.h" |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 46 | #include "y2019/constants.h" |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 47 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 48 | |
| 49 | #ifndef M_PI |
| 50 | #define M_PI 3.14159265358979323846 |
| 51 | #endif |
| 52 | |
| 53 | using ::frc971::control_loops::drivetrain_queue; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 54 | using ::y2019::control_loops::superstructure::superstructure_queue; |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 55 | using ::y2019::constants::Values; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 56 | using ::aos::monotonic_clock; |
| 57 | namespace chrono = ::std::chrono; |
| 58 | using aos::make_unique; |
| 59 | |
| 60 | namespace y2019 { |
| 61 | namespace wpilib { |
| 62 | namespace { |
| 63 | |
| 64 | constexpr double kMaxBringupPower = 12.0; |
| 65 | |
| 66 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 67 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 68 | // The low bit is direction. |
| 69 | |
| 70 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 71 | // constexpr. |
| 72 | template <typename T> |
| 73 | constexpr T max(T a, T b) { |
| 74 | return (a > b) ? a : b; |
| 75 | } |
| 76 | |
| 77 | template <typename T, typename... Rest> |
| 78 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 79 | return max(max(a, b), c, rest...); |
| 80 | } |
| 81 | |
| 82 | double drivetrain_translate(int32_t in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 83 | return ((static_cast<double>(in) / |
| 84 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 85 | (2.0 * M_PI)) * |
| 86 | Values::kDrivetrainEncoderRatio() * |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 87 | control_loops::drivetrain::kWheelRadius; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | double drivetrain_velocity_translate(double in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 91 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 92 | (2.0 * M_PI)) * |
| 93 | Values::kDrivetrainEncoderRatio() * |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 94 | control_loops::drivetrain::kWheelRadius; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 97 | double elevator_pot_translate(double voltage) { |
| 98 | return voltage * Values::kElevatorPotRatio() * |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 99 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | double wrist_pot_translate(double voltage) { |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 103 | return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) * |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 104 | (2 * M_PI /*radians*/); |
| 105 | } |
| 106 | |
| 107 | double stilts_pot_translate(double voltage) { |
| 108 | return voltage * Values::kStiltsPotRatio() * |
| 109 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 110 | } |
| 111 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 112 | constexpr double kMaxFastEncoderPulsesPerSecond = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 113 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 114 | Values::kMaxIntakeEncoderPulsesPerSecond()); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 115 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 116 | "fast encoders are too fast"); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 117 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 118 | max(Values::kMaxElevatorEncoderPulsesPerSecond(), |
| 119 | Values::kMaxWristEncoderPulsesPerSecond()); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 120 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 121 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 122 | "medium encoders are too fast"); |
| 123 | |
| 124 | // Class to send position messages with sensor readings to our loops. |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 125 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 126 | public: |
| 127 | SensorReader() { |
| 128 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 129 | // we should ever see. |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 130 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 131 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 134 | // Elevator |
| 135 | |
| 136 | void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 137 | medium_encoder_filter_.Add(encoder.get()); |
| 138 | elevator_encoder_.set_encoder(::std::move(encoder)); |
| 139 | } |
| 140 | |
| 141 | void set_elevator_absolute_pwm( |
| 142 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 143 | elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 144 | } |
| 145 | |
| 146 | void set_elevator_potentiometer( |
| 147 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 148 | elevator_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 149 | } |
| 150 | |
| 151 | // Intake |
| 152 | |
| 153 | void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 154 | medium_encoder_filter_.Add(encoder.get()); |
| 155 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 156 | } |
| 157 | |
| 158 | void set_intake_absolute_pwm( |
| 159 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 160 | intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 161 | } |
| 162 | |
| 163 | // Wrist |
| 164 | |
| 165 | void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 166 | medium_encoder_filter_.Add(encoder.get()); |
| 167 | wrist_encoder_.set_encoder(::std::move(encoder)); |
| 168 | } |
| 169 | |
| 170 | void set_wrist_absolute_pwm( |
| 171 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 172 | wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 173 | } |
| 174 | |
| 175 | void set_wrist_potentiometer( |
| 176 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 177 | wrist_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 178 | } |
| 179 | |
| 180 | // Stilts |
| 181 | |
| 182 | void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 183 | medium_encoder_filter_.Add(encoder.get()); |
| 184 | stilts_encoder_.set_encoder(::std::move(encoder)); |
| 185 | } |
| 186 | |
| 187 | void set_stilts_absolute_pwm( |
| 188 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 189 | stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 190 | } |
| 191 | |
| 192 | void set_stilts_potentiometer( |
| 193 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 194 | stilts_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 195 | } |
| 196 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 197 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 198 | { |
| 199 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 200 | drivetrain_message->left_encoder = |
| 201 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 202 | drivetrain_message->left_speed = |
| 203 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 204 | |
| 205 | drivetrain_message->right_encoder = |
| 206 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 207 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 208 | drivetrain_right_encoder_->GetPeriod()); |
| 209 | |
| 210 | drivetrain_message.Send(); |
| 211 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 212 | const auto values = constants::GetValues(); |
| 213 | |
| 214 | { |
| 215 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 216 | |
| 217 | // Elevator |
| 218 | CopyPosition(elevator_encoder_, &superstructure_message->elevator, |
| 219 | Values::kElevatorEncoderCountsPerRevolution(), |
| 220 | Values::kElevatorEncoderRatio(), elevator_pot_translate, |
| 221 | false, values.elevator.potentiometer_offset); |
| 222 | // Intake |
| 223 | CopyPosition(intake_encoder_, &superstructure_message->intake_joint, |
| 224 | Values::kIntakeEncoderCountsPerRevolution(), |
| 225 | Values::kIntakeEncoderRatio(), false); |
| 226 | |
| 227 | // Wrist |
| 228 | CopyPosition(wrist_encoder_, &superstructure_message->wrist, |
| 229 | Values::kWristEncoderCountsPerRevolution(), |
| 230 | Values::kWristEncoderRatio(), wrist_pot_translate, false, |
| 231 | values.wrist.potentiometer_offset); |
| 232 | |
| 233 | // Stilts |
| 234 | CopyPosition(stilts_encoder_, &superstructure_message->stilts, |
| 235 | Values::kStiltsEncoderCountsPerRevolution(), |
| 236 | Values::kStiltsEncoderRatio(), stilts_pot_translate, false, |
| 237 | values.stilts.potentiometer_offset); |
| 238 | |
| 239 | superstructure_message.Send(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_, |
| 245 | wrist_encoder_, stilts_encoder_; |
| 246 | |
| 247 | ::frc971::wpilib::AbsoluteEncoder intake_encoder_; |
| 248 | // TODO(sabina): Add wrist and elevator hall effects. |
| 249 | }; |
| 250 | |
| 251 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 252 | public: |
| 253 | void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 254 | elevator_victor_ = ::std::move(t); |
| 255 | } |
| 256 | |
| 257 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 258 | intake_victor_ = ::std::move(t); |
| 259 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 260 | |
| 261 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 262 | wrist_victor_ = ::std::move(t); |
| 263 | } |
| 264 | |
| 265 | void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 266 | stilts_victor_ = ::std::move(t); |
| 267 | } |
| 268 | |
| 269 | private: |
| 270 | virtual void Read() override { |
| 271 | ::y2019::control_loops::superstructure::superstructure_queue.output |
| 272 | .FetchAnother(); |
| 273 | } |
| 274 | |
| 275 | virtual void Write() override { |
| 276 | auto &queue = |
| 277 | ::y2019::control_loops::superstructure::superstructure_queue.output; |
| 278 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 279 | elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 280 | -kMaxBringupPower, |
| 281 | kMaxBringupPower) / |
| 282 | 12.0); |
| 283 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 284 | intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 285 | -kMaxBringupPower, kMaxBringupPower) / |
| 286 | 12.0); |
| 287 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 288 | wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage, |
| 289 | -kMaxBringupPower, kMaxBringupPower) / |
| 290 | 12.0); |
| 291 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 292 | stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 293 | -kMaxBringupPower, kMaxBringupPower) / |
| 294 | 12.0); |
| 295 | } |
| 296 | |
| 297 | virtual void Stop() override { |
| 298 | LOG(WARNING, "Superstructure output too old.\n"); |
| 299 | |
| 300 | elevator_victor_->SetDisabled(); |
| 301 | intake_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 302 | wrist_victor_->SetDisabled(); |
| 303 | stilts_victor_->SetDisabled(); |
| 304 | } |
| 305 | |
| 306 | ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_, |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 307 | wrist_victor_, stilts_victor_; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame^] | 310 | class SolenoidWriter { |
| 311 | public: |
| 312 | SolenoidWriter() |
| 313 | : superstructure_( |
| 314 | ".y2019.control_loops.superstructure.superstructure_queue.output") { |
| 315 | } |
| 316 | |
| 317 | void set_big_suction_cup(int index) { |
| 318 | big_suction_cup_ = pcm_.MakeSolenoid(index); |
| 319 | } |
| 320 | void set_small_suction_cup(int index) { |
| 321 | small_suction_cup_ = pcm_.MakeSolenoid(index); |
| 322 | } |
| 323 | |
| 324 | void set_intake_roller_talon( |
| 325 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) { |
| 326 | intake_rollers_talon_ = ::std::move(t); |
| 327 | intake_rollers_talon_->ConfigContinuousCurrentLimit(40.0, 0); |
| 328 | intake_rollers_talon_->EnableCurrentLimit(true); |
| 329 | } |
| 330 | |
| 331 | void operator()() { |
| 332 | ::aos::SetCurrentThreadName("Solenoids"); |
| 333 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 334 | |
| 335 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 336 | ::std::chrono::milliseconds(1)); |
| 337 | |
| 338 | while (run_) { |
| 339 | { |
| 340 | const int iterations = phased_loop.SleepUntilNext(); |
| 341 | if (iterations != 1) { |
| 342 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | { |
| 347 | superstructure_.FetchLatest(); |
| 348 | if (superstructure_.get()) { |
| 349 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 350 | |
| 351 | big_suction_cup_->Set(!superstructure_->intake_suction_top); |
| 352 | small_suction_cup_->Set(!superstructure_->intake_suction_bottom); |
| 353 | |
| 354 | intake_rollers_talon_->Set( |
| 355 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 356 | ::aos::Clip(superstructure_->intake_roller_voltage, |
| 357 | -kMaxBringupPower, kMaxBringupPower) / |
| 358 | 12.0); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | { |
| 363 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 364 | |
| 365 | pcm_.Flush(); |
| 366 | to_log.read_solenoids = pcm_.GetAll(); |
| 367 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void Quit() { run_ = false; } |
| 373 | |
| 374 | private: |
| 375 | ::frc971::wpilib::BufferedPcm pcm_; |
| 376 | |
| 377 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup_, |
| 378 | small_suction_cup_; |
| 379 | |
| 380 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> |
| 381 | intake_rollers_talon_; |
| 382 | |
| 383 | ::aos::Queue< |
| 384 | ::y2019::control_loops::superstructure::SuperstructureQueue::Output> |
| 385 | superstructure_; |
| 386 | |
| 387 | ::std::atomic<bool> run_{true}; |
| 388 | }; |
| 389 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 390 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 391 | public: |
| 392 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 393 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 394 | frc::Encoder::k4X); |
| 395 | } |
| 396 | |
| 397 | void Run() override { |
| 398 | ::aos::InitNRT(); |
| 399 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 400 | |
| 401 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 402 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 403 | |
| 404 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 405 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 406 | SensorReader reader; |
| 407 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 408 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 409 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 410 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 411 | reader.set_elevator_encoder(make_encoder(4)); |
| 412 | reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 413 | reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 414 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 415 | reader.set_wrist_encoder(make_encoder(5)); |
| 416 | reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5)); |
| 417 | reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 418 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 419 | reader.set_intake_encoder(make_encoder(2)); |
| 420 | reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 421 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 422 | reader.set_stilts_encoder(make_encoder(3)); |
| 423 | reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 424 | reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 425 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 426 | reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25)); |
| 427 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 428 | ::std::thread reader_thread(::std::ref(reader)); |
| 429 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 430 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 431 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 432 | imu_trigger.get()); |
| 433 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 434 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 435 | imu.set_reset(imu_reset.get()); |
| 436 | ::std::thread imu_thread(::std::ref(imu)); |
| 437 | |
| 438 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 439 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 440 | // variety so all the Victors are written as SPs. |
| 441 | |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 442 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer; |
| 443 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 444 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 445 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 446 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 447 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 448 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 449 | SuperstructureWriter superstructure_writer; |
| 450 | superstructure_writer.set_elevator_victor( |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 451 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 452 | // TODO(austin): Do the vacuum |
| 453 | //superstructure_writer.set_vacuum( |
| 454 | //::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
| 455 | superstructure_writer.set_intake_victor( |
| 456 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 457 | superstructure_writer.set_wrist_victor( |
| 458 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 459 | superstructure_writer.set_stilts_victor( |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 460 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 461 | |
| 462 | ::std::thread superstructure_writer_thread( |
| 463 | ::std::ref(superstructure_writer)); |
| 464 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame^] | 465 | SolenoidWriter solenoid_writer; |
| 466 | solenoid_writer.set_intake_roller_talon( |
| 467 | make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10)); |
| 468 | solenoid_writer.set_big_suction_cup(0); |
| 469 | solenoid_writer.set_small_suction_cup(1); |
| 470 | |
| 471 | ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer)); |
| 472 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 473 | // Wait forever. Not much else to do... |
| 474 | while (true) { |
| 475 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 476 | if (r != 0) { |
| 477 | PLOG(WARNING, "infinite select failed"); |
| 478 | } else { |
| 479 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 484 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame^] | 485 | solenoid_writer.Quit(); |
| 486 | solenoid_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 487 | joystick_sender.Quit(); |
| 488 | joystick_thread.join(); |
| 489 | pdp_fetcher.Quit(); |
| 490 | pdp_fetcher_thread.join(); |
| 491 | reader.Quit(); |
| 492 | reader_thread.join(); |
| 493 | imu.Quit(); |
| 494 | imu_thread.join(); |
| 495 | |
| 496 | drivetrain_writer.Quit(); |
| 497 | drivetrain_writer_thread.join(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 498 | superstructure_writer.Quit(); |
| 499 | superstructure_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 500 | |
| 501 | ::aos::Cleanup(); |
| 502 | } |
| 503 | }; |
| 504 | |
| 505 | } // namespace |
| 506 | } // namespace wpilib |
| 507 | } // namespace y2019 |
| 508 | |
| 509 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |