blob: 0fa1de9c4f2067241bfd6386605965c3469ae177 [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
27namespace frc971 {
28namespace control_loops {
29
Brian Silverman17f503e2015-08-02 18:17:18 -070030void DrivetrainLoop::RunIteration(const DrivetrainQueue::Goal *goal,
31 const DrivetrainQueue::Position *position,
32 DrivetrainQueue::Output *output,
Austin Schuh64ebab22015-11-26 13:28:30 -080033 DrivetrainQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -070034 bool bad_pos = false;
35 if (position == nullptr) {
36 LOG_INTERVAL(no_position_);
37 bad_pos = true;
38 }
39 no_position_.Print();
40
41 bool control_loop_driving = false;
42 if (goal) {
43 double wheel = goal->steering;
44 double throttle = goal->throttle;
45 bool quickturn = goal->quickturn;
46#if HAVE_SHIFTERS
47 bool highgear = goal->highgear;
48#endif
49
50 control_loop_driving = goal->control_loop_driving;
51 double left_goal = goal->left_goal;
52 double right_goal = goal->right_goal;
53
Austin Schuh64ebab22015-11-26 13:28:30 -080054 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
55 goal->right_velocity_goal);
Brian Silverman17f503e2015-08-02 18:17:18 -070056#if HAVE_SHIFTERS
Austin Schuh64ebab22015-11-26 13:28:30 -080057 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070058#else
Austin Schuh64ebab22015-11-26 13:28:30 -080059 dt_openloop_.SetGoal(wheel, throttle, quickturn, false);
Brian Silverman17f503e2015-08-02 18:17:18 -070060#endif
61 }
62
63 if (!bad_pos) {
64 const double left_encoder = position->left_encoder;
65 const double right_encoder = position->right_encoder;
66 if (gyro_reading.FetchLatest()) {
67 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
Austin Schuh64ebab22015-11-26 13:28:30 -080068 dt_closedloop_.SetPosition(left_encoder, right_encoder,
69 gyro_reading->angle);
Brian Silverman17f503e2015-08-02 18:17:18 -070070 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080071 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070072 }
73 }
Austin Schuh64ebab22015-11-26 13:28:30 -080074 dt_openloop_.SetPosition(position);
75 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -070076
77 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -080078 dt_closedloop_.Update(output == NULL, true);
79 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070080 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080081 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070082 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -080083 dt_closedloop_.SetExternalMotors(output->left_voltage,
84 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -070085 }
Austin Schuh64ebab22015-11-26 13:28:30 -080086 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -070087 }
88
89 // set the output status of the control loop state
90 if (status) {
91 bool done = false;
92 if (goal) {
93 done = ((::std::abs(goal->left_goal -
Austin Schuh64ebab22015-11-26 13:28:30 -080094 dt_closedloop_.GetEstimatedLeftEncoder()) <
Brian Silverman17f503e2015-08-02 18:17:18 -070095 constants::GetValues().drivetrain_done_distance) &&
96 (::std::abs(goal->right_goal -
Austin Schuh64ebab22015-11-26 13:28:30 -080097 dt_closedloop_.GetEstimatedRightEncoder()) <
Brian Silverman17f503e2015-08-02 18:17:18 -070098 constants::GetValues().drivetrain_done_distance));
99 }
100 status->is_done = done;
Austin Schuh64ebab22015-11-26 13:28:30 -0800101 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
102 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
103 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700104
Austin Schuh64ebab22015-11-26 13:28:30 -0800105 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
106 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
107 status->output_was_capped = dt_closedloop_.OutputWasCapped();
108 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
109 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700110 }
111}
112
113} // namespace control_loops
114} // namespace frc971