blob: f732b3db4f022be54fc21799b5d52ab950d32c62 [file] [log] [blame]
James Kuszmaulf7f5ec12013-11-01 17:58:58 -07001#include "bot3/control_loops/shooter/shooter.h"
James Kuszmaulb74c8112013-11-03 16:13:45 -08002#include "bot3/control_loops/shooter/shooter_motor.q.h"
James Kuszmaulf7f5ec12013-11-01 17:58:58 -07003
4#include "aos/common/control_loop/control_loops.q.h"
5#include "aos/common/logging/logging.h"
6
7#include "bot3/control_loops/shooter/shooter_motor_plant.h"
8
9namespace bot3 {
10namespace control_loops {
11
12ShooterMotor::ShooterMotor(control_loops::ShooterLoop *my_shooter)
13 : aos::control_loops::ControlLoop<control_loops::ShooterLoop>(my_shooter),
Daniel Petti6c3331b2013-11-03 19:18:10 +000014 loop_(new StateFeedbackLoop<1, 1, 1>(MakeShooterLoop())),
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070015 last_velocity_goal_(0) {
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070016}
17
18/*static*/ const double ShooterMotor::dt = 0.01;
19
20void ShooterMotor::RunIteration(
21 const control_loops::ShooterLoop::Goal *goal,
22 const control_loops::ShooterLoop::Position *position,
James Kuszmaulb74c8112013-11-03 16:13:45 -080023 control_loops::ShooterLoop::Output *output,
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070024 control_loops::ShooterLoop::Status *status) {
25 double velocity_goal = goal->velocity;
Daniel Petti6c3331b2013-11-03 19:18:10 +000026 // Our position here is actually a velocity.
27 average_velocity_ =
Daniel Petti5003b772013-11-07 02:19:50 +000028 (position == NULL ? loop_->X_hat(0, 0) : position->velocity);
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070029 double output_voltage = 0.0;
30
31/* if (index_loop.status.FetchLatest() || index_loop.status.get()) {
32 if (index_loop.status->is_shooting) {
33 if (velocity_goal != last_velocity_goal_ &&
34 velocity_goal < 130) {
35 velocity_goal = last_velocity_goal_;
36 }
37 }
38 } else {
39 LOG(WARNING, "assuming index isn't shooting\n");
40 }*/
41 last_velocity_goal_ = velocity_goal;
42
Daniel Petti6c3331b2013-11-03 19:18:10 +000043 loop_->Y << average_velocity_;
44 loop_->R << velocity_goal;
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070045
46 loop_->Update(position, output == NULL);
47
48 // Kill power at low velocity goals.
49 if (velocity_goal < 1.0) {
Daniel Petti6c3331b2013-11-03 19:18:10 +000050 loop_->U(0) = 0.0;
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070051 } else {
Daniel Petti6c3331b2013-11-03 19:18:10 +000052 output_voltage = loop_->U(0);
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070053 }
54
55 LOG(DEBUG,
Daniel Petti6c3331b2013-11-03 19:18:10 +000056 "PWM: %f, raw_velocity: %f, xhat[0]: %f, R[0]: %f\n",
57 output_voltage, average_velocity_, loop_->X_hat[0], loop_->R[0]);
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070058
59 status->average_velocity = average_velocity_;
60
61 // Determine if the velocity is close enough to the goal to be ready.
62 if (std::abs(velocity_goal - average_velocity_) < 10.0 &&
63 velocity_goal != 0.0) {
64 LOG(DEBUG, "Steady: ");
65 status->ready = true;
66 } else {
67 LOG(DEBUG, "Not ready: ");
68 status->ready = false;
69 }
70 LOG(DEBUG, "avg = %f goal = %f\n", average_velocity_, velocity_goal);
71
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070072 if (output) {
73 output->voltage = output_voltage;
James Kuszmaulb74c8112013-11-03 16:13:45 -080074 output->intake = goal->intake;
75 output->push = goal->push;
76 LOG(DEBUG, "goal: %lf, volt: %lf, push:%d\n", goal->intake, output_voltage, goal->push);
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070077 }
78}
79
80} // namespace control_loops
81} // namespace bot3