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