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