blob: 1544cd0a8b7f928e747cb1f83ba00606d9af7070 [file] [log] [blame]
Daniel Petti3b1e48f2015-02-15 15:57:53 -08001#include "frc971/actors/drivetrain_actor.h"
Ben Fredricksond69f38b2015-01-28 20:06:15 -08002
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
Daniel Petti3b1e48f2015-02-15 15:57:53 -080014#include "frc971/actors/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 {
Daniel Petti3b1e48f2015-02-15 15:57:53 -080019namespace actors {
Austin Schuh80ff2e12014-03-08 12:06:19 -080020
Daniel Petti3b1e48f2015-02-15 15:57:53 -080021DrivetrainActor::DrivetrainActor(actors::DrivetrainActionQueueGroup* s)
22 : aos::common::actions::ActorBase<actors::DrivetrainActionQueueGroup>(s) {}
Austin Schuh80ff2e12014-03-08 12:06:19 -080023
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080024bool DrivetrainActor::RunAction(const actors::DrivetrainActionParams &params) {
Brian Silvermanb94069c2014-04-17 14:34:24 -070025 static const auto K = constants::GetValues().make_drivetrain_loop().K();
26
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080027 const double yoffset = params.y_offset;
Brian Silvermanad9e0002014-04-13 14:55:57 -070028 const double turn_offset =
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080029 params.theta_offset * constants::GetValues().turn_width / 2.0;
Brian Silvermanad9e0002014-04-13 14:55:57 -070030 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
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080040 profile.set_maximum_acceleration(params.maximum_acceleration);
41 profile.set_maximum_velocity(params.maximum_velocity);
Austin Schuh3fc6d5f2015-04-18 22:59:20 -070042 turn_profile.set_maximum_acceleration(params.maximum_turn_acceleration *
43 constants::GetValues().turn_width /
44 2.0);
45 turn_profile.set_maximum_velocity(params.maximum_turn_velocity *
46 constants::GetValues().turn_width / 2.0);
Austin Schuh80ff2e12014-03-08 12:06:19 -080047
48 while (true) {
Brian Silverman547abb32015-02-16 00:37:48 -050049 ::aos::time::PhasedLoopXMS(5, 2500);
Austin Schuh80ff2e12014-03-08 12:06:19 -080050
Brian Silvermanada5f2c2015-02-01 02:41:14 -050051 control_loops::drivetrain_queue.status.FetchLatest();
52 if (control_loops::drivetrain_queue.status.get()) {
53 const auto& status = *control_loops::drivetrain_queue.status;
Brian Silvermanb94069c2014-04-17 14:34:24 -070054 if (::std::abs(status.uncapped_left_voltage -
Ben Fredricksond69f38b2015-01-28 20:06:15 -080055 status.uncapped_right_voltage) > 24) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -070056 LOG(DEBUG, "spinning in place\n");
Brian Silvermanb94069c2014-04-17 14:34:24 -070057 // They're more than 24V apart, so stop moving forwards and let it deal
58 // with spinning first.
59 profile.SetGoal(
60 (status.filtered_left_position + status.filtered_right_position) /
61 2.0);
62 } else {
Brian Silverman38ea9bf2014-04-19 22:57:54 -070063 static const double divisor = K(0, 0) + K(0, 2);
Brian Silvermanb94069c2014-04-17 14:34:24 -070064 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.
85 dx = 0;
86 } else if (::std::abs(dx_left) > ::std::abs(dx_right)) {
87 dx = dx_left;
88 } else {
89 dx = dx_right;
90 }
91 if (dx != 0) {
92 LOG(DEBUG, "adjusting goal by %f\n", dx);
93 profile.MoveGoal(-dx);
94 }
95 }
96 } else {
97 // If we ever get here, that's bad and we should just give up
Daniel Petti3b1e48f2015-02-15 15:57:53 -080098 LOG(ERROR, "no drivetrain status!\n");
99 return false;
Brian Silvermanb94069c2014-04-17 14:34:24 -0700100 }
101
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700102 const auto drive_profile_goal_state =
103 profile.Update(yoffset, goal_velocity);
104 const auto turn_profile_goal_state = turn_profile.Update(turn_offset, 0.0);
105 left_goal_state = drive_profile_goal_state - turn_profile_goal_state;
106 right_goal_state = drive_profile_goal_state + turn_profile_goal_state;
107
Brian Silvermanad9e0002014-04-13 14:55:57 -0700108 if (::std::abs(drive_profile_goal_state(0, 0) - yoffset) < epsilon &&
109 ::std::abs(turn_profile_goal_state(0, 0) - turn_offset) < epsilon) {
110 break;
111 }
Daniel Petti3b1e48f2015-02-15 15:57:53 -0800112
113 if (ShouldCancel()) return true;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800114
115 LOG(DEBUG, "Driving left to %f, right to %f\n",
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800116 left_goal_state(0, 0) + params.left_initial_position,
117 right_goal_state(0, 0) + params.right_initial_position);
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500118 control_loops::drivetrain_queue.goal.MakeWithBuilder()
Austin Schuh80ff2e12014-03-08 12:06:19 -0800119 .control_loop_driving(true)
Brian Silverman93335ae2015-01-26 20:43:39 -0500120 //.highgear(false)
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800121 .left_goal(left_goal_state(0, 0) + params.left_initial_position)
122 .right_goal(right_goal_state(0, 0) + params.right_initial_position)
Brian Silvermanad9e0002014-04-13 14:55:57 -0700123 .left_velocity_goal(left_goal_state(1, 0))
124 .right_velocity_goal(right_goal_state(1, 0))
Austin Schuh80ff2e12014-03-08 12:06:19 -0800125 .Send();
126 }
Daniel Petti3b1e48f2015-02-15 15:57:53 -0800127 if (ShouldCancel()) return true;
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500128 control_loops::drivetrain_queue.status.FetchLatest();
129 while (!control_loops::drivetrain_queue.status.get()) {
Austin Schuh577edf62014-04-13 10:33:05 -0700130 LOG(WARNING,
131 "No previous drivetrain status packet, trying to fetch again\n");
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500132 control_loops::drivetrain_queue.status.FetchNextBlocking();
Daniel Petti3b1e48f2015-02-15 15:57:53 -0800133 if (ShouldCancel()) return true;
Austin Schuh577edf62014-04-13 10:33:05 -0700134 }
135 while (true) {
Daniel Petti3b1e48f2015-02-15 15:57:53 -0800136 if (ShouldCancel()) return true;
Austin Schuh577edf62014-04-13 10:33:05 -0700137 const double kPositionThreshold = 0.05;
138
139 const double left_error = ::std::abs(
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500140 control_loops::drivetrain_queue.status->filtered_left_position -
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800141 (left_goal_state(0, 0) + params.left_initial_position));
Austin Schuh577edf62014-04-13 10:33:05 -0700142 const double right_error = ::std::abs(
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500143 control_loops::drivetrain_queue.status->filtered_right_position -
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800144 (right_goal_state(0, 0) + params.right_initial_position));
Austin Schuh577edf62014-04-13 10:33:05 -0700145 const double velocity_error =
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500146 ::std::abs(control_loops::drivetrain_queue.status->robot_speed);
Austin Schuh577edf62014-04-13 10:33:05 -0700147 if (left_error < kPositionThreshold && right_error < kPositionThreshold &&
148 velocity_error < 0.2) {
149 break;
150 } else {
151 LOG(DEBUG, "Drivetrain error is %f, %f, %f\n", left_error, right_error,
152 velocity_error);
153 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500154 control_loops::drivetrain_queue.status.FetchNextBlocking();
Austin Schuh577edf62014-04-13 10:33:05 -0700155 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800156 LOG(INFO, "Done moving\n");
Daniel Petti3b1e48f2015-02-15 15:57:53 -0800157 return true;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800158}
159
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800160::std::unique_ptr<DrivetrainAction> MakeDrivetrainAction(
161 const ::frc971::actors::DrivetrainActionParams& params) {
162 return ::std::unique_ptr<DrivetrainAction>(
163 new DrivetrainAction(&::frc971::actors::drivetrain_action, params));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800164}
165
Daniel Petti3b1e48f2015-02-15 15:57:53 -0800166} // namespace actors
Austin Schuh80ff2e12014-03-08 12:06:19 -0800167} // namespace frc971