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