blob: f1109e6575bba08485de11b8ad3061e076b9217b [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
Ben Fredrickson4283bb42014-02-22 08:31:50 +000087 // track of encoder counts. Calling this read method clears the flag. After
88 // a reset, RunIteration will not be called until there is a valid position
89 // message.
Austin Schuh3d6e3df2014-02-17 01:51:03 -080090 bool reset() {
91 bool ans = reset_;
92 reset_ = false;
93 return ans;
94 }
95
brians343bc112013-02-10 01:53:46 +000096 // Sets the output to zero.
97 // Over-ride if a value of zero is not "off" for this subsystem.
98 virtual void Zero(OutputType *output) { output->Zero(); }
99
100 // Runs the loop forever.
101 virtual void Run();
102
103 // Runs one cycle of the loop.
104 virtual void Iterate();
105
106 // Returns the name of the queue group.
107 const char *name() { return control_loop_->name(); }
108
109 // Methods to serialize all the data that should be sent over the network.
110 virtual size_t SeralizedSize() { return control_loop_->goal->Size(); }
111 virtual void Serialize(char *buffer) const {
112 control_loop_->goal->Serialize(buffer);
113 }
114 virtual void SerializeZeroMessage(char *buffer) const {
115 GoalType zero_goal;
116 zero_goal.Zero();
117 zero_goal.Serialize(buffer);
118 }
119
120 virtual void Deserialize(const char *buffer) {
121 ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage();
122 new_msg->Deserialize(buffer);
123 new_msg.Send();
124 }
125
126 virtual uint32_t UniqueID() { return control_loop_->hash(); }
127
128 protected:
129 // Runs an iteration of the control loop.
130 // goal is the last goal that was sent. It might be any number of cycles old.
131 // position is the current position, or NULL if we didn't get a position this
132 // cycle.
133 // output is the values to be sent to the motors. This is NULL if the output
134 // is going to be ignored and set to 0.
135 // status is the status of the control loop.
136 // Both output and status should be filled in by the implementation.
137 virtual void RunIteration(const GoalType *goal,
138 const PositionType *position,
139 OutputType *output,
140 StatusType *status) = 0;
141
142 private:
143 // Pointer to the queue group
144 T *control_loop_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800145
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800146 bool reset_;
147 bool has_sensor_reset_counters_;
148 int32_t reader_pid_;
149 uint32_t cape_resets_;
150
Brian Silverman50a9d032014-02-16 17:20:57 -0800151 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
152 static constexpr ::aos::time::Time kStaleLogInterval =
153 ::aos::time::Time::InSeconds(0.1);
154 SimpleLogInterval very_stale_position_ =
155 SimpleLogInterval(kStaleLogInterval, ERROR,
156 "outputs disabled because position is very stale");
157 SimpleLogInterval no_prior_goal_ =
158 SimpleLogInterval(kStaleLogInterval, ERROR,
159 "no prior goal");
160 SimpleLogInterval no_prior_position_ =
161 SimpleLogInterval(kStaleLogInterval, ERROR,
162 "no prior position");
163 SimpleLogInterval no_driver_station_ =
164 SimpleLogInterval(kStaleLogInterval, ERROR,
165 "no driver station packet");
166 SimpleLogInterval driver_station_old_ =
167 SimpleLogInterval(kStaleLogInterval, ERROR,
168 "driver station packet is too old");
Brian Silverman4910efb2014-02-17 11:10:10 -0800169 SimpleLogInterval no_sensor_generation_ =
170 SimpleLogInterval(kStaleLogInterval, ERROR,
171 "no sensor_generation message");
brians343bc112013-02-10 01:53:46 +0000172};
173
174} // namespace control_loops
175} // namespace aos
176
177#include "aos/common/control_loop/ControlLoop-tmpl.h" // IWYU pragma: export
178
179#endif