blob: 02aa0677f84f180142e8d35bb6139e8778d99c26 [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
Brian Silvermanb407c672014-04-09 11:58:37 -07004#include <string.h>
brians343bc112013-02-10 01:53:46 +00005
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 {
Brian Silverman38111502014-04-10 12:36:26 -070012namespace controls {
brians343bc112013-02-10 01:53:46 +000013
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.
Austin Schuh214e9c12016-11-25 17:26:20 -080040constexpr ::std::chrono::nanoseconds kLoopFrequency =
41 ::std::chrono::milliseconds(5);
Brian Silverman3204dd82013-03-12 18:42:01 -070042
brians343bc112013-02-10 01:53:46 +000043// Provides helper methods to assist in writing control loops.
44// This template expects to be constructed with a queue group as an argument
45// that has a goal, position, status, and output queue.
46// It will then call the RunIteration method every cycle that it has enough
47// valid data for the control loop to run.
Brian Silverman089f5812015-02-15 01:58:19 -050048template <class T>
brians343bc112013-02-10 01:53:46 +000049class ControlLoop : public SerializableControlLoop {
50 public:
brians343bc112013-02-10 01:53:46 +000051 // Create some convenient typedefs to reference the Goal, Position, Status,
52 // and Output structures.
53 typedef typename std::remove_reference<
54 decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type
55 GoalType;
56 typedef typename std::remove_reference<
57 decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type
58 PositionType;
59 typedef typename std::remove_reference<
60 decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type
61 StatusType;
62 typedef typename std::remove_reference<
63 decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type
64 OutputType;
65
Brian Silverman699f0cb2015-02-05 19:45:01 -050066 ControlLoop(T *control_loop) : control_loop_(control_loop) {}
67
68 // Returns true if all the counters etc in the sensor data have been reset.
69 // This will return true only a single time per reset.
70 bool WasReset() {
71 if (reset_) {
72 reset_ = false;
73 return true;
74 } else {
75 return false;
76 }
77 }
78
Brian Silvermand1e65b92014-03-08 17:07:14 -080079 // Constructs and sends a message on the output queue which sets everything to
80 // a safe state (generally motors off). For some subclasses, this will be a
81 // bit different (ie pistons).
82 // The implementation here creates a new Output message, calls Zero() on it,
83 // and then sends it.
84 virtual void ZeroOutputs();
brians343bc112013-02-10 01:53:46 +000085
86 // Sets the output to zero.
87 // Over-ride if a value of zero is not "off" for this subsystem.
88 virtual void Zero(OutputType *output) { output->Zero(); }
89
90 // Runs the loop forever.
Brian Silverman699f0cb2015-02-05 19:45:01 -050091 void Run() override;
brians343bc112013-02-10 01:53:46 +000092
93 // Runs one cycle of the loop.
Brian Silverman699f0cb2015-02-05 19:45:01 -050094 void Iterate() override;
brians343bc112013-02-10 01:53:46 +000095
96 // Returns the name of the queue group.
97 const char *name() { return control_loop_->name(); }
98
99 // Methods to serialize all the data that should be sent over the network.
Brian Silverman699f0cb2015-02-05 19:45:01 -0500100 size_t SeralizedSize() override { return control_loop_->goal->Size(); }
101 void Serialize(char *buffer) const override {
brians343bc112013-02-10 01:53:46 +0000102 control_loop_->goal->Serialize(buffer);
103 }
Brian Silverman699f0cb2015-02-05 19:45:01 -0500104 void SerializeZeroMessage(char *buffer) const override {
brians343bc112013-02-10 01:53:46 +0000105 GoalType zero_goal;
106 zero_goal.Zero();
107 zero_goal.Serialize(buffer);
108 }
109
Brian Silverman699f0cb2015-02-05 19:45:01 -0500110 void Deserialize(const char *buffer) override {
brians343bc112013-02-10 01:53:46 +0000111 ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage();
112 new_msg->Deserialize(buffer);
113 new_msg.Send();
114 }
115
Brian Silverman699f0cb2015-02-05 19:45:01 -0500116 uint32_t UniqueID() override { return control_loop_->hash(); }
brians343bc112013-02-10 01:53:46 +0000117
118 protected:
119 // Runs an iteration of the control loop.
Brian Silverman089f5812015-02-15 01:58:19 -0500120 // goal is the last goal that was sent. It might be any number of cycles old
121 // or nullptr if we haven't ever received a goal.
122 // position is the current position, or nullptr if we didn't get a position
123 // this cycle.
124 // output is the values to be sent to the motors. This is nullptr if the
125 // output is going to be ignored and set to 0.
brians343bc112013-02-10 01:53:46 +0000126 // status is the status of the control loop.
127 // Both output and status should be filled in by the implementation.
128 virtual void RunIteration(const GoalType *goal,
129 const PositionType *position,
130 OutputType *output,
131 StatusType *status) = 0;
132
Brian Silvermand1e65b92014-03-08 17:07:14 -0800133 T *queue_group() { return control_loop_; }
134 const T *queue_group() const { return control_loop_; }
135
brians343bc112013-02-10 01:53:46 +0000136 private:
Austin Schuh61bdc602016-12-04 19:10:10 -0800137 static constexpr ::std::chrono::milliseconds kStaleLogInterval =
138 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500139 // The amount of time after the last PWM pulse we consider motors enabled for.
140 // 100ms is the result of using an oscilliscope to look at the input and
141 // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX,
142 // and Victor SP.
Austin Schuh61bdc602016-12-04 19:10:10 -0800143 static constexpr ::std::chrono::milliseconds kPwmDisableTime =
144 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500145
brians343bc112013-02-10 01:53:46 +0000146 // Pointer to the queue group
147 T *control_loop_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800148
Brian Silverman699f0cb2015-02-05 19:45:01 -0500149 bool reset_ = false;
150 int32_t sensor_reader_pid_ = 0;
151
Austin Schuh61bdc602016-12-04 19:10:10 -0800152 ::aos::monotonic_clock::time_point last_pwm_sent_ =
153 ::aos::monotonic_clock::min_time;
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800154
Brian Silverman50a9d032014-02-16 17:20:57 -0800155 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500156 SimpleLogInterval no_sensor_state_ =
157 SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state");
Brian Silverman2704ecf2014-04-09 20:24:03 -0700158 SimpleLogInterval motors_off_log_ =
159 SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled");
Brian Silverman699f0cb2015-02-05 19:45:01 -0500160 SimpleLogInterval no_goal_ =
161 SimpleLogInterval(kStaleLogInterval, ERROR, "no goal");
brians343bc112013-02-10 01:53:46 +0000162};
163
Brian Silverman38111502014-04-10 12:36:26 -0700164} // namespace controls
brians343bc112013-02-10 01:53:46 +0000165} // namespace aos
166
Briana6553ed2014-04-02 21:26:46 -0700167#include "aos/common/controls/control_loop-tmpl.h" // IWYU pragma: export
brians343bc112013-02-10 01:53:46 +0000168
169#endif