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 | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame^] | 14 | template <class T> |
| 15 | constexpr ::aos::time::Time ControlLoop<T>::kStaleLogInterval; |
| 16 | template <class T> |
| 17 | constexpr ::aos::time::Time ControlLoop<T>::kPwmDisableTime; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 18 | |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame^] | 19 | template <class T> |
| 20 | void ControlLoop<T>::ZeroOutputs() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | aos::ScopedMessagePtr<OutputType> output = |
| 22 | control_loop_->output.MakeMessage(); |
| 23 | Zero(output.get()); |
| 24 | output.Send(); |
| 25 | } |
| 26 | |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame^] | 27 | template <class T> |
| 28 | void ControlLoop<T>::Iterate() { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 29 | no_goal_.Print(); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 30 | driver_station_old_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 31 | no_sensor_state_.Print(); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 32 | no_driver_station_.Print(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 33 | motors_off_log_.Print(); |
Brian Silverman | 6a1cd21 | 2014-02-20 21:04:34 -0800 | [diff] [blame] | 34 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 35 | 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 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 40 | // 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 Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 47 | if (goal) { |
| 48 | LOG_STRUCT(DEBUG, "goal", *goal); |
| 49 | } else { |
| 50 | LOG_INTERVAL(no_goal_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 51 | } |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 52 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 53 | ::aos::robot_state.FetchLatest(); |
| 54 | if (!::aos::robot_state.get()) { |
| 55 | LOG_INTERVAL(no_sensor_state_); |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 58 | 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 Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 61 | reset_ = true; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 62 | sensor_reader_pid_ = ::aos::robot_state->reader_pid; |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 63 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 64 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 65 | bool outputs_enabled = false; |
| 66 | |
| 67 | // Check to see if we got a driver station packet recently. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 68 | if (::aos::joystick_state.FetchLatest()) { |
Brian Silverman | 295f74b | 2015-02-14 23:00:47 -0500 | [diff] [blame] | 69 | if (::aos::joystick_state->enabled) outputs_enabled = true; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 70 | if (::aos::robot_state->outputs_enabled) { |
Brian Silverman | 295f74b | 2015-02-14 23:00:47 -0500 | [diff] [blame] | 71 | // 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 Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 81 | } |
Brian Silverman | 295f74b | 2015-02-14 23:00:47 -0500 | [diff] [blame] | 82 | } else if (::aos::joystick_state.IsNewerThanMS(kDSPacketTimeoutMs) && |
| 83 | ::aos::joystick_state->enabled) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 84 | outputs_enabled = true; |
| 85 | } else { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 86 | if (::aos::joystick_state.get()) { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 87 | LOG_INTERVAL(driver_station_old_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | } else { |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 89 | LOG_INTERVAL(no_driver_station_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Austin Schuh | 66a3d2f | 2014-10-21 22:24:00 -0700 | [diff] [blame] | 93 | // 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 Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 96 | const bool motors_off = |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 97 | (::aos::time::Time::Now() - last_pwm_sent_) >= kPwmDisableTime; |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 98 | if (motors_off) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 99 | if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) { |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 100 | LOG_INTERVAL(motors_off_log_); |
| 101 | } |
| 102 | outputs_enabled = false; |
| 103 | } |
| 104 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 105 | aos::ScopedMessagePtr<StatusType> status = |
| 106 | control_loop_->status.MakeMessage(); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 107 | if (status.get() == nullptr) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 108 | 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 Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 116 | LOG_STRUCT(DEBUG, "output", *output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | output.Send(); |
| 118 | } else { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 119 | // The outputs are disabled, so pass nullptr in for the output. |
Ben Fredrickson | 4283bb4 | 2014-02-22 08:31:50 +0000 | [diff] [blame] | 120 | RunIteration(goal, position, nullptr, status.get()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 121 | ZeroOutputs(); |
| 122 | } |
| 123 | |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 124 | LOG_STRUCT(DEBUG, "status", *status); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 125 | status.Send(); |
| 126 | } |
| 127 | |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame^] | 128 | template <class T> |
| 129 | void ControlLoop<T>::Run() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | while (true) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 131 | Iterate(); |
| 132 | } |
| 133 | } |
| 134 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 135 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 136 | } // namespace aos |