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 | |
| 37 | queue_group DrivetrainQueue { |
| 38 | implements aos.control_loops.ControlLoop; |
| 39 | |
| 40 | message Goal { |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 41 | // Position of the steering wheel (positive = turning left when going |
| 42 | // forwards). |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 43 | double wheel; |
| 44 | double wheel_velocity; |
| 45 | double wheel_torque; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 46 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 47 | // Position of the throttle (positive forwards). |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 48 | double throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 49 | double throttle_velocity; |
| 50 | double throttle_torque; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 51 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 52 | // True to shift into high, false to shift into low. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 53 | bool highgear; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 54 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 55 | // True to activate quickturn. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 56 | bool quickturn; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 57 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 58 | // True to have the closed-loop controller take over. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 59 | bool control_loop_driving; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 60 | |
| 61 | // Position goals for each drivetrain side (in meters) when the |
| 62 | // closed-loop controller is active. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 63 | double left_goal; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 64 | double right_goal; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 65 | |
| 66 | // Velocity goal for each drivetrain side (in m/s) when the closed-loop |
| 67 | // controller is active. |
| 68 | double left_velocity_goal; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 69 | double right_velocity_goal; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 71 | double max_ss_voltage; |
| 72 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 73 | // Motion profile parameters. |
| 74 | // The control loop will profile if these are all non-zero. |
Austin Schuh | edbb64f | 2016-03-19 01:18:09 -0700 | [diff] [blame] | 75 | .frc971.ProfileParameters linear; |
| 76 | .frc971.ProfileParameters angular; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | message Position { |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 80 | // Relative position of each drivetrain side (in meters). |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 81 | double left_encoder; |
| 82 | double right_encoder; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 83 | |
| 84 | // The speed in m/s of each drivetrain side from the most recent encoder |
| 85 | // pulse, or 0 if there was no edge within the last 5ms. |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 86 | double left_speed; |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 87 | double right_speed; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 88 | |
| 89 | // Position of each drivetrain shifter, scaled from 0.0 to 1.0 where smaller |
| 90 | // is towards low gear. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 91 | double left_shifter_position; |
| 92 | double right_shifter_position; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 93 | |
| 94 | // Raw analog voltages of each shifter hall effect for logging purposes. |
Austin Schuh | a0c1e15 | 2015-11-08 14:10:13 -0800 | [diff] [blame] | 95 | double low_left_hall; |
| 96 | double high_left_hall; |
| 97 | double low_right_hall; |
| 98 | double high_right_hall; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | message Output { |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 102 | // Voltage to send to motor(s) on either side of the drivetrain. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 103 | double left_voltage; |
| 104 | double right_voltage; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 105 | |
| 106 | // Whether to set each shifter piston to high gear. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 107 | bool left_high; |
| 108 | bool right_high; |
| 109 | }; |
| 110 | |
| 111 | message Status { |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 112 | // 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] | 113 | double robot_speed; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 114 | |
| 115 | // Estimated relative position of each drivetrain side (in meters). |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 116 | double estimated_left_position; |
| 117 | double estimated_right_position; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 118 | |
| 119 | // Estimated velocity of each drivetrain side (in m/s). |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 120 | double estimated_left_velocity; |
| 121 | double estimated_right_velocity; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 122 | |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 123 | // The voltage we wanted to send to each drivetrain side last cycle. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 124 | double uncapped_left_voltage; |
| 125 | double uncapped_right_voltage; |
Comran Morshed | 0bf200a | 2016-02-19 15:19:32 +0000 | [diff] [blame] | 126 | |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 127 | // The goal velocities for the polydrive controller. |
| 128 | double left_velocity_goal; |
| 129 | double right_velocity_goal; |
| 130 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 131 | // The voltage error for the left and right sides. |
| 132 | double left_voltage_error; |
| 133 | double right_voltage_error; |
| 134 | |
| 135 | // The profiled goal states. |
| 136 | double profiled_left_position_goal; |
| 137 | double profiled_right_position_goal; |
| 138 | double profiled_left_velocity_goal; |
| 139 | double profiled_right_velocity_goal; |
| 140 | |
| 141 | // The KF offset |
| 142 | double estimated_angular_velocity_error; |
| 143 | // The KF estimated heading. |
| 144 | double estimated_heading; |
| 145 | // The KF wheel estimated heading. |
| 146 | //double estimated_wheel_heading; |
| 147 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 148 | // True if the output voltage was capped last cycle. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 149 | bool output_was_capped; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 150 | |
| 151 | // The angle of the robot relative to the ground. |
| 152 | double ground_angle; |
Kyle Stachowicz | 2f3c20f | 2017-07-13 16:04:05 -0700 | [diff] [blame] | 153 | |
| 154 | // Information about shifting logic and curent gear, for logging purposes |
| 155 | GearLogging gear_logging; |
| 156 | CIMLogging cim_logging; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | queue Goal goal; |
| 160 | queue Position position; |
| 161 | queue Output output; |
| 162 | queue Status status; |
| 163 | }; |
| 164 | |
| 165 | queue_group DrivetrainQueue drivetrain_queue; |