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 | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame^] | 15 | template <class T, bool fail_no_goal> |
| 16 | constexpr ::aos::time::Time ControlLoop<T, fail_no_goal>::kStaleLogInterval; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 17 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame^] | 18 | template <class T, bool fail_no_goal> |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 19 | void |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame^] | 20 | ControlLoop<T, fail_no_goal>::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 | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame^] | 27 | template <class T, bool fail_no_goal> |
| 28 | void ControlLoop<T, fail_no_goal>::Iterate() { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 29 | no_prior_goal_.Print(); |
| 30 | no_sensor_generation_.Print(); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 31 | driver_station_old_.Print(); |
| 32 | no_driver_station_.Print(); |
Brian Silverman | 6a1cd21 | 2014-02-20 21:04:34 -0800 | [diff] [blame] | 33 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame^] | 34 | control_loop_->position.FetchAnother(); |
| 35 | const PositionType *const position = control_loop_->position.get(); |
| 36 | LOG_STRUCT(DEBUG, "position", *position); |
| 37 | |
| 38 | // Fetch the latest control loop goal. If there is no new |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 54 | sensor_generation.FetchLatest(); |
| 55 | if (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_ || |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 61 | sensor_generation->reader_pid != reader_pid_ || |
| 62 | 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", |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 64 | *sensor_generation.get()); |
Brian Silverman | 4910efb | 2014-02-17 11:10:10 -0800 | [diff] [blame] | 65 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 66 | reader_pid_ = sensor_generation->reader_pid; |
| 67 | cape_resets_ = sensor_generation->cape_resets; |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 76 | bool outputs_enabled = false; |
| 77 | |
| 78 | // Check to see if we got a driver station packet recently. |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 79 | if (::aos::robot_state.FetchLatest()) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 80 | outputs_enabled = true; |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 81 | } else if (::aos::robot_state.IsNewerThanMS(kDSPacketTimeoutMs)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 82 | outputs_enabled = true; |
| 83 | } else { |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 84 | if (::aos::robot_state.get()) { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 85 | LOG_INTERVAL(driver_station_old_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 86 | } else { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 87 | LOG_INTERVAL(no_driver_station_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 91 | ::aos::controls::output_check_received.FetchLatest(); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 92 | // True if we're enabled but the motors aren't working. |
Austin Schuh | 66a3d2f | 2014-10-21 22:24:00 -0700 | [diff] [blame] | 93 | // The 100ms is the result of using an oscilliscope to look at the PWM signal |
| 94 | // and output of a talon, and timing the delay between the last pulse and the |
| 95 | // talon turning off. |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 96 | const bool motors_off = |
| 97 | !::aos::controls::output_check_received.get() || |
| 98 | !::aos::controls::output_check_received.IsNewerThanMS(100); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 99 | motors_off_log_.Print(); |
| 100 | if (motors_off) { |
| 101 | if (!::aos::robot_state.get() || ::aos::robot_state->enabled) { |
| 102 | LOG_INTERVAL(motors_off_log_); |
| 103 | } |
| 104 | outputs_enabled = false; |
| 105 | } |
| 106 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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. |
Ben Fredrickson | 4283bb4 | 2014-02-22 08:31:50 +0000 | [diff] [blame] | 123 | RunIteration(goal, position, nullptr, status.get()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame^] | 131 | template <class T, bool fail_no_goal> |
| 132 | void ControlLoop<T, fail_no_goal>::Run() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 133 | while (true) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | Iterate(); |
| 135 | } |
| 136 | } |
| 137 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 138 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 139 | } // namespace aos |