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