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" |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 19 | #include "ctre/phoenix/CANifier.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 20 | #undef ERROR |
| 21 | |
| 22 | #include "aos/commonmath.h" |
| 23 | #include "aos/init.h" |
| 24 | #include "aos/logging/logging.h" |
| 25 | #include "aos/logging/queue_logging.h" |
| 26 | #include "aos/make_unique.h" |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 27 | #include "aos/robot_state/robot_state.q.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 28 | #include "aos/time/time.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 29 | #include "aos/util/log_interval.h" |
| 30 | #include "aos/util/phased_loop.h" |
| 31 | #include "aos/util/wrapping_counter.h" |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 32 | #include "ctre/phoenix/motorcontrol/can/TalonSRX.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 33 | #include "frc971/autonomous/auto.q.h" |
| 34 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 35 | #include "frc971/wpilib/ADIS16448.h" |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/buffered_pcm.h" |
| 37 | #include "frc971/wpilib/buffered_solenoid.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 38 | #include "frc971/wpilib/dma.h" |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 39 | #include "frc971/wpilib/drivetrain_writer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 41 | #include "frc971/wpilib/joystick_sender.h" |
| 42 | #include "frc971/wpilib/logging.q.h" |
| 43 | #include "frc971/wpilib/loop_output_handler.h" |
| 44 | #include "frc971/wpilib/pdp_fetcher.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 45 | #include "frc971/wpilib/sensor_reader.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 46 | #include "frc971/wpilib/wpilib_robot_base.h" |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 47 | #include "y2019/constants.h" |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 48 | #include "y2019/control_loops/drivetrain/camera.q.h" |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 49 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 50 | #include "y2019/jevois/spi.h" |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 51 | #include "y2019/status_light.q.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 52 | |
| 53 | #ifndef M_PI |
| 54 | #define M_PI 3.14159265358979323846 |
| 55 | #endif |
| 56 | |
| 57 | using ::frc971::control_loops::drivetrain_queue; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 58 | using ::y2019::control_loops::superstructure::superstructure_queue; |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 59 | using ::y2019::constants::Values; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 60 | using ::aos::monotonic_clock; |
| 61 | namespace chrono = ::std::chrono; |
| 62 | using aos::make_unique; |
| 63 | |
| 64 | namespace y2019 { |
| 65 | namespace wpilib { |
| 66 | namespace { |
| 67 | |
| 68 | constexpr double kMaxBringupPower = 12.0; |
| 69 | |
| 70 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 71 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 72 | // The low bit is direction. |
| 73 | |
| 74 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 75 | // constexpr. |
| 76 | template <typename T> |
| 77 | constexpr T max(T a, T b) { |
| 78 | return (a > b) ? a : b; |
| 79 | } |
| 80 | |
| 81 | template <typename T, typename... Rest> |
| 82 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 83 | return max(max(a, b), c, rest...); |
| 84 | } |
| 85 | |
| 86 | double drivetrain_translate(int32_t in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 87 | return ((static_cast<double>(in) / |
| 88 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
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 | |
| 94 | double drivetrain_velocity_translate(double in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 95 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 96 | (2.0 * M_PI)) * |
| 97 | Values::kDrivetrainEncoderRatio() * |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 98 | control_loops::drivetrain::kWheelRadius; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 101 | double elevator_pot_translate(double voltage) { |
| 102 | return voltage * Values::kElevatorPotRatio() * |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 103 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | double wrist_pot_translate(double voltage) { |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 107 | return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) * |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 108 | (2 * M_PI /*radians*/); |
| 109 | } |
| 110 | |
| 111 | double stilts_pot_translate(double voltage) { |
| 112 | return voltage * Values::kStiltsPotRatio() * |
| 113 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 114 | } |
| 115 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 116 | constexpr double kMaxFastEncoderPulsesPerSecond = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 117 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 118 | Values::kMaxIntakeEncoderPulsesPerSecond()); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 119 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 120 | "fast encoders are too fast"); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 121 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 122 | max(Values::kMaxElevatorEncoderPulsesPerSecond(), |
| 123 | Values::kMaxWristEncoderPulsesPerSecond()); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 124 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 125 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 126 | "medium encoders are too fast"); |
| 127 | |
| 128 | // Class to send position messages with sensor readings to our loops. |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 129 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 130 | public: |
| 131 | SensorReader() { |
| 132 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 133 | // we should ever see. |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 134 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 135 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 138 | // Elevator |
| 139 | |
| 140 | void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 141 | medium_encoder_filter_.Add(encoder.get()); |
| 142 | elevator_encoder_.set_encoder(::std::move(encoder)); |
| 143 | } |
| 144 | |
| 145 | void set_elevator_absolute_pwm( |
| 146 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 147 | elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 148 | } |
| 149 | |
| 150 | void set_elevator_potentiometer( |
| 151 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 152 | elevator_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 153 | } |
| 154 | |
| 155 | // Intake |
| 156 | |
| 157 | void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 158 | medium_encoder_filter_.Add(encoder.get()); |
| 159 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 160 | } |
| 161 | |
| 162 | void set_intake_absolute_pwm( |
| 163 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 164 | intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 165 | } |
| 166 | |
| 167 | // Wrist |
| 168 | |
| 169 | void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 170 | medium_encoder_filter_.Add(encoder.get()); |
| 171 | wrist_encoder_.set_encoder(::std::move(encoder)); |
| 172 | } |
| 173 | |
| 174 | void set_wrist_absolute_pwm( |
| 175 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 176 | wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 177 | } |
| 178 | |
| 179 | void set_wrist_potentiometer( |
| 180 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 181 | wrist_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 182 | } |
| 183 | |
| 184 | // Stilts |
| 185 | |
| 186 | void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 187 | medium_encoder_filter_.Add(encoder.get()); |
| 188 | stilts_encoder_.set_encoder(::std::move(encoder)); |
| 189 | } |
| 190 | |
| 191 | void set_stilts_absolute_pwm( |
| 192 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 193 | stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 194 | } |
| 195 | |
| 196 | void set_stilts_potentiometer( |
| 197 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 198 | stilts_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 199 | } |
| 200 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 201 | // Vacuum pressure sensor |
| 202 | void set_vacuum_sensor(int port) { |
| 203 | vacuum_sensor_ = make_unique<frc::AnalogInput>(port); |
| 204 | } |
| 205 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame^] | 206 | // Auto mode switches. |
| 207 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 208 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 209 | } |
| 210 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 211 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 212 | { |
| 213 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 214 | drivetrain_message->left_encoder = |
| 215 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 216 | drivetrain_message->left_speed = |
| 217 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 218 | |
| 219 | drivetrain_message->right_encoder = |
| 220 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 221 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 222 | drivetrain_right_encoder_->GetPeriod()); |
| 223 | |
| 224 | drivetrain_message.Send(); |
| 225 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 226 | const auto values = constants::GetValues(); |
| 227 | |
| 228 | { |
| 229 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 230 | |
| 231 | // Elevator |
| 232 | CopyPosition(elevator_encoder_, &superstructure_message->elevator, |
| 233 | Values::kElevatorEncoderCountsPerRevolution(), |
| 234 | Values::kElevatorEncoderRatio(), elevator_pot_translate, |
| 235 | false, values.elevator.potentiometer_offset); |
| 236 | // Intake |
| 237 | CopyPosition(intake_encoder_, &superstructure_message->intake_joint, |
| 238 | Values::kIntakeEncoderCountsPerRevolution(), |
| 239 | Values::kIntakeEncoderRatio(), false); |
| 240 | |
| 241 | // Wrist |
| 242 | CopyPosition(wrist_encoder_, &superstructure_message->wrist, |
| 243 | Values::kWristEncoderCountsPerRevolution(), |
| 244 | Values::kWristEncoderRatio(), wrist_pot_translate, false, |
| 245 | values.wrist.potentiometer_offset); |
| 246 | |
| 247 | // Stilts |
| 248 | CopyPosition(stilts_encoder_, &superstructure_message->stilts, |
| 249 | Values::kStiltsEncoderCountsPerRevolution(), |
| 250 | Values::kStiltsEncoderRatio(), stilts_pot_translate, false, |
| 251 | values.stilts.potentiometer_offset); |
| 252 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 253 | // Suction |
| 254 | constexpr float kMinVoltage = 0.5; |
| 255 | constexpr float kMaxVoltage = 2.1; |
| 256 | superstructure_message->suction_pressure = |
| 257 | (vacuum_sensor_->GetVoltage() - kMinVoltage) / |
| 258 | (kMaxVoltage - kMinVoltage); |
| 259 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 260 | superstructure_message.Send(); |
| 261 | } |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame^] | 262 | |
| 263 | { |
| 264 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
| 265 | auto_mode_message->mode = 0; |
| 266 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 267 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 268 | auto_mode_message->mode |= 1 << i; |
| 269 | } |
| 270 | } |
| 271 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 272 | auto_mode_message.Send(); |
| 273 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | private: |
| 277 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_, |
| 278 | wrist_encoder_, stilts_encoder_; |
| 279 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 280 | ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_; |
| 281 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame^] | 282 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 283 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 284 | ::frc971::wpilib::AbsoluteEncoder intake_encoder_; |
| 285 | // TODO(sabina): Add wrist and elevator hall effects. |
| 286 | }; |
| 287 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 288 | class CameraReader { |
| 289 | public: |
| 290 | CameraReader() = default; |
| 291 | CameraReader(const CameraReader &) = delete; |
| 292 | CameraReader &operator=(const CameraReader &) = delete; |
| 293 | |
| 294 | void set_spi(frc::SPI *spi) { |
| 295 | spi_ = spi; |
| 296 | spi_->SetClockRate(1e6); |
| 297 | spi_->SetChipSelectActiveHigh(); |
| 298 | spi_->SetClockActiveLow(); |
| 299 | spi_->SetSampleDataOnFalling(); |
| 300 | // It ignores you if you try changing this... |
| 301 | spi_->SetMSBFirst(); |
| 302 | } |
| 303 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 304 | void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) { |
| 305 | activate_usb_ = std::move(activate_usb); |
| 306 | } |
| 307 | |
| 308 | void set_activate_passthrough( |
| 309 | std::unique_ptr<frc::DigitalInput> activate_passthrough) { |
| 310 | activate_passthrough_ = std::move(activate_passthrough); |
| 311 | } |
| 312 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 313 | void DoSpiTransaction() { |
| 314 | using namespace frc971::jevois; |
| 315 | RoborioToTeensy to_teensy{}; |
| 316 | to_teensy.realtime_now = aos::realtime_clock::now(); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 317 | if (activate_usb_ && !activate_usb_->Get()) { |
| 318 | to_teensy.camera_command = CameraCommand::kUsb; |
| 319 | } else if (activate_passthrough_ && !activate_passthrough_->Get()) { |
| 320 | to_teensy.camera_command = CameraCommand::kCameraPassthrough; |
| 321 | } else { |
| 322 | to_teensy.camera_command = CameraCommand::kNormal; |
| 323 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 324 | |
| 325 | std::array<char, spi_transfer_size() + 1> to_send{}; |
| 326 | { |
| 327 | const auto to_send_data = |
| 328 | gsl::make_span(to_send).last<spi_transfer_size()>(); |
| 329 | const auto encoded = SpiPackToTeensy(to_teensy); |
| 330 | std::copy(encoded.begin(), encoded.end(), to_send_data.begin()); |
| 331 | } |
| 332 | rx_clearer_.ClearRxFifo(); |
| 333 | // First, send recieve a dummy byte because the Teensy can't control what it |
| 334 | // sends for the first byte. |
| 335 | std::array<char, spi_transfer_size() + 1> to_receive; |
| 336 | DoTransaction(to_send, to_receive); |
| 337 | const auto unpacked = SpiUnpackToRoborio( |
| 338 | gsl::make_span(to_receive).last(spi_transfer_size())); |
| 339 | if (!unpacked) { |
| 340 | LOG(INFO, "Decoding SPI data failed\n"); |
| 341 | return; |
| 342 | } |
| 343 | |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 344 | const auto now = aos::monotonic_clock::now(); |
| 345 | for (const auto &received : unpacked->frames) { |
| 346 | auto to_send = control_loops::drivetrain::camera_frames.MakeMessage(); |
| 347 | to_send->timestamp = |
James Kuszmaul | 85ffeb8 | 2019-03-03 19:41:44 -0800 | [diff] [blame] | 348 | std::chrono::nanoseconds((now - received.age).time_since_epoch()) |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 349 | .count(); |
| 350 | to_send->num_targets = received.targets.size(); |
| 351 | for (size_t i = 0; i < received.targets.size(); ++i) { |
| 352 | to_send->targets[i].distance = received.targets[i].distance; |
| 353 | to_send->targets[i].height = received.targets[i].height; |
| 354 | to_send->targets[i].heading = received.targets[i].heading; |
| 355 | to_send->targets[i].skew = received.targets[i].skew; |
| 356 | } |
| 357 | to_send->camera = received.camera_index; |
Austin Schuh | bb52eec | 2019-03-03 18:32:14 -0800 | [diff] [blame] | 358 | LOG_STRUCT(DEBUG, "camera_frames", *to_send); |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 359 | to_send.Send(); |
| 360 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 361 | |
| 362 | if (dummy_spi_) { |
| 363 | uint8_t dummy_send, dummy_receive; |
| 364 | dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) { |
| 369 | CHECK_EQ(to_send.size(), to_receive.size()); |
| 370 | const auto result = spi_->Transaction( |
| 371 | reinterpret_cast<uint8_t *>(to_send.data()), |
| 372 | reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size()); |
| 373 | if (result == to_send.size()) { |
| 374 | return; |
| 375 | } |
| 376 | if (result == -1) { |
| 377 | LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size()); |
| 378 | return; |
| 379 | } |
| 380 | LOG(FATAL, "SPI::Transaction returned something weird\n"); |
| 381 | } |
| 382 | |
| 383 | void SetDummySPI(frc::SPI::Port port) { |
| 384 | dummy_spi_.reset(new frc::SPI(port)); |
| 385 | // Pick the same settings here in case the roboRIO decides to try something |
| 386 | // stupid when switching. |
| 387 | if (dummy_spi_) { |
| 388 | dummy_spi_->SetClockRate(1e5); |
| 389 | dummy_spi_->SetChipSelectActiveLow(); |
| 390 | dummy_spi_->SetClockActiveLow(); |
| 391 | dummy_spi_->SetSampleDataOnFalling(); |
| 392 | dummy_spi_->SetMSBFirst(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | private: |
| 397 | frc::SPI *spi_ = nullptr; |
| 398 | ::std::unique_ptr<frc::SPI> dummy_spi_; |
| 399 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 400 | std::unique_ptr<frc::DigitalInput> activate_usb_; |
| 401 | std::unique_ptr<frc::DigitalInput> activate_passthrough_; |
| 402 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 403 | frc971::wpilib::SpiRxClearer rx_clearer_; |
| 404 | }; |
| 405 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 406 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 407 | public: |
| 408 | void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 409 | elevator_victor_ = ::std::move(t); |
| 410 | } |
| 411 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 412 | void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 413 | suction_victor_ = ::std::move(t); |
| 414 | } |
| 415 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 416 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 417 | intake_victor_ = ::std::move(t); |
| 418 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 419 | |
| 420 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 421 | wrist_victor_ = ::std::move(t); |
| 422 | } |
| 423 | |
| 424 | void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 425 | stilts_victor_ = ::std::move(t); |
| 426 | } |
| 427 | |
| 428 | private: |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 429 | void Read() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 430 | ::y2019::control_loops::superstructure::superstructure_queue.output |
| 431 | .FetchAnother(); |
| 432 | } |
| 433 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 434 | void Write() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 435 | auto &queue = |
| 436 | ::y2019::control_loops::superstructure::superstructure_queue.output; |
| 437 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 438 | elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 439 | -kMaxBringupPower, |
| 440 | kMaxBringupPower) / |
| 441 | 12.0); |
| 442 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 443 | intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 444 | -kMaxBringupPower, kMaxBringupPower) / |
| 445 | 12.0); |
| 446 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 447 | wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage, |
| 448 | -kMaxBringupPower, kMaxBringupPower) / |
| 449 | 12.0); |
| 450 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 451 | stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 452 | -kMaxBringupPower, kMaxBringupPower) / |
| 453 | 12.0); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 454 | |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 455 | ::aos::robot_state.FetchLatest(); |
| 456 | const double battery_voltage = |
| 457 | ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0; |
| 458 | |
| 459 | // Throw a fast low pass filter on the battery voltage so we don't respond |
| 460 | // too fast to noise. |
| 461 | filtered_battery_voltage_ = |
| 462 | 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage; |
| 463 | |
| 464 | suction_victor_->SetSpeed(::aos::Clip( |
| 465 | queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 468 | void Stop() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 469 | LOG(WARNING, "Superstructure output too old.\n"); |
| 470 | |
| 471 | elevator_victor_->SetDisabled(); |
| 472 | intake_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 473 | wrist_victor_->SetDisabled(); |
| 474 | stilts_victor_->SetDisabled(); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 475 | suction_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_, |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 479 | wrist_victor_, stilts_victor_, suction_victor_; |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 480 | |
| 481 | double filtered_battery_voltage_ = 12.0; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 482 | }; |
| 483 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 484 | class SolenoidWriter { |
| 485 | public: |
| 486 | SolenoidWriter() |
| 487 | : superstructure_( |
| 488 | ".y2019.control_loops.superstructure.superstructure_queue.output") { |
| 489 | } |
| 490 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 491 | void set_big_suction_cup(int index0, int index1) { |
| 492 | big_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 493 | big_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 494 | } |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 495 | void set_small_suction_cup(int index0, int index1) { |
| 496 | small_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 497 | small_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void set_intake_roller_talon( |
| 501 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) { |
| 502 | intake_rollers_talon_ = ::std::move(t); |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 503 | intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 504 | intake_rollers_talon_->EnableCurrentLimit(true); |
| 505 | } |
| 506 | |
| 507 | void operator()() { |
| 508 | ::aos::SetCurrentThreadName("Solenoids"); |
| 509 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 510 | |
| 511 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 512 | ::std::chrono::milliseconds(1)); |
| 513 | |
| 514 | while (run_) { |
| 515 | { |
| 516 | const int iterations = phased_loop.SleepUntilNext(); |
| 517 | if (iterations != 1) { |
| 518 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | { |
| 523 | superstructure_.FetchLatest(); |
| 524 | if (superstructure_.get()) { |
| 525 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 526 | |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 527 | big_suction_cup0_->Set(!superstructure_->intake_suction_bottom); |
| 528 | big_suction_cup1_->Set(!superstructure_->intake_suction_bottom); |
| 529 | small_suction_cup0_->Set(superstructure_->intake_suction_top); |
| 530 | small_suction_cup1_->Set(superstructure_->intake_suction_top); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 531 | |
| 532 | intake_rollers_talon_->Set( |
| 533 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 534 | ::aos::Clip(superstructure_->intake_roller_voltage, |
| 535 | -kMaxBringupPower, kMaxBringupPower) / |
| 536 | 12.0); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | { |
| 541 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 542 | |
| 543 | pcm_.Flush(); |
| 544 | to_log.read_solenoids = pcm_.GetAll(); |
| 545 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 546 | } |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 547 | |
| 548 | status_light.FetchLatest(); |
| 549 | // If we don't have a light request (or it's an old one), we are borked. |
| 550 | // Flash the red light slowly. |
| 551 | if (!status_light.get() || |
| 552 | status_light.Age() > chrono::milliseconds(100)) { |
| 553 | StatusLight color; |
| 554 | color.red = 0.0; |
| 555 | color.green = 0.0; |
| 556 | color.blue = 0.0; |
| 557 | |
| 558 | ++light_flash_; |
| 559 | if (light_flash_ > 10) { |
| 560 | color.red = 0.5; |
| 561 | } |
| 562 | |
| 563 | if (light_flash_ > 20) { |
| 564 | light_flash_ = 0; |
| 565 | } |
| 566 | |
| 567 | LOG_STRUCT(DEBUG, "color", color); |
| 568 | SetColor(color); |
| 569 | } else { |
| 570 | LOG_STRUCT(DEBUG, "color", *status_light); |
| 571 | SetColor(*status_light); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | void SetColor(const StatusLight &status_light) { |
| 577 | // Save CAN bandwidth and CPU at the cost of RT. Only change the light when |
| 578 | // it actually changes. This is pretty low priority anyways. |
| 579 | static int time_since_last_send = 0; |
| 580 | ++time_since_last_send; |
| 581 | if (time_since_last_send > 10) { |
| 582 | time_since_last_send = 0; |
| 583 | } |
| 584 | if (status_light.green != last_green_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 585 | canifier_.SetLEDOutput(status_light.green, |
| 586 | ::ctre::phoenix::CANifier::LEDChannelA); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 587 | last_green_ = status_light.green; |
| 588 | } |
| 589 | |
| 590 | if (status_light.blue != last_blue_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 591 | canifier_.SetLEDOutput(status_light.blue, |
| 592 | ::ctre::phoenix::CANifier::LEDChannelC); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 593 | last_blue_ = status_light.blue; |
| 594 | } |
| 595 | |
| 596 | if (status_light.red != last_red_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 597 | canifier_.SetLEDOutput(status_light.red, |
| 598 | ::ctre::phoenix::CANifier::LEDChannelB); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 599 | last_red_ = status_light.red; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | void Quit() { run_ = false; } |
| 604 | |
| 605 | private: |
| 606 | ::frc971::wpilib::BufferedPcm pcm_; |
| 607 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 608 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_, |
| 609 | big_suction_cup1_, small_suction_cup0_, small_suction_cup1_; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 610 | |
| 611 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> |
| 612 | intake_rollers_talon_; |
| 613 | |
| 614 | ::aos::Queue< |
| 615 | ::y2019::control_loops::superstructure::SuperstructureQueue::Output> |
| 616 | superstructure_; |
| 617 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 618 | ::ctre::phoenix::CANifier canifier_{0}; |
| 619 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 620 | ::std::atomic<bool> run_{true}; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 621 | |
| 622 | double last_red_ = -1.0; |
| 623 | double last_green_ = -1.0; |
| 624 | double last_blue_ = -1.0; |
| 625 | |
| 626 | int light_flash_ = 0; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 627 | }; |
| 628 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 629 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 630 | public: |
| 631 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 632 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 633 | frc::Encoder::k4X); |
| 634 | } |
| 635 | |
| 636 | void Run() override { |
| 637 | ::aos::InitNRT(); |
| 638 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 639 | |
| 640 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 641 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 642 | |
| 643 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 644 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 645 | SensorReader reader; |
| 646 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 647 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 648 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 649 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 650 | reader.set_elevator_encoder(make_encoder(4)); |
| 651 | reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 652 | reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 653 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 654 | reader.set_wrist_encoder(make_encoder(5)); |
| 655 | reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5)); |
| 656 | reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 657 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 658 | reader.set_intake_encoder(make_encoder(2)); |
| 659 | reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 660 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 661 | reader.set_stilts_encoder(make_encoder(3)); |
| 662 | reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 663 | reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 664 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 665 | reader.set_pwm_trigger(true); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 666 | reader.set_vacuum_sensor(7); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 667 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame^] | 668 | reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22)); |
| 669 | reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23)); |
| 670 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 671 | ::std::thread reader_thread(::std::ref(reader)); |
| 672 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 673 | CameraReader camera_reader; |
| 674 | frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3); |
| 675 | camera_reader.set_spi(&camera_spi); |
| 676 | camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 677 | // Austin says 8, 9, 24, and 25 are good options to choose from for these. |
| 678 | camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24)); |
| 679 | camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25)); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 680 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 681 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 682 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 683 | imu_trigger.get()); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 684 | imu.set_spi_idle_callback( |
| 685 | [&camera_reader]() { camera_reader.DoSpiTransaction(); }); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 686 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 687 | imu.set_reset(imu_reset.get()); |
| 688 | ::std::thread imu_thread(::std::ref(imu)); |
| 689 | |
| 690 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 691 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 692 | // variety so all the Victors are written as SPs. |
| 693 | |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 694 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer; |
| 695 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 696 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 697 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 698 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 699 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 700 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 701 | SuperstructureWriter superstructure_writer; |
| 702 | superstructure_writer.set_elevator_victor( |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 703 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 704 | // TODO(austin): Do the vacuum |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 705 | superstructure_writer.set_suction_victor( |
| 706 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 707 | superstructure_writer.set_intake_victor( |
| 708 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 709 | superstructure_writer.set_wrist_victor( |
| 710 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 711 | superstructure_writer.set_stilts_victor( |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 712 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 713 | |
| 714 | ::std::thread superstructure_writer_thread( |
| 715 | ::std::ref(superstructure_writer)); |
| 716 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 717 | SolenoidWriter solenoid_writer; |
| 718 | solenoid_writer.set_intake_roller_talon( |
| 719 | make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10)); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 720 | solenoid_writer.set_big_suction_cup(0, 1); |
| 721 | solenoid_writer.set_small_suction_cup(2, 3); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 722 | |
| 723 | ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer)); |
| 724 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 725 | // Wait forever. Not much else to do... |
| 726 | while (true) { |
| 727 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 728 | if (r != 0) { |
| 729 | PLOG(WARNING, "infinite select failed"); |
| 730 | } else { |
| 731 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 736 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 737 | solenoid_writer.Quit(); |
| 738 | solenoid_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 739 | joystick_sender.Quit(); |
| 740 | joystick_thread.join(); |
| 741 | pdp_fetcher.Quit(); |
| 742 | pdp_fetcher_thread.join(); |
| 743 | reader.Quit(); |
| 744 | reader_thread.join(); |
| 745 | imu.Quit(); |
| 746 | imu_thread.join(); |
| 747 | |
| 748 | drivetrain_writer.Quit(); |
| 749 | drivetrain_writer_thread.join(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 750 | superstructure_writer.Quit(); |
| 751 | superstructure_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 752 | |
| 753 | ::aos::Cleanup(); |
| 754 | } |
| 755 | }; |
| 756 | |
| 757 | } // namespace |
| 758 | } // namespace wpilib |
| 759 | } // namespace y2019 |
| 760 | |
| 761 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |