brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include <stddef.h> |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 2 | #include <inttypes.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
| 4 | #include "aos/common/logging/logging.h" |
Brian Silverman | 2ac0fbc | 2014-03-20 19:45:13 -0700 | [diff] [blame] | 5 | #include "aos/common/messages/robot_state.q.h" |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 6 | #include "aos/common/logging/queue_logging.h" |
Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 7 | #include "aos/common/util/phased_loop.h" |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 8 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | namespace aos { |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 10 | namespace controls { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 11 | |
| 12 | // TODO(aschuh): Tests. |
| 13 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 14 | template <class T, bool fail_no_goal> |
| 15 | constexpr ::aos::time::Time ControlLoop<T, fail_no_goal>::kStaleLogInterval; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 16 | template <class T, bool fail_no_goal> |
| 17 | constexpr ::aos::time::Time ControlLoop<T, fail_no_goal>::kPwmDisableTime; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 18 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 19 | template <class T, bool fail_no_goal> |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 20 | void |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 21 | ControlLoop<T, fail_no_goal>::ZeroOutputs() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | aos::ScopedMessagePtr<OutputType> output = |
| 23 | control_loop_->output.MakeMessage(); |
| 24 | Zero(output.get()); |
| 25 | output.Send(); |
| 26 | } |
| 27 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 28 | template <class T, bool fail_no_goal> |
| 29 | void ControlLoop<T, fail_no_goal>::Iterate() { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 30 | no_goal_.Print(); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 31 | driver_station_old_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 32 | no_sensor_state_.Print(); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 33 | no_driver_station_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 34 | motors_off_log_.Print(); |
Brian Silverman | 6a1cd21 | 2014-02-20 21:04:34 -0800 | [diff] [blame] | 35 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 36 | 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 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | // 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 Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 48 | if (goal) { |
| 49 | LOG_STRUCT(DEBUG, "goal", *goal); |
| 50 | } else { |
| 51 | LOG_INTERVAL(no_goal_); |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 52 | if (fail_no_goal) { |
| 53 | ZeroOutputs(); |
| 54 | return; |
| 55 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | } |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 57 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 58 | ::aos::robot_state.FetchLatest(); |
| 59 | if (!::aos::robot_state.get()) { |
| 60 | LOG_INTERVAL(no_sensor_state_); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 61 | return; |
| 62 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 63 | 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 Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 66 | reset_ = true; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 67 | sensor_reader_pid_ = ::aos::robot_state->reader_pid; |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 68 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 69 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 70 | bool outputs_enabled = false; |
| 71 | |
| 72 | // Check to see if we got a driver station packet recently. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 73 | if (::aos::joystick_state.FetchLatest()) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 74 | outputs_enabled = true; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 75 | if (::aos::robot_state->outputs_enabled) { |
| 76 | last_pwm_sent_ = ::aos::robot_state->sent_time; |
| 77 | } |
| 78 | } else if (::aos::joystick_state.IsNewerThanMS(kDSPacketTimeoutMs)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 79 | outputs_enabled = true; |
| 80 | } else { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 81 | if (::aos::joystick_state.get()) { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 82 | LOG_INTERVAL(driver_station_old_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 83 | } else { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 84 | LOG_INTERVAL(no_driver_station_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Austin Schuh | 66a3d2f | 2014-10-21 22:24:00 -0700 | [diff] [blame] | 88 | // 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 Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 91 | const bool motors_off = |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 92 | (::aos::time::Time::Now() - last_pwm_sent_) >= kPwmDisableTime; |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 93 | if (motors_off) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 94 | if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) { |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 95 | LOG_INTERVAL(motors_off_log_); |
| 96 | } |
| 97 | outputs_enabled = false; |
| 98 | } |
| 99 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 100 | aos::ScopedMessagePtr<StatusType> status = |
| 101 | control_loop_->status.MakeMessage(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 102 | if (status.get() == nullptr) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | 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 Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 111 | LOG_STRUCT(DEBUG, "output", *output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 112 | output.Send(); |
| 113 | } else { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 114 | // The outputs are disabled, so pass nullptr in for the output. |
Ben Fredrickson | 4283bb4 | 2014-02-22 08:31:50 +0000 | [diff] [blame] | 115 | RunIteration(goal, position, nullptr, status.get()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 116 | ZeroOutputs(); |
| 117 | } |
| 118 | |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 119 | LOG_STRUCT(DEBUG, "status", *status); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 120 | status.Send(); |
| 121 | } |
| 122 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 123 | template <class T, bool fail_no_goal> |
| 124 | void ControlLoop<T, fail_no_goal>::Run() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 125 | while (true) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 126 | Iterate(); |
| 127 | } |
| 128 | } |
| 129 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 130 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 131 | } // namespace aos |