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" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace control_loops { |
| 9 | |
| 10 | // TODO(aschuh): Tests. |
| 11 | |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 12 | template <class T, bool has_position, bool fail_no_position, bool ignore_stale> |
| 13 | void ControlLoop<T, has_position, fail_no_position, ignore_stale>::ZeroOutputs() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | aos::ScopedMessagePtr<OutputType> output = |
| 15 | control_loop_->output.MakeMessage(); |
| 16 | Zero(output.get()); |
| 17 | output.Send(); |
| 18 | } |
| 19 | |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 20 | template <class T, bool has_position, bool fail_no_position, bool ignore_stale> |
| 21 | void ControlLoop<T, has_position, fail_no_position, ignore_stale>::Iterate() { |
| 22 | bool use_model = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | // Temporary storage for printing out inputs and outputs. |
| 24 | char state[1024]; |
| 25 | |
| 26 | // Fetch the latest control loop goal and position. If there is no new |
| 27 | // goal, we will just reuse the old one. |
| 28 | // If there is no goal, we haven't started up fully. It isn't worth |
| 29 | // the added complexity for each loop implementation to handle that case. |
| 30 | control_loop_->goal.FetchLatest(); |
| 31 | // TODO(aschuh): Check the age here if we want the loop to stop on old |
| 32 | // goals. |
| 33 | const GoalType *goal = control_loop_->goal.get(); |
| 34 | if (goal == NULL) { |
| 35 | LOG(ERROR, "No prior control loop goal.\n"); |
| 36 | ZeroOutputs(); |
| 37 | return; |
| 38 | } |
| 39 | goal->Print(state, sizeof(state)); |
| 40 | LOG(DEBUG, "goal={%s}\n", state); |
| 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 { |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 52 | if (ignore_stale) { |
| 53 | use_model = true; |
| 54 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | if (control_loop_->position.get()) { |
| 56 | int msec_age = control_loop_->position.Age().ToMSec(); |
| 57 | if (!control_loop_->position.IsNewerThanMS(kPositionTimeoutMs)) { |
| 58 | LOG(ERROR, "Stale position. %d ms > %d ms. Outputs disabled.\n", |
| 59 | msec_age, kPositionTimeoutMs); |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 60 | if (!ignore_stale) { |
| 61 | ZeroOutputs(); |
| 62 | return; |
| 63 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 64 | } else { |
| 65 | LOG(ERROR, "Stale position. %d ms\n", msec_age); |
| 66 | } |
| 67 | } else { |
| 68 | LOG(ERROR, "Never had a position.\n"); |
Brian Silverman | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 69 | if (fail_no_position) { |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 70 | ZeroOutputs(); |
| 71 | return; |
Brian Silverman | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 72 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 75 | if (position) { |
| 76 | position->Print(state, sizeof(state)); |
| 77 | LOG(DEBUG, "position={%s}\n", state); |
| 78 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool outputs_enabled = false; |
| 82 | |
| 83 | // Check to see if we got a driver station packet recently. |
| 84 | if (aos::robot_state.FetchLatest()) { |
| 85 | outputs_enabled = true; |
| 86 | } else if (aos::robot_state.IsNewerThanMS(kDSPacketTimeoutMs)) { |
| 87 | outputs_enabled = true; |
| 88 | } else { |
| 89 | if (aos::robot_state.get()) { |
| 90 | int msec_age = aos::robot_state.Age().ToMSec(); |
| 91 | LOG(ERROR, "Driver Station packet is too old (%d ms).\n", msec_age); |
| 92 | } else { |
| 93 | LOG(ERROR, "No Driver Station packet.\n"); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Run the iteration. |
| 98 | aos::ScopedMessagePtr<StatusType> status = |
| 99 | control_loop_->status.MakeMessage(); |
| 100 | if (status.get() == NULL) { |
| 101 | return; |
| 102 | } |
| 103 | |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 104 | LOG(DEBUG, "Outputs enabled: %d\n", outputs_enabled); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 105 | if (outputs_enabled) { |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 106 | if (use_model) { |
| 107 | position = NULL; |
| 108 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 109 | aos::ScopedMessagePtr<OutputType> output = |
| 110 | control_loop_->output.MakeMessage(); |
| 111 | RunIteration(goal, position, output.get(), status.get()); |
| 112 | |
| 113 | output->Print(state, sizeof(state)); |
| 114 | LOG(DEBUG, "output={%s}\n", state); |
| 115 | output.Send(); |
| 116 | } else { |
| 117 | // The outputs are disabled, so pass NULL in for the output. |
| 118 | RunIteration(goal, position, NULL, status.get()); |
| 119 | ZeroOutputs(); |
| 120 | } |
| 121 | |
| 122 | status->Print(state, sizeof(state)); |
| 123 | LOG(DEBUG, "status={%s}\n", state); |
| 124 | status.Send(); |
| 125 | } |
| 126 | |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 127 | template <class T, bool has_position, bool fail_no_position, bool ignore_stale> |
| 128 | void ControlLoop<T, has_position, fail_no_position, ignore_stale>::Run() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 129 | while (true) { |
Brian Silverman | 15ca985 | 2013-03-17 18:24:15 -0700 | [diff] [blame] | 130 | time::SleepUntil(NextLoopTime()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 131 | Iterate(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace control_loops |
| 136 | } // namespace aos |