blob: c9e33810a54d9a167844c468aeeb85368cbe3aa7 [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/control_loop/Timing.h"
brians343bc112013-02-10 01:53:46 +00007#include "aos/common/type_traits.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08008#include "aos/common/queue.h"
Brian Silverman3204dd82013-03-12 18:42:01 -07009#include "aos/common/time.h"
Brian Silverman50a9d032014-02-16 17:20:57 -080010#include "aos/common/util/log_interval.h"
brians343bc112013-02-10 01:53:46 +000011
12namespace aos {
13namespace control_loops {
14
15// Interface to describe runnable jobs.
16class Runnable {
17 public:
18 virtual ~Runnable() {}
19 // Runs forever.
20 virtual void Run() = 0;
21 // Does one quick piece of work and return. Does _not_ block.
22 virtual void Iterate() = 0;
23};
24
25class SerializableControlLoop : public Runnable {
26 public:
27 // Returns the size of all the data to be sent when serialized.
28 virtual size_t SeralizedSize() = 0;
29 // Serialize the current data.
30 virtual void Serialize(char *buffer) const = 0;
31 // Serialize zeroed data in case the data is out of date.
32 virtual void SerializeZeroMessage(char *buffer) const = 0;
33 // Deserialize data into the control loop.
34 virtual void Deserialize(const char *buffer) = 0;
35 // Unique identifier for the control loop.
36 // Most likely the hash of the queue group.
37 virtual uint32_t UniqueID() = 0;
38};
39
Brian Silverman3204dd82013-03-12 18:42:01 -070040// Control loops run this often, "starting" at time 0.
Brian Silverman1c3cfc02013-10-25 17:02:19 -070041constexpr time::Time kLoopFrequency = time::Time::InSeconds(0.01);
Brian Silverman3204dd82013-03-12 18:42:01 -070042
Brian Silverman15ca9852013-03-17 18:24:15 -070043// Calculates the next time to run control loops after start.
44time::Time NextLoopTime(time::Time start = time::Time::Now());
45
brians343bc112013-02-10 01:53:46 +000046// Provides helper methods to assist in writing control loops.
47// This template expects to be constructed with a queue group as an argument
48// that has a goal, position, status, and output queue.
49// It will then call the RunIteration method every cycle that it has enough
50// valid data for the control loop to run.
51// If has_position is false, the control loop will always use NULL as the
52// position and not check the queue. This is used for "loops" that control
53// motors open loop.
Brian Silverman10f997b2013-10-11 18:01:56 -070054template <class T, bool has_position = true, bool fail_no_position = 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
82 // Constructs and sends a message on the output queue which will stop all the
83 // motors. Calls Zero to clear all the state.
84 void ZeroOutputs();
85
Austin Schuh3d6e3df2014-02-17 01:51:03 -080086 // Returns true if the device reading the sensors reset and potentially lost
87 // track of encoder counts. Calling this read method clears the flag.
88 bool reset() {
89 bool ans = reset_;
90 reset_ = false;
91 return ans;
92 }
93
brians343bc112013-02-10 01:53:46 +000094 // Sets the output to zero.
95 // Over-ride if a value of zero is not "off" for this subsystem.
96 virtual void Zero(OutputType *output) { output->Zero(); }
97
98 // Runs the loop forever.
99 virtual void Run();
100
101 // Runs one cycle of the loop.
102 virtual void Iterate();
103
104 // Returns the name of the queue group.
105 const char *name() { return control_loop_->name(); }
106
107 // Methods to serialize all the data that should be sent over the network.
108 virtual size_t SeralizedSize() { return control_loop_->goal->Size(); }
109 virtual void Serialize(char *buffer) const {
110 control_loop_->goal->Serialize(buffer);
111 }
112 virtual void SerializeZeroMessage(char *buffer) const {
113 GoalType zero_goal;
114 zero_goal.Zero();
115 zero_goal.Serialize(buffer);
116 }
117
118 virtual void Deserialize(const char *buffer) {
119 ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage();
120 new_msg->Deserialize(buffer);
121 new_msg.Send();
122 }
123
124 virtual uint32_t UniqueID() { return control_loop_->hash(); }
125
126 protected:
127 // Runs an iteration of the control loop.
128 // goal is the last goal that was sent. It might be any number of cycles old.
129 // position is the current position, or NULL if we didn't get a position this
130 // cycle.
131 // output is the values to be sent to the motors. This is NULL if the output
132 // is going to be ignored and set to 0.
133 // status is the status of the control loop.
134 // Both output and status should be filled in by the implementation.
135 virtual void RunIteration(const GoalType *goal,
136 const PositionType *position,
137 OutputType *output,
138 StatusType *status) = 0;
139
140 private:
141 // Pointer to the queue group
142 T *control_loop_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800143
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800144 bool reset_;
145 bool has_sensor_reset_counters_;
146 int32_t reader_pid_;
147 uint32_t cape_resets_;
148
Brian Silverman50a9d032014-02-16 17:20:57 -0800149 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
150 static constexpr ::aos::time::Time kStaleLogInterval =
151 ::aos::time::Time::InSeconds(0.1);
152 SimpleLogInterval very_stale_position_ =
153 SimpleLogInterval(kStaleLogInterval, ERROR,
154 "outputs disabled because position is very stale");
155 SimpleLogInterval no_prior_goal_ =
156 SimpleLogInterval(kStaleLogInterval, ERROR,
157 "no prior goal");
158 SimpleLogInterval no_prior_position_ =
159 SimpleLogInterval(kStaleLogInterval, ERROR,
160 "no prior position");
161 SimpleLogInterval no_driver_station_ =
162 SimpleLogInterval(kStaleLogInterval, ERROR,
163 "no driver station packet");
164 SimpleLogInterval driver_station_old_ =
165 SimpleLogInterval(kStaleLogInterval, ERROR,
166 "driver station packet is too old");
brians343bc112013-02-10 01:53:46 +0000167};
168
169} // namespace control_loops
170} // namespace aos
171
172#include "aos/common/control_loop/ControlLoop-tmpl.h" // IWYU pragma: export
173
174#endif