blob: bc6c25f9ac2ebae99165bdd295109e8a85589075 [file] [log] [blame]
Sabina Davisabeae332019-02-01 21:12:57 -08001#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 Davisabeae332019-02-01 21:12:57 -080026#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080027#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 Davisabeae332019-02-01 21:12:57 -080034#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080035#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080036#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080037#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 Davisadc58542019-02-01 22:23:00 -080041#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080042#include "frc971/wpilib/wpilib_robot_base.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080043#include "y2019/constants.h"
Sabina Davisabeae332019-02-01 21:12:57 -080044
45#ifndef M_PI
46#define M_PI 3.14159265358979323846
47#endif
48
49using ::frc971::control_loops::drivetrain_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080050using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080051using ::aos::monotonic_clock;
52namespace chrono = ::std::chrono;
53using aos::make_unique;
54
55namespace y2019 {
56namespace wpilib {
57namespace {
58
59constexpr 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.
67template <typename T>
68constexpr T max(T a, T b) {
69 return (a > b) ? a : b;
70}
71
72template <typename T, typename... Rest>
73constexpr T max(T a, T b, T c, Rest... rest) {
74 return max(max(a, b), c, rest...);
75}
76
77double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080078 return ((static_cast<double>(in) /
79 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080080 (2.0 * M_PI)) *
81 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080082 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080083}
84
85double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080086 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080087 (2.0 * M_PI)) *
88 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080089 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080090}
91
92constexpr double kMaxFastEncoderPulsesPerSecond =
93 max(/*Values::kMaxDrivetrainEncoderPulsesPerSecond(),
94 Values::kMaxIntakeMotorEncoderPulsesPerSecond()*/ 1.0, 1.0);
95static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
96 "fast encoders are too fast");
97
98constexpr double kMaxMediumEncoderPulsesPerSecond =
99 max(/*Values::kMaxProximalEncoderPulsesPerSecond(),
100 Values::kMaxDistalEncoderPulsesPerSecond()*/ 1.0, 1.0);
101static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
102 "medium encoders are too fast");
103
104// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800105class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800106 public:
107 SensorReader() {
108 // Set to filter out anything shorter than 1/4 of the minimum pulse width
109 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800110 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
111 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800112 }
113
Sabina Davis399dbd82019-02-01 23:06:08 -0800114 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800115 {
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 Davisabeae332019-02-01 21:12:57 -0800129 }
Sabina Davisabeae332019-02-01 21:12:57 -0800130};
131
Sabina Davisabeae332019-02-01 21:12:57 -0800132class 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 Davisabeae332019-02-01 21:12:57 -0800156 ::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 Davisd004fd62019-02-02 23:51:46 -0800170 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
171 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800172 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800173 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800174 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800175 ::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
209AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);