blob: 42d58823a332b0e9664749b85c925ec593dfc2fb [file] [log] [blame]
Austin Schuh010eb812014-10-25 18:06:49 -07001#include <stdio.h>
2#include <string.h>
Austin Schuh010eb812014-10-25 18:06:49 -07003#include <unistd.h>
4#include <inttypes.h>
5
Brian Silvermand8f403a2014-12-13 19:12:04 -05006#include <thread>
7#include <mutex>
8#include <functional>
9
Austin Schuh010eb812014-10-25 18:06:49 -070010#include "aos/common/controls/output_check.q.h"
11#include "aos/common/controls/sensor_generation.q.h"
12#include "aos/common/logging/logging.h"
13#include "aos/common/logging/queue_logging.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050014#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070015#include "aos/common/time.h"
16#include "aos/common/util/log_interval.h"
17#include "aos/common/util/phased_loop.h"
18#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040019#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070020#include "aos/linux_code/init.h"
21
22#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070023#include "frc971/constants.h"
Daniel Pettiaece37f2014-10-25 17:13:44 -070024#include "frc971/shifter_hall_effect.h"
Austin Schuh010eb812014-10-25 18:06:49 -070025
Brian Silvermanda45b6c2014-12-28 11:36:50 -080026#include "frc971/wpilib/hall_effect.h"
27#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050028#include "frc971/wpilib/loop_output_handler.h"
29#include "frc971/wpilib/buffered_solenoid.h"
30#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080031#include "frc971/wpilib/gyro_sender.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050032#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080033
Brian Silvermancb77f232014-12-19 21:48:36 -080034#include "Encoder.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080035#include "Talon.h"
36#include "DriverStation.h"
37#include "AnalogInput.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080038#include "Compressor.h"
39#include "RobotBase.h"
Austin Schuh010eb812014-10-25 18:06:49 -070040
41#ifndef M_PI
42#define M_PI 3.14159265358979323846
43#endif
44
45using ::aos::util::SimpleLogInterval;
46using ::frc971::control_loops::drivetrain;
Austin Schuh010eb812014-10-25 18:06:49 -070047using ::aos::util::WrappingCounter;
48
49namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080050namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070051
Austin Schuh010eb812014-10-25 18:06:49 -070052double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080053 return static_cast<double>(in) /
54 (256.0 /*cpr*/ * 2.0 /*2x. Stupid WPILib*/) *
55 (18.0 / 50.0 /*output stage*/) * (56.0 / 30.0 /*encoder gears*/)
56 // * constants::GetValues().drivetrain_encoder_ratio
57 *
58 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
Austin Schuh010eb812014-10-25 18:06:49 -070059}
60
Austin Schuh010eb812014-10-25 18:06:49 -070061class SensorReader {
62 public:
Brian Silverman1f90d672015-01-26 20:20:45 -050063 SensorReader() {
Austin Schuh010eb812014-10-25 18:06:49 -070064 filter_.SetPeriodNanoSeconds(100000);
Austin Schuh010eb812014-10-25 18:06:49 -070065 }
66
Brian Silverman1f90d672015-01-26 20:20:45 -050067 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
68 left_encoder_ = ::std::move(left_encoder);
69 }
70
71 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
72 right_encoder_ = ::std::move(right_encoder);
73 }
74
Austin Schuh010eb812014-10-25 18:06:49 -070075 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -080076 ::aos::SetCurrentThreadName("SensorReader");
77
Brian Silverman1f90d672015-01-26 20:20:45 -050078 static const int kPriority = 30;
79 //static const int kInterruptPriority = 55;
Austin Schuh010eb812014-10-25 18:06:49 -070080
Brian Silverman2fe007c2014-12-28 12:20:01 -080081 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -070082 while (run_) {
Brian Silverman20141f92015-01-05 17:39:01 -080083 ::aos::time::PhasedLoopXMS(5, 9000);
Austin Schuh010eb812014-10-25 18:06:49 -070084 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -070085 }
Austin Schuh010eb812014-10-25 18:06:49 -070086 }
87
88 void RunIteration() {
Austin Schuh010eb812014-10-25 18:06:49 -070089 DriverStation *ds = DriverStation::GetInstance();
90
Austin Schuhdb516032014-12-28 00:12:38 -080091 if (ds->IsSysActive()) {
Austin Schuh010eb812014-10-25 18:06:49 -070092 auto message = ::aos::controls::output_check_received.MakeMessage();
93 // TODO(brians): Actually read a pulse value from the roboRIO.
94 message->pwm_value = 0;
95 message->pulse_length = -1;
96 LOG_STRUCT(DEBUG, "received", *message);
97 message.Send();
98 }
99
Austin Schuh010eb812014-10-25 18:06:49 -0700100 drivetrain.position.MakeWithBuilder()
101 .right_encoder(drivetrain_translate(right_encoder_->GetRaw()))
102 .left_encoder(-drivetrain_translate(left_encoder_->GetRaw()))
Austin Schuh010eb812014-10-25 18:06:49 -0700103 .battery_voltage(ds->GetBatteryVoltage())
104 .Send();
105
Brian Silvermand8f403a2014-12-13 19:12:04 -0500106 // Signal that we are alive.
Austin Schuh010eb812014-10-25 18:06:49 -0700107 ::aos::controls::sensor_generation.MakeWithBuilder()
108 .reader_pid(getpid())
109 .cape_resets(0)
110 .Send();
111 }
112
113 void Quit() { run_ = false; }
114
115 private:
Austin Schuh010eb812014-10-25 18:06:49 -0700116 ::std::unique_ptr<Encoder> left_encoder_;
117 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700118
Brian Silverman1f90d672015-01-26 20:20:45 -0500119 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700120 DigitalGlitchFilter filter_;
121};
122
Brian Silvermand8f403a2014-12-13 19:12:04 -0500123class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700124 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500125 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Brian Silverman20141f92015-01-05 17:39:01 -0800126 : pcm_(pcm), drivetrain_(".frc971.control_loops.drivetrain.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500127
128 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
129 drivetrain_left_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700130 }
131
Brian Silvermand8f403a2014-12-13 19:12:04 -0500132 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
133 drivetrain_right_ = ::std::move(s);
134 }
Austin Schuh010eb812014-10-25 18:06:49 -0700135
Brian Silvermand8f403a2014-12-13 19:12:04 -0500136 void operator()() {
137 ::aos::SetCurrentThreadName("Solenoids");
138 ::aos::SetCurrentThreadRealtimePriority(30);
139
140 while (run_) {
141 ::aos::time::PhasedLoopXMS(20, 1000);
142
143 {
144 drivetrain_.FetchLatest();
145 if (drivetrain_.get()) {
146 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
147 drivetrain_left_->Set(drivetrain_->left_high);
148 drivetrain_right_->Set(drivetrain_->right_high);
149 }
150 }
151
Brian Silvermand8f403a2014-12-13 19:12:04 -0500152 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700153 }
154 }
155
Brian Silvermand8f403a2014-12-13 19:12:04 -0500156 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700157
Brian Silvermand8f403a2014-12-13 19:12:04 -0500158 private:
159 const ::std::unique_ptr<BufferedPcm> &pcm_;
160 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
161 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
Austin Schuh010eb812014-10-25 18:06:49 -0700162
Brian Silvermand8f403a2014-12-13 19:12:04 -0500163 ::aos::Queue<::frc971::control_loops::Drivetrain::Output> drivetrain_;
Austin Schuh010eb812014-10-25 18:06:49 -0700164
Brian Silvermand8f403a2014-12-13 19:12:04 -0500165 ::std::atomic<bool> run_{true};
166};
167
168class DrivetrainWriter : public LoopOutputHandler {
169 public:
170 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
171 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700172 }
173
Brian Silvermand8f403a2014-12-13 19:12:04 -0500174 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
175 right_drivetrain_talon_ = ::std::move(t);
176 }
Austin Schuh010eb812014-10-25 18:06:49 -0700177
Brian Silvermand8f403a2014-12-13 19:12:04 -0500178 private:
179 virtual void Read() override {
180 ::frc971::control_loops::drivetrain.output.FetchAnother();
181 }
182
183 virtual void Write() override {
184 auto &queue = ::frc971::control_loops::drivetrain.output;
185 LOG_STRUCT(DEBUG, "will output", *queue);
186 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
187 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
188 }
189
190 virtual void Stop() override {
191 LOG(WARNING, "drivetrain output too old\n");
192 left_drivetrain_talon_->Disable();
193 right_drivetrain_talon_->Disable();
194 }
195
Austin Schuh010eb812014-10-25 18:06:49 -0700196 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500197 ::std::unique_ptr<Talon> right_drivetrain_talon_;
198};
199
Brian Silverman1f90d672015-01-26 20:20:45 -0500200// TODO(brian): Replace this with ::std::make_unique once all our toolchains
201// have support.
202template <class T, class... U>
203std::unique_ptr<T> make_unique(U &&... u) {
204 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
205}
206
Austin Schuh010eb812014-10-25 18:06:49 -0700207class WPILibRobot : public RobotBase {
208 public:
209 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500210 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800211 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500212
Brian Silverman98f6ee22015-01-26 17:50:12 -0500213 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700214 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500215 ::std::unique_ptr<Compressor> compressor(new Compressor());
216 compressor->SetClosedLoopControl(true);
217
Brian Silverman98f6ee22015-01-26 17:50:12 -0500218 SensorReader reader;
Brian Silverman1f90d672015-01-26 20:20:45 -0500219 // TODO(sensors): Replace all the 99s with real port numbers.
220 reader.set_left_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
221 reader.set_right_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
Brian Silverman98f6ee22015-01-26 17:50:12 -0500222 ::std::thread reader_thread(::std::ref(reader));
223 GyroSender gyro_sender;
224 ::std::thread gyro_thread(::std::ref(gyro_sender));
225
226 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500227 drivetrain_writer.set_left_drivetrain_talon(
228 ::std::unique_ptr<Talon>(new Talon(5)));
229 drivetrain_writer.set_right_drivetrain_talon(
230 ::std::unique_ptr<Talon>(new Talon(2)));
231 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
232
Brian Silverman98f6ee22015-01-26 17:50:12 -0500233 ::std::unique_ptr<BufferedPcm> pcm(new BufferedPcm());
234 SolenoidWriter solenoid_writer(pcm);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500235 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
236 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500237 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
238
239 // Wait forever. Not much else to do...
240 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
241
Austin Schuh010eb812014-10-25 18:06:49 -0700242 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800243
Austin Schuh010eb812014-10-25 18:06:49 -0700244 joystick_sender.Quit();
245 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500246 reader.Quit();
247 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800248 gyro_sender.Quit();
249 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500250
251 drivetrain_writer.Quit();
252 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500253 solenoid_writer.Quit();
254 solenoid_thread.join();
255
Austin Schuh010eb812014-10-25 18:06:49 -0700256 ::aos::Cleanup();
257 }
258};
259
Brian Silverman98f6ee22015-01-26 17:50:12 -0500260} // namespace wpilib
261} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800262
Brian Silverman98f6ee22015-01-26 17:50:12 -0500263
264START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);