blob: 49bad45a6a718367326a26039dbdfed068b8bdeb [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>
26void ControlLoop<T>::Iterate() {
Austin Schuheeec74a2019-01-27 20:58:59 -080027 if (!has_iterate_fetcher_) {
28 iterate_position_fetcher_ =
29 event_loop_->MakeFetcher<PositionType>(name_ + ".position");
30 has_iterate_fetcher_ = true;
31 }
32 const bool did_fetch = iterate_position_fetcher_.Fetch();
33 if (!did_fetch) {
34 LOG(FATAL, "Failed to fetch from position queue\n");
35 }
36 IteratePosition(*iterate_position_fetcher_);
Austin Schuha1654ed2019-01-27 17:24:54 -080037}
38
39template <class T>
40void ControlLoop<T>::IteratePosition(const PositionType &position) {
41 // Since Exit() isn't async safe, we want to call Exit from the periodic
42 // handler.
43 if (!run_) {
44 event_loop_->Exit();
45 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050046 no_goal_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050047 no_sensor_state_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050048 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080049
Austin Schuha1654ed2019-01-27 17:24:54 -080050 LOG_STRUCT(DEBUG, "position", position);
Brian Silvermand8f403a2014-12-13 19:12:04 -050051
52 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000053 // goal, we will just reuse the old one.
Austin Schuha1654ed2019-01-27 17:24:54 -080054 goal_fetcher_.Fetch();
55 const GoalType *goal = goal_fetcher_.get();
Brian Silverman699f0cb2015-02-05 19:45:01 -050056 if (goal) {
57 LOG_STRUCT(DEBUG, "goal", *goal);
58 } else {
59 LOG_INTERVAL(no_goal_);
brians343bc112013-02-10 01:53:46 +000060 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080061
Austin Schuheeec74a2019-01-27 20:58:59 -080062 const bool new_robot_state = robot_state_fetcher_.Fetch();
63 if (!robot_state_fetcher_.get()) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050064 LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080065 return;
66 }
Austin Schuheeec74a2019-01-27 20:58:59 -080067 if (sensor_reader_pid_ != robot_state_fetcher_->reader_pid) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050068 LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
Austin Schuheeec74a2019-01-27 20:58:59 -080069 robot_state_fetcher_->reader_pid, sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080070 reset_ = true;
Austin Schuheeec74a2019-01-27 20:58:59 -080071 sensor_reader_pid_ = robot_state_fetcher_->reader_pid;
Brian Silverman71fbee02014-03-13 17:24:54 -070072 }
brians343bc112013-02-10 01:53:46 +000073
Austin Schuheeec74a2019-01-27 20:58:59 -080074 bool outputs_enabled = robot_state_fetcher_->outputs_enabled;
brians343bc112013-02-10 01:53:46 +000075
76 // Check to see if we got a driver station packet recently.
Austin Schuh61bdc602016-12-04 19:10:10 -080077 if (new_robot_state) {
Austin Schuheeec74a2019-01-27 20:58:59 -080078 if (robot_state_fetcher_->outputs_enabled) {
Brian Silverman295f74b2015-02-14 23:00:47 -050079 // If the driver's station reports being disabled, we're probably not
80 // actually going to send motor values regardless of what the FPGA
81 // reports.
Austin Schuheeec74a2019-01-27 20:58:59 -080082 last_pwm_sent_ = robot_state_fetcher_->sent_time;
brians343bc112013-02-10 01:53:46 +000083 }
84 }
85
Austin Schuh61bdc602016-12-04 19:10:10 -080086 const ::aos::monotonic_clock::time_point now = ::aos::monotonic_clock::now();
87 const bool motors_off = now >= kPwmDisableTime + last_pwm_sent_;
Austin Schuheeec74a2019-01-27 20:58:59 -080088 joystick_state_fetcher_.Fetch();
Brian Silverman2704ecf2014-04-09 20:24:03 -070089 if (motors_off) {
Austin Schuheeec74a2019-01-27 20:58:59 -080090 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled) {
Brian Silverman2704ecf2014-04-09 20:24:03 -070091 LOG_INTERVAL(motors_off_log_);
92 }
93 outputs_enabled = false;
94 }
95
Austin Schuha1654ed2019-01-27 17:24:54 -080096 typename ::aos::Sender<StatusType>::Message status =
97 status_sender_.MakeMessage();
Brian Silverman699f0cb2015-02-05 19:45:01 -050098 if (status.get() == nullptr) {
brians343bc112013-02-10 01:53:46 +000099 return;
100 }
101
102 if (outputs_enabled) {
Austin Schuha1654ed2019-01-27 17:24:54 -0800103 typename ::aos::Sender<OutputType>::Message output =
104 output_sender_.MakeMessage();
105 RunIteration(goal, &position, output.get(), status.get());
brians343bc112013-02-10 01:53:46 +0000106
Austin Schuhf907f2e2018-01-02 01:15:27 -0800107 output->SetTimeToNow();
Brian Silvermand6974f42014-02-14 13:39:21 -0800108 LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +0000109 output.Send();
110 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500111 // The outputs are disabled, so pass nullptr in for the output.
Austin Schuha1654ed2019-01-27 17:24:54 -0800112 RunIteration(goal, &position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +0000113 ZeroOutputs();
114 }
115
Austin Schuhf907f2e2018-01-02 01:15:27 -0800116 status->SetTimeToNow();
Brian Silvermand6974f42014-02-14 13:39:21 -0800117 LOG_STRUCT(DEBUG, "status", *status);
brians343bc112013-02-10 01:53:46 +0000118 status.Send();
119}
120
Brian Silverman089f5812015-02-15 01:58:19 -0500121template <class T>
122void ControlLoop<T>::Run() {
Austin Schuhb58ceb62017-02-05 14:21:57 -0800123 struct sigaction action;
124 action.sa_handler = &ControlLoop<T>::Quit;
125 sigemptyset(&action.sa_mask);
126 action.sa_flags = SA_RESETHAND;
127
128 PCHECK(sigaction(SIGTERM, &action, nullptr));
129 PCHECK(sigaction(SIGQUIT, &action, nullptr));
130 PCHECK(sigaction(SIGINT, &action, nullptr));
131
Austin Schuheeec74a2019-01-27 20:58:59 -0800132 event_loop_->MakeWatcher(name_ + ".position",
Austin Schuha1654ed2019-01-27 17:24:54 -0800133 [this](const PositionType &position) {
134 this->IteratePosition(position);
135 });
136
137 event_loop_->Run();
Austin Schuhb58ceb62017-02-05 14:21:57 -0800138 LOG(INFO, "Shutting down\n");
brians343bc112013-02-10 01:53:46 +0000139}
140
Austin Schuhb58ceb62017-02-05 14:21:57 -0800141template <class T>
142::std::atomic<bool> ControlLoop<T>::run_{true};
143
Brian Silverman38111502014-04-10 12:36:26 -0700144} // namespace controls
brians343bc112013-02-10 01:53:46 +0000145} // namespace aos