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" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 26 | #include "aos/time/time.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 27 | #include "aos/util/log_interval.h" |
| 28 | #include "aos/util/phased_loop.h" |
| 29 | #include "aos/util/wrapping_counter.h" |
| 30 | |
| 31 | #include "frc971/autonomous/auto.q.h" |
| 32 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 33 | #include "frc971/wpilib/ADIS16448.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 34 | #include "frc971/wpilib/dma.h" |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 35 | #include "frc971/wpilib/drivetrain_writer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 37 | #include "frc971/wpilib/joystick_sender.h" |
| 38 | #include "frc971/wpilib/logging.q.h" |
| 39 | #include "frc971/wpilib/loop_output_handler.h" |
| 40 | #include "frc971/wpilib/pdp_fetcher.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 41 | #include "frc971/wpilib/sensor_reader.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 42 | #include "frc971/wpilib/wpilib_robot_base.h" |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 43 | #include "y2019/constants.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 44 | |
| 45 | #ifndef M_PI |
| 46 | #define M_PI 3.14159265358979323846 |
| 47 | #endif |
| 48 | |
| 49 | using ::frc971::control_loops::drivetrain_queue; |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 50 | using ::y2019::constants::Values; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 51 | using ::aos::monotonic_clock; |
| 52 | namespace chrono = ::std::chrono; |
| 53 | using aos::make_unique; |
| 54 | |
| 55 | namespace y2019 { |
| 56 | namespace wpilib { |
| 57 | namespace { |
| 58 | |
| 59 | constexpr double kMaxBringupPower = 12.0; |
| 60 | |
| 61 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 62 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 63 | // The low bit is direction. |
| 64 | |
| 65 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 66 | // constexpr. |
| 67 | template <typename T> |
| 68 | constexpr T max(T a, T b) { |
| 69 | return (a > b) ? a : b; |
| 70 | } |
| 71 | |
| 72 | template <typename T, typename... Rest> |
| 73 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 74 | return max(max(a, b), c, rest...); |
| 75 | } |
| 76 | |
| 77 | double drivetrain_translate(int32_t in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 78 | return ((static_cast<double>(in) / |
| 79 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 80 | (2.0 * M_PI)) * |
| 81 | Values::kDrivetrainEncoderRatio() * |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 82 | control_loops::drivetrain::kWheelRadius; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | double drivetrain_velocity_translate(double in) { |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 86 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 87 | (2.0 * M_PI)) * |
| 88 | Values::kDrivetrainEncoderRatio() * |
Sabina Davis | 7be49f3 | 2019-02-02 00:30:19 -0800 | [diff] [blame] | 89 | control_loops::drivetrain::kWheelRadius; |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 93 | max(/*Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 94 | Values::kMaxIntakeMotorEncoderPulsesPerSecond()*/ 1.0, 1.0); |
| 95 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 96 | "fast encoders are too fast"); |
| 97 | |
| 98 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 99 | max(/*Values::kMaxProximalEncoderPulsesPerSecond(), |
| 100 | Values::kMaxDistalEncoderPulsesPerSecond()*/ 1.0, 1.0); |
| 101 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 102 | "medium encoders are too fast"); |
| 103 | |
| 104 | // Class to send position messages with sensor readings to our loops. |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 105 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 106 | public: |
| 107 | SensorReader() { |
| 108 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 109 | // we should ever see. |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 110 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 111 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 114 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 115 | { |
| 116 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 117 | drivetrain_message->left_encoder = |
| 118 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 119 | drivetrain_message->left_speed = |
| 120 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 121 | |
| 122 | drivetrain_message->right_encoder = |
| 123 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 124 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 125 | drivetrain_right_encoder_->GetPeriod()); |
| 126 | |
| 127 | drivetrain_message.Send(); |
| 128 | } |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 129 | } |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 132 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 133 | public: |
| 134 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 135 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 136 | frc::Encoder::k4X); |
| 137 | } |
| 138 | |
| 139 | void Run() override { |
| 140 | ::aos::InitNRT(); |
| 141 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 142 | |
| 143 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 144 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 145 | |
| 146 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 147 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 148 | SensorReader reader; |
| 149 | |
| 150 | // TODO(Sabina): Update port numbers(Sensors and Victors) |
| 151 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 152 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 153 | |
| 154 | reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25)); |
| 155 | |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 156 | ::std::thread reader_thread(::std::ref(reader)); |
| 157 | |
| 158 | auto imu_trigger = make_unique<frc::DigitalInput>(5); |
| 159 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 160 | imu_trigger.get()); |
| 161 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 162 | auto imu_reset = make_unique<frc::DigitalOutput>(6); |
| 163 | imu.set_reset(imu_reset.get()); |
| 164 | ::std::thread imu_thread(::std::ref(imu)); |
| 165 | |
| 166 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 167 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 168 | // variety so all the Victors are written as SPs. |
| 169 | |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 170 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer; |
| 171 | drivetrain_writer.set_left_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame^] | 172 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
Sabina Davis | d004fd6 | 2019-02-02 23:51:46 -0800 | [diff] [blame] | 173 | drivetrain_writer.set_right_controller0( |
Sabina Davis | 1b84afa | 2019-02-09 01:20:21 -0800 | [diff] [blame^] | 174 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 175 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 176 | |
| 177 | // Wait forever. Not much else to do... |
| 178 | while (true) { |
| 179 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 180 | if (r != 0) { |
| 181 | PLOG(WARNING, "infinite select failed"); |
| 182 | } else { |
| 183 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 188 | |
| 189 | joystick_sender.Quit(); |
| 190 | joystick_thread.join(); |
| 191 | pdp_fetcher.Quit(); |
| 192 | pdp_fetcher_thread.join(); |
| 193 | reader.Quit(); |
| 194 | reader_thread.join(); |
| 195 | imu.Quit(); |
| 196 | imu_thread.join(); |
| 197 | |
| 198 | drivetrain_writer.Quit(); |
| 199 | drivetrain_writer_thread.join(); |
| 200 | |
| 201 | ::aos::Cleanup(); |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | } // namespace |
| 206 | } // namespace wpilib |
| 207 | } // namespace y2019 |
| 208 | |
| 209 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |