blob: e9e827ce396678e531fc2038b5d6a8120fda86b8 [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"
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.
40const time::Time kLoopFrequency = time::Time::InSeconds(0.01);
41
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 Silverman10f997b2013-10-11 18:01:56 -070053template <class T, bool has_position = true, bool fail_no_position = true>
brians343bc112013-02-10 01:53:46 +000054class ControlLoop : public SerializableControlLoop {
55 public:
56 // Maximum age of position packets before the loop will be disabled due to
57 // invalid position data.
Austin Schuhfa033692013-02-24 01:00:55 -080058 static const int kPositionTimeoutMs = 1000;
brians343bc112013-02-10 01:53:46 +000059 // Maximum age of driver station packets before the loop will be disabled.
Austin Schuhfa033692013-02-24 01:00:55 -080060 static const int kDSPacketTimeoutMs = 500;
brians343bc112013-02-10 01:53:46 +000061
62 ControlLoop(T *control_loop) : control_loop_(control_loop) {}
63
64 // Create some convenient typedefs to reference the Goal, Position, Status,
65 // and Output structures.
66 typedef typename std::remove_reference<
67 decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type
68 GoalType;
69 typedef typename std::remove_reference<
70 decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type
71 PositionType;
72 typedef typename std::remove_reference<
73 decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type
74 StatusType;
75 typedef typename std::remove_reference<
76 decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type
77 OutputType;
78
79 // Constructs and sends a message on the output queue which will stop all the
80 // motors. Calls Zero to clear all the state.
81 void ZeroOutputs();
82
83 // Sets the output to zero.
84 // Over-ride if a value of zero is not "off" for this subsystem.
85 virtual void Zero(OutputType *output) { output->Zero(); }
86
87 // Runs the loop forever.
88 virtual void Run();
89
90 // Runs one cycle of the loop.
91 virtual void Iterate();
92
93 // Returns the name of the queue group.
94 const char *name() { return control_loop_->name(); }
95
96 // Methods to serialize all the data that should be sent over the network.
97 virtual size_t SeralizedSize() { return control_loop_->goal->Size(); }
98 virtual void Serialize(char *buffer) const {
99 control_loop_->goal->Serialize(buffer);
100 }
101 virtual void SerializeZeroMessage(char *buffer) const {
102 GoalType zero_goal;
103 zero_goal.Zero();
104 zero_goal.Serialize(buffer);
105 }
106
107 virtual void Deserialize(const char *buffer) {
108 ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage();
109 new_msg->Deserialize(buffer);
110 new_msg.Send();
111 }
112
113 virtual uint32_t UniqueID() { return control_loop_->hash(); }
114
115 protected:
116 // Runs an iteration of the control loop.
117 // goal is the last goal that was sent. It might be any number of cycles old.
118 // position is the current position, or NULL if we didn't get a position this
119 // cycle.
120 // output is the values to be sent to the motors. This is NULL if the output
121 // is going to be ignored and set to 0.
122 // status is the status of the control loop.
123 // Both output and status should be filled in by the implementation.
124 virtual void RunIteration(const GoalType *goal,
125 const PositionType *position,
126 OutputType *output,
127 StatusType *status) = 0;
128
129 private:
130 // Pointer to the queue group
131 T *control_loop_;
132};
133
134} // namespace control_loops
135} // namespace aos
136
137#include "aos/common/control_loop/ControlLoop-tmpl.h" // IWYU pragma: export
138
139#endif