Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | package frc971.control_loops; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 2 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 3 | import "aos/controls/control_loops.q"; |
Austin Schuh | edbb64f | 2016-03-19 01:18:09 -0700 | [diff] [blame] | 4 | import "frc971/control_loops/control_loops.q"; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 5 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 6 | // For logging information about what the code is doing with the shifters. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 7 | struct GearLogging { |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 8 | // Which controller is being used. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 9 | int8_t controller_index; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 10 | |
| 11 | // Whether each loop for the drivetrain sides is the high-gear one. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 12 | bool left_loop_high; |
| 13 | bool right_loop_high; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 14 | |
| 15 | // The states of each drivetrain shifter. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 16 | int8_t left_state; |
| 17 | int8_t right_state; |
| 18 | }; |
| 19 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 20 | // For logging information about the state of the shifters. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 21 | struct CIMLogging { |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 22 | // Whether the code thinks each drivetrain side is currently in gear. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 23 | bool left_in_gear; |
| 24 | bool right_in_gear; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 25 | |
| 26 | // The angular velocities (in rad/s, positive forward) the code thinks motors |
| 27 | // on each side of the drivetrain are moving at. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 28 | double left_motor_speed; |
| 29 | double right_motor_speed; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 30 | |
| 31 | // The velocity estimates for each drivetrain side of the robot (in m/s, |
| 32 | // positive forward) that can be used for shifting. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 33 | double left_velocity; |
| 34 | double right_velocity; |
| 35 | }; |
| 36 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 37 | // For logging information about the state of the trajectory planning. |
| 38 | struct 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 Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 48 | // Whether we have finished the spline specified by current_spline_idx. |
| 49 | bool is_executed; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 50 | |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 51 | // 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 Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 56 | int32_t current_spline_idx; |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 57 | // Handle of the spline that is being optimized and staged. |
| 58 | int32_t planning_spline_idx; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 59 | |
| 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; |
James Kuszmaul | 5a54341 | 2019-04-13 15:49:42 -0700 | [diff] [blame] | 66 | float distance_remaining; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
James Kuszmaul | d0856d6 | 2019-03-02 22:57:10 -0800 | [diff] [blame] | 69 | // For logging state of the line follower. |
| 70 | struct LineFollowLogging { |
| 71 | // Whether we are currently freezing target choice. |
| 72 | bool frozen; |
| 73 | // Whether we currently have a target. |
| 74 | bool have_target; |
| 75 | // Absolute position of the current goal. |
| 76 | float x; |
| 77 | float y; |
| 78 | float theta; |
James Kuszmaul | 38e7964 | 2019-03-09 15:48:27 -0800 | [diff] [blame] | 79 | // Current lateral offset from line pointing straight out of the target. |
| 80 | float offset; |
| 81 | // Current distance from the plane of the target, in meters. |
| 82 | float distance_to_target; |
| 83 | // Current goal heading. |
| 84 | float goal_theta; |
| 85 | // Current relative heading. |
| 86 | float rel_theta; |
James Kuszmaul | d0856d6 | 2019-03-02 22:57:10 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 89 | // Published on ".frc971.control_loops.drivetrain_queue" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 90 | queue_group DrivetrainQueue { |
| 91 | implements aos.control_loops.ControlLoop; |
| 92 | |
| 93 | message Goal { |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 94 | // Position of the steering wheel (positive = turning left when going |
| 95 | // forwards). |
Alex Perry | fc83d3b | 2019-02-03 15:41:58 -0800 | [diff] [blame] | 96 | float wheel; |
| 97 | float wheel_velocity; |
| 98 | float wheel_torque; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 99 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 100 | // Position of the throttle (positive forwards). |
Alex Perry | fc83d3b | 2019-02-03 15:41:58 -0800 | [diff] [blame] | 101 | float throttle; |
| 102 | float throttle_velocity; |
| 103 | float throttle_torque; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 104 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 105 | // True to shift into high, false to shift into low. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 106 | bool highgear; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 107 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 108 | // True to activate quickturn. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 109 | bool quickturn; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 110 | |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 111 | // Type of controller in charge of the drivetrain. |
| 112 | // 0: polydrive |
| 113 | // 1: motion profiled position drive (statespace) |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 114 | // 2: spline follower |
| 115 | // 3: line follower (for guiding into a target) |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 116 | uint8_t controller_type; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 117 | |
| 118 | // Position goals for each drivetrain side (in meters) when the |
| 119 | // closed-loop controller is active. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 120 | double left_goal; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 121 | double right_goal; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 122 | |
Alex Perry | fc83d3b | 2019-02-03 15:41:58 -0800 | [diff] [blame] | 123 | float max_ss_voltage; |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 124 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 125 | // Motion profile parameters. |
| 126 | // The control loop will profile if these are all non-zero. |
Austin Schuh | edbb64f | 2016-03-19 01:18:09 -0700 | [diff] [blame] | 127 | .frc971.ProfileParameters linear; |
| 128 | .frc971.ProfileParameters angular; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 129 | |
| 130 | // Parameters for a spline to follow. This just contains info on a spline to |
| 131 | // compute. Each time this is sent, spline drivetrain will compute a new |
| 132 | // spline. |
| 133 | .frc971.MultiSpline spline; |
| 134 | |
| 135 | // Which spline to follow. |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 136 | int32_t spline_handle; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | message Position { |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 140 | // Relative position of each drivetrain side (in meters). |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 141 | double left_encoder; |
| 142 | double right_encoder; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 143 | |
| 144 | // The speed in m/s of each drivetrain side from the most recent encoder |
| 145 | // pulse, or 0 if there was no edge within the last 5ms. |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 146 | double left_speed; |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 147 | double right_speed; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 148 | |
| 149 | // Position of each drivetrain shifter, scaled from 0.0 to 1.0 where smaller |
| 150 | // is towards low gear. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 151 | double left_shifter_position; |
| 152 | double right_shifter_position; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 153 | |
| 154 | // Raw analog voltages of each shifter hall effect for logging purposes. |
Austin Schuh | a0c1e15 | 2015-11-08 14:10:13 -0800 | [diff] [blame] | 155 | double low_left_hall; |
| 156 | double high_left_hall; |
| 157 | double low_right_hall; |
| 158 | double high_right_hall; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | message Output { |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 162 | // Voltage to send to motor(s) on either side of the drivetrain. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 163 | double left_voltage; |
| 164 | double right_voltage; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 165 | |
| 166 | // Whether to set each shifter piston to high gear. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 167 | bool left_high; |
| 168 | bool right_high; |
| 169 | }; |
| 170 | |
| 171 | message Status { |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 172 | // Estimated speed of the center of the robot in m/s (positive forwards). |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 173 | double robot_speed; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 174 | |
| 175 | // Estimated relative position of each drivetrain side (in meters). |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 176 | double estimated_left_position; |
| 177 | double estimated_right_position; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 178 | |
| 179 | // Estimated velocity of each drivetrain side (in m/s). |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 180 | double estimated_left_velocity; |
| 181 | double estimated_right_velocity; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 182 | |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 183 | // The voltage we wanted to send to each drivetrain side last cycle. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 184 | double uncapped_left_voltage; |
| 185 | double uncapped_right_voltage; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 186 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 187 | // The voltage error for the left and right sides. |
| 188 | double left_voltage_error; |
| 189 | double right_voltage_error; |
| 190 | |
| 191 | // The profiled goal states. |
| 192 | double profiled_left_position_goal; |
| 193 | double profiled_right_position_goal; |
| 194 | double profiled_left_velocity_goal; |
| 195 | double profiled_right_velocity_goal; |
| 196 | |
| 197 | // The KF offset |
| 198 | double estimated_angular_velocity_error; |
| 199 | // The KF estimated heading. |
| 200 | double estimated_heading; |
Austin Schuh | 3a37846 | 2019-01-04 21:48:04 -0800 | [diff] [blame] | 201 | |
| 202 | // xytheta of the robot. |
| 203 | double x; |
| 204 | double y; |
| 205 | double theta; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 206 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 207 | // True if the output voltage was capped last cycle. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 208 | bool output_was_capped; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 209 | |
| 210 | // The angle of the robot relative to the ground. |
| 211 | double ground_angle; |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 212 | |
| 213 | // Information about shifting logic and curent gear, for logging purposes |
| 214 | GearLogging gear_logging; |
| 215 | CIMLogging cim_logging; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 216 | TrajectoryLogging trajectory_logging; |
James Kuszmaul | d0856d6 | 2019-03-02 22:57:10 -0800 | [diff] [blame] | 217 | LineFollowLogging line_follow_logging; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | queue Goal goal; |
| 221 | queue Position position; |
| 222 | queue Output output; |
| 223 | queue Status status; |
| 224 | }; |