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 | } |
| 53 | LOG(INFO, "Done moving\n"); |
| 54 | } |
| 55 | |
| 56 | ::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>> |
| 57 | MakeDrivetrainAction() { |
| 58 | return ::std::unique_ptr< |
| 59 | TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>>( |
| 60 | new TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>( |
| 61 | &::frc971::actions::drivetrain_action)); |
| 62 | } |
| 63 | |
| 64 | } // namespace actions |
| 65 | } // namespace frc971 |