blob: a453502a6b8ab9ba28bdb708f90348803a6ef5d1 [file] [log] [blame]
Ben Fredricksond69f38b2015-01-28 20:06:15 -08001#include "frc971/actions/drivetrain_actor.h"
2
Austin Schuh80ff2e12014-03-08 12:06:19 -08003#include <functional>
4#include <numeric>
5
6#include <Eigen/Dense>
7
Brian3afd6fc2014-04-02 20:41:49 -07008#include "aos/common/util/phased_loop.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -08009#include "aos/common/logging/logging.h"
10#include "aos/common/util/trapezoid_profile.h"
Brian Silvermanb94069c2014-04-17 14:34:24 -070011#include "aos/common/commonmath.h"
Ben Fredricksond69f38b2015-01-28 20:06:15 -080012#include "aos/common/time.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080013
Ben Fredricksond69f38b2015-01-28 20:06:15 -080014#include "frc971/actions/drivetrain_actor.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080015#include "frc971/constants.h"
16#include "frc971/control_loops/drivetrain/drivetrain.q.h"
17
18namespace frc971 {
19namespace actions {
20
Ben Fredricksond69f38b2015-01-28 20:06:15 -080021DrivetrainActor::DrivetrainActor(actions::DrivetrainActionQueueGroup* s)
22 : aos::common::actions::ActorBase<actions::DrivetrainActionQueueGroup>(s) {}
Austin Schuh80ff2e12014-03-08 12:06:19 -080023
Ben Fredricksond69f38b2015-01-28 20:06:15 -080024void DrivetrainActor::RunAction() {
Brian Silvermanb94069c2014-04-17 14:34:24 -070025 static const auto K = constants::GetValues().make_drivetrain_loop().K();
26
Austin Schuh80ff2e12014-03-08 12:06:19 -080027 const double yoffset = action_q_->goal->y_offset;
Brian Silvermanad9e0002014-04-13 14:55:57 -070028 const double turn_offset =
29 action_q_->goal->theta_offset * constants::GetValues().turn_width / 2.0;
30 LOG(INFO, "Going to move %f and turn %f\n", yoffset, turn_offset);
Austin Schuh80ff2e12014-03-08 12:06:19 -080031
32 // Measured conversion to get the distance right.
Ben Fredricksond69f38b2015-01-28 20:06:15 -080033 // TODO(sensors): update this time thing for some reason.
Austin Schuh80ff2e12014-03-08 12:06:19 -080034 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10));
Brian Silvermanad9e0002014-04-13 14:55:57 -070035 ::aos::util::TrapezoidProfile turn_profile(::aos::time::Time::InMS(10));
Austin Schuh80ff2e12014-03-08 12:06:19 -080036 const double goal_velocity = 0.0;
37 const double epsilon = 0.01;
Brian Silvermanad9e0002014-04-13 14:55:57 -070038 ::Eigen::Matrix<double, 2, 1> left_goal_state, right_goal_state;
Austin Schuh80ff2e12014-03-08 12:06:19 -080039
Natalia Frumkind1fa52f2014-06-21 15:24:25 -070040 profile.set_maximum_acceleration(action_q_->goal->maximum_acceleration);
Austin Schuh80ff2e12014-03-08 12:06:19 -080041 profile.set_maximum_velocity(action_q_->goal->maximum_velocity);
Brian Silvermanad9e0002014-04-13 14:55:57 -070042 turn_profile.set_maximum_acceleration(
43 10.0 * constants::GetValues().turn_width / 2.0);
44 turn_profile.set_maximum_velocity(3.0 * constants::GetValues().turn_width /
45 2.0);
Austin Schuh80ff2e12014-03-08 12:06:19 -080046
47 while (true) {
Brian Silverman547abb32015-02-16 00:37:48 -050048 ::aos::time::PhasedLoopXMS(5, 2500);
Austin Schuh80ff2e12014-03-08 12:06:19 -080049
Brian Silvermanada5f2c2015-02-01 02:41:14 -050050 control_loops::drivetrain_queue.status.FetchLatest();
51 if (control_loops::drivetrain_queue.status.get()) {
52 const auto& status = *control_loops::drivetrain_queue.status;
Brian Silvermanb94069c2014-04-17 14:34:24 -070053 if (::std::abs(status.uncapped_left_voltage -
Ben Fredricksond69f38b2015-01-28 20:06:15 -080054 status.uncapped_right_voltage) > 24) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -070055 LOG(DEBUG, "spinning in place\n");
Brian Silvermanb94069c2014-04-17 14:34:24 -070056 // They're more than 24V apart, so stop moving forwards and let it deal
57 // with spinning first.
58 profile.SetGoal(
59 (status.filtered_left_position + status.filtered_right_position) /
60 2.0);
61 } else {
Brian Silverman38ea9bf2014-04-19 22:57:54 -070062 static const double divisor = K(0, 0) + K(0, 2);
Brian Silvermanb94069c2014-04-17 14:34:24 -070063 double dx_left, dx_right;
64 if (status.uncapped_left_voltage > 12.0) {
65 dx_left = (status.uncapped_left_voltage - 12.0) / divisor;
66 } else if (status.uncapped_left_voltage < -12.0) {
67 dx_left = (status.uncapped_left_voltage + 12.0) / divisor;
68 } else {
69 dx_left = 0;
70 }
71 if (status.uncapped_right_voltage > 12.0) {
72 dx_right = (status.uncapped_right_voltage - 12.0) / divisor;
73 } else if (status.uncapped_right_voltage < -12.0) {
74 dx_right = (status.uncapped_right_voltage + 12.0) / divisor;
75 } else {
76 dx_right = 0;
77 }
78 double dx;
79 if (dx_left == 0 && dx_right == 0) {
80 dx = 0;
81 } else if (dx_left != 0 && dx_right != 0 &&
82 ::aos::sign(dx_left) != ::aos::sign(dx_right)) {
83 // Both saturating in opposite directions. Don't do anything.
84 dx = 0;
85 } else if (::std::abs(dx_left) > ::std::abs(dx_right)) {
86 dx = dx_left;
87 } else {
88 dx = dx_right;
89 }
90 if (dx != 0) {
91 LOG(DEBUG, "adjusting goal by %f\n", dx);
92 profile.MoveGoal(-dx);
93 }
94 }
95 } else {
96 // If we ever get here, that's bad and we should just give up
97 LOG(FATAL, "no drivetrain status!\n");
98 }
99
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700100 const auto drive_profile_goal_state =
101 profile.Update(yoffset, goal_velocity);
102 const auto turn_profile_goal_state = turn_profile.Update(turn_offset, 0.0);
103 left_goal_state = drive_profile_goal_state - turn_profile_goal_state;
104 right_goal_state = drive_profile_goal_state + turn_profile_goal_state;
105
Brian Silvermanad9e0002014-04-13 14:55:57 -0700106 if (::std::abs(drive_profile_goal_state(0, 0) - yoffset) < epsilon &&
107 ::std::abs(turn_profile_goal_state(0, 0) - turn_offset) < epsilon) {
108 break;
109 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800110 if (ShouldCancel()) return;
111
112 LOG(DEBUG, "Driving left to %f, right to %f\n",
Brian Silvermanad9e0002014-04-13 14:55:57 -0700113 left_goal_state(0, 0) + action_q_->goal->left_initial_position,
114 right_goal_state(0, 0) + action_q_->goal->right_initial_position);
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500115 control_loops::drivetrain_queue.goal.MakeWithBuilder()
Austin Schuh80ff2e12014-03-08 12:06:19 -0800116 .control_loop_driving(true)
Brian Silverman93335ae2015-01-26 20:43:39 -0500117 //.highgear(false)
Brian Silvermanad9e0002014-04-13 14:55:57 -0700118 .left_goal(left_goal_state(0, 0) + action_q_->goal->left_initial_position)
119 .right_goal(right_goal_state(0, 0) + action_q_->goal->right_initial_position)
120 .left_velocity_goal(left_goal_state(1, 0))
121 .right_velocity_goal(right_goal_state(1, 0))
Austin Schuh80ff2e12014-03-08 12:06:19 -0800122 .Send();
123 }
Brian Silvermanad9e0002014-04-13 14:55:57 -0700124 if (ShouldCancel()) return;
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500125 control_loops::drivetrain_queue.status.FetchLatest();
126 while (!control_loops::drivetrain_queue.status.get()) {
Austin Schuh577edf62014-04-13 10:33:05 -0700127 LOG(WARNING,
128 "No previous drivetrain status packet, trying to fetch again\n");
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500129 control_loops::drivetrain_queue.status.FetchNextBlocking();
Austin Schuh577edf62014-04-13 10:33:05 -0700130 if (ShouldCancel()) return;
131 }
132 while (true) {
133 if (ShouldCancel()) return;
134 const double kPositionThreshold = 0.05;
135
136 const double left_error = ::std::abs(
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500137 control_loops::drivetrain_queue.status->filtered_left_position -
Brian Silvermanad9e0002014-04-13 14:55:57 -0700138 (left_goal_state(0, 0) + action_q_->goal->left_initial_position));
Austin Schuh577edf62014-04-13 10:33:05 -0700139 const double right_error = ::std::abs(
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500140 control_loops::drivetrain_queue.status->filtered_right_position -
Brian Silvermanad9e0002014-04-13 14:55:57 -0700141 (right_goal_state(0, 0) + action_q_->goal->right_initial_position));
Austin Schuh577edf62014-04-13 10:33:05 -0700142 const double velocity_error =
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500143 ::std::abs(control_loops::drivetrain_queue.status->robot_speed);
Austin Schuh577edf62014-04-13 10:33:05 -0700144 if (left_error < kPositionThreshold && right_error < kPositionThreshold &&
145 velocity_error < 0.2) {
146 break;
147 } else {
148 LOG(DEBUG, "Drivetrain error is %f, %f, %f\n", left_error, right_error,
149 velocity_error);
150 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500151 control_loops::drivetrain_queue.status.FetchNextBlocking();
Austin Schuh577edf62014-04-13 10:33:05 -0700152 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800153 LOG(INFO, "Done moving\n");
154}
155
Ben Fredricksond69f38b2015-01-28 20:06:15 -0800156::std::unique_ptr<aos::common::actions::TypedAction<
157 ::frc971::actions::DrivetrainActionQueueGroup>>
Austin Schuh80ff2e12014-03-08 12:06:19 -0800158MakeDrivetrainAction() {
Ben Fredricksond69f38b2015-01-28 20:06:15 -0800159 return ::std::unique_ptr<aos::common::actions::TypedAction<
160 ::frc971::actions::DrivetrainActionQueueGroup>>(
161 new aos::common::actions::TypedAction<
162 ::frc971::actions::DrivetrainActionQueueGroup>(
Austin Schuh80ff2e12014-03-08 12:06:19 -0800163 &::frc971::actions::drivetrain_action));
164}
165
166} // namespace actions
167} // namespace frc971