blob: d6813fb4a9f214e63fd872d0d4efb3bfc12bead7 [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
4#include "aos/common/logging/logging.h"
Brian Silverman2ac0fbc2014-03-20 19:45:13 -07005#include "aos/common/messages/robot_state.q.h"
Brian Silvermand6974f42014-02-14 13:39:21 -08006#include "aos/common/logging/queue_logging.h"
Austin Schuh3d6e3df2014-02-17 01:51:03 -08007
brians343bc112013-02-10 01:53:46 +00008namespace aos {
Brian Silverman38111502014-04-10 12:36:26 -07009namespace controls {
brians343bc112013-02-10 01:53:46 +000010
11// TODO(aschuh): Tests.
12
Brian Silverman089f5812015-02-15 01:58:19 -050013template <class T>
Austin Schuh61bdc602016-12-04 19:10:10 -080014constexpr ::std::chrono::milliseconds ControlLoop<T>::kStaleLogInterval;
Brian Silverman089f5812015-02-15 01:58:19 -050015template <class T>
Austin Schuh61bdc602016-12-04 19:10:10 -080016constexpr ::std::chrono::milliseconds ControlLoop<T>::kPwmDisableTime;
Brian Silverman50a9d032014-02-16 17:20:57 -080017
Brian Silverman089f5812015-02-15 01:58:19 -050018template <class T>
19void ControlLoop<T>::ZeroOutputs() {
brians343bc112013-02-10 01:53:46 +000020 aos::ScopedMessagePtr<OutputType> output =
21 control_loop_->output.MakeMessage();
22 Zero(output.get());
23 output.Send();
24}
25
Brian Silverman089f5812015-02-15 01:58:19 -050026template <class T>
27void ControlLoop<T>::Iterate() {
Brian Silverman699f0cb2015-02-05 19:45:01 -050028 no_goal_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050029 no_sensor_state_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050030 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080031
Brian Silvermand8f403a2014-12-13 19:12:04 -050032 control_loop_->position.FetchAnother();
33 const PositionType *const position = control_loop_->position.get();
34 LOG_STRUCT(DEBUG, "position", *position);
35
36 // 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.
brians343bc112013-02-10 01:53:46 +000038 control_loop_->goal.FetchLatest();
brians343bc112013-02-10 01:53:46 +000039 const GoalType *goal = control_loop_->goal.get();
Brian Silverman699f0cb2015-02-05 19:45:01 -050040 if (goal) {
41 LOG_STRUCT(DEBUG, "goal", *goal);
42 } else {
43 LOG_INTERVAL(no_goal_);
brians343bc112013-02-10 01:53:46 +000044 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080045
Austin Schuh61bdc602016-12-04 19:10:10 -080046 const bool new_robot_state = ::aos::robot_state.FetchLatest();
Brian Silverman699f0cb2015-02-05 19:45:01 -050047 if (!::aos::robot_state.get()) {
48 LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080049 return;
50 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050051 if (sensor_reader_pid_ != ::aos::robot_state->reader_pid) {
52 LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
53 ::aos::robot_state->reader_pid, sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080054 reset_ = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050055 sensor_reader_pid_ = ::aos::robot_state->reader_pid;
Brian Silverman71fbee02014-03-13 17:24:54 -070056 }
brians343bc112013-02-10 01:53:46 +000057
Austin Schuh61bdc602016-12-04 19:10:10 -080058 bool outputs_enabled = ::aos::robot_state->outputs_enabled;
brians343bc112013-02-10 01:53:46 +000059
60 // Check to see if we got a driver station packet recently.
Austin Schuh61bdc602016-12-04 19:10:10 -080061 if (new_robot_state) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050062 if (::aos::robot_state->outputs_enabled) {
Brian Silverman295f74b2015-02-14 23:00:47 -050063 // If the driver's station reports being disabled, we're probably not
64 // actually going to send motor values regardless of what the FPGA
65 // reports.
Austin Schuhf2a50ba2016-12-24 16:16:26 -080066 last_pwm_sent_ = ::aos::robot_state->sent_time;
brians343bc112013-02-10 01:53:46 +000067 }
68 }
69
Austin Schuh61bdc602016-12-04 19:10:10 -080070 const ::aos::monotonic_clock::time_point now = ::aos::monotonic_clock::now();
71 const bool motors_off = now >= kPwmDisableTime + last_pwm_sent_;
72 ::aos::joystick_state.FetchLatest();
Brian Silverman2704ecf2014-04-09 20:24:03 -070073 if (motors_off) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050074 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) {
Brian Silverman2704ecf2014-04-09 20:24:03 -070075 LOG_INTERVAL(motors_off_log_);
76 }
77 outputs_enabled = false;
78 }
79
brians343bc112013-02-10 01:53:46 +000080 aos::ScopedMessagePtr<StatusType> status =
81 control_loop_->status.MakeMessage();
Brian Silverman699f0cb2015-02-05 19:45:01 -050082 if (status.get() == nullptr) {
brians343bc112013-02-10 01:53:46 +000083 return;
84 }
85
86 if (outputs_enabled) {
87 aos::ScopedMessagePtr<OutputType> output =
88 control_loop_->output.MakeMessage();
89 RunIteration(goal, position, output.get(), status.get());
90
Brian Silvermand6974f42014-02-14 13:39:21 -080091 LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +000092 output.Send();
93 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050094 // The outputs are disabled, so pass nullptr in for the output.
Ben Fredrickson4283bb42014-02-22 08:31:50 +000095 RunIteration(goal, position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +000096 ZeroOutputs();
97 }
98
Brian Silvermand6974f42014-02-14 13:39:21 -080099 LOG_STRUCT(DEBUG, "status", *status);
brians343bc112013-02-10 01:53:46 +0000100 status.Send();
101}
102
Brian Silverman089f5812015-02-15 01:58:19 -0500103template <class T>
104void ControlLoop<T>::Run() {
Austin Schuhb58ceb62017-02-05 14:21:57 -0800105 struct sigaction action;
106 action.sa_handler = &ControlLoop<T>::Quit;
107 sigemptyset(&action.sa_mask);
108 action.sa_flags = SA_RESETHAND;
109
110 PCHECK(sigaction(SIGTERM, &action, nullptr));
111 PCHECK(sigaction(SIGQUIT, &action, nullptr));
112 PCHECK(sigaction(SIGINT, &action, nullptr));
113
114 while (run_) {
brians343bc112013-02-10 01:53:46 +0000115 Iterate();
116 }
Austin Schuhb58ceb62017-02-05 14:21:57 -0800117 LOG(INFO, "Shutting down\n");
brians343bc112013-02-10 01:53:46 +0000118}
119
Austin Schuhb58ceb62017-02-05 14:21:57 -0800120template <class T>
121::std::atomic<bool> ControlLoop<T>::run_{true};
122
Brian Silverman38111502014-04-10 12:36:26 -0700123} // namespace controls
brians343bc112013-02-10 01:53:46 +0000124} // namespace aos