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