Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include "y2014/actors/drivetrain_actor.h" |
| 2 | |
| 3 | #include <functional> |
| 4 | #include <numeric> |
| 5 | |
| 6 | #include <Eigen/Dense> |
| 7 | |
| 8 | #include "aos/common/util/phased_loop.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/util/trapezoid_profile.h" |
| 11 | #include "aos/common/commonmath.h" |
| 12 | #include "aos/common/time.h" |
| 13 | |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 15 | #include "y2014/actors/drivetrain_actor.h" |
| 16 | #include "y2014/constants.h" |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 17 | #include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 18 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 19 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 20 | namespace actors { |
| 21 | |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 22 | namespace chrono = ::std::chrono; |
| 23 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 24 | DrivetrainActor::DrivetrainActor(actors::DrivetrainActionQueueGroup* s) |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 25 | : aos::common::actions::ActorBase<actors::DrivetrainActionQueueGroup>(s), |
| 26 | loop_(constants::GetValues().make_drivetrain_loop()) { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 27 | loop_.set_index(3); |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 28 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 29 | |
| 30 | bool DrivetrainActor::RunAction(const actors::DrivetrainActionParams ¶ms) { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 31 | static const auto &K = loop_.controller().K(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 32 | |
| 33 | const double yoffset = params.y_offset; |
| 34 | const double turn_offset = |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 35 | params.theta_offset * control_loops::drivetrain::kRobotRadius; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 36 | LOG(INFO, "Going to move %f and turn %f\n", yoffset, turn_offset); |
| 37 | |
| 38 | // Measured conversion to get the distance right. |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 39 | ::aos::util::TrapezoidProfile profile(chrono::milliseconds(5)); |
| 40 | ::aos::util::TrapezoidProfile turn_profile(chrono::milliseconds(5)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 41 | const double goal_velocity = 0.0; |
| 42 | const double epsilon = 0.01; |
| 43 | ::Eigen::Matrix<double, 2, 1> left_goal_state, right_goal_state; |
| 44 | |
| 45 | profile.set_maximum_acceleration(params.maximum_acceleration); |
| 46 | profile.set_maximum_velocity(params.maximum_velocity); |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 47 | turn_profile.set_maximum_acceleration( |
| 48 | params.maximum_turn_acceleration * |
| 49 | control_loops::drivetrain::kRobotRadius); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 50 | turn_profile.set_maximum_velocity(params.maximum_turn_velocity * |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 51 | control_loops::drivetrain::kRobotRadius); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 52 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 53 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 54 | ::std::chrono::milliseconds(5) / 2); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 55 | while (true) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 56 | phased_loop.SleepUntilNext(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 57 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 58 | ::frc971::control_loops::drivetrain_queue.status.FetchLatest(); |
| 59 | if (::frc971::control_loops::drivetrain_queue.status.get()) { |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 60 | const auto &status = *::frc971::control_loops::drivetrain_queue.status; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 61 | if (::std::abs(status.uncapped_left_voltage - |
| 62 | status.uncapped_right_voltage) > 24) { |
| 63 | LOG(DEBUG, "spinning in place\n"); |
| 64 | // They're more than 24V apart, so stop moving forwards and let it deal |
| 65 | // with spinning first. |
| 66 | profile.SetGoal( |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 67 | (status.estimated_left_position + status.estimated_right_position - |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 68 | params.left_initial_position - params.right_initial_position) / |
| 69 | 2.0); |
| 70 | } else { |
| 71 | static const double divisor = K(0, 0) + K(0, 2); |
| 72 | double dx_left, dx_right; |
| 73 | if (status.uncapped_left_voltage > 12.0) { |
| 74 | dx_left = (status.uncapped_left_voltage - 12.0) / divisor; |
| 75 | } else if (status.uncapped_left_voltage < -12.0) { |
| 76 | dx_left = (status.uncapped_left_voltage + 12.0) / divisor; |
| 77 | } else { |
| 78 | dx_left = 0; |
| 79 | } |
| 80 | if (status.uncapped_right_voltage > 12.0) { |
| 81 | dx_right = (status.uncapped_right_voltage - 12.0) / divisor; |
| 82 | } else if (status.uncapped_right_voltage < -12.0) { |
| 83 | dx_right = (status.uncapped_right_voltage + 12.0) / divisor; |
| 84 | } else { |
| 85 | dx_right = 0; |
| 86 | } |
| 87 | double dx; |
| 88 | if (dx_left == 0 && dx_right == 0) { |
| 89 | dx = 0; |
| 90 | } else if (dx_left != 0 && dx_right != 0 && |
| 91 | ::aos::sign(dx_left) != ::aos::sign(dx_right)) { |
| 92 | // Both saturating in opposite directions. Don't do anything. |
| 93 | LOG(DEBUG, "Saturating opposite ways, not adjusting\n"); |
| 94 | dx = 0; |
| 95 | } else if (::std::abs(dx_left) > ::std::abs(dx_right)) { |
| 96 | dx = dx_left; |
| 97 | } else { |
| 98 | dx = dx_right; |
| 99 | } |
| 100 | if (dx != 0) { |
| 101 | LOG(DEBUG, "adjusting goal by %f\n", dx); |
| 102 | profile.MoveGoal(-dx); |
| 103 | } |
| 104 | } |
| 105 | } else { |
| 106 | // If we ever get here, that's bad and we should just give up |
| 107 | LOG(ERROR, "no drivetrain status!\n"); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | const auto drive_profile_goal_state = |
| 112 | profile.Update(yoffset, goal_velocity); |
| 113 | const auto turn_profile_goal_state = turn_profile.Update(turn_offset, 0.0); |
| 114 | left_goal_state = drive_profile_goal_state - turn_profile_goal_state; |
| 115 | right_goal_state = drive_profile_goal_state + turn_profile_goal_state; |
| 116 | |
| 117 | if (::std::abs(drive_profile_goal_state(0, 0) - yoffset) < epsilon && |
| 118 | ::std::abs(turn_profile_goal_state(0, 0) - turn_offset) < epsilon) { |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | if (ShouldCancel()) return true; |
| 123 | |
| 124 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 125 | left_goal_state(0, 0) + params.left_initial_position, |
| 126 | right_goal_state(0, 0) + params.right_initial_position); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 127 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 128 | .control_loop_driving(true) |
Austin Schuh | 86f895e | 2015-11-08 13:40:51 -0800 | [diff] [blame] | 129 | .highgear(true) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 130 | .left_goal(left_goal_state(0, 0) + params.left_initial_position) |
| 131 | .right_goal(right_goal_state(0, 0) + params.right_initial_position) |
| 132 | .left_velocity_goal(left_goal_state(1, 0)) |
| 133 | .right_velocity_goal(right_goal_state(1, 0)) |
| 134 | .Send(); |
| 135 | } |
| 136 | if (ShouldCancel()) return true; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 137 | ::frc971::control_loops::drivetrain_queue.status.FetchLatest(); |
| 138 | while (!::frc971::control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 139 | LOG(WARNING, |
| 140 | "No previous drivetrain status packet, trying to fetch again\n"); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 141 | ::frc971::control_loops::drivetrain_queue.status.FetchNextBlocking(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 142 | if (ShouldCancel()) return true; |
| 143 | } |
| 144 | while (true) { |
| 145 | if (ShouldCancel()) return true; |
| 146 | const double kPositionThreshold = 0.05; |
| 147 | |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 148 | const double left_error = |
| 149 | ::std::abs(::frc971::control_loops::drivetrain_queue.status |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 150 | ->estimated_left_position - |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 151 | (left_goal_state(0, 0) + params.left_initial_position)); |
| 152 | const double right_error = |
| 153 | ::std::abs(::frc971::control_loops::drivetrain_queue.status |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 154 | ->estimated_right_position - |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 155 | (right_goal_state(0, 0) + params.right_initial_position)); |
| 156 | const double velocity_error = ::std::abs( |
| 157 | ::frc971::control_loops::drivetrain_queue.status->robot_speed); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 158 | if (left_error < kPositionThreshold && right_error < kPositionThreshold && |
| 159 | velocity_error < 0.2) { |
| 160 | break; |
| 161 | } else { |
| 162 | LOG(DEBUG, "Drivetrain error is %f, %f, %f\n", left_error, right_error, |
| 163 | velocity_error); |
| 164 | } |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 165 | ::frc971::control_loops::drivetrain_queue.status.FetchNextBlocking(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 166 | } |
| 167 | LOG(INFO, "Done moving\n"); |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | ::std::unique_ptr<DrivetrainAction> MakeDrivetrainAction( |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 172 | const ::y2014::actors::DrivetrainActionParams ¶ms) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 173 | return ::std::unique_ptr<DrivetrainAction>( |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 174 | new DrivetrainAction(&::y2014::actors::drivetrain_action, params)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | } // namespace actors |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 178 | } // namespace y2014 |