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