blob: d86c97c2a8150561806cd85ef81cf9ececba76d2 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08006namespace frc971::controls {
brians343bc112013-02-10 01:53:46 +00007
8// TODO(aschuh): Tests.
9
Alex Perrycb7da4b2019-08-28 19:35:56 -070010template <class GoalType, class PositionType, class StatusType,
11 class OutputType>
12constexpr ::std::chrono::milliseconds ControlLoop<
13 GoalType, PositionType, StatusType, OutputType>::kStaleLogInterval;
14template <class GoalType, class PositionType, class StatusType,
15 class OutputType>
16constexpr ::std::chrono::milliseconds ControlLoop<
17 GoalType, PositionType, StatusType, OutputType>::kPwmDisableTime;
Brian Silverman50a9d032014-02-16 17:20:57 -080018
Alex Perrycb7da4b2019-08-28 19:35:56 -070019template <class GoalType, class PositionType, class StatusType,
20 class OutputType>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070021void ControlLoop<GoalType, PositionType, StatusType,
22 OutputType>::ZeroOutputs() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 typename ::aos::Sender<OutputType>::Builder builder =
24 output_sender_.MakeBuilder();
milind1f1dca32021-07-03 13:50:07 -070025 builder.CheckOk(builder.Send(Zero(&builder)));
brians343bc112013-02-10 01:53:46 +000026}
27
Alex Perrycb7da4b2019-08-28 19:35:56 -070028template <class GoalType, class PositionType, class StatusType,
29 class OutputType>
30void ControlLoop<GoalType, PositionType, StatusType,
31 OutputType>::IteratePosition(const PositionType &position) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050032 no_goal_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050033 no_sensor_state_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050034 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080035
Brian Silvermand8f403a2014-12-13 19:12:04 -050036 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000037 // goal, we will just reuse the old one.
Austin Schuha1654ed2019-01-27 17:24:54 -080038 goal_fetcher_.Fetch();
39 const GoalType *goal = goal_fetcher_.get();
Austin Schuh3d6e3df2014-02-17 01:51:03 -080040
Austin Schuheeec74a2019-01-27 20:58:59 -080041 const bool new_robot_state = robot_state_fetcher_.Fetch();
42 if (!robot_state_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070043 AOS_LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080044 return;
45 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070046 if (sensor_reader_pid_ != robot_state_fetcher_->reader_pid()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070047 AOS_LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 robot_state_fetcher_->reader_pid(), sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080049 reset_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070050 sensor_reader_pid_ = robot_state_fetcher_->reader_pid();
Brian Silverman71fbee02014-03-13 17:24:54 -070051 }
brians343bc112013-02-10 01:53:46 +000052
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 bool outputs_enabled = robot_state_fetcher_->outputs_enabled();
brians343bc112013-02-10 01:53:46 +000054
55 // Check to see if we got a driver station packet recently.
Austin Schuh61bdc602016-12-04 19:10:10 -080056 if (new_robot_state) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 if (robot_state_fetcher_->outputs_enabled()) {
Brian Silverman295f74b2015-02-14 23:00:47 -050058 // If the driver's station reports being disabled, we're probably not
59 // actually going to send motor values regardless of what the FPGA
60 // reports.
Austin Schuhad154822019-12-27 15:45:13 -080061 last_pwm_sent_ = robot_state_fetcher_.context().monotonic_event_time;
brians343bc112013-02-10 01:53:46 +000062 }
63 }
64
Austin Schuh19845272019-07-07 20:45:22 -070065 const ::aos::monotonic_clock::time_point monotonic_now =
66 event_loop_->monotonic_now();
67 const bool motors_off = monotonic_now >= kPwmDisableTime + last_pwm_sent_;
Austin Schuheeec74a2019-01-27 20:58:59 -080068 joystick_state_fetcher_.Fetch();
Brian Silverman2704ecf2014-04-09 20:24:03 -070069 if (motors_off) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070071 AOS_LOG_INTERVAL(motors_off_log_);
Brian Silverman2704ecf2014-04-09 20:24:03 -070072 }
73 outputs_enabled = false;
74 }
75
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 typename ::aos::Sender<StatusType>::Builder status =
77 status_sender_.MakeBuilder();
brians343bc112013-02-10 01:53:46 +000078 if (outputs_enabled) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 typename ::aos::Sender<OutputType>::Builder output =
80 output_sender_.MakeBuilder();
81 RunIteration(goal, &position, &output, &status);
brians343bc112013-02-10 01:53:46 +000082
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 output.CheckSent();
brians343bc112013-02-10 01:53:46 +000084 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050085 // The outputs are disabled, so pass nullptr in for the output.
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 RunIteration(goal, &position, nullptr, &status);
brians343bc112013-02-10 01:53:46 +000087 ZeroOutputs();
88 }
89
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 status.CheckSent();
brians343bc112013-02-10 01:53:46 +000091}
92
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080093} // namespace frc971::controls