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