blob: 00686f01860cc57f0edca73152d4d0701fdac65e [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/control_loops/drivetrain/drivetrain.h"
2
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
6#include <memory>
7#include "Eigen/Dense"
8
9#include "aos/common/logging/logging.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070010#include "aos/common/logging/queue_logging.h"
11#include "aos/common/logging/matrix_logging.h"
12
13#include "y2014/constants.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070014#include "y2014/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh0e997732015-11-08 15:14:53 -080015#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -080016#include "y2014/control_loops/drivetrain/polydrivetrain.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080017#include "y2014/control_loops/drivetrain/ssdrivetrain.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070018#include "frc971/queues/gyro.q.h"
19#include "frc971/shifter_hall_effect.h"
20
21// A consistent way to mark code that goes away without shifters. It's still
22// here because we will have shifters again in the future.
23#define HAVE_SHIFTERS 1
24
25using frc971::sensors::gyro_reading;
26
Austin Schuh6197a182015-11-28 16:04:40 -080027namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070028namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080029namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070030
Austin Schuh6197a182015-11-28 16:04:40 -080031void DrivetrainLoop::RunIteration(
32 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
33 const ::frc971::control_loops::DrivetrainQueue::Position *position,
34 ::frc971::control_loops::DrivetrainQueue::Output *output,
35 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -070036 bool bad_pos = false;
37 if (position == nullptr) {
38 LOG_INTERVAL(no_position_);
39 bad_pos = true;
40 }
41 no_position_.Print();
42
43 bool control_loop_driving = false;
44 if (goal) {
45 double wheel = goal->steering;
46 double throttle = goal->throttle;
47 bool quickturn = goal->quickturn;
48#if HAVE_SHIFTERS
49 bool highgear = goal->highgear;
50#endif
51
52 control_loop_driving = goal->control_loop_driving;
53 double left_goal = goal->left_goal;
54 double right_goal = goal->right_goal;
55
Austin Schuh64ebab22015-11-26 13:28:30 -080056 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
57 goal->right_velocity_goal);
Brian Silverman17f503e2015-08-02 18:17:18 -070058#if HAVE_SHIFTERS
Austin Schuh64ebab22015-11-26 13:28:30 -080059 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070060#else
Austin Schuh64ebab22015-11-26 13:28:30 -080061 dt_openloop_.SetGoal(wheel, throttle, quickturn, false);
Brian Silverman17f503e2015-08-02 18:17:18 -070062#endif
63 }
64
65 if (!bad_pos) {
66 const double left_encoder = position->left_encoder;
67 const double right_encoder = position->right_encoder;
68 if (gyro_reading.FetchLatest()) {
69 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
Austin Schuh64ebab22015-11-26 13:28:30 -080070 dt_closedloop_.SetPosition(left_encoder, right_encoder,
71 gyro_reading->angle);
Brian Silverman17f503e2015-08-02 18:17:18 -070072 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080073 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070074 }
75 }
Austin Schuh64ebab22015-11-26 13:28:30 -080076 dt_openloop_.SetPosition(position);
77 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -070078
79 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -080080 dt_closedloop_.Update(output == NULL, true);
81 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070082 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080083 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070084 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -080085 dt_closedloop_.SetExternalMotors(output->left_voltage,
86 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -070087 }
Austin Schuh64ebab22015-11-26 13:28:30 -080088 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -070089 }
90
91 // set the output status of the control loop state
92 if (status) {
93 bool done = false;
94 if (goal) {
95 done = ((::std::abs(goal->left_goal -
Austin Schuh64ebab22015-11-26 13:28:30 -080096 dt_closedloop_.GetEstimatedLeftEncoder()) <
Brian Silverman17f503e2015-08-02 18:17:18 -070097 constants::GetValues().drivetrain_done_distance) &&
98 (::std::abs(goal->right_goal -
Austin Schuh64ebab22015-11-26 13:28:30 -080099 dt_closedloop_.GetEstimatedRightEncoder()) <
Brian Silverman17f503e2015-08-02 18:17:18 -0700100 constants::GetValues().drivetrain_done_distance));
101 }
102 status->is_done = done;
Austin Schuh64ebab22015-11-26 13:28:30 -0800103 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
104 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
105 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700106
Austin Schuh64ebab22015-11-26 13:28:30 -0800107 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
108 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
109 status->output_was_capped = dt_closedloop_.OutputWasCapped();
110 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
111 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700112 }
113}
114
Austin Schuh6197a182015-11-28 16:04:40 -0800115} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700116} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -0800117} // namespace y2014