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" |
| 19 | #undef ERROR |
| 20 | |
| 21 | #include "aos/commonmath.h" |
| 22 | #include "aos/init.h" |
| 23 | #include "aos/logging/logging.h" |
| 24 | #include "aos/logging/queue_logging.h" |
| 25 | #include "aos/make_unique.h" |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 26 | #include "aos/robot_state/robot_state.q.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 27 | #include "aos/time/time.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 28 | #include "aos/util/log_interval.h" |
| 29 | #include "aos/util/phased_loop.h" |
| 30 | #include "aos/util/wrapping_counter.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 31 | #include "frc971/autonomous/auto.q.h" |
| 32 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 33 | #include "frc971/wpilib/ADIS16448.h" |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 34 | #include "frc971/wpilib/buffered_pcm.h" |
| 35 | #include "frc971/wpilib/buffered_solenoid.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/dma.h" |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 37 | #include "frc971/wpilib/drivetrain_writer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 38 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 39 | #include "frc971/wpilib/joystick_sender.h" |
| 40 | #include "frc971/wpilib/logging.q.h" |
| 41 | #include "frc971/wpilib/loop_output_handler.h" |
| 42 | #include "frc971/wpilib/pdp_fetcher.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 43 | #include "frc971/wpilib/sensor_reader.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 44 | #include "frc971/wpilib/wpilib_robot_base.h" |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 45 | #include "third_party/Phoenix-frc-lib/cpp/include/ctre/phoenix/MotorControl/CAN/TalonSRX.h" |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 46 | #include "y2019/constants.h" |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 47 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 48 | |
| 49 | #ifndef M_PI |
| 50 | #define M_PI 3.14159265358979323846 |
| 51 | #endif |
| 52 | |
| 53 | using ::frc971::control_loops::drivetrain_queue; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 54 | using ::y2019::control_loops::superstructure::superstructure_queue; |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 55 | using ::y2019::constants::Values; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 56 | using ::aos::monotonic_clock; |
| 57 | namespace chrono = ::std::chrono; |
| 58 | using aos::make_unique; |
| 59 | |
| 60 | namespace y2019 { |
| 61 | namespace wpilib { |
| 62 | namespace { |
| 63 | |
| 64 | constexpr double kMaxBringupPower = 12.0; |
| 65 | |
| 66 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 67 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 68 | // The low bit is direction. |
| 69 | |
| 70 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 71 | // constexpr. |
| 72 | template <typename T> |
| 73 | constexpr T max(T a, T b) { |
| 74 | return (a > b) ? a : b; |
| 75 | } |
| 76 | |
| 77 | template <typename T, typename... Rest> |
| 78 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 79 | return max(max(a, b), c, rest...); |
| 80 | } |
| 81 | |
| 82 | double drivetrain_translate(int32_t in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 83 | return ((static_cast<double>(in) / |
| 84 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 85 | (2.0 * M_PI)) * |
| 86 | Values::kDrivetrainEncoderRatio() * |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 87 | control_loops::drivetrain::kWheelRadius; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | double drivetrain_velocity_translate(double in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 91 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
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 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 97 | double elevator_pot_translate(double voltage) { |
| 98 | return voltage * Values::kElevatorPotRatio() * |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 99 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | double wrist_pot_translate(double voltage) { |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 103 | return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) * |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 104 | (2 * M_PI /*radians*/); |
| 105 | } |
| 106 | |
| 107 | double stilts_pot_translate(double voltage) { |
| 108 | return voltage * Values::kStiltsPotRatio() * |
| 109 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 110 | } |
| 111 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 112 | constexpr double kMaxFastEncoderPulsesPerSecond = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 113 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 114 | Values::kMaxIntakeEncoderPulsesPerSecond()); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 115 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 116 | "fast encoders are too fast"); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 117 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 118 | max(Values::kMaxElevatorEncoderPulsesPerSecond(), |
| 119 | Values::kMaxWristEncoderPulsesPerSecond()); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 120 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 121 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 122 | "medium encoders are too fast"); |
| 123 | |
| 124 | // Class to send position messages with sensor readings to our loops. |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 125 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 126 | public: |
| 127 | SensorReader() { |
| 128 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 129 | // we should ever see. |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 130 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 131 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 134 | // Elevator |
| 135 | |
| 136 | void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 137 | medium_encoder_filter_.Add(encoder.get()); |
| 138 | elevator_encoder_.set_encoder(::std::move(encoder)); |
| 139 | } |
| 140 | |
| 141 | void set_elevator_absolute_pwm( |
| 142 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 143 | elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 144 | } |
| 145 | |
| 146 | void set_elevator_potentiometer( |
| 147 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 148 | elevator_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 149 | } |
| 150 | |
| 151 | // Intake |
| 152 | |
| 153 | void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 154 | medium_encoder_filter_.Add(encoder.get()); |
| 155 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 156 | } |
| 157 | |
| 158 | void set_intake_absolute_pwm( |
| 159 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 160 | intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 161 | } |
| 162 | |
| 163 | // Wrist |
| 164 | |
| 165 | void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 166 | medium_encoder_filter_.Add(encoder.get()); |
| 167 | wrist_encoder_.set_encoder(::std::move(encoder)); |
| 168 | } |
| 169 | |
| 170 | void set_wrist_absolute_pwm( |
| 171 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 172 | wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 173 | } |
| 174 | |
| 175 | void set_wrist_potentiometer( |
| 176 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 177 | wrist_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 178 | } |
| 179 | |
| 180 | // Stilts |
| 181 | |
| 182 | void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 183 | medium_encoder_filter_.Add(encoder.get()); |
| 184 | stilts_encoder_.set_encoder(::std::move(encoder)); |
| 185 | } |
| 186 | |
| 187 | void set_stilts_absolute_pwm( |
| 188 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 189 | stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 190 | } |
| 191 | |
| 192 | void set_stilts_potentiometer( |
| 193 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 194 | stilts_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 195 | } |
| 196 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 197 | // Vacuum pressure sensor |
| 198 | void set_vacuum_sensor(int port) { |
| 199 | vacuum_sensor_ = make_unique<frc::AnalogInput>(port); |
| 200 | } |
| 201 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 202 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 203 | { |
| 204 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 205 | drivetrain_message->left_encoder = |
| 206 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 207 | drivetrain_message->left_speed = |
| 208 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 209 | |
| 210 | drivetrain_message->right_encoder = |
| 211 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 212 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 213 | drivetrain_right_encoder_->GetPeriod()); |
| 214 | |
| 215 | drivetrain_message.Send(); |
| 216 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 217 | const auto values = constants::GetValues(); |
| 218 | |
| 219 | { |
| 220 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 221 | |
| 222 | // Elevator |
| 223 | CopyPosition(elevator_encoder_, &superstructure_message->elevator, |
| 224 | Values::kElevatorEncoderCountsPerRevolution(), |
| 225 | Values::kElevatorEncoderRatio(), elevator_pot_translate, |
| 226 | false, values.elevator.potentiometer_offset); |
| 227 | // Intake |
| 228 | CopyPosition(intake_encoder_, &superstructure_message->intake_joint, |
| 229 | Values::kIntakeEncoderCountsPerRevolution(), |
| 230 | Values::kIntakeEncoderRatio(), false); |
| 231 | |
| 232 | // Wrist |
| 233 | CopyPosition(wrist_encoder_, &superstructure_message->wrist, |
| 234 | Values::kWristEncoderCountsPerRevolution(), |
| 235 | Values::kWristEncoderRatio(), wrist_pot_translate, false, |
| 236 | values.wrist.potentiometer_offset); |
| 237 | |
| 238 | // Stilts |
| 239 | CopyPosition(stilts_encoder_, &superstructure_message->stilts, |
| 240 | Values::kStiltsEncoderCountsPerRevolution(), |
| 241 | Values::kStiltsEncoderRatio(), stilts_pot_translate, false, |
| 242 | values.stilts.potentiometer_offset); |
| 243 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 244 | // Suction |
| 245 | constexpr float kMinVoltage = 0.5; |
| 246 | constexpr float kMaxVoltage = 2.1; |
| 247 | superstructure_message->suction_pressure = |
| 248 | (vacuum_sensor_->GetVoltage() - kMinVoltage) / |
| 249 | (kMaxVoltage - kMinVoltage); |
| 250 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 251 | superstructure_message.Send(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private: |
| 256 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_, |
| 257 | wrist_encoder_, stilts_encoder_; |
| 258 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 259 | ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_; |
| 260 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 261 | ::frc971::wpilib::AbsoluteEncoder intake_encoder_; |
| 262 | // TODO(sabina): Add wrist and elevator hall effects. |
| 263 | }; |
| 264 | |
| 265 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 266 | public: |
| 267 | void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 268 | elevator_victor_ = ::std::move(t); |
| 269 | } |
| 270 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 271 | void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 272 | suction_victor_ = ::std::move(t); |
| 273 | } |
| 274 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 275 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 276 | intake_victor_ = ::std::move(t); |
| 277 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 278 | |
| 279 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 280 | wrist_victor_ = ::std::move(t); |
| 281 | } |
| 282 | |
| 283 | void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 284 | stilts_victor_ = ::std::move(t); |
| 285 | } |
| 286 | |
| 287 | private: |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 288 | void Read() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 289 | ::y2019::control_loops::superstructure::superstructure_queue.output |
| 290 | .FetchAnother(); |
| 291 | } |
| 292 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 293 | void Write() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 294 | auto &queue = |
| 295 | ::y2019::control_loops::superstructure::superstructure_queue.output; |
| 296 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 297 | elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 298 | -kMaxBringupPower, |
| 299 | kMaxBringupPower) / |
| 300 | 12.0); |
| 301 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 302 | intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 303 | -kMaxBringupPower, kMaxBringupPower) / |
| 304 | 12.0); |
| 305 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 306 | wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage, |
| 307 | -kMaxBringupPower, kMaxBringupPower) / |
| 308 | 12.0); |
| 309 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 310 | stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 311 | -kMaxBringupPower, kMaxBringupPower) / |
| 312 | 12.0); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 313 | |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 314 | ::aos::robot_state.FetchLatest(); |
| 315 | const double battery_voltage = |
| 316 | ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0; |
| 317 | |
| 318 | // Throw a fast low pass filter on the battery voltage so we don't respond |
| 319 | // too fast to noise. |
| 320 | filtered_battery_voltage_ = |
| 321 | 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage; |
| 322 | |
| 323 | suction_victor_->SetSpeed(::aos::Clip( |
| 324 | queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 327 | void Stop() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 328 | LOG(WARNING, "Superstructure output too old.\n"); |
| 329 | |
| 330 | elevator_victor_->SetDisabled(); |
| 331 | intake_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 332 | wrist_victor_->SetDisabled(); |
| 333 | stilts_victor_->SetDisabled(); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 334 | suction_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_, |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 338 | wrist_victor_, stilts_victor_, suction_victor_; |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 339 | |
| 340 | double filtered_battery_voltage_ = 12.0; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 341 | }; |
| 342 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 343 | class SolenoidWriter { |
| 344 | public: |
| 345 | SolenoidWriter() |
| 346 | : superstructure_( |
| 347 | ".y2019.control_loops.superstructure.superstructure_queue.output") { |
| 348 | } |
| 349 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 350 | void set_big_suction_cup(int index0, int index1) { |
| 351 | big_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 352 | big_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 353 | } |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 354 | void set_small_suction_cup(int index0, int index1) { |
| 355 | small_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 356 | small_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void set_intake_roller_talon( |
| 360 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) { |
| 361 | intake_rollers_talon_ = ::std::move(t); |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 362 | intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 363 | intake_rollers_talon_->EnableCurrentLimit(true); |
| 364 | } |
| 365 | |
| 366 | void operator()() { |
| 367 | ::aos::SetCurrentThreadName("Solenoids"); |
| 368 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 369 | |
| 370 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 371 | ::std::chrono::milliseconds(1)); |
| 372 | |
| 373 | while (run_) { |
| 374 | { |
| 375 | const int iterations = phased_loop.SleepUntilNext(); |
| 376 | if (iterations != 1) { |
| 377 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | { |
| 382 | superstructure_.FetchLatest(); |
| 383 | if (superstructure_.get()) { |
| 384 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 385 | |
Sabina Davis | a569ac8 | 2019-02-23 10:47:42 -0800 | [diff] [blame] | 386 | big_suction_cup0_->Set(superstructure_->intake_suction_top); |
| 387 | big_suction_cup1_->Set(superstructure_->intake_suction_top); |
| 388 | small_suction_cup0_->Set(superstructure_->intake_suction_bottom); |
| 389 | small_suction_cup1_->Set(superstructure_->intake_suction_bottom); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 390 | |
| 391 | intake_rollers_talon_->Set( |
| 392 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 393 | ::aos::Clip(superstructure_->intake_roller_voltage, |
| 394 | -kMaxBringupPower, kMaxBringupPower) / |
| 395 | 12.0); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | { |
| 400 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 401 | |
| 402 | pcm_.Flush(); |
| 403 | to_log.read_solenoids = pcm_.GetAll(); |
| 404 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void Quit() { run_ = false; } |
| 410 | |
| 411 | private: |
| 412 | ::frc971::wpilib::BufferedPcm pcm_; |
| 413 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 414 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_, |
| 415 | big_suction_cup1_, small_suction_cup0_, small_suction_cup1_; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 416 | |
| 417 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> |
| 418 | intake_rollers_talon_; |
| 419 | |
| 420 | ::aos::Queue< |
| 421 | ::y2019::control_loops::superstructure::SuperstructureQueue::Output> |
| 422 | superstructure_; |
| 423 | |
| 424 | ::std::atomic<bool> run_{true}; |
| 425 | }; |
| 426 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 427 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 428 | public: |
| 429 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 430 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 431 | frc::Encoder::k4X); |
| 432 | } |
| 433 | |
| 434 | void Run() override { |
| 435 | ::aos::InitNRT(); |
| 436 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 437 | |
| 438 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 439 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 440 | |
| 441 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 442 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 443 | SensorReader reader; |
| 444 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 445 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 446 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 447 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 448 | reader.set_elevator_encoder(make_encoder(4)); |
| 449 | reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 450 | reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 451 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 452 | reader.set_wrist_encoder(make_encoder(5)); |
| 453 | reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5)); |
| 454 | reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 455 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 456 | reader.set_intake_encoder(make_encoder(2)); |
| 457 | reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 458 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 459 | reader.set_stilts_encoder(make_encoder(3)); |
| 460 | reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 461 | reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 462 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 463 | reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25)); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 464 | reader.set_vacuum_sensor(7); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 465 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 466 | ::std::thread reader_thread(::std::ref(reader)); |
| 467 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 468 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 469 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 470 | imu_trigger.get()); |
| 471 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 472 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 473 | imu.set_reset(imu_reset.get()); |
| 474 | ::std::thread imu_thread(::std::ref(imu)); |
| 475 | |
| 476 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 477 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 478 | // variety so all the Victors are written as SPs. |
| 479 | |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 480 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer; |
| 481 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 482 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 483 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 484 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 485 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 486 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 487 | SuperstructureWriter superstructure_writer; |
| 488 | superstructure_writer.set_elevator_victor( |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 489 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 490 | // TODO(austin): Do the vacuum |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 491 | superstructure_writer.set_suction_victor( |
| 492 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 493 | superstructure_writer.set_intake_victor( |
| 494 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 495 | superstructure_writer.set_wrist_victor( |
| 496 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 497 | superstructure_writer.set_stilts_victor( |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 498 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 499 | |
| 500 | ::std::thread superstructure_writer_thread( |
| 501 | ::std::ref(superstructure_writer)); |
| 502 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 503 | SolenoidWriter solenoid_writer; |
| 504 | solenoid_writer.set_intake_roller_talon( |
| 505 | make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10)); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 506 | solenoid_writer.set_big_suction_cup(0, 1); |
| 507 | solenoid_writer.set_small_suction_cup(2, 3); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 508 | |
| 509 | ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer)); |
| 510 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 511 | // Wait forever. Not much else to do... |
| 512 | while (true) { |
| 513 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 514 | if (r != 0) { |
| 515 | PLOG(WARNING, "infinite select failed"); |
| 516 | } else { |
| 517 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 522 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 523 | solenoid_writer.Quit(); |
| 524 | solenoid_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 525 | joystick_sender.Quit(); |
| 526 | joystick_thread.join(); |
| 527 | pdp_fetcher.Quit(); |
| 528 | pdp_fetcher_thread.join(); |
| 529 | reader.Quit(); |
| 530 | reader_thread.join(); |
| 531 | imu.Quit(); |
| 532 | imu_thread.join(); |
| 533 | |
| 534 | drivetrain_writer.Quit(); |
| 535 | drivetrain_writer_thread.join(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 536 | superstructure_writer.Quit(); |
| 537 | superstructure_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 538 | |
| 539 | ::aos::Cleanup(); |
| 540 | } |
| 541 | }; |
| 542 | |
| 543 | } // namespace |
| 544 | } // namespace wpilib |
| 545 | } // namespace y2019 |
| 546 | |
| 547 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |