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