blob: 0db17866ed8deca925c78df97711bb3d7641cea1 [file] [log] [blame]
Tyler Chatowbf0609c2021-07-31 16:13:27 -07001#include <cinttypes>
2#include <cstddef>
brians343bc112013-02-10 01:53:46 +00003
John Park33858a32018-09-28 23:05:48 -07004#include "aos/logging/logging.h"
Austin Schuh3d6e3df2014-02-17 01:51:03 -08005
James Kuszmaul61750662021-06-21 21:32:33 -07006namespace frc971 {
Brian Silverman38111502014-04-10 12:36:26 -07007namespace controls {
brians343bc112013-02-10 01:53:46 +00008
9// TODO(aschuh): Tests.
10
Alex Perrycb7da4b2019-08-28 19:35:56 -070011template <class GoalType, class PositionType, class StatusType,
12 class OutputType>
13constexpr ::std::chrono::milliseconds ControlLoop<
14 GoalType, PositionType, StatusType, OutputType>::kStaleLogInterval;
15template <class GoalType, class PositionType, class StatusType,
16 class OutputType>
17constexpr ::std::chrono::milliseconds ControlLoop<
18 GoalType, PositionType, StatusType, OutputType>::kPwmDisableTime;
Brian Silverman50a9d032014-02-16 17:20:57 -080019
Alex Perrycb7da4b2019-08-28 19:35:56 -070020template <class GoalType, class PositionType, class StatusType,
21 class OutputType>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070022void ControlLoop<GoalType, PositionType, StatusType,
23 OutputType>::ZeroOutputs() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 typename ::aos::Sender<OutputType>::Builder builder =
25 output_sender_.MakeBuilder();
26 builder.Send(Zero(&builder));
brians343bc112013-02-10 01:53:46 +000027}
28
Alex Perrycb7da4b2019-08-28 19:35:56 -070029template <class GoalType, class PositionType, class StatusType,
30 class OutputType>
31void ControlLoop<GoalType, PositionType, StatusType,
32 OutputType>::IteratePosition(const PositionType &position) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050033 no_goal_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050034 no_sensor_state_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050035 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080036
Brian Silvermand8f403a2014-12-13 19:12:04 -050037 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000038 // goal, we will just reuse the old one.
Austin Schuha1654ed2019-01-27 17:24:54 -080039 goal_fetcher_.Fetch();
40 const GoalType *goal = goal_fetcher_.get();
Austin Schuh3d6e3df2014-02-17 01:51:03 -080041
Austin Schuheeec74a2019-01-27 20:58:59 -080042 const bool new_robot_state = robot_state_fetcher_.Fetch();
43 if (!robot_state_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070044 AOS_LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080045 return;
46 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070047 if (sensor_reader_pid_ != robot_state_fetcher_->reader_pid()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070048 AOS_LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 robot_state_fetcher_->reader_pid(), sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080050 reset_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070051 sensor_reader_pid_ = robot_state_fetcher_->reader_pid();
Brian Silverman71fbee02014-03-13 17:24:54 -070052 }
brians343bc112013-02-10 01:53:46 +000053
Alex Perrycb7da4b2019-08-28 19:35:56 -070054 bool outputs_enabled = robot_state_fetcher_->outputs_enabled();
brians343bc112013-02-10 01:53:46 +000055
56 // Check to see if we got a driver station packet recently.
Austin Schuh61bdc602016-12-04 19:10:10 -080057 if (new_robot_state) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 if (robot_state_fetcher_->outputs_enabled()) {
Brian Silverman295f74b2015-02-14 23:00:47 -050059 // If the driver's station reports being disabled, we're probably not
60 // actually going to send motor values regardless of what the FPGA
61 // reports.
Austin Schuhad154822019-12-27 15:45:13 -080062 last_pwm_sent_ = robot_state_fetcher_.context().monotonic_event_time;
brians343bc112013-02-10 01:53:46 +000063 }
64 }
65
Austin Schuh19845272019-07-07 20:45:22 -070066 const ::aos::monotonic_clock::time_point monotonic_now =
67 event_loop_->monotonic_now();
68 const bool motors_off = monotonic_now >= kPwmDisableTime + last_pwm_sent_;
Austin Schuheeec74a2019-01-27 20:58:59 -080069 joystick_state_fetcher_.Fetch();
Brian Silverman2704ecf2014-04-09 20:24:03 -070070 if (motors_off) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070071 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070072 AOS_LOG_INTERVAL(motors_off_log_);
Brian Silverman2704ecf2014-04-09 20:24:03 -070073 }
74 outputs_enabled = false;
75 }
76
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 typename ::aos::Sender<StatusType>::Builder status =
78 status_sender_.MakeBuilder();
brians343bc112013-02-10 01:53:46 +000079 if (outputs_enabled) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 typename ::aos::Sender<OutputType>::Builder output =
81 output_sender_.MakeBuilder();
82 RunIteration(goal, &position, &output, &status);
brians343bc112013-02-10 01:53:46 +000083
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 output.CheckSent();
brians343bc112013-02-10 01:53:46 +000085 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050086 // The outputs are disabled, so pass nullptr in for the output.
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 RunIteration(goal, &position, nullptr, &status);
brians343bc112013-02-10 01:53:46 +000088 ZeroOutputs();
89 }
90
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 status.CheckSent();
brians343bc112013-02-10 01:53:46 +000092}
93
Brian Silverman38111502014-04-10 12:36:26 -070094} // namespace controls
James Kuszmaul61750662021-06-21 21:32:33 -070095} // namespace frc971