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