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