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