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> |
| 26 | void ControlLoop<T>::Iterate() { |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 27 | if (!has_iterate_fetcher_) { |
| 28 | iterate_position_fetcher_ = |
| 29 | event_loop_->MakeFetcher<PositionType>(name_ + ".position"); |
| 30 | has_iterate_fetcher_ = true; |
| 31 | } |
| 32 | const bool did_fetch = iterate_position_fetcher_.Fetch(); |
| 33 | if (!did_fetch) { |
| 34 | LOG(FATAL, "Failed to fetch from position queue\n"); |
| 35 | } |
| 36 | IteratePosition(*iterate_position_fetcher_); |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | template <class T> |
| 40 | void ControlLoop<T>::IteratePosition(const PositionType &position) { |
| 41 | // Since Exit() isn't async safe, we want to call Exit from the periodic |
| 42 | // handler. |
| 43 | if (!run_) { |
| 44 | event_loop_->Exit(); |
| 45 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 46 | no_goal_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 47 | no_sensor_state_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 48 | motors_off_log_.Print(); |
Brian Silverman | 6a1cd21 | 2014-02-20 21:04:34 -0800 | [diff] [blame] | 49 | |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 50 | LOG_STRUCT(DEBUG, "position", position); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 51 | |
| 52 | // Fetch the latest control loop goal. If there is no new |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 53 | // goal, we will just reuse the old one. |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 54 | goal_fetcher_.Fetch(); |
| 55 | const GoalType *goal = goal_fetcher_.get(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 56 | if (goal) { |
| 57 | LOG_STRUCT(DEBUG, "goal", *goal); |
| 58 | } else { |
| 59 | LOG_INTERVAL(no_goal_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 60 | } |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 61 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 62 | const bool new_robot_state = robot_state_fetcher_.Fetch(); |
| 63 | if (!robot_state_fetcher_.get()) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 64 | LOG_INTERVAL(no_sensor_state_); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 65 | return; |
| 66 | } |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 67 | if (sensor_reader_pid_ != robot_state_fetcher_->reader_pid) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 68 | LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n", |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 69 | robot_state_fetcher_->reader_pid, sensor_reader_pid_); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 70 | reset_ = true; |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 71 | sensor_reader_pid_ = robot_state_fetcher_->reader_pid; |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 72 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 74 | bool outputs_enabled = robot_state_fetcher_->outputs_enabled; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 75 | |
| 76 | // Check to see if we got a driver station packet recently. |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 77 | if (new_robot_state) { |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 78 | if (robot_state_fetcher_->outputs_enabled) { |
Brian Silverman | 295f74b | 2015-02-14 23:00:47 -0500 | [diff] [blame] | 79 | // If the driver's station reports being disabled, we're probably not |
| 80 | // actually going to send motor values regardless of what the FPGA |
| 81 | // reports. |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 82 | last_pwm_sent_ = robot_state_fetcher_->sent_time; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Austin Schuh | 1984527 | 2019-07-07 20:45:22 -0700 | [diff] [blame] | 86 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 87 | event_loop_->monotonic_now(); |
| 88 | const bool motors_off = monotonic_now >= kPwmDisableTime + last_pwm_sent_; |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 89 | joystick_state_fetcher_.Fetch(); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 90 | if (motors_off) { |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 91 | if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled) { |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 92 | LOG_INTERVAL(motors_off_log_); |
| 93 | } |
| 94 | outputs_enabled = false; |
| 95 | } |
| 96 | |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 97 | typename ::aos::Sender<StatusType>::Message status = |
| 98 | status_sender_.MakeMessage(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 99 | if (status.get() == nullptr) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (outputs_enabled) { |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 104 | typename ::aos::Sender<OutputType>::Message output = |
| 105 | output_sender_.MakeMessage(); |
| 106 | RunIteration(goal, &position, output.get(), status.get()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 107 | |
Austin Schuh | f907f2e | 2018-01-02 01:15:27 -0800 | [diff] [blame] | 108 | output->SetTimeToNow(); |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 109 | LOG_STRUCT(DEBUG, "output", *output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 110 | output.Send(); |
| 111 | } else { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 112 | // The outputs are disabled, so pass nullptr in for the output. |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 113 | RunIteration(goal, &position, nullptr, status.get()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 114 | ZeroOutputs(); |
| 115 | } |
| 116 | |
Austin Schuh | f907f2e | 2018-01-02 01:15:27 -0800 | [diff] [blame] | 117 | status->SetTimeToNow(); |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 118 | LOG_STRUCT(DEBUG, "status", *status); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | status.Send(); |
| 120 | } |
| 121 | |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame] | 122 | template <class T> |
| 123 | void ControlLoop<T>::Run() { |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 124 | struct sigaction action; |
| 125 | action.sa_handler = &ControlLoop<T>::Quit; |
| 126 | sigemptyset(&action.sa_mask); |
| 127 | action.sa_flags = SA_RESETHAND; |
| 128 | |
| 129 | PCHECK(sigaction(SIGTERM, &action, nullptr)); |
| 130 | PCHECK(sigaction(SIGQUIT, &action, nullptr)); |
| 131 | PCHECK(sigaction(SIGINT, &action, nullptr)); |
| 132 | |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 133 | event_loop_->MakeWatcher(name_ + ".position", |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 134 | [this](const PositionType &position) { |
| 135 | this->IteratePosition(position); |
| 136 | }); |
| 137 | |
| 138 | event_loop_->Run(); |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 139 | LOG(INFO, "Shutting down\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 142 | template <class T> |
| 143 | ::std::atomic<bool> ControlLoop<T>::run_{true}; |
| 144 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 145 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 146 | } // namespace aos |