James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 1 | #include "bot3/control_loops/shooter/shooter.h" |
| 2 | |
| 3 | #include "aos/common/control_loop/control_loops.q.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | |
| 6 | #include "bot3/control_loops/shooter/shooter_motor_plant.h" |
| 7 | |
| 8 | namespace bot3 { |
| 9 | namespace control_loops { |
| 10 | |
| 11 | ShooterMotor::ShooterMotor(control_loops::ShooterLoop *my_shooter) |
| 12 | : aos::control_loops::ControlLoop<control_loops::ShooterLoop>(my_shooter), |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 13 | loop_(new StateFeedbackLoop<1, 1, 1>(MakeShooterLoop())), |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 14 | last_velocity_goal_(0) { |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 15 | loop_->Reset(); |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | /*static*/ const double ShooterMotor::dt = 0.01; |
| 19 | |
| 20 | void ShooterMotor::RunIteration( |
| 21 | const control_loops::ShooterLoop::Goal *goal, |
| 22 | const control_loops::ShooterLoop::Position *position, |
| 23 | ::aos::control_loops::Output *output, |
| 24 | control_loops::ShooterLoop::Status *status) { |
| 25 | double velocity_goal = goal->velocity; |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 26 | // Our position here is actually a velocity. |
| 27 | average_velocity_ = |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 28 | (position == NULL ? loop_->X_hat(0, 0) : position->position); |
| 29 | double output_voltage = 0.0; |
| 30 | |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 31 | // TODO (danielp): This must be modified for our index. |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 32 | /* if (index_loop.status.FetchLatest() || index_loop.status.get()) { |
| 33 | if (index_loop.status->is_shooting) { |
| 34 | if (velocity_goal != last_velocity_goal_ && |
| 35 | velocity_goal < 130) { |
| 36 | velocity_goal = last_velocity_goal_; |
| 37 | } |
| 38 | } |
| 39 | } else { |
| 40 | LOG(WARNING, "assuming index isn't shooting\n"); |
| 41 | }*/ |
| 42 | last_velocity_goal_ = velocity_goal; |
| 43 | |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 44 | loop_->Y << average_velocity_; |
| 45 | loop_->R << velocity_goal; |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 46 | |
| 47 | loop_->Update(position, output == NULL); |
| 48 | |
| 49 | // Kill power at low velocity goals. |
| 50 | if (velocity_goal < 1.0) { |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 51 | loop_->U(0) = 0.0; |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 52 | } else { |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 53 | output_voltage = loop_->U(0); |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | LOG(DEBUG, |
Daniel Petti | 6c3331b | 2013-11-03 19:18:10 +0000 | [diff] [blame^] | 57 | "PWM: %f, raw_velocity: %f, xhat[0]: %f, R[0]: %f\n", |
| 58 | output_voltage, average_velocity_, loop_->X_hat[0], loop_->R[0]); |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 59 | |
| 60 | status->average_velocity = average_velocity_; |
| 61 | |
| 62 | // Determine if the velocity is close enough to the goal to be ready. |
| 63 | if (std::abs(velocity_goal - average_velocity_) < 10.0 && |
| 64 | velocity_goal != 0.0) { |
| 65 | LOG(DEBUG, "Steady: "); |
| 66 | status->ready = true; |
| 67 | } else { |
| 68 | LOG(DEBUG, "Not ready: "); |
| 69 | status->ready = false; |
| 70 | } |
| 71 | LOG(DEBUG, "avg = %f goal = %f\n", average_velocity_, velocity_goal); |
| 72 | |
James Kuszmaul | f7f5ec1 | 2013-11-01 17:58:58 -0700 | [diff] [blame] | 73 | if (output) { |
| 74 | output->voltage = output_voltage; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } // namespace control_loops |
| 79 | } // namespace bot3 |