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