blob: efe349393d75ecf0a1d299c3327c8a27e378dd51 [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>
James Kuszmaul7077d342021-06-09 20:23:58 -07005
Austin Schuhb58ceb62017-02-05 14:21:57 -08006#include <atomic>
brians343bc112013-02-10 01:53:46 +00007
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/events/event_loop.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/time/time.h"
10#include "aos/type_traits/type_traits.h"
11#include "aos/util/log_interval.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070012#include "frc971/input/joystick_state_generated.h"
13#include "frc971/input/robot_state_generated.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.
brians343bc112013-02-10 01:53:46 +000023// It will then call the RunIteration method every cycle that it has enough
24// valid data for the control loop to run.
Alex Perrycb7da4b2019-08-28 19:35:56 -070025template <class GoalType, class PositionType, class StatusType,
26 class OutputType>
Austin Schuh9fe68f72019-08-10 19:32:03 -070027class ControlLoop {
brians343bc112013-02-10 01:53:46 +000028 public:
Austin Schuhd3cd6992019-01-27 22:44:07 -080029 ControlLoop(EventLoop *event_loop, const ::std::string &name)
30 : event_loop_(event_loop), name_(name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 output_sender_ = event_loop_->MakeSender<OutputType>(name_);
32 status_sender_ = event_loop_->MakeSender<StatusType>(name_);
33 goal_fetcher_ = event_loop_->MakeFetcher<GoalType>(name_);
34 robot_state_fetcher_ = event_loop_->MakeFetcher<::aos::RobotState>("/aos");
Austin Schuheeec74a2019-01-27 20:58:59 -080035 joystick_state_fetcher_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 event_loop_->MakeFetcher<::aos::JoystickState>("/aos");
Austin Schuh9fe68f72019-08-10 19:32:03 -070037
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 event_loop_->MakeWatcher(name_, [this](const PositionType &position) {
39 this->IteratePosition(position);
40 });
Austin Schuheeec74a2019-01-27 20:58:59 -080041 }
42
43 const ::aos::RobotState &robot_state() const { return *robot_state_fetcher_; }
44 bool has_joystick_state() const { return joystick_state_fetcher_.get(); }
45 const ::aos::JoystickState &joystick_state() const {
46 return *joystick_state_fetcher_;
Austin Schuha1654ed2019-01-27 17:24:54 -080047 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050048
49 // Returns true if all the counters etc in the sensor data have been reset.
50 // This will return true only a single time per reset.
51 bool WasReset() {
52 if (reset_) {
53 reset_ = false;
54 return true;
55 } else {
56 return false;
57 }
58 }
59
Brian Silvermand1e65b92014-03-08 17:07:14 -080060 // Constructs and sends a message on the output queue which sets everything to
Austin Schuhaebfb4f2019-01-27 13:26:38 -080061 // a safe state. Default is to set everything to zero. Override Zero below
62 // to change that behavior.
63 void ZeroOutputs();
brians343bc112013-02-10 01:53:46 +000064
65 // Sets the output to zero.
Austin Schuhaebfb4f2019-01-27 13:26:38 -080066 // Override this if a value of zero (or false) is not "off" for this
67 // subsystem.
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 virtual flatbuffers::Offset<OutputType> Zero(
69 typename ::aos::Sender<OutputType>::Builder *builder) {
70 return builder->template MakeBuilder<OutputType>().Finish();
71 }
brians343bc112013-02-10 01:53:46 +000072
brians343bc112013-02-10 01:53:46 +000073 protected:
Austin Schuh9fe68f72019-08-10 19:32:03 -070074 // Runs one cycle of the loop.
Austin Schuha1654ed2019-01-27 17:24:54 -080075 void IteratePosition(const PositionType &position);
76
Theo Bafrali3274a182019-02-17 20:01:38 -080077 EventLoop *event_loop() { return event_loop_; }
78
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 // Returns the position context. This is only valid inside the RunIteration
80 // method.
81 const aos::Context &position_context() { return event_loop_->context(); }
82
brians343bc112013-02-10 01:53:46 +000083 // Runs an iteration of the control loop.
Brian Silverman089f5812015-02-15 01:58:19 -050084 // goal is the last goal that was sent. It might be any number of cycles old
85 // or nullptr if we haven't ever received a goal.
86 // position is the current position, or nullptr if we didn't get a position
87 // this cycle.
88 // output is the values to be sent to the motors. This is nullptr if the
89 // output is going to be ignored and set to 0.
brians343bc112013-02-10 01:53:46 +000090 // status is the status of the control loop.
91 // Both output and status should be filled in by the implementation.
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 virtual void RunIteration(
93 const GoalType *goal, const PositionType *position,
94 typename ::aos::Sender<OutputType>::Builder *output,
95 typename ::aos::Sender<StatusType>::Builder *status) = 0;
brians343bc112013-02-10 01:53:46 +000096
97 private:
Austin Schuh61bdc602016-12-04 19:10:10 -080098 static constexpr ::std::chrono::milliseconds kStaleLogInterval =
99 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500100 // The amount of time after the last PWM pulse we consider motors enabled for.
101 // 100ms is the result of using an oscilliscope to look at the input and
102 // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX,
103 // and Victor SP.
Austin Schuh61bdc602016-12-04 19:10:10 -0800104 static constexpr ::std::chrono::milliseconds kPwmDisableTime =
105 ::std::chrono::milliseconds(100);
Brian Silverman699f0cb2015-02-05 19:45:01 -0500106
brians343bc112013-02-10 01:53:46 +0000107 // Pointer to the queue group
Austin Schuheeec74a2019-01-27 20:58:59 -0800108 EventLoop *event_loop_;
109 ::std::string name_;
Brian Silverman50a9d032014-02-16 17:20:57 -0800110
Austin Schuha1654ed2019-01-27 17:24:54 -0800111 ::aos::Sender<OutputType> output_sender_;
112 ::aos::Sender<StatusType> status_sender_;
113 ::aos::Fetcher<GoalType> goal_fetcher_;
Austin Schuheeec74a2019-01-27 20:58:59 -0800114 ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_;
115 ::aos::Fetcher<::aos::JoystickState> joystick_state_fetcher_;
116
117 // Fetcher only to be used for the Iterate method. If Iterate is called, Run
118 // can't be called.
119 bool has_iterate_fetcher_ = false;
120 ::aos::Fetcher<PositionType> iterate_position_fetcher_;
Austin Schuha1654ed2019-01-27 17:24:54 -0800121
Brian Silverman699f0cb2015-02-05 19:45:01 -0500122 bool reset_ = false;
123 int32_t sensor_reader_pid_ = 0;
124
Austin Schuh61bdc602016-12-04 19:10:10 -0800125 ::aos::monotonic_clock::time_point last_pwm_sent_ =
126 ::aos::monotonic_clock::min_time;
Austin Schuh3d6e3df2014-02-17 01:51:03 -0800127
Brian Silverman50a9d032014-02-16 17:20:57 -0800128 typedef ::aos::util::SimpleLogInterval SimpleLogInterval;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500129 SimpleLogInterval no_sensor_state_ =
130 SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state");
Brian Silverman2704ecf2014-04-09 20:24:03 -0700131 SimpleLogInterval motors_off_log_ =
132 SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled");
Brian Silverman699f0cb2015-02-05 19:45:01 -0500133 SimpleLogInterval no_goal_ =
134 SimpleLogInterval(kStaleLogInterval, ERROR, "no goal");
brians343bc112013-02-10 01:53:46 +0000135};
136
Brian Silverman38111502014-04-10 12:36:26 -0700137} // namespace controls
brians343bc112013-02-10 01:53:46 +0000138} // namespace aos
139
John Park33858a32018-09-28 23:05:48 -0700140#include "aos/controls/control_loop-tmpl.h" // IWYU pragma: export
brians343bc112013-02-10 01:53:46 +0000141
142#endif