Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 1 | #include <cinttypes> |
| 2 | #include <cstddef> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/logging/logging.h" |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 5 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 6 | namespace frc971 { |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 7 | namespace controls { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
| 9 | // TODO(aschuh): Tests. |
| 10 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | template <class GoalType, class PositionType, class StatusType, |
| 12 | class OutputType> |
| 13 | constexpr ::std::chrono::milliseconds ControlLoop< |
| 14 | GoalType, PositionType, StatusType, OutputType>::kStaleLogInterval; |
| 15 | template <class GoalType, class PositionType, class StatusType, |
| 16 | class OutputType> |
| 17 | constexpr ::std::chrono::milliseconds ControlLoop< |
| 18 | GoalType, PositionType, StatusType, OutputType>::kPwmDisableTime; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 19 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | template <class GoalType, class PositionType, class StatusType, |
| 21 | class OutputType> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 22 | void ControlLoop<GoalType, PositionType, StatusType, |
| 23 | OutputType>::ZeroOutputs() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | typename ::aos::Sender<OutputType>::Builder builder = |
| 25 | output_sender_.MakeBuilder(); |
| 26 | builder.Send(Zero(&builder)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | template <class GoalType, class PositionType, class StatusType, |
| 30 | class OutputType> |
| 31 | void ControlLoop<GoalType, PositionType, StatusType, |
| 32 | OutputType>::IteratePosition(const PositionType &position) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 33 | no_goal_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 34 | no_sensor_state_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 35 | motors_off_log_.Print(); |
Brian Silverman | 6a1cd21 | 2014-02-20 21:04:34 -0800 | [diff] [blame] | 36 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 37 | // Fetch the latest control loop goal. If there is no new |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | // goal, we will just reuse the old one. |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 39 | goal_fetcher_.Fetch(); |
| 40 | const GoalType *goal = goal_fetcher_.get(); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 41 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 42 | const bool new_robot_state = robot_state_fetcher_.Fetch(); |
| 43 | if (!robot_state_fetcher_.get()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 44 | AOS_LOG_INTERVAL(no_sensor_state_); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 45 | return; |
| 46 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | if (sensor_reader_pid_ != robot_state_fetcher_->reader_pid()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 48 | AOS_LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | robot_state_fetcher_->reader_pid(), sensor_reader_pid_); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 50 | reset_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | sensor_reader_pid_ = robot_state_fetcher_->reader_pid(); |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 52 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 53 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 54 | bool outputs_enabled = robot_state_fetcher_->outputs_enabled(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | |
| 56 | // Check to see if we got a driver station packet recently. |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 57 | if (new_robot_state) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | if (robot_state_fetcher_->outputs_enabled()) { |
Brian Silverman | 295f74b | 2015-02-14 23:00:47 -0500 | [diff] [blame] | 59 | // If the driver's station reports being disabled, we're probably not |
| 60 | // actually going to send motor values regardless of what the FPGA |
| 61 | // reports. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 62 | last_pwm_sent_ = robot_state_fetcher_.context().monotonic_event_time; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Austin Schuh | 1984527 | 2019-07-07 20:45:22 -0700 | [diff] [blame] | 66 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 67 | event_loop_->monotonic_now(); |
| 68 | const bool motors_off = monotonic_now >= kPwmDisableTime + last_pwm_sent_; |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 69 | joystick_state_fetcher_.Fetch(); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 70 | if (motors_off) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 71 | if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 72 | AOS_LOG_INTERVAL(motors_off_log_); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 73 | } |
| 74 | outputs_enabled = false; |
| 75 | } |
| 76 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 77 | typename ::aos::Sender<StatusType>::Builder status = |
| 78 | status_sender_.MakeBuilder(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 79 | if (outputs_enabled) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 80 | typename ::aos::Sender<OutputType>::Builder output = |
| 81 | output_sender_.MakeBuilder(); |
| 82 | RunIteration(goal, &position, &output, &status); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 83 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | output.CheckSent(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | } else { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 86 | // The outputs are disabled, so pass nullptr in for the output. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | RunIteration(goal, &position, nullptr, &status); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | ZeroOutputs(); |
| 89 | } |
| 90 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | status.CheckSent(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 94 | } // namespace controls |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 95 | } // namespace frc971 |