blob: 3d5958ba03e5f604d368a5719ceb3b7841f292f6 [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:
63 SensorReader()
Brian Silverman20141f92015-01-05 17:39:01 -080064 : left_encoder_(new Encoder(11, 10, false, Encoder::k2X)), // E0
Brian Silvermancb77f232014-12-19 21:48:36 -080065 right_encoder_(new Encoder(13, 12, false, Encoder::k2X)), // E1
Austin Schuh010eb812014-10-25 18:06:49 -070066 run_(true) {
67 filter_.SetPeriodNanoSeconds(100000);
Austin Schuh010eb812014-10-25 18:06:49 -070068 }
69
70 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -080071 ::aos::SetCurrentThreadName("SensorReader");
72
Austin Schuh010eb812014-10-25 18:06:49 -070073 const int kPriority = 30;
Brian Silverman20141f92015-01-05 17:39:01 -080074 //const int kInterruptPriority = 55;
Austin Schuh010eb812014-10-25 18:06:49 -070075
Brian Silverman2fe007c2014-12-28 12:20:01 -080076 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -070077 while (run_) {
Brian Silverman20141f92015-01-05 17:39:01 -080078 ::aos::time::PhasedLoopXMS(5, 9000);
Austin Schuh010eb812014-10-25 18:06:49 -070079 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -070080 }
Austin Schuh010eb812014-10-25 18:06:49 -070081 }
82
83 void RunIteration() {
Austin Schuh010eb812014-10-25 18:06:49 -070084 DriverStation *ds = DriverStation::GetInstance();
85
Austin Schuhdb516032014-12-28 00:12:38 -080086 if (ds->IsSysActive()) {
Austin Schuh010eb812014-10-25 18:06:49 -070087 auto message = ::aos::controls::output_check_received.MakeMessage();
88 // TODO(brians): Actually read a pulse value from the roboRIO.
89 message->pwm_value = 0;
90 message->pulse_length = -1;
91 LOG_STRUCT(DEBUG, "received", *message);
92 message.Send();
93 }
94
Austin Schuh010eb812014-10-25 18:06:49 -070095 // TODO(austin): Calibrate the shifter constants again.
Brian Silverman20141f92015-01-05 17:39:01 -080096 // TODO(sensors): Hook up the new dog position sensors.
Austin Schuh010eb812014-10-25 18:06:49 -070097 drivetrain.position.MakeWithBuilder()
98 .right_encoder(drivetrain_translate(right_encoder_->GetRaw()))
99 .left_encoder(-drivetrain_translate(left_encoder_->GetRaw()))
Brian Silverman20141f92015-01-05 17:39:01 -0800100 .left_shifter_position(0)
101 .right_shifter_position(0)
Austin Schuh010eb812014-10-25 18:06:49 -0700102 .battery_voltage(ds->GetBatteryVoltage())
103 .Send();
104
Brian Silvermand8f403a2014-12-13 19:12:04 -0500105 // Signal that we are alive.
Austin Schuh010eb812014-10-25 18:06:49 -0700106 ::aos::controls::sensor_generation.MakeWithBuilder()
107 .reader_pid(getpid())
108 .cape_resets(0)
109 .Send();
110 }
111
112 void Quit() { run_ = false; }
113
114 private:
115 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
116
117 ::std::unique_ptr<Encoder> left_encoder_;
118 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700119
120 ::std::atomic<bool> run_;
121 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
Austin Schuh010eb812014-10-25 18:06:49 -0700201class WPILibRobot : public RobotBase {
202 public:
203 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500204 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800205 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500206
Brian Silverman98f6ee22015-01-26 17:50:12 -0500207 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700208 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500209 ::std::unique_ptr<Compressor> compressor(new Compressor());
210 compressor->SetClosedLoopControl(true);
211
Brian Silverman98f6ee22015-01-26 17:50:12 -0500212 SensorReader reader;
213 ::std::thread reader_thread(::std::ref(reader));
214 GyroSender gyro_sender;
215 ::std::thread gyro_thread(::std::ref(gyro_sender));
216
217 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500218 drivetrain_writer.set_left_drivetrain_talon(
219 ::std::unique_ptr<Talon>(new Talon(5)));
220 drivetrain_writer.set_right_drivetrain_talon(
221 ::std::unique_ptr<Talon>(new Talon(2)));
222 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
223
Brian Silverman98f6ee22015-01-26 17:50:12 -0500224 ::std::unique_ptr<BufferedPcm> pcm(new BufferedPcm());
225 SolenoidWriter solenoid_writer(pcm);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500226 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
227 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500228 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
229
230 // Wait forever. Not much else to do...
231 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
232
Austin Schuh010eb812014-10-25 18:06:49 -0700233 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800234
Austin Schuh010eb812014-10-25 18:06:49 -0700235 joystick_sender.Quit();
236 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500237 reader.Quit();
238 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800239 gyro_sender.Quit();
240 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500241
242 drivetrain_writer.Quit();
243 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500244 solenoid_writer.Quit();
245 solenoid_thread.join();
246
Austin Schuh010eb812014-10-25 18:06:49 -0700247 ::aos::Cleanup();
248 }
249};
250
Brian Silverman98f6ee22015-01-26 17:50:12 -0500251} // namespace wpilib
252} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800253
Brian Silverman98f6ee22015-01-26 17:50:12 -0500254
255START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);