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