blob: 66e9c322d9438ba1ff957a62a7500d98c309c332 [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;
48
49 int32_t current_spline_handle;
50 int32_t current_spline_idx;
51
52 // Expected position and velocity on the spline
53 float x;
54 float y;
55 float theta;
56 float left_velocity;
57 float right_velocity;
58};
59
Brian Silverman17f503e2015-08-02 18:17:18 -070060queue_group DrivetrainQueue {
61 implements aos.control_loops.ControlLoop;
62
63 message Goal {
Brian Silverman51091a02015-12-26 15:56:58 -080064 // Position of the steering wheel (positive = turning left when going
65 // forwards).
Alex Perryfc83d3b2019-02-03 15:41:58 -080066 float wheel;
67 float wheel_velocity;
68 float wheel_torque;
Comran Morshed0bf200a2016-02-19 15:19:32 +000069
Brian Silverman51091a02015-12-26 15:56:58 -080070 // Position of the throttle (positive forwards).
Alex Perryfc83d3b2019-02-03 15:41:58 -080071 float throttle;
72 float throttle_velocity;
73 float throttle_torque;
Comran Morshed0bf200a2016-02-19 15:19:32 +000074
Brian Silverman51091a02015-12-26 15:56:58 -080075 // True to shift into high, false to shift into low.
Brian Silverman17f503e2015-08-02 18:17:18 -070076 bool highgear;
Comran Morshed0bf200a2016-02-19 15:19:32 +000077
Brian Silverman51091a02015-12-26 15:56:58 -080078 // True to activate quickturn.
Brian Silverman17f503e2015-08-02 18:17:18 -070079 bool quickturn;
Comran Morshed0bf200a2016-02-19 15:19:32 +000080
Austin Schuh78379ea2019-01-04 20:39:45 -080081 // Type of controller in charge of the drivetrain.
82 // 0: polydrive
83 // 1: motion profiled position drive (statespace)
James Kuszmaule39cbcf2019-02-27 20:48:34 -080084 // 2: spline follower
85 // 3: line follower (for guiding into a target)
Austin Schuh78379ea2019-01-04 20:39:45 -080086 uint8_t controller_type;
Comran Morshed0bf200a2016-02-19 15:19:32 +000087
88 // Position goals for each drivetrain side (in meters) when the
89 // closed-loop controller is active.
Brian Silverman17f503e2015-08-02 18:17:18 -070090 double left_goal;
Brian Silverman17f503e2015-08-02 18:17:18 -070091 double right_goal;
Comran Morshed0bf200a2016-02-19 15:19:32 +000092
Alex Perryfc83d3b2019-02-03 15:41:58 -080093 float max_ss_voltage;
Austin Schuh0aae9242018-03-14 19:49:44 -070094
Austin Schuh093535c2016-03-05 23:21:00 -080095 // Motion profile parameters.
96 // The control loop will profile if these are all non-zero.
Austin Schuhedbb64f2016-03-19 01:18:09 -070097 .frc971.ProfileParameters linear;
98 .frc971.ProfileParameters angular;
Alex Perry731b4602019-02-02 22:13:01 -080099
100 // Parameters for a spline to follow. This just contains info on a spline to
101 // compute. Each time this is sent, spline drivetrain will compute a new
102 // spline.
103 .frc971.MultiSpline spline;
104
105 // Which spline to follow.
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800106 int32_t spline_handle;
Brian Silverman17f503e2015-08-02 18:17:18 -0700107 };
108
109 message Position {
Comran Morshed0bf200a2016-02-19 15:19:32 +0000110 // Relative position of each drivetrain side (in meters).
Brian Silverman17f503e2015-08-02 18:17:18 -0700111 double left_encoder;
112 double right_encoder;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000113
114 // The speed in m/s of each drivetrain side from the most recent encoder
115 // pulse, or 0 if there was no edge within the last 5ms.
Brian Silverman51091a02015-12-26 15:56:58 -0800116 double left_speed;
Brian Silverman51091a02015-12-26 15:56:58 -0800117 double right_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000118
119 // Position of each drivetrain shifter, scaled from 0.0 to 1.0 where smaller
120 // is towards low gear.
Brian Silverman17f503e2015-08-02 18:17:18 -0700121 double left_shifter_position;
122 double right_shifter_position;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000123
124 // Raw analog voltages of each shifter hall effect for logging purposes.
Austin Schuha0c1e152015-11-08 14:10:13 -0800125 double low_left_hall;
126 double high_left_hall;
127 double low_right_hall;
128 double high_right_hall;
Brian Silverman17f503e2015-08-02 18:17:18 -0700129 };
130
131 message Output {
Comran Morshed0bf200a2016-02-19 15:19:32 +0000132 // Voltage to send to motor(s) on either side of the drivetrain.
Brian Silverman17f503e2015-08-02 18:17:18 -0700133 double left_voltage;
134 double right_voltage;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000135
136 // Whether to set each shifter piston to high gear.
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 bool left_high;
138 bool right_high;
139 };
140
141 message Status {
Brian Silverman51091a02015-12-26 15:56:58 -0800142 // Estimated speed of the center of the robot in m/s (positive forwards).
Brian Silverman17f503e2015-08-02 18:17:18 -0700143 double robot_speed;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000144
145 // Estimated relative position of each drivetrain side (in meters).
Austin Schuh093535c2016-03-05 23:21:00 -0800146 double estimated_left_position;
147 double estimated_right_position;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000148
149 // Estimated velocity of each drivetrain side (in m/s).
Austin Schuh093535c2016-03-05 23:21:00 -0800150 double estimated_left_velocity;
151 double estimated_right_velocity;
Brian Silverman17f503e2015-08-02 18:17:18 -0700152
Comran Morshed0bf200a2016-02-19 15:19:32 +0000153 // The voltage we wanted to send to each drivetrain side last cycle.
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 double uncapped_left_voltage;
155 double uncapped_right_voltage;
Comran Morshed0bf200a2016-02-19 15:19:32 +0000156
Austin Schuh093535c2016-03-05 23:21:00 -0800157 // The voltage error for the left and right sides.
158 double left_voltage_error;
159 double right_voltage_error;
160
161 // The profiled goal states.
162 double profiled_left_position_goal;
163 double profiled_right_position_goal;
164 double profiled_left_velocity_goal;
165 double profiled_right_velocity_goal;
166
167 // The KF offset
168 double estimated_angular_velocity_error;
169 // The KF estimated heading.
170 double estimated_heading;
Austin Schuh3a378462019-01-04 21:48:04 -0800171
172 // xytheta of the robot.
173 double x;
174 double y;
175 double theta;
Austin Schuh093535c2016-03-05 23:21:00 -0800176
Brian Silverman51091a02015-12-26 15:56:58 -0800177 // True if the output voltage was capped last cycle.
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 bool output_was_capped;
Austin Schuh5900d142016-04-03 21:35:12 -0700179
180 // The angle of the robot relative to the ground.
181 double ground_angle;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700182
183 // Information about shifting logic and curent gear, for logging purposes
184 GearLogging gear_logging;
185 CIMLogging cim_logging;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800186 TrajectoryLogging trajectory_logging;
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 };
188
189 queue Goal goal;
190 queue Position position;
191 queue Output output;
192 queue Status status;
193};
194
195queue_group DrivetrainQueue drivetrain_queue;