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