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