blob: 42f16e812c6e81bff3981021a9bbec8b94475c9d [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 Silvermanff7b3472015-01-26 17:53:04 -050032#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050033#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080034
Brian Silvermancb77f232014-12-19 21:48:36 -080035#include "Encoder.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080036#include "Talon.h"
37#include "DriverStation.h"
38#include "AnalogInput.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080039#include "Compressor.h"
40#include "RobotBase.h"
Austin Schuh010eb812014-10-25 18:06:49 -070041
42#ifndef M_PI
43#define M_PI 3.14159265358979323846
44#endif
45
46using ::aos::util::SimpleLogInterval;
47using ::frc971::control_loops::drivetrain;
Austin Schuh010eb812014-10-25 18:06:49 -070048using ::aos::util::WrappingCounter;
49
50namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080051namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070052
Austin Schuh010eb812014-10-25 18:06:49 -070053double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080054 return static_cast<double>(in) /
55 (256.0 /*cpr*/ * 2.0 /*2x. Stupid WPILib*/) *
56 (18.0 / 50.0 /*output stage*/) * (56.0 / 30.0 /*encoder gears*/)
57 // * constants::GetValues().drivetrain_encoder_ratio
58 *
59 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
Austin Schuh010eb812014-10-25 18:06:49 -070060}
61
Austin Schuh010eb812014-10-25 18:06:49 -070062class SensorReader {
63 public:
Brian Silverman1f90d672015-01-26 20:20:45 -050064 SensorReader() {
Austin Schuh010eb812014-10-25 18:06:49 -070065 filter_.SetPeriodNanoSeconds(100000);
Austin Schuh010eb812014-10-25 18:06:49 -070066 }
67
Brian Silverman1f90d672015-01-26 20:20:45 -050068 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
69 left_encoder_ = ::std::move(left_encoder);
70 }
71
72 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
73 right_encoder_ = ::std::move(right_encoder);
74 }
75
Austin Schuh010eb812014-10-25 18:06:49 -070076 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -080077 ::aos::SetCurrentThreadName("SensorReader");
78
Brian Silverman1f90d672015-01-26 20:20:45 -050079 static const int kPriority = 30;
80 //static const int kInterruptPriority = 55;
Austin Schuh010eb812014-10-25 18:06:49 -070081
Brian Silverman2fe007c2014-12-28 12:20:01 -080082 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -070083 while (run_) {
Brian Silverman20141f92015-01-05 17:39:01 -080084 ::aos::time::PhasedLoopXMS(5, 9000);
Austin Schuh010eb812014-10-25 18:06:49 -070085 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -070086 }
Austin Schuh010eb812014-10-25 18:06:49 -070087 }
88
89 void RunIteration() {
Austin Schuh010eb812014-10-25 18:06:49 -070090 DriverStation *ds = DriverStation::GetInstance();
91
Austin Schuhdb516032014-12-28 00:12:38 -080092 if (ds->IsSysActive()) {
Austin Schuh010eb812014-10-25 18:06:49 -070093 auto message = ::aos::controls::output_check_received.MakeMessage();
94 // TODO(brians): Actually read a pulse value from the roboRIO.
95 message->pwm_value = 0;
96 message->pulse_length = -1;
97 LOG_STRUCT(DEBUG, "received", *message);
98 message.Send();
99 }
100
Austin Schuh010eb812014-10-25 18:06:49 -0700101 drivetrain.position.MakeWithBuilder()
102 .right_encoder(drivetrain_translate(right_encoder_->GetRaw()))
103 .left_encoder(-drivetrain_translate(left_encoder_->GetRaw()))
Austin Schuh010eb812014-10-25 18:06:49 -0700104 .battery_voltage(ds->GetBatteryVoltage())
105 .Send();
106
Brian Silvermand8f403a2014-12-13 19:12:04 -0500107 // Signal that we are alive.
Austin Schuh010eb812014-10-25 18:06:49 -0700108 ::aos::controls::sensor_generation.MakeWithBuilder()
109 .reader_pid(getpid())
110 .cape_resets(0)
111 .Send();
112 }
113
114 void Quit() { run_ = false; }
115
116 private:
Austin Schuh010eb812014-10-25 18:06:49 -0700117 ::std::unique_ptr<Encoder> left_encoder_;
118 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700119
Brian Silverman1f90d672015-01-26 20:20:45 -0500120 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700121 DigitalGlitchFilter filter_;
122};
123
Brian Silvermand8f403a2014-12-13 19:12:04 -0500124class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700125 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500126 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Brian Silverman20141f92015-01-05 17:39:01 -0800127 : pcm_(pcm), drivetrain_(".frc971.control_loops.drivetrain.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500128
129 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
130 drivetrain_left_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700131 }
132
Brian Silvermand8f403a2014-12-13 19:12:04 -0500133 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
134 drivetrain_right_ = ::std::move(s);
135 }
Austin Schuh010eb812014-10-25 18:06:49 -0700136
Brian Silvermand8f403a2014-12-13 19:12:04 -0500137 void operator()() {
138 ::aos::SetCurrentThreadName("Solenoids");
139 ::aos::SetCurrentThreadRealtimePriority(30);
140
141 while (run_) {
142 ::aos::time::PhasedLoopXMS(20, 1000);
143
144 {
145 drivetrain_.FetchLatest();
146 if (drivetrain_.get()) {
147 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
148 drivetrain_left_->Set(drivetrain_->left_high);
149 drivetrain_right_->Set(drivetrain_->right_high);
150 }
151 }
152
Brian Silvermand8f403a2014-12-13 19:12:04 -0500153 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700154 }
155 }
156
Brian Silvermand8f403a2014-12-13 19:12:04 -0500157 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700158
Brian Silvermand8f403a2014-12-13 19:12:04 -0500159 private:
160 const ::std::unique_ptr<BufferedPcm> &pcm_;
161 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
162 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
Austin Schuh010eb812014-10-25 18:06:49 -0700163
Brian Silvermand8f403a2014-12-13 19:12:04 -0500164 ::aos::Queue<::frc971::control_loops::Drivetrain::Output> drivetrain_;
Austin Schuh010eb812014-10-25 18:06:49 -0700165
Brian Silvermand8f403a2014-12-13 19:12:04 -0500166 ::std::atomic<bool> run_{true};
167};
168
169class DrivetrainWriter : public LoopOutputHandler {
170 public:
171 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
172 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700173 }
174
Brian Silvermand8f403a2014-12-13 19:12:04 -0500175 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
176 right_drivetrain_talon_ = ::std::move(t);
177 }
Austin Schuh010eb812014-10-25 18:06:49 -0700178
Brian Silvermand8f403a2014-12-13 19:12:04 -0500179 private:
180 virtual void Read() override {
181 ::frc971::control_loops::drivetrain.output.FetchAnother();
182 }
183
184 virtual void Write() override {
185 auto &queue = ::frc971::control_loops::drivetrain.output;
186 LOG_STRUCT(DEBUG, "will output", *queue);
187 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
188 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
189 }
190
191 virtual void Stop() override {
192 LOG(WARNING, "drivetrain output too old\n");
193 left_drivetrain_talon_->Disable();
194 right_drivetrain_talon_->Disable();
195 }
196
Austin Schuh010eb812014-10-25 18:06:49 -0700197 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500198 ::std::unique_ptr<Talon> right_drivetrain_talon_;
199};
200
Brian Silverman1f90d672015-01-26 20:20:45 -0500201// TODO(brian): Replace this with ::std::make_unique once all our toolchains
202// have support.
203template <class T, class... U>
204std::unique_ptr<T> make_unique(U &&... u) {
205 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
206}
207
Austin Schuh010eb812014-10-25 18:06:49 -0700208class WPILibRobot : public RobotBase {
209 public:
210 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500211 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800212 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500213
Brian Silverman98f6ee22015-01-26 17:50:12 -0500214 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700215 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500216 ::std::unique_ptr<Compressor> compressor(new Compressor());
217 compressor->SetClosedLoopControl(true);
218
Brian Silverman98f6ee22015-01-26 17:50:12 -0500219 SensorReader reader;
Brian Silverman1f90d672015-01-26 20:20:45 -0500220 // TODO(sensors): Replace all the 99s with real port numbers.
221 reader.set_left_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
222 reader.set_right_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
Brian Silverman98f6ee22015-01-26 17:50:12 -0500223 ::std::thread reader_thread(::std::ref(reader));
224 GyroSender gyro_sender;
225 ::std::thread gyro_thread(::std::ref(gyro_sender));
226
227 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500228 drivetrain_writer.set_left_drivetrain_talon(
229 ::std::unique_ptr<Talon>(new Talon(5)));
230 drivetrain_writer.set_right_drivetrain_talon(
231 ::std::unique_ptr<Talon>(new Talon(2)));
232 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
233
Brian Silverman98f6ee22015-01-26 17:50:12 -0500234 ::std::unique_ptr<BufferedPcm> pcm(new BufferedPcm());
235 SolenoidWriter solenoid_writer(pcm);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500236 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
237 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500238 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
239
240 // Wait forever. Not much else to do...
241 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
242
Austin Schuh010eb812014-10-25 18:06:49 -0700243 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800244
Austin Schuh010eb812014-10-25 18:06:49 -0700245 joystick_sender.Quit();
246 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500247 reader.Quit();
248 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800249 gyro_sender.Quit();
250 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500251
252 drivetrain_writer.Quit();
253 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500254 solenoid_writer.Quit();
255 solenoid_thread.join();
256
Austin Schuh010eb812014-10-25 18:06:49 -0700257 ::aos::Cleanup();
258 }
259};
260
Brian Silverman98f6ee22015-01-26 17:50:12 -0500261} // namespace wpilib
262} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800263
Brian Silverman98f6ee22015-01-26 17:50:12 -0500264
265START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);