blob: 6903bc2686545b581b672f09aa5664478278abf5 [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"
Brian3afd6fc2014-04-02 20:41:49 -07007#include "aos/common/util/phased_loop.h"
Austin Schuh3d6e3df2014-02-17 01:51:03 -08008
brians343bc112013-02-10 01:53:46 +00009namespace aos {
Brian Silverman38111502014-04-10 12:36:26 -070010namespace controls {
brians343bc112013-02-10 01:53:46 +000011
12// TODO(aschuh): Tests.
13
Brian Silvermand8f403a2014-12-13 19:12:04 -050014template <class T, bool fail_no_goal>
15constexpr ::aos::time::Time ControlLoop<T, fail_no_goal>::kStaleLogInterval;
Brian Silverman699f0cb2015-02-05 19:45:01 -050016template <class T, bool fail_no_goal>
17constexpr ::aos::time::Time ControlLoop<T, fail_no_goal>::kPwmDisableTime;
Brian Silverman50a9d032014-02-16 17:20:57 -080018
Brian Silvermand8f403a2014-12-13 19:12:04 -050019template <class T, bool fail_no_goal>
Brian Silverman71fbee02014-03-13 17:24:54 -070020void
Brian Silvermand8f403a2014-12-13 19:12:04 -050021ControlLoop<T, fail_no_goal>::ZeroOutputs() {
brians343bc112013-02-10 01:53:46 +000022 aos::ScopedMessagePtr<OutputType> output =
23 control_loop_->output.MakeMessage();
24 Zero(output.get());
25 output.Send();
26}
27
Brian Silvermand8f403a2014-12-13 19:12:04 -050028template <class T, bool fail_no_goal>
29void ControlLoop<T, fail_no_goal>::Iterate() {
Brian Silverman699f0cb2015-02-05 19:45:01 -050030 no_goal_.Print();
Brian Silverman9c9ab422014-02-25 13:42:53 -080031 driver_station_old_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050032 no_sensor_state_.Print();
Brian Silverman9c9ab422014-02-25 13:42:53 -080033 no_driver_station_.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 control_loop_->position.FetchAnother();
37 const PositionType *const position = control_loop_->position.get();
38 LOG_STRUCT(DEBUG, "position", *position);
39
40 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000041 // goal, we will just reuse the old one.
42 // If there is no goal, we haven't started up fully. It isn't worth
43 // the added complexity for each loop implementation to handle that case.
44 control_loop_->goal.FetchLatest();
45 // TODO(aschuh): Check the age here if we want the loop to stop on old
46 // goals.
47 const GoalType *goal = control_loop_->goal.get();
Brian Silverman699f0cb2015-02-05 19:45:01 -050048 if (goal) {
49 LOG_STRUCT(DEBUG, "goal", *goal);
50 } else {
51 LOG_INTERVAL(no_goal_);
Brian Silverman71fbee02014-03-13 17:24:54 -070052 if (fail_no_goal) {
53 ZeroOutputs();
54 return;
55 }
brians343bc112013-02-10 01:53:46 +000056 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080057
Brian Silverman699f0cb2015-02-05 19:45:01 -050058 ::aos::robot_state.FetchLatest();
59 if (!::aos::robot_state.get()) {
60 LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080061 return;
62 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050063 if (sensor_reader_pid_ != ::aos::robot_state->reader_pid) {
64 LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
65 ::aos::robot_state->reader_pid, sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080066 reset_ = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050067 sensor_reader_pid_ = ::aos::robot_state->reader_pid;
Brian Silverman71fbee02014-03-13 17:24:54 -070068 }
brians343bc112013-02-10 01:53:46 +000069
brians343bc112013-02-10 01:53:46 +000070 bool outputs_enabled = false;
71
72 // Check to see if we got a driver station packet recently.
Brian Silverman699f0cb2015-02-05 19:45:01 -050073 if (::aos::joystick_state.FetchLatest()) {
brians343bc112013-02-10 01:53:46 +000074 outputs_enabled = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050075 if (::aos::robot_state->outputs_enabled) {
76 last_pwm_sent_ = ::aos::robot_state->sent_time;
77 }
78 } else if (::aos::joystick_state.IsNewerThanMS(kDSPacketTimeoutMs)) {
brians343bc112013-02-10 01:53:46 +000079 outputs_enabled = true;
80 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050081 if (::aos::joystick_state.get()) {
Brian Silverman9c9ab422014-02-25 13:42:53 -080082 LOG_INTERVAL(driver_station_old_);
brians343bc112013-02-10 01:53:46 +000083 } else {
Brian Silverman9c9ab422014-02-25 13:42:53 -080084 LOG_INTERVAL(no_driver_station_);
brians343bc112013-02-10 01:53:46 +000085 }
86 }
87
Austin Schuh66a3d2f2014-10-21 22:24:00 -070088 // The 100ms is the result of using an oscilliscope to look at the PWM signal
89 // and output of a talon, and timing the delay between the last pulse and the
90 // talon turning off.
Brian Silverman38111502014-04-10 12:36:26 -070091 const bool motors_off =
Brian Silverman699f0cb2015-02-05 19:45:01 -050092 (::aos::time::Time::Now() - last_pwm_sent_) >= kPwmDisableTime;
Brian Silverman2704ecf2014-04-09 20:24:03 -070093 if (motors_off) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050094 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) {
Brian Silverman2704ecf2014-04-09 20:24:03 -070095 LOG_INTERVAL(motors_off_log_);
96 }
97 outputs_enabled = false;
98 }
99
brians343bc112013-02-10 01:53:46 +0000100 aos::ScopedMessagePtr<StatusType> status =
101 control_loop_->status.MakeMessage();
Brian Silverman699f0cb2015-02-05 19:45:01 -0500102 if (status.get() == nullptr) {
brians343bc112013-02-10 01:53:46 +0000103 return;
104 }
105
106 if (outputs_enabled) {
107 aos::ScopedMessagePtr<OutputType> output =
108 control_loop_->output.MakeMessage();
109 RunIteration(goal, position, output.get(), status.get());
110
Brian Silvermand6974f42014-02-14 13:39:21 -0800111 LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +0000112 output.Send();
113 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500114 // The outputs are disabled, so pass nullptr in for the output.
Ben Fredrickson4283bb42014-02-22 08:31:50 +0000115 RunIteration(goal, position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +0000116 ZeroOutputs();
117 }
118
Brian Silvermand6974f42014-02-14 13:39:21 -0800119 LOG_STRUCT(DEBUG, "status", *status);
brians343bc112013-02-10 01:53:46 +0000120 status.Send();
121}
122
Brian Silvermand8f403a2014-12-13 19:12:04 -0500123template <class T, bool fail_no_goal>
124void ControlLoop<T, fail_no_goal>::Run() {
brians343bc112013-02-10 01:53:46 +0000125 while (true) {
brians343bc112013-02-10 01:53:46 +0000126 Iterate();
127 }
128}
129
Brian Silverman38111502014-04-10 12:36:26 -0700130} // namespace controls
brians343bc112013-02-10 01:53:46 +0000131} // namespace aos