blob: 939ad757405dcc184fe76fb78315ffa068c21681 [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
Alex Perrycc3ee4c2019-02-09 21:20:41 -080037// For logging information about the state of the trajectory planning.
38struct TrajectoryLogging {
39 // state of planning the trajectory.
40 // 0: not currently planning
41 // 1: received a multispline to plan
42 // 2: Built the spline and planning.
43 // 3: Finished the plan and ready to excecute.
44 int8_t planning_state;
45
46 // State of the spline execution.
47 bool is_executing;
James Kuszmaul29e417d2019-04-13 10:03:35 -070048 // Whether we have finished the spline specified by current_spline_idx.
49 bool is_executed;
Alex Perrycc3ee4c2019-02-09 21:20:41 -080050
Austin Schuh6bcc2302019-03-23 22:28:06 -070051 // The handle of the goal spline. 0 means stop requested.
52 int32_t goal_spline_handle;
53 // Handle of the executing spline. -1 means none requested. If there was no
54 // spline executing when a spline finished optimizing, it will become the
55 // current spline even if we aren't ready to start yet.
Alex Perrycc3ee4c2019-02-09 21:20:41 -080056 int32_t current_spline_idx;
Austin Schuh6bcc2302019-03-23 22:28:06 -070057 // Handle of the spline that is being optimized and staged.
58 int32_t planning_spline_idx;
Alex Perrycc3ee4c2019-02-09 21:20:41 -080059
60 // Expected position and velocity on the spline
61 float x;
62 float y;
63 float theta;
64 float left_velocity;
65 float right_velocity;
66};
67
James Kuszmauld0856d62019-03-02 22:57:10 -080068// For logging state of the line follower.
69struct LineFollowLogging {
70 // Whether we are currently freezing target choice.
71 bool frozen;
72 // Whether we currently have a target.
73 bool have_target;
74 // Absolute position of the current goal.
75 float x;
76 float y;
77 float theta;
James Kuszmaul38e79642019-03-09 15:48:27 -080078 // Current lateral offset from line pointing straight out of the target.
79 float offset;
80 // Current distance from the plane of the target, in meters.
81 float distance_to_target;
82 // Current goal heading.
83 float goal_theta;
84 // Current relative heading.
85 float rel_theta;
James Kuszmauld0856d62019-03-02 22:57:10 -080086};
87
Brian Silverman17f503e2015-08-02 18:17:18 -070088queue_group DrivetrainQueue {
89 implements aos.control_loops.ControlLoop;
90
91 message Goal {
Brian Silverman51091a02015-12-26 15:56:58 -080092 // Position of the steering wheel (positive = turning left when going
93 // forwards).
Alex Perryfc83d3b2019-02-03 15:41:58 -080094 float wheel;
95 float wheel_velocity;
96 float wheel_torque;
Comran Morshed0bf200a2016-02-19 15:19:32 +000097
Brian Silverman51091a02015-12-26 15:56:58 -080098 // Position of the throttle (positive forwards).
Alex Perryfc83d3b2019-02-03 15:41:58 -080099 float throttle;
100 float throttle_velocity;
101 float throttle_torque;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000102
Brian Silverman51091a02015-12-26 15:56:58 -0800103 // True to shift into high, false to shift into low.
Brian Silverman17f503e2015-08-02 18:17:18 -0700104 bool highgear;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000105
Brian Silverman51091a02015-12-26 15:56:58 -0800106 // True to activate quickturn.
Brian Silverman17f503e2015-08-02 18:17:18 -0700107 bool quickturn;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000108
Austin Schuh78379ea2019-01-04 20:39:45 -0800109 // Type of controller in charge of the drivetrain.
110 // 0: polydrive
111 // 1: motion profiled position drive (statespace)
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800112 // 2: spline follower
113 // 3: line follower (for guiding into a target)
Austin Schuh78379ea2019-01-04 20:39:45 -0800114 uint8_t controller_type;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000115
116 // Position goals for each drivetrain side (in meters) when the
117 // closed-loop controller is active.
Brian Silverman17f503e2015-08-02 18:17:18 -0700118 double left_goal;
Brian Silverman17f503e2015-08-02 18:17:18 -0700119 double right_goal;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000120
Alex Perryfc83d3b2019-02-03 15:41:58 -0800121 float max_ss_voltage;
Austin Schuh0aae9242018-03-14 19:49:44 -0700122
Austin Schuh093535c2016-03-05 23:21:00 -0800123 // Motion profile parameters.
124 // The control loop will profile if these are all non-zero.
Austin Schuhedbb64f2016-03-19 01:18:09 -0700125 .frc971.ProfileParameters linear;
126 .frc971.ProfileParameters angular;
Alex Perry731b4602019-02-02 22:13:01 -0800127
128 // Parameters for a spline to follow. This just contains info on a spline to
129 // compute. Each time this is sent, spline drivetrain will compute a new
130 // spline.
131 .frc971.MultiSpline spline;
132
133 // Which spline to follow.
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800134 int32_t spline_handle;
Brian Silverman17f503e2015-08-02 18:17:18 -0700135 };
136
137 message Position {
Comran Morshed0bf200a2016-02-19 15:19:32 +0000138 // Relative position of each drivetrain side (in meters).
Brian Silverman17f503e2015-08-02 18:17:18 -0700139 double left_encoder;
140 double right_encoder;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000141
142 // The speed in m/s of each drivetrain side from the most recent encoder
143 // pulse, or 0 if there was no edge within the last 5ms.
Brian Silverman51091a02015-12-26 15:56:58 -0800144 double left_speed;
Brian Silverman51091a02015-12-26 15:56:58 -0800145 double right_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000146
147 // Position of each drivetrain shifter, scaled from 0.0 to 1.0 where smaller
148 // is towards low gear.
Brian Silverman17f503e2015-08-02 18:17:18 -0700149 double left_shifter_position;
150 double right_shifter_position;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000151
152 // Raw analog voltages of each shifter hall effect for logging purposes.
Austin Schuha0c1e152015-11-08 14:10:13 -0800153 double low_left_hall;
154 double high_left_hall;
155 double low_right_hall;
156 double high_right_hall;
Brian Silverman17f503e2015-08-02 18:17:18 -0700157 };
158
159 message Output {
Comran Morshed0bf200a2016-02-19 15:19:32 +0000160 // Voltage to send to motor(s) on either side of the drivetrain.
Brian Silverman17f503e2015-08-02 18:17:18 -0700161 double left_voltage;
162 double right_voltage;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000163
164 // Whether to set each shifter piston to high gear.
Brian Silverman17f503e2015-08-02 18:17:18 -0700165 bool left_high;
166 bool right_high;
167 };
168
169 message Status {
Brian Silverman51091a02015-12-26 15:56:58 -0800170 // Estimated speed of the center of the robot in m/s (positive forwards).
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 double robot_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000172
173 // Estimated relative position of each drivetrain side (in meters).
Austin Schuh093535c2016-03-05 23:21:00 -0800174 double estimated_left_position;
175 double estimated_right_position;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000176
177 // Estimated velocity of each drivetrain side (in m/s).
Austin Schuh093535c2016-03-05 23:21:00 -0800178 double estimated_left_velocity;
179 double estimated_right_velocity;
Brian Silverman17f503e2015-08-02 18:17:18 -0700180
Comran Morshed0bf200a2016-02-19 15:19:32 +0000181 // The voltage we wanted to send to each drivetrain side last cycle.
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 double uncapped_left_voltage;
183 double uncapped_right_voltage;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000184
Austin Schuh093535c2016-03-05 23:21:00 -0800185 // The voltage error for the left and right sides.
186 double left_voltage_error;
187 double right_voltage_error;
188
189 // The profiled goal states.
190 double profiled_left_position_goal;
191 double profiled_right_position_goal;
192 double profiled_left_velocity_goal;
193 double profiled_right_velocity_goal;
194
195 // The KF offset
196 double estimated_angular_velocity_error;
197 // The KF estimated heading.
198 double estimated_heading;
Austin Schuh3a378462019-01-04 21:48:04 -0800199
200 // xytheta of the robot.
201 double x;
202 double y;
203 double theta;
Austin Schuh093535c2016-03-05 23:21:00 -0800204
Brian Silverman51091a02015-12-26 15:56:58 -0800205 // True if the output voltage was capped last cycle.
Brian Silverman17f503e2015-08-02 18:17:18 -0700206 bool output_was_capped;
Austin Schuh5900d142016-04-03 21:35:12 -0700207
208 // The angle of the robot relative to the ground.
209 double ground_angle;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700210
211 // Information about shifting logic and curent gear, for logging purposes
212 GearLogging gear_logging;
213 CIMLogging cim_logging;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800214 TrajectoryLogging trajectory_logging;
James Kuszmauld0856d62019-03-02 22:57:10 -0800215 LineFollowLogging line_follow_logging;
Brian Silverman17f503e2015-08-02 18:17:18 -0700216 };
217
218 queue Goal goal;
219 queue Position position;
220 queue Output output;
221 queue Status status;
222};
223
224queue_group DrivetrainQueue drivetrain_queue;