blob: 2e78e51ceb98dbb5c1414ecafb1dc006b4bbcacb [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001package frc971.control_loops;
Brian Silverman17f503e2015-08-02 18:17:18 -07002
John Park33858a32018-09-28 23:05:48 -07003import "aos/controls/control_loops.q";
Austin Schuhedbb64f2016-03-19 01:18:09 -07004import "frc971/control_loops/control_loops.q";
Austin Schuh093535c2016-03-05 23:21:00 -08005
Brian Silverman51091a02015-12-26 15:56:58 -08006// For logging information about what the code is doing with the shifters.
Brian Silverman17f503e2015-08-02 18:17:18 -07007struct GearLogging {
Brian Silverman51091a02015-12-26 15:56:58 -08008 // Which controller is being used.
Brian Silverman17f503e2015-08-02 18:17:18 -07009 int8_t controller_index;
Comran Morshed0bf200a2016-02-19 15:19:32 +000010
11 // Whether each loop for the drivetrain sides is the high-gear one.
Brian Silverman17f503e2015-08-02 18:17:18 -070012 bool left_loop_high;
13 bool right_loop_high;
Comran Morshed0bf200a2016-02-19 15:19:32 +000014
15 // The states of each drivetrain shifter.
Brian Silverman17f503e2015-08-02 18:17:18 -070016 int8_t left_state;
17 int8_t right_state;
18};
19
Brian Silverman51091a02015-12-26 15:56:58 -080020// For logging information about the state of the shifters.
Brian Silverman17f503e2015-08-02 18:17:18 -070021struct CIMLogging {
Comran Morshed0bf200a2016-02-19 15:19:32 +000022 // Whether the code thinks each drivetrain side is currently in gear.
Brian Silverman17f503e2015-08-02 18:17:18 -070023 bool left_in_gear;
24 bool right_in_gear;
Comran Morshed0bf200a2016-02-19 15:19:32 +000025
26 // The angular velocities (in rad/s, positive forward) the code thinks motors
27 // on each side of the drivetrain are moving at.
Brian Silverman17f503e2015-08-02 18:17:18 -070028 double left_motor_speed;
29 double right_motor_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +000030
31 // The velocity estimates for each drivetrain side of the robot (in m/s,
32 // positive forward) that can be used for shifting.
Brian Silverman17f503e2015-08-02 18:17:18 -070033 double left_velocity;
34 double right_velocity;
35};
36
37queue_group DrivetrainQueue {
38 implements aos.control_loops.ControlLoop;
39
40 message Goal {
Brian Silverman51091a02015-12-26 15:56:58 -080041 // Position of the steering wheel (positive = turning left when going
42 // forwards).
Austin Schuh2b1fce02018-03-02 20:05:20 -080043 double wheel;
44 double wheel_velocity;
45 double wheel_torque;
Comran Morshed0bf200a2016-02-19 15:19:32 +000046
Brian Silverman51091a02015-12-26 15:56:58 -080047 // Position of the throttle (positive forwards).
Brian Silverman17f503e2015-08-02 18:17:18 -070048 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -080049 double throttle_velocity;
50 double throttle_torque;
Comran Morshed0bf200a2016-02-19 15:19:32 +000051
Brian Silverman51091a02015-12-26 15:56:58 -080052 // True to shift into high, false to shift into low.
Brian Silverman17f503e2015-08-02 18:17:18 -070053 bool highgear;
Comran Morshed0bf200a2016-02-19 15:19:32 +000054
Brian Silverman51091a02015-12-26 15:56:58 -080055 // True to activate quickturn.
Brian Silverman17f503e2015-08-02 18:17:18 -070056 bool quickturn;
Comran Morshed0bf200a2016-02-19 15:19:32 +000057
Austin Schuh78379ea2019-01-04 20:39:45 -080058 // Type of controller in charge of the drivetrain.
59 // 0: polydrive
60 // 1: motion profiled position drive (statespace)
61 // 2: spline follower.
62 uint8_t controller_type;
Comran Morshed0bf200a2016-02-19 15:19:32 +000063
64 // Position goals for each drivetrain side (in meters) when the
65 // closed-loop controller is active.
Brian Silverman17f503e2015-08-02 18:17:18 -070066 double left_goal;
Brian Silverman17f503e2015-08-02 18:17:18 -070067 double right_goal;
Comran Morshed0bf200a2016-02-19 15:19:32 +000068
69 // Velocity goal for each drivetrain side (in m/s) when the closed-loop
70 // controller is active.
71 double left_velocity_goal;
Brian Silverman17f503e2015-08-02 18:17:18 -070072 double right_velocity_goal;
Austin Schuh093535c2016-03-05 23:21:00 -080073
Austin Schuh0aae9242018-03-14 19:49:44 -070074 double max_ss_voltage;
75
Austin Schuh093535c2016-03-05 23:21:00 -080076 // Motion profile parameters.
77 // The control loop will profile if these are all non-zero.
Austin Schuhedbb64f2016-03-19 01:18:09 -070078 .frc971.ProfileParameters linear;
79 .frc971.ProfileParameters angular;
Brian Silverman17f503e2015-08-02 18:17:18 -070080 };
81
82 message Position {
Comran Morshed0bf200a2016-02-19 15:19:32 +000083 // Relative position of each drivetrain side (in meters).
Brian Silverman17f503e2015-08-02 18:17:18 -070084 double left_encoder;
85 double right_encoder;
Comran Morshed0bf200a2016-02-19 15:19:32 +000086
87 // The speed in m/s of each drivetrain side from the most recent encoder
88 // pulse, or 0 if there was no edge within the last 5ms.
Brian Silverman51091a02015-12-26 15:56:58 -080089 double left_speed;
Brian Silverman51091a02015-12-26 15:56:58 -080090 double right_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +000091
92 // Position of each drivetrain shifter, scaled from 0.0 to 1.0 where smaller
93 // is towards low gear.
Brian Silverman17f503e2015-08-02 18:17:18 -070094 double left_shifter_position;
95 double right_shifter_position;
Comran Morshed0bf200a2016-02-19 15:19:32 +000096
97 // Raw analog voltages of each shifter hall effect for logging purposes.
Austin Schuha0c1e152015-11-08 14:10:13 -080098 double low_left_hall;
99 double high_left_hall;
100 double low_right_hall;
101 double high_right_hall;
Brian Silverman17f503e2015-08-02 18:17:18 -0700102 };
103
104 message Output {
Comran Morshed0bf200a2016-02-19 15:19:32 +0000105 // Voltage to send to motor(s) on either side of the drivetrain.
Brian Silverman17f503e2015-08-02 18:17:18 -0700106 double left_voltage;
107 double right_voltage;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000108
109 // Whether to set each shifter piston to high gear.
Brian Silverman17f503e2015-08-02 18:17:18 -0700110 bool left_high;
111 bool right_high;
112 };
113
114 message Status {
Brian Silverman51091a02015-12-26 15:56:58 -0800115 // Estimated speed of the center of the robot in m/s (positive forwards).
Brian Silverman17f503e2015-08-02 18:17:18 -0700116 double robot_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000117
118 // Estimated relative position of each drivetrain side (in meters).
Austin Schuh093535c2016-03-05 23:21:00 -0800119 double estimated_left_position;
120 double estimated_right_position;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000121
122 // Estimated velocity of each drivetrain side (in m/s).
Austin Schuh093535c2016-03-05 23:21:00 -0800123 double estimated_left_velocity;
124 double estimated_right_velocity;
Brian Silverman17f503e2015-08-02 18:17:18 -0700125
Comran Morshed0bf200a2016-02-19 15:19:32 +0000126 // The voltage we wanted to send to each drivetrain side last cycle.
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 double uncapped_left_voltage;
128 double uncapped_right_voltage;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000129
Austin Schuh41565602016-02-28 20:10:49 -0800130 // The goal velocities for the polydrive controller.
131 double left_velocity_goal;
132 double right_velocity_goal;
133
Austin Schuh093535c2016-03-05 23:21:00 -0800134 // The voltage error for the left and right sides.
135 double left_voltage_error;
136 double right_voltage_error;
137
138 // The profiled goal states.
139 double profiled_left_position_goal;
140 double profiled_right_position_goal;
141 double profiled_left_velocity_goal;
142 double profiled_right_velocity_goal;
143
144 // The KF offset
145 double estimated_angular_velocity_error;
146 // The KF estimated heading.
147 double estimated_heading;
Austin Schuh3a378462019-01-04 21:48:04 -0800148
149 // xytheta of the robot.
150 double x;
151 double y;
152 double theta;
Austin Schuh093535c2016-03-05 23:21:00 -0800153
Brian Silverman51091a02015-12-26 15:56:58 -0800154 // True if the output voltage was capped last cycle.
Brian Silverman17f503e2015-08-02 18:17:18 -0700155 bool output_was_capped;
Austin Schuh5900d142016-04-03 21:35:12 -0700156
157 // The angle of the robot relative to the ground.
158 double ground_angle;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700159
160 // Information about shifting logic and curent gear, for logging purposes
161 GearLogging gear_logging;
162 CIMLogging cim_logging;
Brian Silverman17f503e2015-08-02 18:17:18 -0700163 };
164
165 queue Goal goal;
166 queue Position position;
167 queue Output output;
168 queue Status status;
169};
170
171queue_group DrivetrainQueue drivetrain_queue;