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