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" |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 7 | #include "aos/common/controls/sensor_generation.q.h" |
| 8 | #include "aos/common/controls/output_check.q.h" |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 9 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | namespace aos { |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 11 | namespace controls { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 53 | sensor_generation.FetchLatest(); |
| 54 | if (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_ || |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 60 | sensor_generation->reader_pid != reader_pid_ || |
| 61 | 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", |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 63 | *sensor_generation.get()); |
Brian Silverman | 4910efb | 2014-02-17 11:10:10 -0800 | [diff] [blame] | 64 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 65 | reader_pid_ = sensor_generation->reader_pid; |
| 66 | cape_resets_ = sensor_generation->cape_resets; |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 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 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 123 | ::aos::controls::output_check_received.FetchLatest(); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 124 | // True if we're enabled but the motors aren't working. |
Austin Schuh | 66a3d2f | 2014-10-21 22:24:00 -0700 | [diff] [blame^] | 125 | // The 100ms is the result of using an oscilliscope to look at the PWM signal |
| 126 | // and output of a talon, and timing the delay between the last pulse and the |
| 127 | // talon turning off. |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 128 | const bool motors_off = |
| 129 | !::aos::controls::output_check_received.get() || |
| 130 | !::aos::controls::output_check_received.IsNewerThanMS(100); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 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 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 175 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 176 | } // namespace aos |