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 | |
| 12 | template <class T, bool has_position> |
| 13 | void ControlLoop<T, has_position>::ZeroOutputs() { |
| 14 | aos::ScopedMessagePtr<OutputType> output = |
| 15 | control_loop_->output.MakeMessage(); |
| 16 | Zero(output.get()); |
| 17 | output.Send(); |
| 18 | } |
| 19 | |
| 20 | template <class T, bool has_position> |
| 21 | void ControlLoop<T, has_position>::Iterate() { |
| 22 | // Temporary storage for printing out inputs and outputs. |
| 23 | char state[1024]; |
| 24 | |
| 25 | // Fetch the latest control loop goal and position. If there is no new |
| 26 | // goal, we will just reuse the old one. |
| 27 | // If there is no goal, we haven't started up fully. It isn't worth |
| 28 | // the added complexity for each loop implementation to handle that case. |
| 29 | control_loop_->goal.FetchLatest(); |
| 30 | // TODO(aschuh): Check the age here if we want the loop to stop on old |
| 31 | // goals. |
| 32 | const GoalType *goal = control_loop_->goal.get(); |
| 33 | if (goal == NULL) { |
| 34 | LOG(ERROR, "No prior control loop goal.\n"); |
| 35 | ZeroOutputs(); |
| 36 | return; |
| 37 | } |
| 38 | goal->Print(state, sizeof(state)); |
| 39 | LOG(DEBUG, "goal={%s}\n", state); |
| 40 | |
| 41 | // Only pass in a position if we got one this cycle. |
| 42 | const PositionType *position = NULL; |
| 43 | |
| 44 | // Only fetch the latest position if we have one. |
| 45 | if (has_position) { |
| 46 | // If the position is stale, this is really bad. Try fetching a position |
| 47 | // and check how fresh it is, and then take the appropriate action. |
| 48 | if (control_loop_->position.FetchLatest()) { |
| 49 | position = control_loop_->position.get(); |
| 50 | } else { |
| 51 | if (control_loop_->position.get()) { |
| 52 | int msec_age = control_loop_->position.Age().ToMSec(); |
| 53 | if (!control_loop_->position.IsNewerThanMS(kPositionTimeoutMs)) { |
| 54 | LOG(ERROR, "Stale position. %d ms > %d ms. Outputs disabled.\n", |
| 55 | msec_age, kPositionTimeoutMs); |
| 56 | ZeroOutputs(); |
| 57 | return; |
| 58 | } else { |
| 59 | LOG(ERROR, "Stale position. %d ms\n", msec_age); |
| 60 | } |
| 61 | } else { |
| 62 | LOG(ERROR, "Never had a position.\n"); |
| 63 | ZeroOutputs(); |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | position->Print(state, sizeof(state)); |
| 68 | LOG(DEBUG, "position={%s}\n", state); |
| 69 | } |
| 70 | |
| 71 | bool outputs_enabled = false; |
| 72 | |
| 73 | // Check to see if we got a driver station packet recently. |
| 74 | if (aos::robot_state.FetchLatest()) { |
| 75 | outputs_enabled = true; |
| 76 | } else if (aos::robot_state.IsNewerThanMS(kDSPacketTimeoutMs)) { |
| 77 | outputs_enabled = true; |
| 78 | } else { |
| 79 | if (aos::robot_state.get()) { |
| 80 | int msec_age = aos::robot_state.Age().ToMSec(); |
| 81 | LOG(ERROR, "Driver Station packet is too old (%d ms).\n", msec_age); |
| 82 | } else { |
| 83 | LOG(ERROR, "No Driver Station packet.\n"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Run the iteration. |
| 88 | aos::ScopedMessagePtr<StatusType> status = |
| 89 | control_loop_->status.MakeMessage(); |
| 90 | if (status.get() == NULL) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (outputs_enabled) { |
| 95 | aos::ScopedMessagePtr<OutputType> output = |
| 96 | control_loop_->output.MakeMessage(); |
| 97 | RunIteration(goal, position, output.get(), status.get()); |
| 98 | |
| 99 | output->Print(state, sizeof(state)); |
| 100 | LOG(DEBUG, "output={%s}\n", state); |
| 101 | output.Send(); |
| 102 | } else { |
| 103 | // The outputs are disabled, so pass NULL in for the output. |
| 104 | RunIteration(goal, position, NULL, status.get()); |
| 105 | ZeroOutputs(); |
| 106 | } |
| 107 | |
| 108 | status->Print(state, sizeof(state)); |
| 109 | LOG(DEBUG, "status={%s}\n", state); |
| 110 | status.Send(); |
| 111 | } |
| 112 | |
| 113 | template <class T, bool has_position> |
| 114 | void ControlLoop<T, has_position>::Run() { |
| 115 | while (true) { |
| 116 | ::aos::time::PhasedLoop10MS(0); |
| 117 | Iterate(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } // namespace control_loops |
| 122 | } // namespace aos |