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