Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 1 | #include <functional> |
| 2 | #include <numeric> |
| 3 | |
| 4 | #include <Eigen/Dense> |
| 5 | |
Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 6 | #include "aos/common/util/phased_loop.h" |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 7 | #include "aos/common/logging/logging.h" |
| 8 | #include "aos/common/util/trapezoid_profile.h" |
| 9 | |
| 10 | #include "frc971/actions/drivetrain_action.h" |
| 11 | #include "frc971/constants.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 13 | |
| 14 | namespace frc971 { |
| 15 | namespace actions { |
| 16 | |
| 17 | DrivetrainAction::DrivetrainAction(actions::DrivetrainActionQueueGroup* s) |
| 18 | : actions::ActionBase<actions::DrivetrainActionQueueGroup>(s) {} |
| 19 | |
| 20 | void DrivetrainAction::RunAction() { |
| 21 | const double yoffset = action_q_->goal->y_offset; |
| 22 | LOG(INFO, "Going to move %f\n", yoffset); |
| 23 | |
| 24 | // Measured conversion to get the distance right. |
| 25 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 26 | ::Eigen::Matrix<double, 2, 1> profile_goal_state; |
| 27 | const double goal_velocity = 0.0; |
| 28 | const double epsilon = 0.01; |
| 29 | |
Austin Schuh | a25a041 | 2014-03-09 00:50:04 -0800 | [diff] [blame] | 30 | profile.set_maximum_acceleration(2.2); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 31 | profile.set_maximum_velocity(action_q_->goal->maximum_velocity); |
| 32 | |
| 33 | while (true) { |
| 34 | // wait until next 10ms tick |
| 35 | ::aos::time::PhasedLoop10MS(5000); |
| 36 | profile_goal_state = profile.Update(yoffset, goal_velocity); |
| 37 | |
| 38 | if (::std::abs(profile_goal_state(0, 0) - yoffset) < epsilon) break; |
| 39 | if (ShouldCancel()) return; |
| 40 | |
| 41 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 42 | profile_goal_state(0, 0) + action_q_->goal->left_initial_position, |
| 43 | profile_goal_state(0, 0) + action_q_->goal->right_initial_position); |
| 44 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 45 | .control_loop_driving(true) |
| 46 | .highgear(false) |
| 47 | .left_goal(profile_goal_state(0, 0) + action_q_->goal->left_initial_position) |
| 48 | .right_goal(profile_goal_state(0, 0) + action_q_->goal->right_initial_position) |
| 49 | .left_velocity_goal(profile_goal_state(1, 0)) |
| 50 | .right_velocity_goal(profile_goal_state(1, 0)) |
| 51 | .Send(); |
| 52 | } |
Austin Schuh | 577edf6 | 2014-04-13 10:33:05 -0700 | [diff] [blame^] | 53 | control_loops::drivetrain.status.FetchLatest(); |
| 54 | while (!control_loops::drivetrain.status.get()) { |
| 55 | LOG(WARNING, |
| 56 | "No previous drivetrain status packet, trying to fetch again\n"); |
| 57 | control_loops::drivetrain.status.FetchNextBlocking(); |
| 58 | if (ShouldCancel()) return; |
| 59 | } |
| 60 | while (true) { |
| 61 | if (ShouldCancel()) return; |
| 62 | const double kPositionThreshold = 0.05; |
| 63 | |
| 64 | const double left_error = ::std::abs( |
| 65 | control_loops::drivetrain.status->filtered_left_position - |
| 66 | (profile_goal_state(0, 0) + action_q_->goal->left_initial_position)); |
| 67 | const double right_error = ::std::abs( |
| 68 | control_loops::drivetrain.status->filtered_right_position - |
| 69 | (profile_goal_state(0, 0) + action_q_->goal->right_initial_position)); |
| 70 | const double velocity_error = |
| 71 | ::std::abs(control_loops::drivetrain.status->robot_speed); |
| 72 | if (left_error < kPositionThreshold && right_error < kPositionThreshold && |
| 73 | velocity_error < 0.2) { |
| 74 | break; |
| 75 | } else { |
| 76 | LOG(DEBUG, "Drivetrain error is %f, %f, %f\n", left_error, right_error, |
| 77 | velocity_error); |
| 78 | } |
| 79 | control_loops::drivetrain.status.FetchNextBlocking(); |
| 80 | } |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 81 | LOG(INFO, "Done moving\n"); |
| 82 | } |
| 83 | |
| 84 | ::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>> |
| 85 | MakeDrivetrainAction() { |
| 86 | return ::std::unique_ptr< |
| 87 | TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>>( |
| 88 | new TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>( |
| 89 | &::frc971::actions::drivetrain_action)); |
| 90 | } |
| 91 | |
| 92 | } // namespace actions |
| 93 | } // namespace frc971 |