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