Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
| 6 | #include <thread> |
| 7 | #include <mutex> |
| 8 | #include <functional> |
| 9 | |
| 10 | #include "aos/common/logging/logging.h" |
| 11 | #include "aos/common/logging/queue_logging.h" |
| 12 | #include "aos/common/time.h" |
| 13 | #include "aos/common/util/log_interval.h" |
| 14 | #include "aos/common/util/phased_loop.h" |
| 15 | #include "aos/common/util/wrapping_counter.h" |
| 16 | #include "aos/common/stl_mutex.h" |
| 17 | #include "aos/linux_code/init.h" |
| 18 | #include "aos/common/messages/robot_state.q.h" |
| 19 | |
| 20 | #include "frc971/control_loops/control_loops.q.h" |
| 21 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
| 22 | |
| 23 | #include "frc971/wpilib/hall_effect.h" |
| 24 | #include "frc971/wpilib/joystick_sender.h" |
| 25 | #include "frc971/wpilib/loop_output_handler.h" |
| 26 | #include "frc971/wpilib/buffered_solenoid.h" |
| 27 | #include "frc971/wpilib/buffered_pcm.h" |
| 28 | #include "frc971/wpilib/gyro_sender.h" |
| 29 | #include "frc971/wpilib/dma_edge_counting.h" |
| 30 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 31 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 32 | #include "frc971/wpilib/logging.q.h" |
| 33 | #include "bot3/control_loops/drivetrain/drivetrain.h" |
| 34 | |
| 35 | #include "Encoder.h" |
| 36 | #include "Talon.h" |
| 37 | #include "DriverStation.h" |
| 38 | #include "AnalogInput.h" |
| 39 | #include "Compressor.h" |
| 40 | #include "Relay.h" |
| 41 | #include "RobotBase.h" |
| 42 | #include "dma.h" |
| 43 | #include "ControllerPower.h" |
| 44 | |
| 45 | #ifndef M_PI |
| 46 | #define M_PI 3.14159265358979323846 |
| 47 | #endif |
| 48 | |
| 49 | using ::aos::util::SimpleLogInterval; |
| 50 | using ::bot3::control_loops::drivetrain_queue; |
| 51 | using ::frc971::wpilib::DMAEncoderAndPotentiometer; |
| 52 | using ::frc971::PotAndIndexPosition; |
| 53 | using ::frc971::wpilib::InterruptEncoderAndPotentiometer; |
| 54 | using ::frc971::wpilib::DMASynchronizer; |
| 55 | using ::frc971::wpilib::BufferedPcm; |
| 56 | using ::frc971::wpilib::LoopOutputHandler; |
| 57 | using ::frc971::wpilib::JoystickSender; |
| 58 | using ::frc971::wpilib::GyroSender; |
| 59 | |
| 60 | namespace bot3 { |
| 61 | namespace wpilib { |
| 62 | |
| 63 | double drivetrain_translate(int32_t in) { |
| 64 | return static_cast<double>(in) / |
| 65 | (256.0 /*cpr*/ * 4.0 /*4x*/) * |
| 66 | ::bot3::control_loops::kDrivetrainEncoderRatio * |
| 67 | (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 68 | } |
| 69 | |
| 70 | // TODO(comran): Check/update the values below for the bot3. |
| 71 | static const double kMaximumEncoderPulsesPerSecond = |
| 72 | 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ / |
| 73 | 60.0 /* seconds / minute */ * 256.0 /* CPR */ * |
| 74 | 4.0 /* index pulse = 1/4 cycle */; |
| 75 | |
| 76 | class SensorReader { |
| 77 | public: |
| 78 | SensorReader() { |
| 79 | // Set it to filter out anything shorter than 1/4 of the minimum pulse width |
| 80 | // we should ever see. |
| 81 | filter_.SetPeriodNanoSeconds( |
| 82 | static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5)); |
| 83 | } |
| 84 | |
| 85 | void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) { |
| 86 | left_encoder_ = ::std::move(left_encoder); |
| 87 | } |
| 88 | |
| 89 | void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) { |
| 90 | right_encoder_ = ::std::move(right_encoder); |
| 91 | } |
| 92 | |
| 93 | // All of the DMA-related set_* calls must be made before this, and it doesn't |
| 94 | // hurt to do all of them. |
| 95 | // TODO(comran): Do we still need dma? |
| 96 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 97 | dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma))); |
| 98 | } |
| 99 | |
| 100 | void operator()() { |
| 101 | LOG(INFO, "In sensor reader thread\n"); |
| 102 | ::aos::SetCurrentThreadName("SensorReader"); |
| 103 | |
| 104 | my_pid_ = getpid(); |
| 105 | ds_ = DriverStation::GetInstance(); |
| 106 | |
| 107 | dma_synchronizer_->Start(); |
| 108 | LOG(INFO, "Things are now started\n"); |
| 109 | |
| 110 | ::aos::SetCurrentThreadRealtimePriority(kPriority); |
| 111 | while (run_) { |
| 112 | ::aos::time::PhasedLoopXMS(5, 4000); |
| 113 | RunIteration(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void RunIteration() { |
| 118 | { |
| 119 | auto new_state = ::aos::robot_state.MakeMessage(); |
| 120 | |
| 121 | new_state->reader_pid = my_pid_; |
| 122 | new_state->outputs_enabled = ds_->IsSysActive(); |
| 123 | new_state->browned_out = ds_->IsSysBrownedOut(); |
| 124 | |
| 125 | new_state->is_3v3_active = ControllerPower::GetEnabled3V3(); |
| 126 | new_state->is_5v_active = ControllerPower::GetEnabled5V(); |
| 127 | new_state->voltage_3v3 = ControllerPower::GetVoltage3V3(); |
| 128 | new_state->voltage_5v = ControllerPower::GetVoltage5V(); |
| 129 | |
| 130 | new_state->voltage_roborio_in = ControllerPower::GetInputVoltage(); |
| 131 | new_state->voltage_battery = ds_->GetBatteryVoltage(); |
| 132 | |
| 133 | LOG_STRUCT(DEBUG, "robot_state", *new_state); |
| 134 | |
| 135 | new_state.Send(); |
| 136 | } |
| 137 | |
| 138 | { |
| 139 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 140 | drivetrain_message->right_encoder = |
| 141 | -drivetrain_translate(right_encoder_->GetRaw()); |
| 142 | drivetrain_message->left_encoder = |
| 143 | drivetrain_translate(left_encoder_->GetRaw()); |
| 144 | |
| 145 | drivetrain_message.Send(); |
| 146 | } |
| 147 | |
| 148 | dma_synchronizer_->RunIteration(); |
| 149 | } |
| 150 | |
| 151 | void Quit() { run_ = false; } |
| 152 | |
| 153 | private: |
| 154 | static const int kPriority = 30; |
| 155 | static const int kInterruptPriority = 55; |
| 156 | |
| 157 | int32_t my_pid_; |
| 158 | DriverStation *ds_; |
| 159 | |
| 160 | void CopyPotAndIndexPosition( |
| 161 | const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position, |
| 162 | ::std::function<double(int32_t)> encoder_translate, |
| 163 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 164 | double potentiometer_offset) { |
| 165 | const double multiplier = reverse ? -1.0 : 1.0; |
| 166 | position->encoder = |
| 167 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 168 | position->pot = multiplier * potentiometer_translate( |
| 169 | encoder.polled_potentiometer_voltage()) + |
| 170 | potentiometer_offset; |
| 171 | position->latched_encoder = |
| 172 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 173 | position->latched_pot = |
| 174 | multiplier * |
| 175 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 176 | potentiometer_offset; |
| 177 | position->index_pulses = encoder.index_posedge_count(); |
| 178 | } |
| 179 | |
| 180 | void CopyPotAndIndexPosition( |
| 181 | const InterruptEncoderAndPotentiometer &encoder, |
| 182 | PotAndIndexPosition *position, |
| 183 | ::std::function<double(int32_t)> encoder_translate, |
| 184 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 185 | double potentiometer_offset) { |
| 186 | const double multiplier = reverse ? -1.0 : 1.0; |
| 187 | position->encoder = |
| 188 | multiplier * encoder_translate(encoder.encoder()->GetRaw()); |
| 189 | position->pot = multiplier * potentiometer_translate( |
| 190 | encoder.potentiometer()->GetVoltage()) + |
| 191 | potentiometer_offset; |
| 192 | position->latched_encoder = |
| 193 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 194 | position->latched_pot = |
| 195 | multiplier * |
| 196 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 197 | potentiometer_offset; |
| 198 | position->index_pulses = encoder.index_posedge_count(); |
| 199 | } |
| 200 | |
| 201 | ::std::unique_ptr<DMASynchronizer> dma_synchronizer_; |
| 202 | |
| 203 | ::std::unique_ptr<Encoder> left_encoder_; |
| 204 | ::std::unique_ptr<Encoder> right_encoder_; |
| 205 | |
| 206 | ::std::atomic<bool> run_{true}; |
| 207 | DigitalGlitchFilter filter_; |
| 208 | }; |
| 209 | |
| 210 | class SolenoidWriter { |
| 211 | public: |
| 212 | SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 213 | : pcm_(pcm) {} |
| 214 | |
| 215 | void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) { |
| 216 | pressure_switch_ = ::std::move(pressure_switch); |
| 217 | } |
| 218 | |
| 219 | void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) { |
| 220 | compressor_relay_ = ::std::move(compressor_relay); |
| 221 | } |
| 222 | |
| 223 | void operator()() { |
| 224 | ::aos::SetCurrentThreadName("Solenoids"); |
| 225 | ::aos::SetCurrentThreadRealtimePriority(30); |
| 226 | |
| 227 | while (run_) { |
| 228 | ::aos::time::PhasedLoopXMS(20, 1000); |
| 229 | |
| 230 | ::aos::joystick_state.FetchLatest(); |
| 231 | |
| 232 | { |
| 233 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 234 | { |
| 235 | const bool compressor_on = !pressure_switch_->Get(); |
| 236 | to_log.compressor_on = compressor_on; |
| 237 | if (compressor_on) { |
| 238 | compressor_relay_->Set(Relay::kForward); |
| 239 | } else { |
| 240 | compressor_relay_->Set(Relay::kOff); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | pcm_->Flush(); |
| 245 | to_log.read_solenoids = pcm_->GetAll(); |
| 246 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void Quit() { run_ = false; } |
| 252 | |
| 253 | private: |
| 254 | const ::std::unique_ptr<BufferedPcm> &pcm_; |
| 255 | ::std::unique_ptr<DigitalSource> pressure_switch_; |
| 256 | ::std::unique_ptr<Relay> compressor_relay_; |
| 257 | |
| 258 | ::std::atomic<bool> run_{true}; |
| 259 | }; |
| 260 | |
| 261 | class DrivetrainWriter : public LoopOutputHandler { |
| 262 | public: |
| 263 | void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) { |
| 264 | left_drivetrain_talon_ = ::std::move(t); |
| 265 | } |
| 266 | |
| 267 | void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) { |
| 268 | right_drivetrain_talon_ = ::std::move(t); |
| 269 | } |
| 270 | |
| 271 | private: |
| 272 | virtual void Read() override { |
| 273 | ::bot3::control_loops::drivetrain_queue.output.FetchAnother(); |
| 274 | } |
| 275 | |
| 276 | virtual void Write() override { |
| 277 | auto &queue = ::bot3::control_loops::drivetrain_queue.output; |
| 278 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 279 | left_drivetrain_talon_->Set(queue->left_voltage / 12.0); |
| 280 | right_drivetrain_talon_->Set(-queue->right_voltage / 12.0); |
| 281 | } |
| 282 | |
| 283 | virtual void Stop() override { |
| 284 | LOG(WARNING, "drivetrain output too old\n"); |
| 285 | left_drivetrain_talon_->Disable(); |
| 286 | right_drivetrain_talon_->Disable(); |
| 287 | } |
| 288 | |
| 289 | ::std::unique_ptr<Talon> left_drivetrain_talon_; |
| 290 | ::std::unique_ptr<Talon> right_drivetrain_talon_; |
| 291 | }; |
| 292 | |
| 293 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 294 | // have support. |
| 295 | template <class T, class... U> |
| 296 | std::unique_ptr<T> make_unique(U &&... u) { |
| 297 | return std::unique_ptr<T>(new T(std::forward<U>(u)...)); |
| 298 | } |
| 299 | |
| 300 | class WPILibRobot : public RobotBase { |
| 301 | public: |
| 302 | ::std::unique_ptr<Encoder> encoder(int index) { |
| 303 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 304 | Encoder::k4X); |
| 305 | } |
| 306 | virtual void StartCompetition() { |
| 307 | ::aos::InitNRT(); |
| 308 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 309 | |
| 310 | JoystickSender joystick_sender; |
| 311 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 312 | |
| 313 | SensorReader reader; |
| 314 | LOG(INFO, "Creating the reader\n"); |
| 315 | |
| 316 | // TODO(comran): Find talon/encoder numbers. |
| 317 | reader.set_left_encoder(encoder(2)); |
| 318 | reader.set_right_encoder(encoder(3)); |
| 319 | reader.set_dma(make_unique<DMA>()); |
| 320 | ::std::thread reader_thread(::std::ref(reader)); |
| 321 | GyroSender gyro_sender; |
| 322 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 323 | |
| 324 | DrivetrainWriter drivetrain_writer; |
| 325 | drivetrain_writer.set_left_drivetrain_talon( |
| 326 | ::std::unique_ptr<Talon>(new Talon(8))); |
| 327 | drivetrain_writer.set_right_drivetrain_talon( |
| 328 | ::std::unique_ptr<Talon>(new Talon(0))); |
| 329 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 330 | |
| 331 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 332 | new ::frc971::wpilib::BufferedPcm()); |
| 333 | SolenoidWriter solenoid_writer(pcm); |
| 334 | solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9)); |
| 335 | solenoid_writer.set_compressor_relay(make_unique<Relay>(0)); |
| 336 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 337 | |
| 338 | // Wait forever. Not much else to do... |
| 339 | PCHECK(select(0, nullptr, nullptr, nullptr, nullptr)); |
| 340 | |
| 341 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 342 | |
| 343 | joystick_sender.Quit(); |
| 344 | joystick_thread.join(); |
| 345 | reader.Quit(); |
| 346 | reader_thread.join(); |
| 347 | gyro_sender.Quit(); |
| 348 | gyro_thread.join(); |
| 349 | |
| 350 | drivetrain_writer.Quit(); |
| 351 | drivetrain_writer_thread.join(); |
| 352 | solenoid_writer.Quit(); |
| 353 | solenoid_thread.join(); |
| 354 | |
| 355 | ::aos::Cleanup(); |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | } // namespace wpilib |
| 360 | } // namespace bot3 |
| 361 | |
| 362 | |
| 363 | START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot); |