blob: 7628c26665af2047b99f8db68a64f0b866e21a44 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <stddef.h>
Brian Silverman699f0cb2015-02-05 19:45:01 -05002#include <inttypes.h>
brians343bc112013-02-10 01:53:46 +00003
John Park33858a32018-09-28 23:05:48 -07004#include "aos/logging/logging.h"
5#include "aos/logging/queue_logging.h"
Austin Schuh3d6e3df2014-02-17 01:51:03 -08006
brians343bc112013-02-10 01:53:46 +00007namespace aos {
Brian Silverman38111502014-04-10 12:36:26 -07008namespace controls {
brians343bc112013-02-10 01:53:46 +00009
10// TODO(aschuh): Tests.
11
Brian Silverman089f5812015-02-15 01:58:19 -050012template <class T>
Austin Schuh61bdc602016-12-04 19:10:10 -080013constexpr ::std::chrono::milliseconds ControlLoop<T>::kStaleLogInterval;
Brian Silverman089f5812015-02-15 01:58:19 -050014template <class T>
Austin Schuh61bdc602016-12-04 19:10:10 -080015constexpr ::std::chrono::milliseconds ControlLoop<T>::kPwmDisableTime;
Brian Silverman50a9d032014-02-16 17:20:57 -080016
Brian Silverman089f5812015-02-15 01:58:19 -050017template <class T>
18void ControlLoop<T>::ZeroOutputs() {
Austin Schuha1654ed2019-01-27 17:24:54 -080019 typename ::aos::Sender<OutputType>::Message output =
20 output_sender_.MakeMessage();
brians343bc112013-02-10 01:53:46 +000021 Zero(output.get());
22 output.Send();
23}
24
Brian Silverman089f5812015-02-15 01:58:19 -050025template <class T>
Austin Schuha1654ed2019-01-27 17:24:54 -080026void ControlLoop<T>::IteratePosition(const PositionType &position) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050027 no_goal_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050028 no_sensor_state_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050029 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080030
Austin Schuhf257f3c2019-10-27 21:00:43 -070031 AOS_LOG_STRUCT(DEBUG, "position", position);
Brian Silvermand8f403a2014-12-13 19:12:04 -050032
33 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000034 // goal, we will just reuse the old one.
Austin Schuha1654ed2019-01-27 17:24:54 -080035 goal_fetcher_.Fetch();
36 const GoalType *goal = goal_fetcher_.get();
Brian Silverman699f0cb2015-02-05 19:45:01 -050037 if (goal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070038 AOS_LOG_STRUCT(DEBUG, "goal", *goal);
Brian Silverman699f0cb2015-02-05 19:45:01 -050039 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -070040 AOS_LOG_INTERVAL(no_goal_);
brians343bc112013-02-10 01:53:46 +000041 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080042
Austin Schuheeec74a2019-01-27 20:58:59 -080043 const bool new_robot_state = robot_state_fetcher_.Fetch();
44 if (!robot_state_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070045 AOS_LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080046 return;
47 }
Austin Schuheeec74a2019-01-27 20:58:59 -080048 if (sensor_reader_pid_ != robot_state_fetcher_->reader_pid) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070049 AOS_LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
50 robot_state_fetcher_->reader_pid, sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080051 reset_ = true;
Austin Schuheeec74a2019-01-27 20:58:59 -080052 sensor_reader_pid_ = robot_state_fetcher_->reader_pid;
Brian Silverman71fbee02014-03-13 17:24:54 -070053 }
brians343bc112013-02-10 01:53:46 +000054
Austin Schuheeec74a2019-01-27 20:58:59 -080055 bool outputs_enabled = robot_state_fetcher_->outputs_enabled;
brians343bc112013-02-10 01:53:46 +000056
57 // Check to see if we got a driver station packet recently.
Austin Schuh61bdc602016-12-04 19:10:10 -080058 if (new_robot_state) {
Austin Schuheeec74a2019-01-27 20:58:59 -080059 if (robot_state_fetcher_->outputs_enabled) {
Brian Silverman295f74b2015-02-14 23:00:47 -050060 // If the driver's station reports being disabled, we're probably not
61 // actually going to send motor values regardless of what the FPGA
62 // reports.
Austin Schuheeec74a2019-01-27 20:58:59 -080063 last_pwm_sent_ = robot_state_fetcher_->sent_time;
brians343bc112013-02-10 01:53:46 +000064 }
65 }
66
Austin Schuh19845272019-07-07 20:45:22 -070067 const ::aos::monotonic_clock::time_point monotonic_now =
68 event_loop_->monotonic_now();
69 const bool motors_off = monotonic_now >= kPwmDisableTime + last_pwm_sent_;
Austin Schuheeec74a2019-01-27 20:58:59 -080070 joystick_state_fetcher_.Fetch();
Brian Silverman2704ecf2014-04-09 20:24:03 -070071 if (motors_off) {
Austin Schuheeec74a2019-01-27 20:58:59 -080072 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070073 AOS_LOG_INTERVAL(motors_off_log_);
Brian Silverman2704ecf2014-04-09 20:24:03 -070074 }
75 outputs_enabled = false;
76 }
77
Austin Schuha1654ed2019-01-27 17:24:54 -080078 typename ::aos::Sender<StatusType>::Message status =
79 status_sender_.MakeMessage();
Brian Silverman699f0cb2015-02-05 19:45:01 -050080 if (status.get() == nullptr) {
brians343bc112013-02-10 01:53:46 +000081 return;
82 }
83
84 if (outputs_enabled) {
Austin Schuha1654ed2019-01-27 17:24:54 -080085 typename ::aos::Sender<OutputType>::Message output =
86 output_sender_.MakeMessage();
87 RunIteration(goal, &position, output.get(), status.get());
brians343bc112013-02-10 01:53:46 +000088
Austin Schuhf907f2e2018-01-02 01:15:27 -080089 output->SetTimeToNow();
Austin Schuhf257f3c2019-10-27 21:00:43 -070090 AOS_LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +000091 output.Send();
92 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050093 // The outputs are disabled, so pass nullptr in for the output.
Austin Schuha1654ed2019-01-27 17:24:54 -080094 RunIteration(goal, &position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +000095 ZeroOutputs();
96 }
97
Austin Schuhf907f2e2018-01-02 01:15:27 -080098 status->SetTimeToNow();
Austin Schuhf257f3c2019-10-27 21:00:43 -070099 AOS_LOG_STRUCT(DEBUG, "status", *status);
brians343bc112013-02-10 01:53:46 +0000100 status.Send();
101}
102
Brian Silverman38111502014-04-10 12:36:26 -0700103} // namespace controls
brians343bc112013-02-10 01:53:46 +0000104} // namespace aos