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" |
| 43 | |
| 44 | #ifndef M_PI |
| 45 | #define M_PI 3.14159265358979323846 |
| 46 | #endif |
| 47 | |
| 48 | using ::frc971::control_loops::drivetrain_queue; |
| 49 | using ::aos::monotonic_clock; |
| 50 | namespace chrono = ::std::chrono; |
| 51 | using aos::make_unique; |
| 52 | |
| 53 | namespace y2019 { |
| 54 | namespace wpilib { |
| 55 | namespace { |
| 56 | |
| 57 | constexpr double kMaxBringupPower = 12.0; |
| 58 | |
| 59 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 60 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 61 | // The low bit is direction. |
| 62 | |
| 63 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 64 | // constexpr. |
| 65 | template <typename T> |
| 66 | constexpr T max(T a, T b) { |
| 67 | return (a > b) ? a : b; |
| 68 | } |
| 69 | |
| 70 | template <typename T, typename... Rest> |
| 71 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 72 | return max(max(a, b), c, rest...); |
| 73 | } |
| 74 | |
| 75 | double drivetrain_translate(int32_t in) { |
| 76 | return ((static_cast<double>(in) |
| 77 | /* / Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 78 | (2.0 * M_PI)) * |
| 79 | Values::kDrivetrainEncoderRatio() * |
| 80 | control_loops::drivetrain::kWheelRadius*/)); |
| 81 | } |
| 82 | |
| 83 | double drivetrain_velocity_translate(double in) { |
| 84 | return (((1.0 / in) /* / Values::kDrivetrainCyclesPerRevolution()) * |
| 85 | (2.0 * M_PI)) * |
| 86 | Values::kDrivetrainEncoderRatio() * |
| 87 | control_loops::drivetrain::kWheelRadius*/)); |
| 88 | } |
| 89 | |
| 90 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 91 | max(/*Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 92 | Values::kMaxIntakeMotorEncoderPulsesPerSecond()*/ 1.0, 1.0); |
| 93 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 94 | "fast encoders are too fast"); |
| 95 | |
| 96 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 97 | max(/*Values::kMaxProximalEncoderPulsesPerSecond(), |
| 98 | Values::kMaxDistalEncoderPulsesPerSecond()*/ 1.0, 1.0); |
| 99 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 100 | "medium encoders are too fast"); |
| 101 | |
| 102 | // Class to send position messages with sensor readings to our loops. |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 103 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 104 | public: |
| 105 | SensorReader() { |
| 106 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 107 | // we should ever see. |
| 108 | fast_encoder_filter_.SetPeriodNanoSeconds( |
| 109 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 110 | kMaxFastEncoderPulsesPerSecond * 1e9 + |
| 111 | 0.5)); |
| 112 | medium_encoder_filter_.SetPeriodNanoSeconds( |
| 113 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 114 | kMaxMediumEncoderPulsesPerSecond * 1e9 + |
| 115 | 0.5)); |
| 116 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 117 | } |
| 118 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame^] | 119 | void RunIteration() override { |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 120 | ::frc971::wpilib::SendRobotState(my_pid_); |
| 121 | |
| 122 | { |
| 123 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 124 | drivetrain_message->left_encoder = |
| 125 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 126 | drivetrain_message->left_speed = |
| 127 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 128 | |
| 129 | drivetrain_message->right_encoder = |
| 130 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 131 | drivetrain_message->right_speed = -drivetrain_velocity_translate( |
| 132 | drivetrain_right_encoder_->GetPeriod()); |
| 133 | |
| 134 | drivetrain_message.Send(); |
| 135 | } |
| 136 | |
| 137 | dma_synchronizer_->RunIteration(); |
| 138 | } |
Sabina Davis | abeae33 | 2019-02-01 21:12:57 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 142 | public: |
| 143 | void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 144 | drivetrain_left_victor_ = ::std::move(t); |
| 145 | } |
| 146 | |
| 147 | void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 148 | drivetrain_right_victor_ = ::std::move(t); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | virtual void Read() override { |
| 153 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 154 | } |
| 155 | |
| 156 | virtual void Write() override { |
| 157 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 158 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 159 | drivetrain_left_victor_->SetSpeed( |
| 160 | ::aos::Clip(queue->left_voltage, -12.0, 12.0) / 12.0); |
| 161 | drivetrain_right_victor_->SetSpeed( |
| 162 | ::aos::Clip(-queue->right_voltage, -12.0, 12.0) / 12.0); |
| 163 | } |
| 164 | |
| 165 | virtual void Stop() override { |
| 166 | LOG(WARNING, "drivetrain output too old\n"); |
| 167 | drivetrain_left_victor_->SetDisabled(); |
| 168 | drivetrain_right_victor_->SetDisabled(); |
| 169 | } |
| 170 | |
| 171 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_, |
| 172 | drivetrain_right_victor_; |
| 173 | }; |
| 174 | |
| 175 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 176 | public: |
| 177 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 178 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 179 | frc::Encoder::k4X); |
| 180 | } |
| 181 | |
| 182 | void Run() override { |
| 183 | ::aos::InitNRT(); |
| 184 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 185 | |
| 186 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 187 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 188 | |
| 189 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 190 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 191 | SensorReader reader; |
| 192 | |
| 193 | // TODO(Sabina): Update port numbers(Sensors and Victors) |
| 194 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 195 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 196 | |
| 197 | reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25)); |
| 198 | |
| 199 | reader.set_dma(make_unique<DMA>()); |
| 200 | ::std::thread reader_thread(::std::ref(reader)); |
| 201 | |
| 202 | auto imu_trigger = make_unique<frc::DigitalInput>(5); |
| 203 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 204 | imu_trigger.get()); |
| 205 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 206 | auto imu_reset = make_unique<frc::DigitalOutput>(6); |
| 207 | imu.set_reset(imu_reset.get()); |
| 208 | ::std::thread imu_thread(::std::ref(imu)); |
| 209 | |
| 210 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 211 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 212 | // variety so all the Victors are written as SPs. |
| 213 | |
| 214 | DrivetrainWriter drivetrain_writer; |
| 215 | drivetrain_writer.set_drivetrain_left_victor( |
| 216 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
| 217 | drivetrain_writer.set_drivetrain_right_victor( |
| 218 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
| 219 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 220 | |
| 221 | // Wait forever. Not much else to do... |
| 222 | while (true) { |
| 223 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 224 | if (r != 0) { |
| 225 | PLOG(WARNING, "infinite select failed"); |
| 226 | } else { |
| 227 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 232 | |
| 233 | joystick_sender.Quit(); |
| 234 | joystick_thread.join(); |
| 235 | pdp_fetcher.Quit(); |
| 236 | pdp_fetcher_thread.join(); |
| 237 | reader.Quit(); |
| 238 | reader_thread.join(); |
| 239 | imu.Quit(); |
| 240 | imu_thread.join(); |
| 241 | |
| 242 | drivetrain_writer.Quit(); |
| 243 | drivetrain_writer_thread.join(); |
| 244 | |
| 245 | ::aos::Cleanup(); |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | } // namespace |
| 250 | } // namespace wpilib |
| 251 | } // namespace y2019 |
| 252 | |
| 253 | AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot); |