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" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 35 | #include "frc971/autonomous/auto_mode_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 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" |
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 | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 61 | using ::y2019::constants::Values; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 62 | using ::aos::monotonic_clock; |
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; |
| 65 | using aos::make_unique; |
| 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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 250 | 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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 311 | 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 | |
| 329 | 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(); |
| 369 | spi_->SetClockActiveLow(); |
| 370 | spi_->SetSampleDataOnFalling(); |
| 371 | // It ignores you if you try changing this... |
| 372 | spi_->SetMSBFirst(); |
| 373 | } |
| 374 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 375 | void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) { |
| 376 | activate_usb_ = std::move(activate_usb); |
| 377 | } |
| 378 | |
| 379 | void set_activate_passthrough( |
| 380 | std::unique_ptr<frc::DigitalInput> activate_passthrough) { |
| 381 | activate_passthrough_ = std::move(activate_passthrough); |
| 382 | } |
| 383 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 384 | void DoSpiTransaction() { |
| 385 | using namespace frc971::jevois; |
| 386 | RoborioToTeensy to_teensy{}; |
| 387 | to_teensy.realtime_now = aos::realtime_clock::now(); |
Austin Schuh | 5671a8c | 2019-05-19 17:01:04 -0700 | [diff] [blame] | 388 | camera_log_fetcher_.Fetch(); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 389 | if (activate_usb_ && !activate_usb_->Get()) { |
| 390 | to_teensy.camera_command = CameraCommand::kUsb; |
| 391 | } else if (activate_passthrough_ && !activate_passthrough_->Get()) { |
| 392 | to_teensy.camera_command = CameraCommand::kCameraPassthrough; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 393 | } else if (camera_log_fetcher_.get() && camera_log_fetcher_->log()) { |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 394 | to_teensy.camera_command = CameraCommand::kLog; |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 395 | } else { |
| 396 | to_teensy.camera_command = CameraCommand::kNormal; |
| 397 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 398 | |
| 399 | std::array<char, spi_transfer_size() + 1> to_send{}; |
| 400 | { |
| 401 | const auto to_send_data = |
| 402 | gsl::make_span(to_send).last<spi_transfer_size()>(); |
| 403 | const auto encoded = SpiPackToTeensy(to_teensy); |
| 404 | std::copy(encoded.begin(), encoded.end(), to_send_data.begin()); |
| 405 | } |
| 406 | rx_clearer_.ClearRxFifo(); |
| 407 | // First, send recieve a dummy byte because the Teensy can't control what it |
| 408 | // sends for the first byte. |
| 409 | std::array<char, spi_transfer_size() + 1> to_receive; |
| 410 | DoTransaction(to_send, to_receive); |
| 411 | const auto unpacked = SpiUnpackToRoborio( |
| 412 | gsl::make_span(to_receive).last(spi_transfer_size())); |
| 413 | if (!unpacked) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 414 | AOS_LOG(INFO, "Decoding SPI data failed\n"); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 418 | const aos::monotonic_clock::time_point now = aos::monotonic_clock::now(); |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 419 | for (const auto &received : unpacked->frames) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | auto builder = camera_frame_sender_.MakeBuilder(); |
| 421 | |
| 422 | std::array< |
| 423 | flatbuffers::Offset<y2019::control_loops::drivetrain::CameraTarget>, |
| 424 | 3> |
| 425 | targets; |
| 426 | |
| 427 | for (size_t i = 0; i < received.targets.size(); ++i) { |
| 428 | y2019::control_loops::drivetrain::CameraTarget::Builder |
| 429 | camera_target_builder = builder.MakeBuilder< |
| 430 | y2019::control_loops::drivetrain::CameraTarget>(); |
| 431 | |
| 432 | camera_target_builder.add_distance(received.targets[i].distance); |
| 433 | camera_target_builder.add_height(received.targets[i].height); |
| 434 | camera_target_builder.add_heading(received.targets[i].heading); |
| 435 | camera_target_builder.add_skew(received.targets[i].skew); |
| 436 | |
| 437 | targets[i] = camera_target_builder.Finish(); |
| 438 | } |
| 439 | |
| 440 | flatbuffers::Offset<flatbuffers::Vector< |
| 441 | flatbuffers::Offset<y2019::control_loops::drivetrain::CameraTarget>>> |
| 442 | targets_offset = builder.fbb()->CreateVector(targets.begin(), |
| 443 | received.targets.size()); |
| 444 | |
| 445 | y2019::control_loops::drivetrain::CameraFrame::Builder |
| 446 | camera_frame_builder = |
| 447 | builder |
| 448 | .MakeBuilder<y2019::control_loops::drivetrain::CameraFrame>(); |
| 449 | |
| 450 | camera_frame_builder.add_targets(targets_offset); |
| 451 | |
James Kuszmaul | e08f04e | 2019-05-01 21:46:50 -0500 | [diff] [blame] | 452 | // Add an extra 10ms delay to account for unmodeled delays that Austin |
| 453 | // thinks exists. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 454 | camera_frame_builder.add_timestamp( |
James Kuszmaul | e08f04e | 2019-05-01 21:46:50 -0500 | [diff] [blame] | 455 | std::chrono::nanoseconds( |
| 456 | (now - received.age - ::std::chrono::milliseconds(10)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 457 | .time_since_epoch()) |
| 458 | .count()); |
| 459 | camera_frame_builder.add_camera(received.camera_index); |
| 460 | builder.Send(camera_frame_builder.Finish()); |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 461 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 462 | |
| 463 | if (dummy_spi_) { |
| 464 | uint8_t dummy_send, dummy_receive; |
| 465 | dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 470 | AOS_CHECK_EQ(to_send.size(), to_receive.size()); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 471 | const auto result = spi_->Transaction( |
| 472 | reinterpret_cast<uint8_t *>(to_send.data()), |
| 473 | reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size()); |
| 474 | if (result == to_send.size()) { |
| 475 | return; |
| 476 | } |
| 477 | if (result == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 478 | 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] | 479 | return; |
| 480 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 481 | AOS_LOG(FATAL, "SPI::Transaction returned something weird\n"); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void SetDummySPI(frc::SPI::Port port) { |
| 485 | dummy_spi_.reset(new frc::SPI(port)); |
| 486 | // Pick the same settings here in case the roboRIO decides to try something |
| 487 | // stupid when switching. |
| 488 | if (dummy_spi_) { |
| 489 | dummy_spi_->SetClockRate(1e5); |
| 490 | dummy_spi_->SetChipSelectActiveLow(); |
| 491 | dummy_spi_->SetClockActiveLow(); |
| 492 | dummy_spi_->SetSampleDataOnFalling(); |
| 493 | dummy_spi_->SetMSBFirst(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | private: |
Austin Schuh | 8a633d5 | 2019-05-12 15:04:01 -0700 | [diff] [blame] | 498 | ::aos::Sender<::y2019::control_loops::drivetrain::CameraFrame> |
| 499 | camera_frame_sender_; |
Austin Schuh | 5671a8c | 2019-05-19 17:01:04 -0700 | [diff] [blame] | 500 | ::aos::Fetcher<::y2019::CameraLog> camera_log_fetcher_; |
Austin Schuh | 8a633d5 | 2019-05-12 15:04:01 -0700 | [diff] [blame] | 501 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 502 | frc::SPI *spi_ = nullptr; |
| 503 | ::std::unique_ptr<frc::SPI> dummy_spi_; |
| 504 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 505 | std::unique_ptr<frc::DigitalInput> activate_usb_; |
| 506 | std::unique_ptr<frc::DigitalInput> activate_passthrough_; |
| 507 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 508 | frc971::wpilib::SpiRxClearer rx_clearer_; |
| 509 | }; |
| 510 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 511 | class SuperstructureWriter |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 512 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 513 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 514 | SuperstructureWriter(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 515 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 516 | event_loop, "/superstructure"), |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 517 | robot_state_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 518 | event_loop->MakeFetcher<::aos::RobotState>("/aos")) {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 519 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 520 | void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 521 | elevator_victor_ = ::std::move(t); |
| 522 | } |
| 523 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 524 | void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 525 | suction_victor_ = ::std::move(t); |
| 526 | } |
| 527 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 528 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 529 | intake_victor_ = ::std::move(t); |
| 530 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 531 | |
| 532 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 533 | wrist_victor_ = ::std::move(t); |
| 534 | } |
| 535 | |
| 536 | void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 537 | stilts_victor_ = ::std::move(t); |
| 538 | } |
| 539 | |
| 540 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 541 | void Write(const superstructure::Output &output) override { |
| 542 | elevator_victor_->SetSpeed(::aos::Clip(output.elevator_voltage(), |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 543 | -kMaxBringupPower, |
| 544 | kMaxBringupPower) / |
| 545 | 12.0); |
| 546 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | intake_victor_->SetSpeed(::aos::Clip(output.intake_joint_voltage(), |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 548 | -kMaxBringupPower, kMaxBringupPower) / |
| 549 | 12.0); |
| 550 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 551 | wrist_victor_->SetSpeed(::aos::Clip(-output.wrist_voltage(), |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 552 | -kMaxBringupPower, kMaxBringupPower) / |
| 553 | 12.0); |
| 554 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 555 | stilts_victor_->SetSpeed(::aos::Clip(output.stilts_voltage(), |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 556 | -kMaxBringupPower, kMaxBringupPower) / |
| 557 | 12.0); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 558 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 559 | robot_state_fetcher_.Fetch(); |
| 560 | const double battery_voltage = robot_state_fetcher_.get() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 561 | ? robot_state_fetcher_->voltage_battery() |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 562 | : 12.0; |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 563 | |
| 564 | // Throw a fast low pass filter on the battery voltage so we don't respond |
| 565 | // too fast to noise. |
| 566 | filtered_battery_voltage_ = |
| 567 | 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage; |
| 568 | |
| 569 | suction_victor_->SetSpeed(::aos::Clip( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 570 | output.pump_voltage() / filtered_battery_voltage_, -1.0, 1.0)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 573 | void Stop() override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 574 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 575 | |
| 576 | elevator_victor_->SetDisabled(); |
| 577 | intake_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 578 | wrist_victor_->SetDisabled(); |
| 579 | stilts_victor_->SetDisabled(); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 580 | suction_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 583 | ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_; |
| 584 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 585 | ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_, |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 586 | wrist_victor_, stilts_victor_, suction_victor_; |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 587 | |
| 588 | double filtered_battery_voltage_ = 12.0; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 589 | }; |
| 590 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 591 | class SolenoidWriter { |
| 592 | public: |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 593 | SolenoidWriter(::aos::EventLoop *event_loop) |
| 594 | : event_loop_(event_loop), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 595 | superstructure_fetcher_( |
| 596 | event_loop->MakeFetcher<superstructure::Output>("/superstructure")), |
| 597 | status_light_fetcher_( |
| 598 | event_loop->MakeFetcher<::y2019::StatusLight>("/superstructure")), |
| 599 | pneumatics_to_log_sender_( |
| 600 | event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 601 | ::aos::SetCurrentThreadName("Solenoids"); |
| 602 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 603 | |
| 604 | event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 605 | ::std::chrono::milliseconds(20), |
| 606 | ::std::chrono::milliseconds(1)); |
| 607 | } |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 608 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 609 | void set_big_suction_cup(int index0, int index1) { |
| 610 | big_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 611 | big_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 612 | } |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 613 | void set_small_suction_cup(int index0, int index1) { |
| 614 | small_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 615 | small_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void set_intake_roller_talon( |
| 619 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) { |
| 620 | intake_rollers_talon_ = ::std::move(t); |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 621 | intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 622 | intake_rollers_talon_->EnableCurrentLimit(true); |
| 623 | } |
| 624 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 625 | void Loop(const int iterations) { |
| 626 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 627 | AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 628 | } |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 629 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 630 | { |
| 631 | superstructure_fetcher_.Fetch(); |
| 632 | if (superstructure_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 633 | big_suction_cup0_->Set( |
| 634 | !superstructure_fetcher_->intake_suction_bottom()); |
| 635 | big_suction_cup1_->Set( |
| 636 | !superstructure_fetcher_->intake_suction_bottom()); |
| 637 | small_suction_cup0_->Set(superstructure_fetcher_->intake_suction_top()); |
| 638 | small_suction_cup1_->Set(superstructure_fetcher_->intake_suction_top()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 639 | |
| 640 | intake_rollers_talon_->Set( |
| 641 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 642 | ::aos::Clip(superstructure_fetcher_->intake_roller_voltage(), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 643 | -kMaxBringupPower, kMaxBringupPower) / |
| 644 | 12.0); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 649 | auto builder = pneumatics_to_log_sender_.MakeBuilder(); |
| 650 | |
| 651 | ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder = |
| 652 | builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 653 | |
| 654 | pcm_.Flush(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 655 | to_log_builder.add_read_solenoids(pcm_.GetAll()); |
| 656 | builder.Send(to_log_builder.Finish()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | status_light_fetcher_.Fetch(); |
| 660 | // If we don't have a light request (or it's an old one), we are borked. |
| 661 | // Flash the red light slowly. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 662 | StatusLightT color; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 663 | if (!status_light_fetcher_.get() || |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 664 | status_light_fetcher_.context().monotonic_event_time + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 665 | chrono::milliseconds(100) < |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 666 | event_loop_->monotonic_now()) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 667 | color.red = 0.0; |
| 668 | color.green = 0.0; |
| 669 | color.blue = 0.0; |
| 670 | |
| 671 | ++light_flash_; |
| 672 | if (light_flash_ > 10) { |
| 673 | color.red = 0.5; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 676 | if (light_flash_ > 20) { |
| 677 | light_flash_ = 0; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 680 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 681 | status_light_fetcher_->UnPackTo(&color); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 682 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 683 | SetColor(color); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 686 | void SetColor(const StatusLightT status_light) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 687 | // Save CAN bandwidth and CPU at the cost of RT. Only change the light when |
| 688 | // it actually changes. This is pretty low priority anyways. |
| 689 | static int time_since_last_send = 0; |
| 690 | ++time_since_last_send; |
| 691 | if (time_since_last_send > 10) { |
| 692 | time_since_last_send = 0; |
| 693 | } |
| 694 | if (status_light.green != last_green_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 695 | canifier_.SetLEDOutput(status_light.green, |
| 696 | ::ctre::phoenix::CANifier::LEDChannelA); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 697 | last_green_ = status_light.green; |
| 698 | } |
| 699 | |
| 700 | if (status_light.blue != last_blue_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 701 | canifier_.SetLEDOutput(status_light.blue, |
| 702 | ::ctre::phoenix::CANifier::LEDChannelC); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 703 | last_blue_ = status_light.blue; |
| 704 | } |
| 705 | |
| 706 | if (status_light.red != last_red_ || time_since_last_send == 0) { |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 707 | canifier_.SetLEDOutput(status_light.red, |
| 708 | ::ctre::phoenix::CANifier::LEDChannelB); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 709 | last_red_ = status_light.red; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 713 | private: |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 714 | ::aos::EventLoop *event_loop_; |
| 715 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 716 | ::frc971::wpilib::BufferedPcm pcm_; |
| 717 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 718 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_, |
| 719 | big_suction_cup1_, small_suction_cup0_, small_suction_cup1_; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 720 | |
| 721 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> |
| 722 | intake_rollers_talon_; |
| 723 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 724 | ::aos::Fetcher<::y2019::control_loops::superstructure::Output> |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 725 | superstructure_fetcher_; |
| 726 | ::aos::Fetcher<::y2019::StatusLight> status_light_fetcher_; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 727 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 728 | aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_; |
| 729 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 730 | ::ctre::phoenix::CANifier canifier_{0}; |
| 731 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 732 | double last_red_ = -1.0; |
| 733 | double last_green_ = -1.0; |
| 734 | double last_blue_ = -1.0; |
| 735 | |
| 736 | int light_flash_ = 0; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 737 | }; |
| 738 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 739 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 740 | public: |
| 741 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 742 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 743 | frc::Encoder::k4X); |
| 744 | } |
| 745 | |
| 746 | void Run() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 747 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 748 | aos::configuration::ReadConfig("config.json"); |
| 749 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 750 | // Thread 1. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 751 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 752 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 753 | &joystick_sender_event_loop); |
| 754 | AddLoop(&joystick_sender_event_loop); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 755 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 756 | // Thread 2. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 757 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 758 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 759 | AddLoop(&pdp_fetcher_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 760 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 761 | // Thread 3. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 762 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 763 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 764 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 765 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 766 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 767 | sensor_reader.set_elevator_encoder(make_encoder(4)); |
| 768 | sensor_reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 769 | sensor_reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4)); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 770 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 771 | sensor_reader.set_wrist_encoder(make_encoder(5)); |
| 772 | sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5)); |
| 773 | sensor_reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5)); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 774 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 775 | sensor_reader.set_intake_encoder(make_encoder(2)); |
| 776 | sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 777 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 778 | sensor_reader.set_stilts_encoder(make_encoder(3)); |
| 779 | sensor_reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
| 780 | sensor_reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3)); |
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_pwm_trigger(true); |
| 783 | sensor_reader.set_vacuum_sensor(7); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 784 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 785 | sensor_reader.set_platform_right_detect(make_unique<frc::DigitalInput>(6)); |
| 786 | sensor_reader.set_platform_left_detect(make_unique<frc::DigitalInput>(7)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 787 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 788 | sensor_reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22)); |
| 789 | sensor_reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23)); |
| 790 | AddLoop(&sensor_reader_event_loop); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 791 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 792 | // Thread 4. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 793 | ::aos::ShmEventLoop imu_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 794 | CameraReader camera_reader(&imu_event_loop); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 795 | frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3); |
| 796 | camera_reader.set_spi(&camera_spi); |
| 797 | camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame] | 798 | // Austin says 8, 9, 24, and 25 are good options to choose from for these. |
| 799 | camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24)); |
| 800 | camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25)); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 801 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 802 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 803 | ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, frc::SPI::Port::kOnboardCS1, |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 804 | imu_trigger.get()); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 805 | imu.set_spi_idle_callback( |
| 806 | [&camera_reader]() { camera_reader.DoSpiTransaction(); }); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 807 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 808 | imu.set_reset(imu_reset.get()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 809 | AddLoop(&imu_event_loop); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 810 | |
| 811 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 812 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 813 | // variety so all the Victors are written as SPs. |
| 814 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 815 | // Thread 5. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 816 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 817 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 818 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 819 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 820 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 821 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 822 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 823 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 824 | superstructure_writer.set_elevator_victor( |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 825 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 826 | // TODO(austin): Do the vacuum |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 827 | superstructure_writer.set_suction_victor( |
| 828 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 829 | superstructure_writer.set_intake_victor( |
| 830 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 831 | superstructure_writer.set_wrist_victor( |
| 832 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 833 | superstructure_writer.set_stilts_victor( |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 834 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 835 | AddLoop(&output_event_loop); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 836 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 837 | // Thread 6. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 838 | ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 839 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 840 | solenoid_writer.set_intake_roller_talon( |
| 841 | make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10)); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 842 | solenoid_writer.set_big_suction_cup(0, 1); |
| 843 | solenoid_writer.set_small_suction_cup(2, 3); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 844 | AddLoop(&solenoid_writer_event_loop); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 845 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 846 | RunLoops(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 847 | } |
| 848 | }; |
| 849 | |
| 850 | } // namespace |
| 851 | } // namespace wpilib |
| 852 | } // namespace y2019 |
| 853 | |
| 854 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |