blob: 018a2f5e9cf4d759ec9990d796886e3325cb9581 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <stddef.h>
2
3#include "aos/common/logging/logging.h"
4#include "aos/common/control_loop/Timing.h"
5#include "aos/common/messages/RobotState.q.h"
6
7namespace aos {
8namespace control_loops {
9
10// TODO(aschuh): Tests.
11
12template <class T, bool has_position>
13void ControlLoop<T, has_position>::ZeroOutputs() {
14 aos::ScopedMessagePtr<OutputType> output =
15 control_loop_->output.MakeMessage();
16 Zero(output.get());
17 output.Send();
18}
19
20template <class T, bool has_position>
21void ControlLoop<T, has_position>::Iterate() {
22 // Temporary storage for printing out inputs and outputs.
23 char state[1024];
24
25 // Fetch the latest control loop goal and position. If there is no new
26 // goal, we will just reuse the old one.
27 // If there is no goal, we haven't started up fully. It isn't worth
28 // the added complexity for each loop implementation to handle that case.
29 control_loop_->goal.FetchLatest();
30 // TODO(aschuh): Check the age here if we want the loop to stop on old
31 // goals.
32 const GoalType *goal = control_loop_->goal.get();
33 if (goal == NULL) {
34 LOG(ERROR, "No prior control loop goal.\n");
35 ZeroOutputs();
36 return;
37 }
38 goal->Print(state, sizeof(state));
39 LOG(DEBUG, "goal={%s}\n", state);
40
41 // Only pass in a position if we got one this cycle.
42 const PositionType *position = NULL;
43
44 // Only fetch the latest position if we have one.
45 if (has_position) {
46 // If the position is stale, this is really bad. Try fetching a position
47 // and check how fresh it is, and then take the appropriate action.
48 if (control_loop_->position.FetchLatest()) {
49 position = control_loop_->position.get();
50 } else {
51 if (control_loop_->position.get()) {
52 int msec_age = control_loop_->position.Age().ToMSec();
53 if (!control_loop_->position.IsNewerThanMS(kPositionTimeoutMs)) {
54 LOG(ERROR, "Stale position. %d ms > %d ms. Outputs disabled.\n",
55 msec_age, kPositionTimeoutMs);
56 ZeroOutputs();
57 return;
58 } else {
59 LOG(ERROR, "Stale position. %d ms\n", msec_age);
60 }
61 } else {
62 LOG(ERROR, "Never had a position.\n");
63 ZeroOutputs();
64 return;
65 }
66 }
Austin Schuhfa033692013-02-24 01:00:55 -080067 if (position) {
68 position->Print(state, sizeof(state));
69 LOG(DEBUG, "position={%s}\n", state);
70 }
brians343bc112013-02-10 01:53:46 +000071 }
72
73 bool outputs_enabled = false;
74
75 // Check to see if we got a driver station packet recently.
76 if (aos::robot_state.FetchLatest()) {
77 outputs_enabled = true;
78 } else if (aos::robot_state.IsNewerThanMS(kDSPacketTimeoutMs)) {
79 outputs_enabled = true;
80 } else {
81 if (aos::robot_state.get()) {
82 int msec_age = aos::robot_state.Age().ToMSec();
83 LOG(ERROR, "Driver Station packet is too old (%d ms).\n", msec_age);
84 } else {
85 LOG(ERROR, "No Driver Station packet.\n");
86 }
87 }
88
89 // Run the iteration.
90 aos::ScopedMessagePtr<StatusType> status =
91 control_loop_->status.MakeMessage();
92 if (status.get() == NULL) {
93 return;
94 }
95
96 if (outputs_enabled) {
97 aos::ScopedMessagePtr<OutputType> output =
98 control_loop_->output.MakeMessage();
99 RunIteration(goal, position, output.get(), status.get());
100
101 output->Print(state, sizeof(state));
102 LOG(DEBUG, "output={%s}\n", state);
103 output.Send();
104 } else {
105 // The outputs are disabled, so pass NULL in for the output.
106 RunIteration(goal, position, NULL, status.get());
107 ZeroOutputs();
108 }
109
110 status->Print(state, sizeof(state));
111 LOG(DEBUG, "status={%s}\n", state);
112 status.Send();
113}
114
115template <class T, bool has_position>
116void ControlLoop<T, has_position>::Run() {
117 while (true) {
Brian Silverman3204dd82013-03-12 18:42:01 -0700118 ::aos::time::PhasedLoopXMS(kLoopFrequency.ToMSec(), 0);
brians343bc112013-02-10 01:53:46 +0000119 Iterate();
120 }
121}
122
123} // namespace control_loops
124} // namespace aos