blob: 142da9f54ab352e1edf6846a6384af8636d2ec55 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <stddef.h>
2
3#include "aos/common/logging/logging.h"
Brian Silverman2ac0fbc2014-03-20 19:45:13 -07004#include "aos/common/messages/robot_state.q.h"
Brian Silvermand6974f42014-02-14 13:39:21 -08005#include "aos/common/logging/queue_logging.h"
Brian3afd6fc2014-04-02 20:41:49 -07006#include "aos/common/util/phased_loop.h"
Brian Silverman38111502014-04-10 12:36:26 -07007#include "aos/common/controls/sensor_generation.q.h"
8#include "aos/common/controls/output_check.q.h"
Austin Schuh3d6e3df2014-02-17 01:51:03 -08009
brians343bc112013-02-10 01:53:46 +000010namespace aos {
Brian Silverman38111502014-04-10 12:36:26 -070011namespace controls {
brians343bc112013-02-10 01:53:46 +000012
13// TODO(aschuh): Tests.
14
Brian Silverman71fbee02014-03-13 17:24:54 -070015template <class T, bool has_position, bool fail_no_position, bool fail_no_goal>
16constexpr ::aos::time::Time ControlLoop<T, has_position, fail_no_position,
17 fail_no_goal>::kStaleLogInterval;
Brian Silverman50a9d032014-02-16 17:20:57 -080018
Brian Silverman71fbee02014-03-13 17:24:54 -070019template <class T, bool has_position, bool fail_no_position, bool fail_no_goal>
20void
21ControlLoop<T, has_position, fail_no_position, fail_no_goal>::ZeroOutputs() {
brians343bc112013-02-10 01:53:46 +000022 aos::ScopedMessagePtr<OutputType> output =
23 control_loop_->output.MakeMessage();
24 Zero(output.get());
25 output.Send();
26}
27
Brian Silverman71fbee02014-03-13 17:24:54 -070028template <class T, bool has_position, bool fail_no_position, bool fail_no_goal>
29void ControlLoop<T, has_position, fail_no_position, fail_no_goal>::Iterate() {
Brian Silverman9c9ab422014-02-25 13:42:53 -080030 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 Silverman6a1cd212014-02-20 21:04:34 -080036
brians343bc112013-02-10 01:53:46 +000037 // 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 Silverman9c9ab422014-02-25 13:42:53 -080046 LOG_INTERVAL(no_prior_goal_);
Brian Silverman71fbee02014-03-13 17:24:54 -070047 if (fail_no_goal) {
48 ZeroOutputs();
49 return;
50 }
brians343bc112013-02-10 01:53:46 +000051 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080052
Brian Silverman38111502014-04-10 12:36:26 -070053 sensor_generation.FetchLatest();
54 if (sensor_generation.get() == nullptr) {
Brian Silverman9c9ab422014-02-25 13:42:53 -080055 LOG_INTERVAL(no_sensor_generation_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080056 ZeroOutputs();
57 return;
58 }
59 if (!has_sensor_reset_counters_ ||
Brian Silverman38111502014-04-10 12:36:26 -070060 sensor_generation->reader_pid != reader_pid_ ||
61 sensor_generation->cape_resets != cape_resets_) {
Brian Silverman71fbee02014-03-13 17:24:54 -070062 LOG_STRUCT(INFO, "new sensor_generation message",
Brian Silverman38111502014-04-10 12:36:26 -070063 *sensor_generation.get());
Brian Silverman4910efb2014-02-17 11:10:10 -080064
Brian Silverman38111502014-04-10 12:36:26 -070065 reader_pid_ = sensor_generation->reader_pid;
66 cape_resets_ = sensor_generation->cape_resets;
Austin Schuh3d6e3df2014-02-17 01:51:03 -080067 has_sensor_reset_counters_ = true;
68 reset_ = true;
69 }
Brian Silverman71fbee02014-03-13 17:24:54 -070070
71 if (goal) {
72 LOG_STRUCT(DEBUG, "goal", *goal);
73 }
brians343bc112013-02-10 01:53:46 +000074
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 Fredrickson4283bb42014-02-22 08:31:50 +000085 if (control_loop_->position.get() && !reset_) {
brians343bc112013-02-10 01:53:46 +000086 int msec_age = control_loop_->position.Age().ToMSec();
87 if (!control_loop_->position.IsNewerThanMS(kPositionTimeoutMs)) {
Brian Silverman9c9ab422014-02-25 13:42:53 -080088 LOG_INTERVAL(very_stale_position_);
brians343bc112013-02-10 01:53:46 +000089 ZeroOutputs();
90 return;
91 } else {
Brian Silverman50a9d032014-02-16 17:20:57 -080092 LOG(ERROR, "Stale position. %d ms (< %d ms)\n", msec_age,
93 kPositionTimeoutMs);
brians343bc112013-02-10 01:53:46 +000094 }
95 } else {
Brian Silverman9c9ab422014-02-25 13:42:53 -080096 LOG_INTERVAL(no_prior_position_);
Brian Silverman10f997b2013-10-11 18:01:56 -070097 if (fail_no_position) {
98 ZeroOutputs();
99 return;
100 }
brians343bc112013-02-10 01:53:46 +0000101 }
102 }
Austin Schuhfa033692013-02-24 01:00:55 -0800103 if (position) {
Brian Silvermand6974f42014-02-14 13:39:21 -0800104 LOG_STRUCT(DEBUG, "position", *position);
Austin Schuhfa033692013-02-24 01:00:55 -0800105 }
brians343bc112013-02-10 01:53:46 +0000106 }
107
108 bool outputs_enabled = false;
109
110 // Check to see if we got a driver station packet recently.
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800111 if (::aos::robot_state.FetchLatest()) {
brians343bc112013-02-10 01:53:46 +0000112 outputs_enabled = true;
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800113 } else if (::aos::robot_state.IsNewerThanMS(kDSPacketTimeoutMs)) {
brians343bc112013-02-10 01:53:46 +0000114 outputs_enabled = true;
115 } else {
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800116 if (::aos::robot_state.get()) {
Brian Silverman9c9ab422014-02-25 13:42:53 -0800117 LOG_INTERVAL(driver_station_old_);
brians343bc112013-02-10 01:53:46 +0000118 } else {
Brian Silverman9c9ab422014-02-25 13:42:53 -0800119 LOG_INTERVAL(no_driver_station_);
brians343bc112013-02-10 01:53:46 +0000120 }
121 }
122
Brian Silverman38111502014-04-10 12:36:26 -0700123 ::aos::controls::output_check_received.FetchLatest();
Brian Silverman2704ecf2014-04-09 20:24:03 -0700124 // True if we're enabled but the motors aren't working.
Austin Schuh66a3d2f2014-10-21 22:24:00 -0700125 // 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 Silverman38111502014-04-10 12:36:26 -0700128 const bool motors_off =
129 !::aos::controls::output_check_received.get() ||
130 !::aos::controls::output_check_received.IsNewerThanMS(100);
Brian Silverman2704ecf2014-04-09 20:24:03 -0700131 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
brians343bc112013-02-10 01:53:46 +0000139 // 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 Silvermand6974f42014-02-14 13:39:21 -0800151 LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +0000152 output.Send();
153 } else {
154 // The outputs are disabled, so pass NULL in for the output.
Ben Fredrickson4283bb42014-02-22 08:31:50 +0000155 RunIteration(goal, position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +0000156 ZeroOutputs();
157 }
158
Brian Silvermand6974f42014-02-14 13:39:21 -0800159 LOG_STRUCT(DEBUG, "status", *status);
brians343bc112013-02-10 01:53:46 +0000160 status.Send();
161}
162
Brian Silverman71fbee02014-03-13 17:24:54 -0700163template <class T, bool has_position, bool fail_no_position, bool fail_no_goal>
164void ControlLoop<T, has_position, fail_no_position, fail_no_goal>::Run() {
Brian Silvermanb407c672014-04-09 11:58:37 -0700165 ::aos::time::Time::EnableMockTime();
brians343bc112013-02-10 01:53:46 +0000166 while (true) {
Brian Silvermanb407c672014-04-09 11:58:37 -0700167 ::aos::time::Time::UpdateMockTime();
168 const ::aos::time::Time next_loop = NextLoopTime();
169 time::SleepUntil(next_loop);
170 ::aos::time::Time::SetMockTime(next_loop);
brians343bc112013-02-10 01:53:46 +0000171 Iterate();
172 }
173}
174
Brian Silverman38111502014-04-10 12:36:26 -0700175} // namespace controls
brians343bc112013-02-10 01:53:46 +0000176} // namespace aos