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 | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 35 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/joystick_sender.h" |
| 37 | #include "frc971/wpilib/logging.q.h" |
| 38 | #include "frc971/wpilib/loop_output_handler.h" |
| 39 | #include "frc971/wpilib/pdp_fetcher.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/sensor_reader.h" |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 41 | #include "frc971/wpilib/wpilib_interface.h" |
| 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. |
| 110 | fast_encoder_filter_.SetPeriodNanoSeconds( |
| 111 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 112 | kMaxFastEncoderPulsesPerSecond * 1e9 + |
| 113 | 0.5)); |
| 114 | medium_encoder_filter_.SetPeriodNanoSeconds( |
| 115 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 116 | kMaxMediumEncoderPulsesPerSecond * 1e9 + |
| 117 | 0.5)); |
| 118 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 119 | } |
| 120 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 121 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 122 | ::frc971::wpilib::SendRobotState(my_pid_); |
| 123 | |
| 124 | { |
| 125 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 126 | drivetrain_message->left_encoder = |
| 127 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 128 | drivetrain_message->left_speed = |
| 129 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 130 | |
| 131 | drivetrain_message->right_encoder = |
| 132 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 133 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 134 | drivetrain_right_encoder_->GetPeriod()); |
| 135 | |
| 136 | drivetrain_message.Send(); |
| 137 | } |
| 138 | |
| 139 | dma_synchronizer_->RunIteration(); |
| 140 | } |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 144 | public: |
| 145 | void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 146 | drivetrain_left_victor_ = ::std::move(t); |
| 147 | } |
| 148 | |
| 149 | void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 150 | drivetrain_right_victor_ = ::std::move(t); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | virtual void Read() override { |
| 155 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 156 | } |
| 157 | |
| 158 | virtual void Write() override { |
| 159 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 160 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 161 | drivetrain_left_victor_->SetSpeed( |
| 162 | ::aos::Clip(queue->left_voltage, -12.0, 12.0) / 12.0); |
| 163 | drivetrain_right_victor_->SetSpeed( |
| 164 | ::aos::Clip(-queue->right_voltage, -12.0, 12.0) / 12.0); |
| 165 | } |
| 166 | |
| 167 | virtual void Stop() override { |
| 168 | LOG(WARNING, "drivetrain output too old\n"); |
| 169 | drivetrain_left_victor_->SetDisabled(); |
| 170 | drivetrain_right_victor_->SetDisabled(); |
| 171 | } |
| 172 | |
| 173 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_, |
| 174 | drivetrain_right_victor_; |
| 175 | }; |
| 176 | |
| 177 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 178 | public: |
| 179 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 180 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 181 | frc::Encoder::k4X); |
| 182 | } |
| 183 | |
| 184 | void Run() override { |
| 185 | ::aos::InitNRT(); |
| 186 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 187 | |
| 188 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 189 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 190 | |
| 191 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 192 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 193 | SensorReader reader; |
| 194 | |
| 195 | // TODO(Sabina): Update port numbers(Sensors and Victors) |
| 196 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 197 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 198 | |
| 199 | reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25)); |
| 200 | |
| 201 | reader.set_dma(make_unique<DMA>()); |
| 202 | ::std::thread reader_thread(::std::ref(reader)); |
| 203 | |
| 204 | auto imu_trigger = make_unique<frc::DigitalInput>(5); |
| 205 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 206 | imu_trigger.get()); |
| 207 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 208 | auto imu_reset = make_unique<frc::DigitalOutput>(6); |
| 209 | imu.set_reset(imu_reset.get()); |
| 210 | ::std::thread imu_thread(::std::ref(imu)); |
| 211 | |
| 212 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 213 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 214 | // variety so all the Victors are written as SPs. |
| 215 | |
| 216 | DrivetrainWriter drivetrain_writer; |
| 217 | drivetrain_writer.set_drivetrain_left_victor( |
| 218 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
| 219 | drivetrain_writer.set_drivetrain_right_victor( |
| 220 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
| 221 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 222 | |
| 223 | // Wait forever. Not much else to do... |
| 224 | while (true) { |
| 225 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 226 | if (r != 0) { |
| 227 | PLOG(WARNING, "infinite select failed"); |
| 228 | } else { |
| 229 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 234 | |
| 235 | joystick_sender.Quit(); |
| 236 | joystick_thread.join(); |
| 237 | pdp_fetcher.Quit(); |
| 238 | pdp_fetcher_thread.join(); |
| 239 | reader.Quit(); |
| 240 | reader_thread.join(); |
| 241 | imu.Quit(); |
| 242 | imu_thread.join(); |
| 243 | |
| 244 | drivetrain_writer.Quit(); |
| 245 | drivetrain_writer_thread.join(); |
| 246 | |
| 247 | ::aos::Cleanup(); |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | } // namespace |
| 252 | } // namespace wpilib |
| 253 | } // namespace y2019 |
| 254 | |
| 255 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |