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