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 | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 201 | void set_platform_left_detect( |
| 202 | ::std::unique_ptr<frc::DigitalInput> platform_left_detect) { |
| 203 | platform_left_detect_ = ::std::move(platform_left_detect); |
| 204 | } |
| 205 | |
| 206 | void set_platform_right_detect( |
| 207 | ::std::unique_ptr<frc::DigitalInput> platform_right_detect) { |
| 208 | platform_right_detect_ = ::std::move(platform_right_detect); |
| 209 | } |
| 210 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 211 | // Vacuum pressure sensor |
| 212 | void set_vacuum_sensor(int port) { |
| 213 | vacuum_sensor_ = make_unique<frc::AnalogInput>(port); |
| 214 | } |
| 215 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 216 | // Auto mode switches. |
| 217 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 218 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 219 | } |
| 220 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 221 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 222 | { |
| 223 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 224 | drivetrain_message->left_encoder = |
| 225 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 226 | drivetrain_message->left_speed = |
| 227 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 228 | |
| 229 | drivetrain_message->right_encoder = |
| 230 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 231 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 232 | drivetrain_right_encoder_->GetPeriod()); |
| 233 | |
| 234 | drivetrain_message.Send(); |
| 235 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 236 | const auto values = constants::GetValues(); |
| 237 | |
| 238 | { |
| 239 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 240 | |
| 241 | // Elevator |
| 242 | CopyPosition(elevator_encoder_, &superstructure_message->elevator, |
| 243 | Values::kElevatorEncoderCountsPerRevolution(), |
| 244 | Values::kElevatorEncoderRatio(), elevator_pot_translate, |
| 245 | false, values.elevator.potentiometer_offset); |
| 246 | // Intake |
| 247 | CopyPosition(intake_encoder_, &superstructure_message->intake_joint, |
| 248 | Values::kIntakeEncoderCountsPerRevolution(), |
| 249 | Values::kIntakeEncoderRatio(), false); |
| 250 | |
| 251 | // Wrist |
| 252 | CopyPosition(wrist_encoder_, &superstructure_message->wrist, |
| 253 | Values::kWristEncoderCountsPerRevolution(), |
| 254 | Values::kWristEncoderRatio(), wrist_pot_translate, false, |
| 255 | values.wrist.potentiometer_offset); |
| 256 | |
| 257 | // Stilts |
| 258 | CopyPosition(stilts_encoder_, &superstructure_message->stilts, |
| 259 | Values::kStiltsEncoderCountsPerRevolution(), |
| 260 | Values::kStiltsEncoderRatio(), stilts_pot_translate, false, |
| 261 | values.stilts.potentiometer_offset); |
| 262 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 263 | // Suction |
| 264 | constexpr float kMinVoltage = 0.5; |
| 265 | constexpr float kMaxVoltage = 2.1; |
| 266 | superstructure_message->suction_pressure = |
| 267 | (vacuum_sensor_->GetVoltage() - kMinVoltage) / |
| 268 | (kMaxVoltage - kMinVoltage); |
| 269 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 270 | superstructure_message->platform_left_detect = |
| 271 | !platform_left_detect_->Get(); |
| 272 | superstructure_message->platform_right_detect = |
| 273 | !platform_right_detect_->Get(); |
| 274 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 275 | superstructure_message.Send(); |
| 276 | } |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 277 | |
| 278 | { |
| 279 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
| 280 | auto_mode_message->mode = 0; |
| 281 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 282 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 283 | auto_mode_message->mode |= 1 << i; |
| 284 | } |
| 285 | } |
| 286 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 287 | auto_mode_message.Send(); |
| 288 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | private: |
| 292 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_, |
| 293 | wrist_encoder_, stilts_encoder_; |
| 294 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 295 | ::std::unique_ptr<frc::DigitalInput> platform_left_detect_; |
| 296 | ::std::unique_ptr<frc::DigitalInput> platform_right_detect_; |
| 297 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 298 | ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_; |
| 299 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 300 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 301 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 302 | ::frc971::wpilib::AbsoluteEncoder intake_encoder_; |
| 303 | // TODO(sabina): Add wrist and elevator hall effects. |
| 304 | }; |
| 305 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 306 | class CameraReader { |
| 307 | public: |
| 308 | CameraReader() = default; |
| 309 | CameraReader(const CameraReader &) = delete; |
| 310 | CameraReader &operator=(const CameraReader &) = delete; |
| 311 | |
| 312 | void set_spi(frc::SPI *spi) { |
| 313 | spi_ = spi; |
| 314 | spi_->SetClockRate(1e6); |
| 315 | spi_->SetChipSelectActiveHigh(); |
| 316 | spi_->SetClockActiveLow(); |
| 317 | spi_->SetSampleDataOnFalling(); |
| 318 | // It ignores you if you try changing this... |
| 319 | spi_->SetMSBFirst(); |
| 320 | } |
| 321 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 322 | void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) { |
| 323 | activate_usb_ = std::move(activate_usb); |
| 324 | } |
| 325 | |
| 326 | void set_activate_passthrough( |
| 327 | std::unique_ptr<frc::DigitalInput> activate_passthrough) { |
| 328 | activate_passthrough_ = std::move(activate_passthrough); |
| 329 | } |
| 330 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 331 | void DoSpiTransaction() { |
| 332 | using namespace frc971::jevois; |
| 333 | RoborioToTeensy to_teensy{}; |
| 334 | to_teensy.realtime_now = aos::realtime_clock::now(); |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 335 | camera_log.FetchLatest(); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 336 | if (activate_usb_ && !activate_usb_->Get()) { |
| 337 | to_teensy.camera_command = CameraCommand::kUsb; |
| 338 | } else if (activate_passthrough_ && !activate_passthrough_->Get()) { |
| 339 | to_teensy.camera_command = CameraCommand::kCameraPassthrough; |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 340 | } else if (camera_log.get() && camera_log->log) { |
| 341 | to_teensy.camera_command = CameraCommand::kLog; |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 342 | } else { |
| 343 | to_teensy.camera_command = CameraCommand::kNormal; |
| 344 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 345 | |
| 346 | std::array<char, spi_transfer_size() + 1> to_send{}; |
| 347 | { |
| 348 | const auto to_send_data = |
| 349 | gsl::make_span(to_send).last<spi_transfer_size()>(); |
| 350 | const auto encoded = SpiPackToTeensy(to_teensy); |
| 351 | std::copy(encoded.begin(), encoded.end(), to_send_data.begin()); |
| 352 | } |
| 353 | rx_clearer_.ClearRxFifo(); |
| 354 | // First, send recieve a dummy byte because the Teensy can't control what it |
| 355 | // sends for the first byte. |
| 356 | std::array<char, spi_transfer_size() + 1> to_receive; |
| 357 | DoTransaction(to_send, to_receive); |
| 358 | const auto unpacked = SpiUnpackToRoborio( |
| 359 | gsl::make_span(to_receive).last(spi_transfer_size())); |
| 360 | if (!unpacked) { |
| 361 | LOG(INFO, "Decoding SPI data failed\n"); |
| 362 | return; |
| 363 | } |
| 364 | |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 365 | const auto now = aos::monotonic_clock::now(); |
| 366 | for (const auto &received : unpacked->frames) { |
| 367 | auto to_send = control_loops::drivetrain::camera_frames.MakeMessage(); |
James Kuszmaul | e08f04e | 2019-05-01 21:46:50 -0500 | [diff] [blame] | 368 | // Add an extra 10ms delay to account for unmodeled delays that Austin |
| 369 | // thinks exists. |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 370 | to_send->timestamp = |
James Kuszmaul | e08f04e | 2019-05-01 21:46:50 -0500 | [diff] [blame] | 371 | std::chrono::nanoseconds( |
| 372 | (now - received.age - ::std::chrono::milliseconds(10)) |
| 373 | .time_since_epoch()).count(); |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 374 | to_send->num_targets = received.targets.size(); |
| 375 | for (size_t i = 0; i < received.targets.size(); ++i) { |
| 376 | to_send->targets[i].distance = received.targets[i].distance; |
| 377 | to_send->targets[i].height = received.targets[i].height; |
| 378 | to_send->targets[i].heading = received.targets[i].heading; |
| 379 | to_send->targets[i].skew = received.targets[i].skew; |
| 380 | } |
| 381 | to_send->camera = received.camera_index; |
Austin Schuh | bb52eec | 2019-03-03 18:32:14 -0800 | [diff] [blame] | 382 | LOG_STRUCT(DEBUG, "camera_frames", *to_send); |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 383 | to_send.Send(); |
| 384 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 385 | |
| 386 | if (dummy_spi_) { |
| 387 | uint8_t dummy_send, dummy_receive; |
| 388 | dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) { |
| 393 | CHECK_EQ(to_send.size(), to_receive.size()); |
| 394 | const auto result = spi_->Transaction( |
| 395 | reinterpret_cast<uint8_t *>(to_send.data()), |
| 396 | reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size()); |
| 397 | if (result == to_send.size()) { |
| 398 | return; |
| 399 | } |
| 400 | if (result == -1) { |
| 401 | LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size()); |
| 402 | return; |
| 403 | } |
| 404 | LOG(FATAL, "SPI::Transaction returned something weird\n"); |
| 405 | } |
| 406 | |
| 407 | void SetDummySPI(frc::SPI::Port port) { |
| 408 | dummy_spi_.reset(new frc::SPI(port)); |
| 409 | // Pick the same settings here in case the roboRIO decides to try something |
| 410 | // stupid when switching. |
| 411 | if (dummy_spi_) { |
| 412 | dummy_spi_->SetClockRate(1e5); |
| 413 | dummy_spi_->SetChipSelectActiveLow(); |
| 414 | dummy_spi_->SetClockActiveLow(); |
| 415 | dummy_spi_->SetSampleDataOnFalling(); |
| 416 | dummy_spi_->SetMSBFirst(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | private: |
| 421 | frc::SPI *spi_ = nullptr; |
| 422 | ::std::unique_ptr<frc::SPI> dummy_spi_; |
| 423 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 424 | std::unique_ptr<frc::DigitalInput> activate_usb_; |
| 425 | std::unique_ptr<frc::DigitalInput> activate_passthrough_; |
| 426 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 427 | frc971::wpilib::SpiRxClearer rx_clearer_; |
| 428 | }; |
| 429 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 430 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 431 | public: |
| 432 | void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 433 | elevator_victor_ = ::std::move(t); |
| 434 | } |
| 435 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 436 | void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 437 | suction_victor_ = ::std::move(t); |
| 438 | } |
| 439 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 440 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 441 | intake_victor_ = ::std::move(t); |
| 442 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 443 | |
| 444 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 445 | wrist_victor_ = ::std::move(t); |
| 446 | } |
| 447 | |
| 448 | void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 449 | stilts_victor_ = ::std::move(t); |
| 450 | } |
| 451 | |
| 452 | private: |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 453 | void Read() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 454 | ::y2019::control_loops::superstructure::superstructure_queue.output |
| 455 | .FetchAnother(); |
| 456 | } |
| 457 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 458 | void Write() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 459 | auto &queue = |
| 460 | ::y2019::control_loops::superstructure::superstructure_queue.output; |
| 461 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 462 | elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 463 | -kMaxBringupPower, |
| 464 | kMaxBringupPower) / |
| 465 | 12.0); |
| 466 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 467 | intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 468 | -kMaxBringupPower, kMaxBringupPower) / |
| 469 | 12.0); |
| 470 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 471 | wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage, |
| 472 | -kMaxBringupPower, kMaxBringupPower) / |
| 473 | 12.0); |
| 474 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 475 | stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 476 | -kMaxBringupPower, kMaxBringupPower) / |
| 477 | 12.0); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 478 | |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 479 | ::aos::robot_state.FetchLatest(); |
| 480 | const double battery_voltage = |
| 481 | ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0; |
| 482 | |
| 483 | // Throw a fast low pass filter on the battery voltage so we don't respond |
| 484 | // too fast to noise. |
| 485 | filtered_battery_voltage_ = |
| 486 | 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage; |
| 487 | |
| 488 | suction_victor_->SetSpeed(::aos::Clip( |
| 489 | queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 492 | void Stop() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 493 | LOG(WARNING, "Superstructure output too old.\n"); |
| 494 | |
| 495 | elevator_victor_->SetDisabled(); |
| 496 | intake_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 497 | wrist_victor_->SetDisabled(); |
| 498 | stilts_victor_->SetDisabled(); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 499 | suction_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_, |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 503 | wrist_victor_, stilts_victor_, suction_victor_; |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 504 | |
| 505 | double filtered_battery_voltage_ = 12.0; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 506 | }; |
| 507 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 508 | class SolenoidWriter { |
| 509 | public: |
| 510 | SolenoidWriter() |
| 511 | : superstructure_( |
| 512 | ".y2019.control_loops.superstructure.superstructure_queue.output") { |
| 513 | } |
| 514 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 515 | void set_big_suction_cup(int index0, int index1) { |
| 516 | big_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 517 | big_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 518 | } |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 519 | void set_small_suction_cup(int index0, int index1) { |
| 520 | small_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 521 | small_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void set_intake_roller_talon( |
| 525 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) { |
| 526 | intake_rollers_talon_ = ::std::move(t); |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 527 | intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 528 | intake_rollers_talon_->EnableCurrentLimit(true); |
| 529 | } |
| 530 | |
| 531 | void operator()() { |
| 532 | ::aos::SetCurrentThreadName("Solenoids"); |
| 533 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 534 | |
| 535 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 536 | ::std::chrono::milliseconds(1)); |
| 537 | |
| 538 | while (run_) { |
| 539 | { |
| 540 | const int iterations = phased_loop.SleepUntilNext(); |
| 541 | if (iterations != 1) { |
| 542 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | { |
| 547 | superstructure_.FetchLatest(); |
| 548 | if (superstructure_.get()) { |
| 549 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 550 | |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 551 | big_suction_cup0_->Set(!superstructure_->intake_suction_bottom); |
| 552 | big_suction_cup1_->Set(!superstructure_->intake_suction_bottom); |
| 553 | small_suction_cup0_->Set(superstructure_->intake_suction_top); |
| 554 | small_suction_cup1_->Set(superstructure_->intake_suction_top); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 555 | |
| 556 | intake_rollers_talon_->Set( |
| 557 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 558 | ::aos::Clip(superstructure_->intake_roller_voltage, |
| 559 | -kMaxBringupPower, kMaxBringupPower) / |
| 560 | 12.0); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | { |
| 565 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 566 | |
| 567 | pcm_.Flush(); |
| 568 | to_log.read_solenoids = pcm_.GetAll(); |
| 569 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 570 | } |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 571 | |
| 572 | status_light.FetchLatest(); |
| 573 | // If we don't have a light request (or it's an old one), we are borked. |
| 574 | // Flash the red light slowly. |
| 575 | if (!status_light.get() || |
| 576 | status_light.Age() > chrono::milliseconds(100)) { |
| 577 | StatusLight color; |
| 578 | color.red = 0.0; |
| 579 | color.green = 0.0; |
| 580 | color.blue = 0.0; |
| 581 | |
| 582 | ++light_flash_; |
| 583 | if (light_flash_ > 10) { |
| 584 | color.red = 0.5; |
| 585 | } |
| 586 | |
| 587 | if (light_flash_ > 20) { |
| 588 | light_flash_ = 0; |
| 589 | } |
| 590 | |
| 591 | LOG_STRUCT(DEBUG, "color", color); |
| 592 | SetColor(color); |
| 593 | } else { |
| 594 | LOG_STRUCT(DEBUG, "color", *status_light); |
| 595 | SetColor(*status_light); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void SetColor(const StatusLight &status_light) { |
| 601 | // Save CAN bandwidth and CPU at the cost of RT. Only change the light when |
| 602 | // it actually changes. This is pretty low priority anyways. |
| 603 | static int time_since_last_send = 0; |
| 604 | ++time_since_last_send; |
| 605 | if (time_since_last_send > 10) { |
| 606 | time_since_last_send = 0; |
| 607 | } |
| 608 | if (status_light.green != last_green_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 609 | canifier_.SetLEDOutput(status_light.green, |
| 610 | ::ctre::phoenix::CANifier::LEDChannelA); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 611 | last_green_ = status_light.green; |
| 612 | } |
| 613 | |
| 614 | if (status_light.blue != last_blue_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 615 | canifier_.SetLEDOutput(status_light.blue, |
| 616 | ::ctre::phoenix::CANifier::LEDChannelC); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 617 | last_blue_ = status_light.blue; |
| 618 | } |
| 619 | |
| 620 | if (status_light.red != last_red_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 621 | canifier_.SetLEDOutput(status_light.red, |
| 622 | ::ctre::phoenix::CANifier::LEDChannelB); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 623 | last_red_ = status_light.red; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | void Quit() { run_ = false; } |
| 628 | |
| 629 | private: |
| 630 | ::frc971::wpilib::BufferedPcm pcm_; |
| 631 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 632 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_, |
| 633 | big_suction_cup1_, small_suction_cup0_, small_suction_cup1_; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 634 | |
| 635 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> |
| 636 | intake_rollers_talon_; |
| 637 | |
| 638 | ::aos::Queue< |
| 639 | ::y2019::control_loops::superstructure::SuperstructureQueue::Output> |
| 640 | superstructure_; |
| 641 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 642 | ::ctre::phoenix::CANifier canifier_{0}; |
| 643 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 644 | ::std::atomic<bool> run_{true}; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 645 | |
| 646 | double last_red_ = -1.0; |
| 647 | double last_green_ = -1.0; |
| 648 | double last_blue_ = -1.0; |
| 649 | |
| 650 | int light_flash_ = 0; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 651 | }; |
| 652 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 653 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 654 | public: |
| 655 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 656 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 657 | frc::Encoder::k4X); |
| 658 | } |
| 659 | |
| 660 | void Run() override { |
| 661 | ::aos::InitNRT(); |
| 662 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 663 | |
| 664 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 665 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 666 | |
| 667 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 668 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 669 | SensorReader reader; |
| 670 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 671 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 672 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 673 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 674 | reader.set_elevator_encoder(make_encoder(4)); |
| 675 | reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 676 | reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 677 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 678 | reader.set_wrist_encoder(make_encoder(5)); |
| 679 | reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5)); |
| 680 | reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 681 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 682 | reader.set_intake_encoder(make_encoder(2)); |
| 683 | reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 684 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 685 | reader.set_stilts_encoder(make_encoder(3)); |
| 686 | reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 687 | reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 688 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 689 | reader.set_pwm_trigger(true); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 690 | reader.set_vacuum_sensor(7); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 691 | |
Austin Schuh | e2f2248 | 2019-04-13 23:05:43 -0700 | [diff] [blame] | 692 | reader.set_platform_right_detect(make_unique<frc::DigitalInput>(6)); |
| 693 | reader.set_platform_left_detect(make_unique<frc::DigitalInput>(7)); |
| 694 | |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 695 | reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22)); |
| 696 | reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23)); |
| 697 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 698 | ::std::thread reader_thread(::std::ref(reader)); |
| 699 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 700 | CameraReader camera_reader; |
| 701 | frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3); |
| 702 | camera_reader.set_spi(&camera_spi); |
| 703 | camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 704 | // Austin says 8, 9, 24, and 25 are good options to choose from for these. |
| 705 | camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24)); |
| 706 | camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25)); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 707 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 708 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 709 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 710 | imu_trigger.get()); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 711 | imu.set_spi_idle_callback( |
| 712 | [&camera_reader]() { camera_reader.DoSpiTransaction(); }); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 713 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 714 | imu.set_reset(imu_reset.get()); |
| 715 | ::std::thread imu_thread(::std::ref(imu)); |
| 716 | |
| 717 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 718 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 719 | // variety so all the Victors are written as SPs. |
| 720 | |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 721 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer; |
| 722 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 723 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 724 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 725 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 726 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 727 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 728 | SuperstructureWriter superstructure_writer; |
| 729 | superstructure_writer.set_elevator_victor( |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 730 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 731 | // TODO(austin): Do the vacuum |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 732 | superstructure_writer.set_suction_victor( |
| 733 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 734 | superstructure_writer.set_intake_victor( |
| 735 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 736 | superstructure_writer.set_wrist_victor( |
| 737 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 738 | superstructure_writer.set_stilts_victor( |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 739 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 740 | |
| 741 | ::std::thread superstructure_writer_thread( |
| 742 | ::std::ref(superstructure_writer)); |
| 743 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 744 | SolenoidWriter solenoid_writer; |
| 745 | solenoid_writer.set_intake_roller_talon( |
| 746 | make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10)); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 747 | solenoid_writer.set_big_suction_cup(0, 1); |
| 748 | solenoid_writer.set_small_suction_cup(2, 3); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 749 | |
| 750 | ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer)); |
| 751 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 752 | // Wait forever. Not much else to do... |
| 753 | while (true) { |
| 754 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 755 | if (r != 0) { |
| 756 | PLOG(WARNING, "infinite select failed"); |
| 757 | } else { |
| 758 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 763 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 764 | solenoid_writer.Quit(); |
| 765 | solenoid_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 766 | joystick_sender.Quit(); |
| 767 | joystick_thread.join(); |
| 768 | pdp_fetcher.Quit(); |
| 769 | pdp_fetcher_thread.join(); |
| 770 | reader.Quit(); |
| 771 | reader_thread.join(); |
| 772 | imu.Quit(); |
| 773 | imu_thread.join(); |
| 774 | |
| 775 | drivetrain_writer.Quit(); |
| 776 | drivetrain_writer_thread.join(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 777 | superstructure_writer.Quit(); |
| 778 | superstructure_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 779 | |
| 780 | ::aos::Cleanup(); |
| 781 | } |
| 782 | }; |
| 783 | |
| 784 | } // namespace |
| 785 | } // namespace wpilib |
| 786 | } // namespace y2019 |
| 787 | |
| 788 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |