brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include <stddef.h> |
| 2 | |
| 3 | #include "aos/common/logging/logging.h" |
| 4 | #include "aos/common/control_loop/Timing.h" |
| 5 | #include "aos/common/messages/RobotState.q.h" |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 6 | #include "aos/common/logging/queue_logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | namespace control_loops { |
| 10 | |
| 11 | // TODO(aschuh): Tests. |
| 12 | |
Brian Silverman | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 13 | template <class T, bool has_position, bool fail_no_position> |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 14 | constexpr ::aos::time::Time |
| 15 | ControlLoop<T, has_position, fail_no_position>::kStaleLogInterval; |
| 16 | |
| 17 | template <class T, bool has_position, bool fail_no_position> |
Brian Silverman | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 18 | void ControlLoop<T, has_position, fail_no_position>::ZeroOutputs() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 19 | aos::ScopedMessagePtr<OutputType> output = |
| 20 | control_loop_->output.MakeMessage(); |
| 21 | Zero(output.get()); |
| 22 | output.Send(); |
| 23 | } |
| 24 | |
Brian Silverman | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 25 | template <class T, bool has_position, bool fail_no_position> |
| 26 | void ControlLoop<T, has_position, fail_no_position>::Iterate() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | // Fetch the latest control loop goal and position. If there is no new |
| 28 | // goal, we will just reuse the old one. |
| 29 | // If there is no goal, we haven't started up fully. It isn't worth |
| 30 | // the added complexity for each loop implementation to handle that case. |
| 31 | control_loop_->goal.FetchLatest(); |
| 32 | // TODO(aschuh): Check the age here if we want the loop to stop on old |
| 33 | // goals. |
| 34 | const GoalType *goal = control_loop_->goal.get(); |
| 35 | if (goal == NULL) { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 36 | LOG_INTERVAL(no_prior_goal_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 37 | ZeroOutputs(); |
| 38 | return; |
| 39 | } |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 40 | LOG_STRUCT(DEBUG, "goal", *goal); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | |
| 42 | // Only pass in a position if we got one this cycle. |
| 43 | const PositionType *position = NULL; |
| 44 | |
| 45 | // Only fetch the latest position if we have one. |
| 46 | if (has_position) { |
| 47 | // If the position is stale, this is really bad. Try fetching a position |
| 48 | // and check how fresh it is, and then take the appropriate action. |
| 49 | if (control_loop_->position.FetchLatest()) { |
| 50 | position = control_loop_->position.get(); |
| 51 | } else { |
| 52 | if (control_loop_->position.get()) { |
| 53 | int msec_age = control_loop_->position.Age().ToMSec(); |
| 54 | if (!control_loop_->position.IsNewerThanMS(kPositionTimeoutMs)) { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 55 | LOG_INTERVAL(very_stale_position_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | ZeroOutputs(); |
| 57 | return; |
| 58 | } else { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 59 | LOG(ERROR, "Stale position. %d ms (< %d ms)\n", msec_age, |
| 60 | kPositionTimeoutMs); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 61 | } |
| 62 | } else { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 63 | LOG_INTERVAL(no_prior_position_); |
Brian Silverman | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 64 | if (fail_no_position) { |
| 65 | ZeroOutputs(); |
| 66 | return; |
| 67 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 70 | if (position) { |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 71 | LOG_STRUCT(DEBUG, "position", *position); |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 72 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool outputs_enabled = false; |
| 76 | |
| 77 | // Check to see if we got a driver station packet recently. |
| 78 | if (aos::robot_state.FetchLatest()) { |
| 79 | outputs_enabled = true; |
| 80 | } else if (aos::robot_state.IsNewerThanMS(kDSPacketTimeoutMs)) { |
| 81 | outputs_enabled = true; |
| 82 | } else { |
| 83 | if (aos::robot_state.get()) { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 84 | LOG_INTERVAL(driver_station_old_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | } else { |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 86 | LOG_INTERVAL(no_driver_station_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | // Run the iteration. |
| 91 | aos::ScopedMessagePtr<StatusType> status = |
| 92 | control_loop_->status.MakeMessage(); |
| 93 | if (status.get() == NULL) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (outputs_enabled) { |
| 98 | aos::ScopedMessagePtr<OutputType> output = |
| 99 | control_loop_->output.MakeMessage(); |
| 100 | RunIteration(goal, position, output.get(), status.get()); |
| 101 | |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 102 | LOG_STRUCT(DEBUG, "output", *output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | output.Send(); |
| 104 | } else { |
| 105 | // The outputs are disabled, so pass NULL in for the output. |
| 106 | RunIteration(goal, position, NULL, status.get()); |
| 107 | ZeroOutputs(); |
| 108 | } |
| 109 | |
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 | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 114 | template <class T, bool has_position, bool fail_no_position> |
| 115 | void ControlLoop<T, has_position, fail_no_position>::Run() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 116 | while (true) { |
Brian Silverman | 15ca985 | 2013-03-17 18:24:15 -0700 | [diff] [blame] | 117 | time::SleepUntil(NextLoopTime()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 118 | Iterate(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } // namespace control_loops |
| 123 | } // namespace aos |