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