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 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame^] | 282 | void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) { |
| 283 | activate_usb_ = std::move(activate_usb); |
| 284 | } |
| 285 | |
| 286 | void set_activate_passthrough( |
| 287 | std::unique_ptr<frc::DigitalInput> activate_passthrough) { |
| 288 | activate_passthrough_ = std::move(activate_passthrough); |
| 289 | } |
| 290 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 291 | void DoSpiTransaction() { |
| 292 | using namespace frc971::jevois; |
| 293 | RoborioToTeensy to_teensy{}; |
| 294 | to_teensy.realtime_now = aos::realtime_clock::now(); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame^] | 295 | if (activate_usb_ && !activate_usb_->Get()) { |
| 296 | to_teensy.camera_command = CameraCommand::kUsb; |
| 297 | } else if (activate_passthrough_ && !activate_passthrough_->Get()) { |
| 298 | to_teensy.camera_command = CameraCommand::kCameraPassthrough; |
| 299 | } else { |
| 300 | to_teensy.camera_command = CameraCommand::kNormal; |
| 301 | } |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 302 | |
| 303 | std::array<char, spi_transfer_size() + 1> to_send{}; |
| 304 | { |
| 305 | const auto to_send_data = |
| 306 | gsl::make_span(to_send).last<spi_transfer_size()>(); |
| 307 | const auto encoded = SpiPackToTeensy(to_teensy); |
| 308 | std::copy(encoded.begin(), encoded.end(), to_send_data.begin()); |
| 309 | } |
| 310 | rx_clearer_.ClearRxFifo(); |
| 311 | // First, send recieve a dummy byte because the Teensy can't control what it |
| 312 | // sends for the first byte. |
| 313 | std::array<char, spi_transfer_size() + 1> to_receive; |
| 314 | DoTransaction(to_send, to_receive); |
| 315 | const auto unpacked = SpiUnpackToRoborio( |
| 316 | gsl::make_span(to_receive).last(spi_transfer_size())); |
| 317 | if (!unpacked) { |
| 318 | LOG(INFO, "Decoding SPI data failed\n"); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // TODO(Brian): Do something useful with the targets. |
| 323 | |
| 324 | if (dummy_spi_) { |
| 325 | uint8_t dummy_send, dummy_receive; |
| 326 | dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) { |
| 331 | CHECK_EQ(to_send.size(), to_receive.size()); |
| 332 | const auto result = spi_->Transaction( |
| 333 | reinterpret_cast<uint8_t *>(to_send.data()), |
| 334 | reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size()); |
| 335 | if (result == to_send.size()) { |
| 336 | return; |
| 337 | } |
| 338 | if (result == -1) { |
| 339 | LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size()); |
| 340 | return; |
| 341 | } |
| 342 | LOG(FATAL, "SPI::Transaction returned something weird\n"); |
| 343 | } |
| 344 | |
| 345 | void SetDummySPI(frc::SPI::Port port) { |
| 346 | dummy_spi_.reset(new frc::SPI(port)); |
| 347 | // Pick the same settings here in case the roboRIO decides to try something |
| 348 | // stupid when switching. |
| 349 | if (dummy_spi_) { |
| 350 | dummy_spi_->SetClockRate(1e5); |
| 351 | dummy_spi_->SetChipSelectActiveLow(); |
| 352 | dummy_spi_->SetClockActiveLow(); |
| 353 | dummy_spi_->SetSampleDataOnFalling(); |
| 354 | dummy_spi_->SetMSBFirst(); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private: |
| 359 | frc::SPI *spi_ = nullptr; |
| 360 | ::std::unique_ptr<frc::SPI> dummy_spi_; |
| 361 | |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame^] | 362 | std::unique_ptr<frc::DigitalInput> activate_usb_; |
| 363 | std::unique_ptr<frc::DigitalInput> activate_passthrough_; |
| 364 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 365 | frc971::wpilib::SpiRxClearer rx_clearer_; |
| 366 | }; |
| 367 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 368 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 369 | public: |
| 370 | void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 371 | elevator_victor_ = ::std::move(t); |
| 372 | } |
| 373 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 374 | void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 375 | suction_victor_ = ::std::move(t); |
| 376 | } |
| 377 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 378 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 379 | intake_victor_ = ::std::move(t); |
| 380 | } |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 381 | |
| 382 | void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 383 | wrist_victor_ = ::std::move(t); |
| 384 | } |
| 385 | |
| 386 | void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 387 | stilts_victor_ = ::std::move(t); |
| 388 | } |
| 389 | |
| 390 | private: |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 391 | void Read() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 392 | ::y2019::control_loops::superstructure::superstructure_queue.output |
| 393 | .FetchAnother(); |
| 394 | } |
| 395 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 396 | void Write() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 397 | auto &queue = |
| 398 | ::y2019::control_loops::superstructure::superstructure_queue.output; |
| 399 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 400 | elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 401 | -kMaxBringupPower, |
| 402 | kMaxBringupPower) / |
| 403 | 12.0); |
| 404 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 405 | intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 406 | -kMaxBringupPower, kMaxBringupPower) / |
| 407 | 12.0); |
| 408 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 409 | wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage, |
| 410 | -kMaxBringupPower, kMaxBringupPower) / |
| 411 | 12.0); |
| 412 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 413 | stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage, |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 414 | -kMaxBringupPower, kMaxBringupPower) / |
| 415 | 12.0); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 416 | |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 417 | ::aos::robot_state.FetchLatest(); |
| 418 | const double battery_voltage = |
| 419 | ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0; |
| 420 | |
| 421 | // Throw a fast low pass filter on the battery voltage so we don't respond |
| 422 | // too fast to noise. |
| 423 | filtered_battery_voltage_ = |
| 424 | 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage; |
| 425 | |
| 426 | suction_victor_->SetSpeed(::aos::Clip( |
| 427 | queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 430 | void Stop() override { |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 431 | LOG(WARNING, "Superstructure output too old.\n"); |
| 432 | |
| 433 | elevator_victor_->SetDisabled(); |
| 434 | intake_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 435 | wrist_victor_->SetDisabled(); |
| 436 | stilts_victor_->SetDisabled(); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 437 | suction_victor_->SetDisabled(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_, |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 441 | wrist_victor_, stilts_victor_, suction_victor_; |
Austin Schuh | c2ee66b | 2019-02-19 13:37:46 -0800 | [diff] [blame] | 442 | |
| 443 | double filtered_battery_voltage_ = 12.0; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 444 | }; |
| 445 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 446 | class SolenoidWriter { |
| 447 | public: |
| 448 | SolenoidWriter() |
| 449 | : superstructure_( |
| 450 | ".y2019.control_loops.superstructure.superstructure_queue.output") { |
| 451 | } |
| 452 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 453 | void set_big_suction_cup(int index0, int index1) { |
| 454 | big_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 455 | big_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 456 | } |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 457 | void set_small_suction_cup(int index0, int index1) { |
| 458 | small_suction_cup0_ = pcm_.MakeSolenoid(index0); |
| 459 | small_suction_cup1_ = pcm_.MakeSolenoid(index1); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void set_intake_roller_talon( |
| 463 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) { |
| 464 | intake_rollers_talon_ = ::std::move(t); |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 465 | intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 466 | intake_rollers_talon_->EnableCurrentLimit(true); |
| 467 | } |
| 468 | |
| 469 | void operator()() { |
| 470 | ::aos::SetCurrentThreadName("Solenoids"); |
| 471 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 472 | |
| 473 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 474 | ::std::chrono::milliseconds(1)); |
| 475 | |
| 476 | while (run_) { |
| 477 | { |
| 478 | const int iterations = phased_loop.SleepUntilNext(); |
| 479 | if (iterations != 1) { |
| 480 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | { |
| 485 | superstructure_.FetchLatest(); |
| 486 | if (superstructure_.get()) { |
| 487 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 488 | |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 489 | big_suction_cup0_->Set(!superstructure_->intake_suction_bottom); |
| 490 | big_suction_cup1_->Set(!superstructure_->intake_suction_bottom); |
| 491 | small_suction_cup0_->Set(superstructure_->intake_suction_top); |
| 492 | small_suction_cup1_->Set(superstructure_->intake_suction_top); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 493 | |
| 494 | intake_rollers_talon_->Set( |
| 495 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 496 | ::aos::Clip(superstructure_->intake_roller_voltage, |
| 497 | -kMaxBringupPower, kMaxBringupPower) / |
| 498 | 12.0); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | { |
| 503 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 504 | |
| 505 | pcm_.Flush(); |
| 506 | to_log.read_solenoids = pcm_.GetAll(); |
| 507 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void Quit() { run_ = false; } |
| 513 | |
| 514 | private: |
| 515 | ::frc971::wpilib::BufferedPcm pcm_; |
| 516 | |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 517 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_, |
| 518 | big_suction_cup1_, small_suction_cup0_, small_suction_cup1_; |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 519 | |
| 520 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> |
| 521 | intake_rollers_talon_; |
| 522 | |
| 523 | ::aos::Queue< |
| 524 | ::y2019::control_loops::superstructure::SuperstructureQueue::Output> |
| 525 | superstructure_; |
| 526 | |
| 527 | ::std::atomic<bool> run_{true}; |
| 528 | }; |
| 529 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 530 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 531 | public: |
| 532 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 533 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 534 | frc::Encoder::k4X); |
| 535 | } |
| 536 | |
| 537 | void Run() override { |
| 538 | ::aos::InitNRT(); |
| 539 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 540 | |
| 541 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 542 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 543 | |
| 544 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 545 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 546 | SensorReader reader; |
| 547 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 548 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 549 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 550 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 551 | reader.set_elevator_encoder(make_encoder(4)); |
| 552 | reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 553 | reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 554 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 555 | reader.set_wrist_encoder(make_encoder(5)); |
| 556 | reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5)); |
| 557 | reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 558 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 559 | reader.set_intake_encoder(make_encoder(2)); |
| 560 | reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 561 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 562 | reader.set_stilts_encoder(make_encoder(3)); |
| 563 | reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 564 | reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3)); |
| 565 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 566 | reader.set_pwm_trigger(true); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 567 | reader.set_vacuum_sensor(7); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 568 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 569 | ::std::thread reader_thread(::std::ref(reader)); |
| 570 | |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 571 | CameraReader camera_reader; |
| 572 | frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3); |
| 573 | camera_reader.set_spi(&camera_spi); |
| 574 | camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
Brian Silverman | 7ecf067 | 2019-03-02 15:30:03 -0800 | [diff] [blame^] | 575 | // Austin says 8, 9, 24, and 25 are good options to choose from for these. |
| 576 | camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24)); |
| 577 | camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25)); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 578 | |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 579 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 580 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 581 | imu_trigger.get()); |
Brian Silverman | f8b7525 | 2019-02-24 16:13:58 -0800 | [diff] [blame] | 582 | imu.set_spi_idle_callback( |
| 583 | [&camera_reader]() { camera_reader.DoSpiTransaction(); }); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 584 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 585 | imu.set_reset(imu_reset.get()); |
| 586 | ::std::thread imu_thread(::std::ref(imu)); |
| 587 | |
| 588 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 589 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 590 | // variety so all the Victors are written as SPs. |
| 591 | |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 592 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer; |
| 593 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 594 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 595 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame] | 596 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 597 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 598 | |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 599 | SuperstructureWriter superstructure_writer; |
| 600 | superstructure_writer.set_elevator_victor( |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 601 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 602 | // TODO(austin): Do the vacuum |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 603 | superstructure_writer.set_suction_victor( |
| 604 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 605 | superstructure_writer.set_intake_victor( |
| 606 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 607 | superstructure_writer.set_wrist_victor( |
| 608 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 609 | superstructure_writer.set_stilts_victor( |
Austin Schuh | 3e3d4ba | 2019-02-15 23:14:52 -0800 | [diff] [blame] | 610 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 611 | |
| 612 | ::std::thread superstructure_writer_thread( |
| 613 | ::std::ref(superstructure_writer)); |
| 614 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 615 | SolenoidWriter solenoid_writer; |
| 616 | solenoid_writer.set_intake_roller_talon( |
| 617 | make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10)); |
Austin Schuh | 461e118 | 2019-02-17 14:56:44 -0800 | [diff] [blame] | 618 | solenoid_writer.set_big_suction_cup(0, 1); |
| 619 | solenoid_writer.set_small_suction_cup(2, 3); |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 620 | |
| 621 | ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer)); |
| 622 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 623 | // Wait forever. Not much else to do... |
| 624 | while (true) { |
| 625 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 626 | if (r != 0) { |
| 627 | PLOG(WARNING, "infinite select failed"); |
| 628 | } else { |
| 629 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 634 | |
Austin Schuh | c1d6f83 | 2019-02-15 23:22:17 -0800 | [diff] [blame] | 635 | solenoid_writer.Quit(); |
| 636 | solenoid_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 637 | joystick_sender.Quit(); |
| 638 | joystick_thread.join(); |
| 639 | pdp_fetcher.Quit(); |
| 640 | pdp_fetcher_thread.join(); |
| 641 | reader.Quit(); |
| 642 | reader_thread.join(); |
| 643 | imu.Quit(); |
| 644 | imu_thread.join(); |
| 645 | |
| 646 | drivetrain_writer.Quit(); |
| 647 | drivetrain_writer_thread.join(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 648 | superstructure_writer.Quit(); |
| 649 | superstructure_writer_thread.join(); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 650 | |
| 651 | ::aos::Cleanup(); |
| 652 | } |
| 653 | }; |
| 654 | |
| 655 | } // namespace |
| 656 | } // namespace wpilib |
| 657 | } // namespace y2019 |
| 658 | |
| 659 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |