blob: adec4947776b9c68ed3e6aa1c78ce82dbb8e24f1 [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 // TODO(austin): Calibrate the shifter constants again.
Brian Silverman20141f92015-01-05 17:39:01 -0800101 // TODO(sensors): Hook up the new dog position sensors.
Austin Schuh010eb812014-10-25 18:06:49 -0700102 drivetrain.position.MakeWithBuilder()
103 .right_encoder(drivetrain_translate(right_encoder_->GetRaw()))
104 .left_encoder(-drivetrain_translate(left_encoder_->GetRaw()))
Brian Silverman20141f92015-01-05 17:39:01 -0800105 .left_shifter_position(0)
106 .right_shifter_position(0)
Austin Schuh010eb812014-10-25 18:06:49 -0700107 .battery_voltage(ds->GetBatteryVoltage())
108 .Send();
109
Brian Silvermand8f403a2014-12-13 19:12:04 -0500110 // Signal that we are alive.
Austin Schuh010eb812014-10-25 18:06:49 -0700111 ::aos::controls::sensor_generation.MakeWithBuilder()
112 .reader_pid(getpid())
113 .cape_resets(0)
114 .Send();
115 }
116
117 void Quit() { run_ = false; }
118
119 private:
120 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
121
122 ::std::unique_ptr<Encoder> left_encoder_;
123 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700124
Brian Silverman1f90d672015-01-26 20:20:45 -0500125 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700126 DigitalGlitchFilter filter_;
127};
128
Brian Silvermand8f403a2014-12-13 19:12:04 -0500129class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700130 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500131 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Brian Silverman20141f92015-01-05 17:39:01 -0800132 : pcm_(pcm), drivetrain_(".frc971.control_loops.drivetrain.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500133
134 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
135 drivetrain_left_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700136 }
137
Brian Silvermand8f403a2014-12-13 19:12:04 -0500138 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
139 drivetrain_right_ = ::std::move(s);
140 }
Austin Schuh010eb812014-10-25 18:06:49 -0700141
Brian Silvermand8f403a2014-12-13 19:12:04 -0500142 void operator()() {
143 ::aos::SetCurrentThreadName("Solenoids");
144 ::aos::SetCurrentThreadRealtimePriority(30);
145
146 while (run_) {
147 ::aos::time::PhasedLoopXMS(20, 1000);
148
149 {
150 drivetrain_.FetchLatest();
151 if (drivetrain_.get()) {
152 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
153 drivetrain_left_->Set(drivetrain_->left_high);
154 drivetrain_right_->Set(drivetrain_->right_high);
155 }
156 }
157
Brian Silvermand8f403a2014-12-13 19:12:04 -0500158 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700159 }
160 }
161
Brian Silvermand8f403a2014-12-13 19:12:04 -0500162 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700163
Brian Silvermand8f403a2014-12-13 19:12:04 -0500164 private:
165 const ::std::unique_ptr<BufferedPcm> &pcm_;
166 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
167 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
Austin Schuh010eb812014-10-25 18:06:49 -0700168
Brian Silvermand8f403a2014-12-13 19:12:04 -0500169 ::aos::Queue<::frc971::control_loops::Drivetrain::Output> drivetrain_;
Austin Schuh010eb812014-10-25 18:06:49 -0700170
Brian Silvermand8f403a2014-12-13 19:12:04 -0500171 ::std::atomic<bool> run_{true};
172};
173
174class DrivetrainWriter : public LoopOutputHandler {
175 public:
176 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
177 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700178 }
179
Brian Silvermand8f403a2014-12-13 19:12:04 -0500180 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
181 right_drivetrain_talon_ = ::std::move(t);
182 }
Austin Schuh010eb812014-10-25 18:06:49 -0700183
Brian Silvermand8f403a2014-12-13 19:12:04 -0500184 private:
185 virtual void Read() override {
186 ::frc971::control_loops::drivetrain.output.FetchAnother();
187 }
188
189 virtual void Write() override {
190 auto &queue = ::frc971::control_loops::drivetrain.output;
191 LOG_STRUCT(DEBUG, "will output", *queue);
192 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
193 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
194 }
195
196 virtual void Stop() override {
197 LOG(WARNING, "drivetrain output too old\n");
198 left_drivetrain_talon_->Disable();
199 right_drivetrain_talon_->Disable();
200 }
201
Austin Schuh010eb812014-10-25 18:06:49 -0700202 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500203 ::std::unique_ptr<Talon> right_drivetrain_talon_;
204};
205
Brian Silverman1f90d672015-01-26 20:20:45 -0500206// TODO(brian): Replace this with ::std::make_unique once all our toolchains
207// have support.
208template <class T, class... U>
209std::unique_ptr<T> make_unique(U &&... u) {
210 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
211}
212
Austin Schuh010eb812014-10-25 18:06:49 -0700213class WPILibRobot : public RobotBase {
214 public:
215 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500216 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800217 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500218
Brian Silverman98f6ee22015-01-26 17:50:12 -0500219 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700220 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500221 ::std::unique_ptr<Compressor> compressor(new Compressor());
222 compressor->SetClosedLoopControl(true);
223
Brian Silverman98f6ee22015-01-26 17:50:12 -0500224 SensorReader reader;
Brian Silverman1f90d672015-01-26 20:20:45 -0500225 // TODO(sensors): Replace all the 99s with real port numbers.
226 reader.set_left_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
227 reader.set_right_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
Brian Silverman98f6ee22015-01-26 17:50:12 -0500228 ::std::thread reader_thread(::std::ref(reader));
229 GyroSender gyro_sender;
230 ::std::thread gyro_thread(::std::ref(gyro_sender));
231
232 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500233 drivetrain_writer.set_left_drivetrain_talon(
234 ::std::unique_ptr<Talon>(new Talon(5)));
235 drivetrain_writer.set_right_drivetrain_talon(
236 ::std::unique_ptr<Talon>(new Talon(2)));
237 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
238
Brian Silverman98f6ee22015-01-26 17:50:12 -0500239 ::std::unique_ptr<BufferedPcm> pcm(new BufferedPcm());
240 SolenoidWriter solenoid_writer(pcm);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500241 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
242 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500243 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
244
245 // Wait forever. Not much else to do...
246 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
247
Austin Schuh010eb812014-10-25 18:06:49 -0700248 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800249
Austin Schuh010eb812014-10-25 18:06:49 -0700250 joystick_sender.Quit();
251 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500252 reader.Quit();
253 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800254 gyro_sender.Quit();
255 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500256
257 drivetrain_writer.Quit();
258 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500259 solenoid_writer.Quit();
260 solenoid_thread.join();
261
Austin Schuh010eb812014-10-25 18:06:49 -0700262 ::aos::Cleanup();
263 }
264};
265
Brian Silverman98f6ee22015-01-26 17:50:12 -0500266} // namespace wpilib
267} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800268
Brian Silverman98f6ee22015-01-26 17:50:12 -0500269
270START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);