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