blob: 0889b48772f9d3bdea7e30cf0a2feb5bdd037d66 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_CONTROL_LOOP_CONTROL_LOOP_H_
2#define AOS_CONTROL_LOOP_CONTROL_LOOP_H_
3
4#include <cstring>
5
brians343bc112013-02-10 01:53:46 +00006#include "aos/common/type_traits.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08007#include "aos/common/queue.h"
Brian Silverman3204dd82013-03-12 18:42:01 -07008#include "aos/common/time.h"
Brian Silverman50a9d032014-02-16 17:20:57 -08009#include "aos/common/util/log_interval.h"
brians343bc112013-02-10 01:53:46 +000010
11namespace aos {
12namespace control_loops {
13
14// Interface to describe runnable jobs.
15class Runnable {
16 public:
17 virtual ~Runnable() {}
18 // Runs forever.
19 virtual void Run() = 0;
20 // Does one quick piece of work and return. Does _not_ block.
21 virtual void Iterate() = 0;
22};
23
24class SerializableControlLoop : public Runnable {
25 public:
26 // Returns the size of all the data to be sent when serialized.
27 virtual size_t SeralizedSize() = 0;
28 // Serialize the current data.
29 virtual void Serialize(char *buffer) const = 0;
30 // Serialize zeroed data in case the data is out of date.
31 virtual void SerializeZeroMessage(char *buffer) const = 0;
32 // Deserialize data into the control loop.
33 virtual void Deserialize(const char *buffer) = 0;
34 // Unique identifier for the control loop.
35 // Most likely the hash of the queue group.
36 virtual uint32_t UniqueID() = 0;
37};
38
Brian Silverman3204dd82013-03-12 18:42:01 -070039// Control loops run this often, "starting" at time 0.
Brian Silverman1c3cfc02013-10-25 17:02:19 -070040constexpr time::Time kLoopFrequency = time::Time::InSeconds(0.01);
Brian Silverman3204dd82013-03-12 18:42:01 -070041
Brian Silverman15ca9852013-03-17 18:24:15 -070042// Calculates the next time to run control loops after start.
43time::Time NextLoopTime(time::Time start = time::Time::Now());
44
brians343bc112013-02-10 01:53:46 +000045// Provides helper methods to assist in writing control loops.
46// This template expects to be constructed with a queue group as an argument
47// that has a goal, position, status, and output queue.
48// It will then call the RunIteration method every cycle that it has enough
49// valid data for the control loop to run.
50// If has_position is false, the control loop will always use NULL as the
51// position and not check the queue. This is used for "loops" that control
52// motors open loop.
Brian Silverman71fbee02014-03-13 17:24:54 -070053template <class T, bool has_position = true, bool fail_no_position = true,
54 bool fail_no_goal = true>
brians343bc112013-02-10 01:53:46 +000055class ControlLoop : public SerializableControlLoop {
56 public:
57 // Maximum age of position packets before the loop will be disabled due to
58 // invalid position data.
Austin Schuhfa033692013-02-24 01:00:55 -080059 static const int kPositionTimeoutMs = 1000;
brians343bc112013-02-10 01:53:46 +000060 // Maximum age of driver station packets before the loop will be disabled.
Austin Schuhfa033692013-02-24 01:00:55 -080061 static const int kDSPacketTimeoutMs = 500;
brians343bc112013-02-10 01:53:46 +000062
Austin Schuh3d6e3df2014-02-17 01:51:03 -080063 ControlLoop(T *control_loop)
64 : control_loop_(control_loop),
65 reset_(true),
66 has_sensor_reset_counters_(false) {}
brians343bc112013-02-10 01:53:46 +000067 // Create some convenient typedefs to reference the Goal, Position, Status,
68 // and Output structures.
69 typedef typename std::remove_reference<
70 decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type
71 GoalType;
72 typedef typename std::remove_reference<
73 decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type
74 PositionType;
75 typedef typename std::remove_reference<
76 decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type
77 StatusType;
78 typedef typename std::remove_reference<
79 decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type
80 OutputType;
81
Brian Silvermand1e65b92014-03-08 17:07:14 -080082 // Constructs and sends a message on the output queue which sets everything to
83 // a safe state (generally motors off). For some subclasses, this will be a
84 // bit different (ie pistons).
85 // The implementation here creates a new Output message, calls Zero() on it,
86 // and then sends it.
87 virtual void ZeroOutputs();
brians343bc112013-02-10 01:53:46 +000088
Austin Schuh3d6e3df2014-02-17 01:51:03 -080089 // Returns true if the device reading the sensors reset and potentially lost
Ben Fredrickson4283bb42014-02-22 08:31:50 +000090 // track of encoder counts. Calling this read method clears the flag. After
91 // a reset, RunIteration will not be called until there is a valid position
92 // message.
Austin Schuh3d6e3df2014-02-17 01:51:03 -080093 bool reset() {
94 bool ans = reset_;
95 reset_ = false;
96 return ans;
97 }
98
brians343bc112013-02-10 01:53:46 +000099 // Sets the output to zero.
100 // Over-ride if a value of zero is not "off" for this subsystem.
101 virtual void Zero(OutputType *output) { output->Zero(); }
102
103 // Runs the loop forever.
104 virtual void Run();
105
106 // Runs one cycle of the loop.
107 virtual void Iterate();
108
109 // Returns the name of the queue group.
110 const char *name() { return control_loop_->name(); }
111
112 // Methods to serialize all the data that should be sent over the network.
113 virtual size_t SeralizedSize() { return control_loop_->goal->Size(); }
114 virtual void Serialize(char *buffer) const {
115 control_loop_->goal->Serialize(buffer);
116 }
117 virtual void SerializeZeroMessage(char *buffer) const {
118 GoalType zero_goal;
119 zero_goal.Zero();
120 zero_goal.Serialize(buffer);
121 }
122
123 virtual void Deserialize(const char *buffer) {
124 ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage();
125 new_msg->Deserialize(buffer);
126 new_msg.Send();
127 }
128
129 virtual uint32_t UniqueID() { return control_loop_->hash(); }
130
131 protected:
132 // Runs an iteration of the control loop.
133 // goal is the last goal that was sent. It might be any number of cycles old.
134 // position is the current position, or NULL if we didn't get a position this
135 // cycle.
136 // output is the values to be sent to the motors. This is NULL if the output
137 // is going to be ignored and set to 0.
138 // status is the status of the control loop.
139 // Both output and status should be filled in by the implementation.
140 virtual void RunIteration(const GoalType *goal,
141 const PositionType *position,
142 OutputType *output,
143 StatusType *status) = 0;
144
Brian Silvermand1e65b92014-03-08 17:07:14 -0800145 T *queue_group() { return control_loop_; }
146 const T *queue_group() const { return control_loop_; }
147
brians343bc112013-02-10 01:53:46 +0000148 private:
149 // Pointer to the queue group
150 T *control_loop_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800151
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800152 bool reset_;
153 bool has_sensor_reset_counters_;
154 int32_t reader_pid_;
155 uint32_t cape_resets_;
156
Brian Silverman50a9d032014-02-16 17:20:57 -0800157 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
158 static constexpr ::aos::time::Time kStaleLogInterval =
159 ::aos::time::Time::InSeconds(0.1);
160 SimpleLogInterval very_stale_position_ =
161 SimpleLogInterval(kStaleLogInterval, ERROR,
162 "outputs disabled because position is very stale");
163 SimpleLogInterval no_prior_goal_ =
164 SimpleLogInterval(kStaleLogInterval, ERROR,
165 "no prior goal");
166 SimpleLogInterval no_prior_position_ =
167 SimpleLogInterval(kStaleLogInterval, ERROR,
168 "no prior position");
169 SimpleLogInterval no_driver_station_ =
170 SimpleLogInterval(kStaleLogInterval, ERROR,
171 "no driver station packet");
172 SimpleLogInterval driver_station_old_ =
173 SimpleLogInterval(kStaleLogInterval, ERROR,
174 "driver station packet is too old");
Brian Silverman4910efb2014-02-17 11:10:10 -0800175 SimpleLogInterval no_sensor_generation_ =
176 SimpleLogInterval(kStaleLogInterval, ERROR,
177 "no sensor_generation message");
brians343bc112013-02-10 01:53:46 +0000178};
179
180} // namespace control_loops
181} // namespace aos
182
Briana6553ed2014-04-02 21:26:46 -0700183#include "aos/common/controls/control_loop-tmpl.h" // IWYU pragma: export
brians343bc112013-02-10 01:53:46 +0000184
185#endif