blob: 6b698ba8d6b81a16628cd04e8ff8a17bc7401e48 [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>
14constexpr ::aos::time::Time ControlLoop<T>::kStaleLogInterval;
15template <class T>
16constexpr ::aos::time::Time 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 Silverman9c9ab422014-02-25 13:42:53 -080029 driver_station_old_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050030 no_sensor_state_.Print();
Brian Silverman9c9ab422014-02-25 13:42:53 -080031 no_driver_station_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050032 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080033
Brian Silvermand8f403a2014-12-13 19:12:04 -050034 control_loop_->position.FetchAnother();
35 const PositionType *const position = control_loop_->position.get();
36 LOG_STRUCT(DEBUG, "position", *position);
37
38 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000039 // goal, we will just reuse the old one.
brians343bc112013-02-10 01:53:46 +000040 control_loop_->goal.FetchLatest();
brians343bc112013-02-10 01:53:46 +000041 const GoalType *goal = control_loop_->goal.get();
Brian Silverman699f0cb2015-02-05 19:45:01 -050042 if (goal) {
43 LOG_STRUCT(DEBUG, "goal", *goal);
44 } else {
45 LOG_INTERVAL(no_goal_);
brians343bc112013-02-10 01:53:46 +000046 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080047
Brian Silverman699f0cb2015-02-05 19:45:01 -050048 ::aos::robot_state.FetchLatest();
49 if (!::aos::robot_state.get()) {
50 LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080051 return;
52 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050053 if (sensor_reader_pid_ != ::aos::robot_state->reader_pid) {
54 LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
55 ::aos::robot_state->reader_pid, sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080056 reset_ = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050057 sensor_reader_pid_ = ::aos::robot_state->reader_pid;
Brian Silverman71fbee02014-03-13 17:24:54 -070058 }
brians343bc112013-02-10 01:53:46 +000059
brians343bc112013-02-10 01:53:46 +000060 bool outputs_enabled = false;
61
62 // Check to see if we got a driver station packet recently.
Brian Silverman699f0cb2015-02-05 19:45:01 -050063 if (::aos::joystick_state.FetchLatest()) {
Brian Silverman295f74b2015-02-14 23:00:47 -050064 if (::aos::joystick_state->enabled) outputs_enabled = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050065 if (::aos::robot_state->outputs_enabled) {
Brian Silverman295f74b2015-02-14 23:00:47 -050066 // If the driver's station reports being disabled, we're probably not
67 // actually going to send motor values regardless of what the FPGA
68 // reports.
69 if (::aos::joystick_state->enabled) {
70 last_pwm_sent_ = ::aos::robot_state->sent_time;
71 } else {
72 LOG(WARNING, "outputs enabled while disabled\n");
73 }
74 } else if (::aos::joystick_state->enabled) {
75 LOG(WARNING, "outputs disabled while enabled\n");
Brian Silverman699f0cb2015-02-05 19:45:01 -050076 }
Brian Silverman488005d2015-03-15 14:05:42 -070077 } else if (::aos::joystick_state.IsNewerThanMS(kDSPacketTimeoutMs)) {
78 if (::aos::joystick_state->enabled) {
79 outputs_enabled = true;
80 }
brians343bc112013-02-10 01:53:46 +000081 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050082 if (::aos::joystick_state.get()) {
Brian Silverman9c9ab422014-02-25 13:42:53 -080083 LOG_INTERVAL(driver_station_old_);
brians343bc112013-02-10 01:53:46 +000084 } else {
Brian Silverman9c9ab422014-02-25 13:42:53 -080085 LOG_INTERVAL(no_driver_station_);
brians343bc112013-02-10 01:53:46 +000086 }
87 }
88
Brian Silverman38111502014-04-10 12:36:26 -070089 const bool motors_off =
Brian Silverman699f0cb2015-02-05 19:45:01 -050090 (::aos::time::Time::Now() - last_pwm_sent_) >= kPwmDisableTime;
Brian Silverman2704ecf2014-04-09 20:24:03 -070091 if (motors_off) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050092 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) {
Brian Silverman2704ecf2014-04-09 20:24:03 -070093 LOG_INTERVAL(motors_off_log_);
94 }
95 outputs_enabled = false;
96 }
97
brians343bc112013-02-10 01:53:46 +000098 aos::ScopedMessagePtr<StatusType> status =
99 control_loop_->status.MakeMessage();
Brian Silverman699f0cb2015-02-05 19:45:01 -0500100 if (status.get() == nullptr) {
brians343bc112013-02-10 01:53:46 +0000101 return;
102 }
103
104 if (outputs_enabled) {
105 aos::ScopedMessagePtr<OutputType> output =
106 control_loop_->output.MakeMessage();
107 RunIteration(goal, position, output.get(), status.get());
108
Brian Silvermand6974f42014-02-14 13:39:21 -0800109 LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +0000110 output.Send();
111 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500112 // The outputs are disabled, so pass nullptr in for the output.
Ben Fredrickson4283bb42014-02-22 08:31:50 +0000113 RunIteration(goal, position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +0000114 ZeroOutputs();
115 }
116
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() {
brians343bc112013-02-10 01:53:46 +0000123 while (true) {
brians343bc112013-02-10 01:53:46 +0000124 Iterate();
125 }
126}
127
Brian Silverman38111502014-04-10 12:36:26 -0700128} // namespace controls
brians343bc112013-02-10 01:53:46 +0000129} // namespace aos