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