blob: bf2eea495f6ee8872ad1f1f26c258686a5de0ffb [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 Silverman089f5812015-02-15 01:58:19 -050014template <class T>
15constexpr ::aos::time::Time ControlLoop<T>::kStaleLogInterval;
16template <class T>
17constexpr ::aos::time::Time ControlLoop<T>::kPwmDisableTime;
Brian Silverman50a9d032014-02-16 17:20:57 -080018
Brian Silverman089f5812015-02-15 01:58:19 -050019template <class T>
20void ControlLoop<T>::ZeroOutputs() {
brians343bc112013-02-10 01:53:46 +000021 aos::ScopedMessagePtr<OutputType> output =
22 control_loop_->output.MakeMessage();
23 Zero(output.get());
24 output.Send();
25}
26
Brian Silverman089f5812015-02-15 01:58:19 -050027template <class T>
28void ControlLoop<T>::Iterate() {
Brian Silverman699f0cb2015-02-05 19:45:01 -050029 no_goal_.Print();
Brian Silverman9c9ab422014-02-25 13:42:53 -080030 driver_station_old_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050031 no_sensor_state_.Print();
Brian Silverman9c9ab422014-02-25 13:42:53 -080032 no_driver_station_.Print();
Brian Silverman699f0cb2015-02-05 19:45:01 -050033 motors_off_log_.Print();
Brian Silverman6a1cd212014-02-20 21:04:34 -080034
Brian Silvermand8f403a2014-12-13 19:12:04 -050035 control_loop_->position.FetchAnother();
36 const PositionType *const position = control_loop_->position.get();
37 LOG_STRUCT(DEBUG, "position", *position);
38
39 // Fetch the latest control loop goal. If there is no new
brians343bc112013-02-10 01:53:46 +000040 // goal, we will just reuse the old one.
41 // If there is no goal, we haven't started up fully. It isn't worth
42 // the added complexity for each loop implementation to handle that case.
43 control_loop_->goal.FetchLatest();
44 // TODO(aschuh): Check the age here if we want the loop to stop on old
45 // goals.
46 const GoalType *goal = control_loop_->goal.get();
Brian Silverman699f0cb2015-02-05 19:45:01 -050047 if (goal) {
48 LOG_STRUCT(DEBUG, "goal", *goal);
49 } else {
50 LOG_INTERVAL(no_goal_);
brians343bc112013-02-10 01:53:46 +000051 }
Austin Schuh3d6e3df2014-02-17 01:51:03 -080052
Brian Silverman699f0cb2015-02-05 19:45:01 -050053 ::aos::robot_state.FetchLatest();
54 if (!::aos::robot_state.get()) {
55 LOG_INTERVAL(no_sensor_state_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080056 return;
57 }
Brian Silverman699f0cb2015-02-05 19:45:01 -050058 if (sensor_reader_pid_ != ::aos::robot_state->reader_pid) {
59 LOG(INFO, "new sensor reader PID %" PRId32 ", old was %" PRId32 "\n",
60 ::aos::robot_state->reader_pid, sensor_reader_pid_);
Austin Schuh3d6e3df2014-02-17 01:51:03 -080061 reset_ = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050062 sensor_reader_pid_ = ::aos::robot_state->reader_pid;
Brian Silverman71fbee02014-03-13 17:24:54 -070063 }
brians343bc112013-02-10 01:53:46 +000064
brians343bc112013-02-10 01:53:46 +000065 bool outputs_enabled = false;
66
67 // Check to see if we got a driver station packet recently.
Brian Silverman699f0cb2015-02-05 19:45:01 -050068 if (::aos::joystick_state.FetchLatest()) {
Brian Silverman295f74b2015-02-14 23:00:47 -050069 if (::aos::joystick_state->enabled) outputs_enabled = true;
Brian Silverman699f0cb2015-02-05 19:45:01 -050070 if (::aos::robot_state->outputs_enabled) {
Brian Silverman295f74b2015-02-14 23:00:47 -050071 // If the driver's station reports being disabled, we're probably not
72 // actually going to send motor values regardless of what the FPGA
73 // reports.
74 if (::aos::joystick_state->enabled) {
75 last_pwm_sent_ = ::aos::robot_state->sent_time;
76 } else {
77 LOG(WARNING, "outputs enabled while disabled\n");
78 }
79 } else if (::aos::joystick_state->enabled) {
80 LOG(WARNING, "outputs disabled while enabled\n");
Brian Silverman699f0cb2015-02-05 19:45:01 -050081 }
Brian Silverman295f74b2015-02-14 23:00:47 -050082 } else if (::aos::joystick_state.IsNewerThanMS(kDSPacketTimeoutMs) &&
83 ::aos::joystick_state->enabled) {
brians343bc112013-02-10 01:53:46 +000084 outputs_enabled = true;
85 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -050086 if (::aos::joystick_state.get()) {
Brian Silverman9c9ab422014-02-25 13:42:53 -080087 LOG_INTERVAL(driver_station_old_);
brians343bc112013-02-10 01:53:46 +000088 } else {
Brian Silverman9c9ab422014-02-25 13:42:53 -080089 LOG_INTERVAL(no_driver_station_);
brians343bc112013-02-10 01:53:46 +000090 }
91 }
92
Austin Schuh66a3d2f2014-10-21 22:24:00 -070093 // The 100ms is the result of using an oscilliscope to look at the PWM signal
94 // and output of a talon, and timing the delay between the last pulse and the
95 // talon turning off.
Brian Silverman38111502014-04-10 12:36:26 -070096 const bool motors_off =
Brian Silverman699f0cb2015-02-05 19:45:01 -050097 (::aos::time::Time::Now() - last_pwm_sent_) >= kPwmDisableTime;
Brian Silverman2704ecf2014-04-09 20:24:03 -070098 if (motors_off) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050099 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) {
Brian Silverman2704ecf2014-04-09 20:24:03 -0700100 LOG_INTERVAL(motors_off_log_);
101 }
102 outputs_enabled = false;
103 }
104
brians343bc112013-02-10 01:53:46 +0000105 aos::ScopedMessagePtr<StatusType> status =
106 control_loop_->status.MakeMessage();
Brian Silverman699f0cb2015-02-05 19:45:01 -0500107 if (status.get() == nullptr) {
brians343bc112013-02-10 01:53:46 +0000108 return;
109 }
110
111 if (outputs_enabled) {
112 aos::ScopedMessagePtr<OutputType> output =
113 control_loop_->output.MakeMessage();
114 RunIteration(goal, position, output.get(), status.get());
115
Brian Silvermand6974f42014-02-14 13:39:21 -0800116 LOG_STRUCT(DEBUG, "output", *output);
brians343bc112013-02-10 01:53:46 +0000117 output.Send();
118 } else {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500119 // The outputs are disabled, so pass nullptr in for the output.
Ben Fredrickson4283bb42014-02-22 08:31:50 +0000120 RunIteration(goal, position, nullptr, status.get());
brians343bc112013-02-10 01:53:46 +0000121 ZeroOutputs();
122 }
123
Brian Silvermand6974f42014-02-14 13:39:21 -0800124 LOG_STRUCT(DEBUG, "status", *status);
brians343bc112013-02-10 01:53:46 +0000125 status.Send();
126}
127
Brian Silverman089f5812015-02-15 01:58:19 -0500128template <class T>
129void ControlLoop<T>::Run() {
brians343bc112013-02-10 01:53:46 +0000130 while (true) {
brians343bc112013-02-10 01:53:46 +0000131 Iterate();
132 }
133}
134
Brian Silverman38111502014-04-10 12:36:26 -0700135} // namespace controls
brians343bc112013-02-10 01:53:46 +0000136} // namespace aos