blob: fe7202260bef24eb200b128e8b4cca77c3df0f11 [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
Austin Schuha1654ed2019-01-27 17:24:54 -08007#include "aos/events/event-loop.h"
8#include "aos/events/shm-event-loop.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/queue.h"
Austin Schuheeec74a2019-01-27 20:58:59 -080010#include "aos/robot_state/robot_state.q.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/time/time.h"
12#include "aos/type_traits/type_traits.h"
13#include "aos/util/log_interval.h"
brians343bc112013-02-10 01:53:46 +000014
15namespace aos {
Brian Silverman38111502014-04-10 12:36:26 -070016namespace controls {
brians343bc112013-02-10 01:53:46 +000017
Brian Silverman3204dd82013-03-12 18:42:01 -070018// Control loops run this often, "starting" at time 0.
Austin Schuh214e9c12016-11-25 17:26:20 -080019constexpr ::std::chrono::nanoseconds kLoopFrequency =
20 ::std::chrono::milliseconds(5);
Brian Silverman3204dd82013-03-12 18:42:01 -070021
brians343bc112013-02-10 01:53:46 +000022// Provides helper methods to assist in writing control loops.
23// This template expects to be constructed with a queue group as an argument
24// that has a goal, position, status, and output queue.
25// It will then call the RunIteration method every cycle that it has enough
26// valid data for the control loop to run.
Brian Silverman089f5812015-02-15 01:58:19 -050027template <class T>
Austin Schuh9fe68f72019-08-10 19:32:03 -070028class ControlLoop {
brians343bc112013-02-10 01:53:46 +000029 public:
brians343bc112013-02-10 01:53:46 +000030 // Create some convenient typedefs to reference the Goal, Position, Status,
31 // and Output structures.
Theo Bafrali3274a182019-02-17 20:01:38 -080032 typedef typename std::remove_reference<decltype(
33 *(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type GoalType;
brians343bc112013-02-10 01:53:46 +000034 typedef typename std::remove_reference<
35 decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type
Theo Bafrali3274a182019-02-17 20:01:38 -080036 PositionType;
37 typedef typename std::remove_reference<decltype(
38 *(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type StatusType;
39 typedef typename std::remove_reference<decltype(
40 *(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type OutputType;
brians343bc112013-02-10 01:53:46 +000041
Austin Schuhd3cd6992019-01-27 22:44:07 -080042 ControlLoop(EventLoop *event_loop, const ::std::string &name)
43 : event_loop_(event_loop), name_(name) {
Austin Schuheeec74a2019-01-27 20:58:59 -080044 output_sender_ = event_loop_->MakeSender<OutputType>(name_ + ".output");
45 status_sender_ = event_loop_->MakeSender<StatusType>(name_ + ".status");
46 goal_fetcher_ = event_loop_->MakeFetcher<GoalType>(name_ + ".goal");
47 robot_state_fetcher_ =
48 event_loop_->MakeFetcher<::aos::RobotState>(".aos.robot_state");
49 joystick_state_fetcher_ =
50 event_loop_->MakeFetcher<::aos::JoystickState>(".aos.joystick_state");
Austin Schuh9fe68f72019-08-10 19:32:03 -070051
52 event_loop_->MakeWatcher(name_ + ".position",
53 [this](const PositionType &position) {
54 this->IteratePosition(position);
55 });
Austin Schuheeec74a2019-01-27 20:58:59 -080056 }
57
58 const ::aos::RobotState &robot_state() const { return *robot_state_fetcher_; }
59 bool has_joystick_state() const { return joystick_state_fetcher_.get(); }
60 const ::aos::JoystickState &joystick_state() const {
61 return *joystick_state_fetcher_;
Austin Schuha1654ed2019-01-27 17:24:54 -080062 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050063
64 // Returns true if all the counters etc in the sensor data have been reset.
65 // This will return true only a single time per reset.
66 bool WasReset() {
67 if (reset_) {
68 reset_ = false;
69 return true;
70 } else {
71 return false;
72 }
73 }
74
Brian Silvermand1e65b92014-03-08 17:07:14 -080075 // Constructs and sends a message on the output queue which sets everything to
Austin Schuhaebfb4f2019-01-27 13:26:38 -080076 // a safe state. Default is to set everything to zero. Override Zero below
77 // to change that behavior.
78 void ZeroOutputs();
brians343bc112013-02-10 01:53:46 +000079
80 // Sets the output to zero.
Austin Schuhaebfb4f2019-01-27 13:26:38 -080081 // Override this if a value of zero (or false) is not "off" for this
82 // subsystem.
brians343bc112013-02-10 01:53:46 +000083 virtual void Zero(OutputType *output) { output->Zero(); }
84
brians343bc112013-02-10 01:53:46 +000085 protected:
Austin Schuh9fe68f72019-08-10 19:32:03 -070086 // Runs one cycle of the loop.
Austin Schuha1654ed2019-01-27 17:24:54 -080087 void IteratePosition(const PositionType &position);
88
Theo Bafrali3274a182019-02-17 20:01:38 -080089 EventLoop *event_loop() { return event_loop_; }
90
brians343bc112013-02-10 01:53:46 +000091 // Runs an iteration of the control loop.
Brian Silverman089f5812015-02-15 01:58:19 -050092 // goal is the last goal that was sent. It might be any number of cycles old
93 // or nullptr if we haven't ever received a goal.
94 // position is the current position, or nullptr if we didn't get a position
95 // this cycle.
96 // output is the values to be sent to the motors. This is nullptr if the
97 // output is going to be ignored and set to 0.
brians343bc112013-02-10 01:53:46 +000098 // status is the status of the control loop.
99 // Both output and status should be filled in by the implementation.
Theo Bafrali3274a182019-02-17 20:01:38 -0800100 virtual void RunIteration(const GoalType *goal, const PositionType *position,
101 OutputType *output, StatusType *status) = 0;
brians343bc112013-02-10 01:53:46 +0000102
103 private:
Austin Schuh61bdc602016-12-04 19:10:10 -0800104 static constexpr ::std::chrono::milliseconds kStaleLogInterval =
105 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500106 // The amount of time after the last PWM pulse we consider motors enabled for.
107 // 100ms is the result of using an oscilliscope to look at the input and
108 // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX,
109 // and Victor SP.
Austin Schuh61bdc602016-12-04 19:10:10 -0800110 static constexpr ::std::chrono::milliseconds kPwmDisableTime =
111 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500112
brians343bc112013-02-10 01:53:46 +0000113 // Pointer to the queue group
Austin Schuheeec74a2019-01-27 20:58:59 -0800114 EventLoop *event_loop_;
115 ::std::string name_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800116
Austin Schuha1654ed2019-01-27 17:24:54 -0800117 ::aos::Sender<OutputType> output_sender_;
118 ::aos::Sender<StatusType> status_sender_;
119 ::aos::Fetcher<GoalType> goal_fetcher_;
Austin Schuheeec74a2019-01-27 20:58:59 -0800120 ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_;
121 ::aos::Fetcher<::aos::JoystickState> joystick_state_fetcher_;
122
123 // Fetcher only to be used for the Iterate method. If Iterate is called, Run
124 // can't be called.
125 bool has_iterate_fetcher_ = false;
126 ::aos::Fetcher<PositionType> iterate_position_fetcher_;
Austin Schuha1654ed2019-01-27 17:24:54 -0800127
Brian Silverman699f0cb2015-02-05 19:45:01 -0500128 bool reset_ = false;
129 int32_t sensor_reader_pid_ = 0;
130
Austin Schuh61bdc602016-12-04 19:10:10 -0800131 ::aos::monotonic_clock::time_point last_pwm_sent_ =
132 ::aos::monotonic_clock::min_time;
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800133
Brian Silverman50a9d032014-02-16 17:20:57 -0800134 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500135 SimpleLogInterval no_sensor_state_ =
136 SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state");
Brian Silverman2704ecf2014-04-09 20:24:03 -0700137 SimpleLogInterval motors_off_log_ =
138 SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled");
Brian Silverman699f0cb2015-02-05 19:45:01 -0500139 SimpleLogInterval no_goal_ =
140 SimpleLogInterval(kStaleLogInterval, ERROR, "no goal");
brians343bc112013-02-10 01:53:46 +0000141};
142
Brian Silverman38111502014-04-10 12:36:26 -0700143} // namespace controls
brians343bc112013-02-10 01:53:46 +0000144} // namespace aos
145
John Park33858a32018-09-28 23:05:48 -0700146#include "aos/controls/control_loop-tmpl.h" // IWYU pragma: export
brians343bc112013-02-10 01:53:46 +0000147
148#endif