blob: c0ee47bc8f2bad9027018a704f2d0f93ba634acb [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#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 Schuh3130b372016-02-17 00:34:51 -080014#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070015#include "y2014/actors/drivetrain_actor.h"
16#include "y2014/constants.h"
Austin Schuh3130b372016-02-17 00:34:51 -080017#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070018
Brian Silvermanb601d892015-12-20 18:24:38 -050019namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070020namespace actors {
21
Austin Schuh214e9c12016-11-25 17:26:20 -080022namespace chrono = ::std::chrono;
23
Brian Silverman17f503e2015-08-02 18:17:18 -070024DrivetrainActor::DrivetrainActor(actors::DrivetrainActionQueueGroup* s)
Austin Schuhadf2cde2015-11-08 20:35:16 -080025 : aos::common::actions::ActorBase<actors::DrivetrainActionQueueGroup>(s),
26 loop_(constants::GetValues().make_drivetrain_loop()) {
27 loop_.set_controller_index(3);
28}
Brian Silverman17f503e2015-08-02 18:17:18 -070029
30bool DrivetrainActor::RunAction(const actors::DrivetrainActionParams &params) {
Austin Schuhadf2cde2015-11-08 20:35:16 -080031 static const auto K = loop_.K();
Brian Silverman17f503e2015-08-02 18:17:18 -070032
33 const double yoffset = params.y_offset;
34 const double turn_offset =
Austin Schuh3130b372016-02-17 00:34:51 -080035 params.theta_offset * control_loops::drivetrain::kRobotRadius;
Brian Silverman17f503e2015-08-02 18:17:18 -070036 LOG(INFO, "Going to move %f and turn %f\n", yoffset, turn_offset);
37
38 // Measured conversion to get the distance right.
Austin Schuh214e9c12016-11-25 17:26:20 -080039 ::aos::util::TrapezoidProfile profile(chrono::milliseconds(5));
40 ::aos::util::TrapezoidProfile turn_profile(chrono::milliseconds(5));
Brian Silverman17f503e2015-08-02 18:17:18 -070041 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 Schuh3130b372016-02-17 00:34:51 -080047 turn_profile.set_maximum_acceleration(
48 params.maximum_turn_acceleration *
49 control_loops::drivetrain::kRobotRadius);
Brian Silverman17f503e2015-08-02 18:17:18 -070050 turn_profile.set_maximum_velocity(params.maximum_turn_velocity *
Austin Schuh3130b372016-02-17 00:34:51 -080051 control_loops::drivetrain::kRobotRadius);
Brian Silverman17f503e2015-08-02 18:17:18 -070052
53 while (true) {
Austin Schuhadf2cde2015-11-08 20:35:16 -080054 ::aos::time::PhasedLoopXMS(5, 2500);
Brian Silverman17f503e2015-08-02 18:17:18 -070055
Comran Morshed5323ecb2015-12-26 20:50:55 +000056 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
57 if (::frc971::control_loops::drivetrain_queue.status.get()) {
Austin Schuh3130b372016-02-17 00:34:51 -080058 const auto &status = *::frc971::control_loops::drivetrain_queue.status;
Brian Silverman17f503e2015-08-02 18:17:18 -070059 if (::std::abs(status.uncapped_left_voltage -
60 status.uncapped_right_voltage) > 24) {
61 LOG(DEBUG, "spinning in place\n");
62 // They're more than 24V apart, so stop moving forwards and let it deal
63 // with spinning first.
64 profile.SetGoal(
Austin Schuh093535c2016-03-05 23:21:00 -080065 (status.estimated_left_position + status.estimated_right_position -
Brian Silverman17f503e2015-08-02 18:17:18 -070066 params.left_initial_position - params.right_initial_position) /
67 2.0);
68 } else {
69 static const double divisor = K(0, 0) + K(0, 2);
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.
91 LOG(DEBUG, "Saturating opposite ways, not adjusting\n");
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
105 LOG(ERROR, "no drivetrain status!\n");
106 return false;
107 }
108
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
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 }
119
120 if (ShouldCancel()) return true;
121
122 LOG(DEBUG, "Driving left to %f, right to %f\n",
123 left_goal_state(0, 0) + params.left_initial_position,
124 right_goal_state(0, 0) + params.right_initial_position);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000125 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman17f503e2015-08-02 18:17:18 -0700126 .control_loop_driving(true)
Austin Schuh86f895e2015-11-08 13:40:51 -0800127 .highgear(true)
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 .left_goal(left_goal_state(0, 0) + params.left_initial_position)
129 .right_goal(right_goal_state(0, 0) + params.right_initial_position)
130 .left_velocity_goal(left_goal_state(1, 0))
131 .right_velocity_goal(right_goal_state(1, 0))
132 .Send();
133 }
134 if (ShouldCancel()) return true;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000135 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
136 while (!::frc971::control_loops::drivetrain_queue.status.get()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 LOG(WARNING,
138 "No previous drivetrain status packet, trying to fetch again\n");
Comran Morshed5323ecb2015-12-26 20:50:55 +0000139 ::frc971::control_loops::drivetrain_queue.status.FetchNextBlocking();
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 if (ShouldCancel()) return true;
141 }
142 while (true) {
143 if (ShouldCancel()) return true;
144 const double kPositionThreshold = 0.05;
145
Austin Schuh3130b372016-02-17 00:34:51 -0800146 const double left_error =
147 ::std::abs(::frc971::control_loops::drivetrain_queue.status
Austin Schuh093535c2016-03-05 23:21:00 -0800148 ->estimated_left_position -
Austin Schuh3130b372016-02-17 00:34:51 -0800149 (left_goal_state(0, 0) + params.left_initial_position));
150 const double right_error =
151 ::std::abs(::frc971::control_loops::drivetrain_queue.status
Austin Schuh093535c2016-03-05 23:21:00 -0800152 ->estimated_right_position -
Austin Schuh3130b372016-02-17 00:34:51 -0800153 (right_goal_state(0, 0) + params.right_initial_position));
154 const double velocity_error = ::std::abs(
155 ::frc971::control_loops::drivetrain_queue.status->robot_speed);
Brian Silverman17f503e2015-08-02 18:17:18 -0700156 if (left_error < kPositionThreshold && right_error < kPositionThreshold &&
157 velocity_error < 0.2) {
158 break;
159 } else {
160 LOG(DEBUG, "Drivetrain error is %f, %f, %f\n", left_error, right_error,
161 velocity_error);
162 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000163 ::frc971::control_loops::drivetrain_queue.status.FetchNextBlocking();
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 }
165 LOG(INFO, "Done moving\n");
166 return true;
167}
168
169::std::unique_ptr<DrivetrainAction> MakeDrivetrainAction(
Austin Schuh3130b372016-02-17 00:34:51 -0800170 const ::y2014::actors::DrivetrainActionParams &params) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 return ::std::unique_ptr<DrivetrainAction>(
Brian Silvermanb601d892015-12-20 18:24:38 -0500172 new DrivetrainAction(&::y2014::actors::drivetrain_action, params));
Brian Silverman17f503e2015-08-02 18:17:18 -0700173}
174
175} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500176} // namespace y2014