blob: 4cb69cabaedd35eee4da484fef771abbda06afc7 [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
14#include "y2014/actors/drivetrain_actor.h"
15#include "y2014/constants.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000016#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070017
Brian Silvermanb601d892015-12-20 18:24:38 -050018namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070019namespace actors {
20
21DrivetrainActor::DrivetrainActor(actors::DrivetrainActionQueueGroup* s)
Austin Schuhadf2cde2015-11-08 20:35:16 -080022 : aos::common::actions::ActorBase<actors::DrivetrainActionQueueGroup>(s),
23 loop_(constants::GetValues().make_drivetrain_loop()) {
24 loop_.set_controller_index(3);
25}
Brian Silverman17f503e2015-08-02 18:17:18 -070026
27bool DrivetrainActor::RunAction(const actors::DrivetrainActionParams &params) {
Austin Schuhadf2cde2015-11-08 20:35:16 -080028 static const auto K = loop_.K();
Brian Silverman17f503e2015-08-02 18:17:18 -070029
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 Schuhadf2cde2015-11-08 20:35:16 -080036 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(5));
37 ::aos::util::TrapezoidProfile turn_profile(::aos::time::Time::InMS(5));
Brian Silverman17f503e2015-08-02 18:17:18 -070038 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 Schuhadf2cde2015-11-08 20:35:16 -080051 ::aos::time::PhasedLoopXMS(5, 2500);
Brian Silverman17f503e2015-08-02 18:17:18 -070052
Comran Morshed5323ecb2015-12-26 20:50:55 +000053 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
54 if (::frc971::control_loops::drivetrain_queue.status.get()) {
55 const auto& status = *::frc971::control_loops::drivetrain_queue.status;
Brian Silverman17f503e2015-08-02 18:17:18 -070056 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);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000122 ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 .control_loop_driving(true)
Austin Schuh86f895e2015-11-08 13:40:51 -0800124 .highgear(true)
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 .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;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000132 ::frc971::control_loops::drivetrain_queue.status.FetchLatest();
133 while (!::frc971::control_loops::drivetrain_queue.status.get()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700134 LOG(WARNING,
135 "No previous drivetrain status packet, trying to fetch again\n");
Comran Morshed5323ecb2015-12-26 20:50:55 +0000136 ::frc971::control_loops::drivetrain_queue.status.FetchNextBlocking();
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 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(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000144 ::frc971::control_loops::drivetrain_queue.status->filtered_left_position -
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 (left_goal_state(0, 0) + params.left_initial_position));
146 const double right_error = ::std::abs(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000147 ::frc971::control_loops::drivetrain_queue.status->filtered_right_position -
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 (right_goal_state(0, 0) + params.right_initial_position));
149 const double velocity_error =
Comran Morshed5323ecb2015-12-26 20:50:55 +0000150 ::std::abs(::frc971::control_loops::drivetrain_queue.status->robot_speed);
Brian Silverman17f503e2015-08-02 18:17:18 -0700151 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 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000158 ::frc971::control_loops::drivetrain_queue.status.FetchNextBlocking();
Brian Silverman17f503e2015-08-02 18:17:18 -0700159 }
160 LOG(INFO, "Done moving\n");
161 return true;
162}
163
164::std::unique_ptr<DrivetrainAction> MakeDrivetrainAction(
Brian Silvermanb601d892015-12-20 18:24:38 -0500165 const ::y2014::actors::DrivetrainActionParams& params) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700166 return ::std::unique_ptr<DrivetrainAction>(
Brian Silvermanb601d892015-12-20 18:24:38 -0500167 new DrivetrainAction(&::y2014::actors::drivetrain_action, params));
Brian Silverman17f503e2015-08-02 18:17:18 -0700168}
169
170} // namespace actors
Brian Silvermanb601d892015-12-20 18:24:38 -0500171} // namespace y2014