blob: 335a1bbaa7fe5acb55736cf9227bd079ded45d07 [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>
Austin Schuhb58ceb62017-02-05 14:21:57 -08005#include <atomic>
brians343bc112013-02-10 01:53:46 +00006
John Park33858a32018-09-28 23:05:48 -07007#include "aos/queue.h"
8#include "aos/time/time.h"
9#include "aos/type_traits/type_traits.h"
10#include "aos/util/log_interval.h"
brians343bc112013-02-10 01:53:46 +000011
12namespace aos {
Brian Silverman38111502014-04-10 12:36:26 -070013namespace controls {
brians343bc112013-02-10 01:53:46 +000014
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
Brian Silverman3204dd82013-03-12 18:42:01 -070025// Control loops run this often, "starting" at time 0.
Austin Schuh214e9c12016-11-25 17:26:20 -080026constexpr ::std::chrono::nanoseconds kLoopFrequency =
27 ::std::chrono::milliseconds(5);
Brian Silverman3204dd82013-03-12 18:42:01 -070028
brians343bc112013-02-10 01:53:46 +000029// Provides helper methods to assist in writing control loops.
30// This template expects to be constructed with a queue group as an argument
31// that has a goal, position, status, and output queue.
32// It will then call the RunIteration method every cycle that it has enough
33// valid data for the control loop to run.
Brian Silverman089f5812015-02-15 01:58:19 -050034template <class T>
Austin Schuhfd6e16c2019-01-27 12:22:47 -080035class ControlLoop : public Runnable {
brians343bc112013-02-10 01:53:46 +000036 public:
brians343bc112013-02-10 01:53:46 +000037 // Create some convenient typedefs to reference the Goal, Position, Status,
38 // and Output structures.
39 typedef typename std::remove_reference<
40 decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type
41 GoalType;
42 typedef typename std::remove_reference<
43 decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type
44 PositionType;
45 typedef typename std::remove_reference<
46 decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type
47 StatusType;
48 typedef typename std::remove_reference<
49 decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type
50 OutputType;
51
Brian Silverman699f0cb2015-02-05 19:45:01 -050052 ControlLoop(T *control_loop) : control_loop_(control_loop) {}
53
54 // Returns true if all the counters etc in the sensor data have been reset.
55 // This will return true only a single time per reset.
56 bool WasReset() {
57 if (reset_) {
58 reset_ = false;
59 return true;
60 } else {
61 return false;
62 }
63 }
64
Brian Silvermand1e65b92014-03-08 17:07:14 -080065 // Constructs and sends a message on the output queue which sets everything to
Austin Schuhaebfb4f2019-01-27 13:26:38 -080066 // a safe state. Default is to set everything to zero. Override Zero below
67 // to change that behavior.
68 void ZeroOutputs();
brians343bc112013-02-10 01:53:46 +000069
70 // Sets the output to zero.
Austin Schuhaebfb4f2019-01-27 13:26:38 -080071 // Override this if a value of zero (or false) is not "off" for this
72 // subsystem.
brians343bc112013-02-10 01:53:46 +000073 virtual void Zero(OutputType *output) { output->Zero(); }
74
75 // Runs the loop forever.
Brian Silverman699f0cb2015-02-05 19:45:01 -050076 void Run() override;
brians343bc112013-02-10 01:53:46 +000077
78 // Runs one cycle of the loop.
Brian Silverman699f0cb2015-02-05 19:45:01 -050079 void Iterate() override;
brians343bc112013-02-10 01:53:46 +000080
brians343bc112013-02-10 01:53:46 +000081 protected:
Austin Schuhb58ceb62017-02-05 14:21:57 -080082 static void Quit(int /*signum*/) {
83 run_ = false;
84 }
85
brians343bc112013-02-10 01:53:46 +000086 // Runs an iteration of the control loop.
Brian Silverman089f5812015-02-15 01:58:19 -050087 // goal is the last goal that was sent. It might be any number of cycles old
88 // or nullptr if we haven't ever received a goal.
89 // position is the current position, or nullptr if we didn't get a position
90 // this cycle.
91 // output is the values to be sent to the motors. This is nullptr if the
92 // output is going to be ignored and set to 0.
brians343bc112013-02-10 01:53:46 +000093 // status is the status of the control loop.
94 // Both output and status should be filled in by the implementation.
95 virtual void RunIteration(const GoalType *goal,
96 const PositionType *position,
97 OutputType *output,
98 StatusType *status) = 0;
99
100 private:
Austin Schuh61bdc602016-12-04 19:10:10 -0800101 static constexpr ::std::chrono::milliseconds kStaleLogInterval =
102 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500103 // The amount of time after the last PWM pulse we consider motors enabled for.
104 // 100ms is the result of using an oscilliscope to look at the input and
105 // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX,
106 // and Victor SP.
Austin Schuh61bdc602016-12-04 19:10:10 -0800107 static constexpr ::std::chrono::milliseconds kPwmDisableTime =
108 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500109
brians343bc112013-02-10 01:53:46 +0000110 // Pointer to the queue group
111 T *control_loop_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800112
Brian Silverman699f0cb2015-02-05 19:45:01 -0500113 bool reset_ = false;
114 int32_t sensor_reader_pid_ = 0;
115
Austin Schuh61bdc602016-12-04 19:10:10 -0800116 ::aos::monotonic_clock::time_point last_pwm_sent_ =
117 ::aos::monotonic_clock::min_time;
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800118
Brian Silverman50a9d032014-02-16 17:20:57 -0800119 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500120 SimpleLogInterval no_sensor_state_ =
121 SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state");
Brian Silverman2704ecf2014-04-09 20:24:03 -0700122 SimpleLogInterval motors_off_log_ =
123 SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled");
Brian Silverman699f0cb2015-02-05 19:45:01 -0500124 SimpleLogInterval no_goal_ =
125 SimpleLogInterval(kStaleLogInterval, ERROR, "no goal");
Austin Schuhb58ceb62017-02-05 14:21:57 -0800126
127 static ::std::atomic<bool> run_;
brians343bc112013-02-10 01:53:46 +0000128};
129
Brian Silverman38111502014-04-10 12:36:26 -0700130} // namespace controls
brians343bc112013-02-10 01:53:46 +0000131} // namespace aos
132
John Park33858a32018-09-28 23:05:48 -0700133#include "aos/controls/control_loop-tmpl.h" // IWYU pragma: export
brians343bc112013-02-10 01:53:46 +0000134
135#endif