Austin Schuh | d36c369 | 2014-02-18 21:00:57 -0800 | [diff] [blame^] | 1 | #include "frc971/control_loops/claw/claw.q.h" |
| 2 | #include "frc971/control_loops/control_loops.q.h" |
| 3 | |
| 4 | #include "aos/linux_code/init.h" |
| 5 | #include "frc971/constants.h" |
| 6 | |
| 7 | namespace frc971 { |
| 8 | |
| 9 | typedef constants::Values::Claws Claws; |
| 10 | |
| 11 | bool DoGetPositionOfEdge( |
| 12 | const double start_position, |
| 13 | const control_loops::HalfClawPosition &last_claw_position, |
| 14 | const control_loops::HalfClawPosition &claw_position, |
| 15 | const HallEffectStruct &last_hall_effect, |
| 16 | const HallEffectStruct &hall_effect, |
| 17 | Claws::AnglePair *limits) { |
| 18 | |
| 19 | if (hall_effect.posedge_count != last_hall_effect.posedge_count) { |
| 20 | if (claw_position.posedge_value < last_claw_position.position) { |
| 21 | limits->upper_angle = claw_position.posedge_value - start_position; |
| 22 | } else { |
| 23 | limits->lower_decreasing_angle = |
| 24 | claw_position.posedge_value - start_position; |
| 25 | } |
| 26 | return true; |
| 27 | } |
| 28 | if (hall_effect.negedge_count != last_hall_effect.negedge_count) { |
| 29 | if (claw_position.negedge_value > last_claw_position.position) { |
| 30 | limits->upper_decreasing_angle = |
| 31 | claw_position.negedge_value - start_position; |
| 32 | } else { |
| 33 | limits->lower_angle = claw_position.negedge_value - start_position; |
| 34 | } |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | bool GetPositionOfEdge( |
| 42 | const double start_position, |
| 43 | const control_loops::HalfClawPosition &last_claw_position, |
| 44 | const control_loops::HalfClawPosition &claw_position, Claws::Claw *claw) { |
| 45 | |
| 46 | if (DoGetPositionOfEdge(start_position, last_claw_position, claw_position, |
| 47 | last_claw_position.front, claw_position.front, |
| 48 | &claw->front)) { |
| 49 | return true; |
| 50 | } |
| 51 | if (DoGetPositionOfEdge(start_position, last_claw_position, claw_position, |
| 52 | last_claw_position.calibration, |
| 53 | claw_position.calibration, &claw->calibration)) { |
| 54 | return true; |
| 55 | } |
| 56 | if (DoGetPositionOfEdge(start_position, last_claw_position, claw_position, |
| 57 | last_claw_position.back, claw_position.back, |
| 58 | &claw->back)) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | double position = claw_position.position - start_position; |
| 63 | |
| 64 | if (position > claw->upper_limit) { |
| 65 | claw->upper_hard_limit = claw->upper_limit = position; |
| 66 | return true; |
| 67 | } |
| 68 | if (position < claw->lower_limit) { |
| 69 | claw->lower_hard_limit = claw->lower_limit = position; |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | int Main() { |
| 76 | while (!control_loops::claw_queue_group.position.FetchNextBlocking()); |
| 77 | |
| 78 | const double top_start_position = |
| 79 | control_loops::claw_queue_group.position->top.position; |
| 80 | const double bottom_start_position = |
| 81 | control_loops::claw_queue_group.position->bottom.position; |
| 82 | |
| 83 | Claws limits; |
| 84 | |
| 85 | limits.claw_zeroing_off_speed = 0.5; |
| 86 | limits.claw_zeroing_speed = 0.1; |
| 87 | limits.claw_zeroing_separation = 0.1; |
| 88 | |
| 89 | // claw separation that would be considered a collision |
| 90 | limits.claw_min_separation = 0.0; |
| 91 | limits.claw_max_separation = 0.0; |
| 92 | |
| 93 | // We should never get closer/farther than these. |
| 94 | limits.soft_min_separation = 0.0; |
| 95 | limits.soft_max_separation = 0.0; |
| 96 | |
| 97 | limits.upper_claw.lower_hard_limit = 0.0; |
| 98 | limits.upper_claw.upper_hard_limit = 0.0; |
| 99 | limits.upper_claw.lower_limit = 0.0; |
| 100 | limits.upper_claw.upper_limit = 0.0; |
| 101 | limits.upper_claw.front.lower_angle = 0.0; |
| 102 | limits.upper_claw.front.upper_angle = 0.0; |
| 103 | limits.upper_claw.front.lower_decreasing_angle = 0.0; |
| 104 | limits.upper_claw.front.upper_decreasing_angle = 0.0; |
| 105 | limits.upper_claw.calibration.lower_angle = 0.0; |
| 106 | limits.upper_claw.calibration.upper_angle = 0.0; |
| 107 | limits.upper_claw.calibration.lower_decreasing_angle = 0.0; |
| 108 | limits.upper_claw.calibration.upper_decreasing_angle = 0.0; |
| 109 | limits.upper_claw.back.lower_angle = 0.0; |
| 110 | limits.upper_claw.back.upper_angle = 0.0; |
| 111 | limits.upper_claw.back.lower_decreasing_angle = 0.0; |
| 112 | limits.upper_claw.back.upper_decreasing_angle = 0.0; |
| 113 | |
| 114 | limits.lower_claw.lower_hard_limit = 0.0; |
| 115 | limits.lower_claw.upper_hard_limit = 0.0; |
| 116 | limits.lower_claw.lower_limit = 0.0; |
| 117 | limits.lower_claw.upper_limit = 0.0; |
| 118 | limits.lower_claw.front.lower_angle = 0.0; |
| 119 | limits.lower_claw.front.upper_angle = 0.0; |
| 120 | limits.lower_claw.front.lower_decreasing_angle = 0.0; |
| 121 | limits.lower_claw.front.upper_decreasing_angle = 0.0; |
| 122 | limits.lower_claw.calibration.lower_angle = 0.0; |
| 123 | limits.lower_claw.calibration.upper_angle = 0.0; |
| 124 | limits.lower_claw.calibration.lower_decreasing_angle = 0.0; |
| 125 | limits.lower_claw.calibration.upper_decreasing_angle = 0.0; |
| 126 | limits.lower_claw.back.lower_angle = 0.0; |
| 127 | limits.lower_claw.back.upper_angle = 0.0; |
| 128 | limits.lower_claw.back.lower_decreasing_angle = 0.0; |
| 129 | limits.lower_claw.back.upper_decreasing_angle = 0.0; |
| 130 | |
| 131 | limits.claw_unimportant_epsilon = 0.01; |
| 132 | limits.start_fine_tune_pos = -0.2; |
| 133 | limits.max_zeroing_voltage = 4.0; |
| 134 | |
| 135 | control_loops::ClawGroup::Position last_position = |
| 136 | *control_loops::claw_queue_group.position; |
| 137 | |
| 138 | while (true) { |
| 139 | if (control_loops::claw_queue_group.position.FetchNextBlocking()) { |
| 140 | bool print = false; |
| 141 | if (GetPositionOfEdge(top_start_position, last_position.top, |
| 142 | control_loops::claw_queue_group.position->top, |
| 143 | &limits.upper_claw)) { |
| 144 | print = true; |
| 145 | LOG(DEBUG, "Got an edge on the upper claw\n"); |
| 146 | } |
| 147 | if (GetPositionOfEdge(bottom_start_position, last_position.bottom, |
| 148 | control_loops::claw_queue_group.position->bottom, |
| 149 | &limits.lower_claw)) { |
| 150 | print = true; |
| 151 | LOG(DEBUG, "Got an edge on the lower claw\n"); |
| 152 | } |
| 153 | const double top_position = |
| 154 | control_loops::claw_queue_group.position->top.position - |
| 155 | top_start_position; |
| 156 | const double bottom_position = |
| 157 | control_loops::claw_queue_group.position->bottom.position - |
| 158 | bottom_start_position; |
| 159 | const double separation = top_position - bottom_position; |
| 160 | if (separation > limits.claw_max_separation) { |
| 161 | limits.soft_max_separation = limits.claw_max_separation = separation; |
| 162 | print = true; |
| 163 | } |
| 164 | if (separation < limits.claw_min_separation) { |
| 165 | limits.soft_min_separation = limits.claw_min_separation = separation; |
| 166 | print = true; |
| 167 | } |
| 168 | |
| 169 | if (print) { |
| 170 | printf("{%f,\n", limits.claw_zeroing_off_speed); |
| 171 | printf("%f,\n", limits.claw_zeroing_speed); |
| 172 | printf("%f,\n", limits.claw_zeroing_separation); |
| 173 | printf("%f,\n", limits.claw_min_separation); |
| 174 | printf("%f,\n", limits.claw_max_separation); |
| 175 | printf("%f,\n", limits.soft_min_separation); |
| 176 | printf("%f,\n", limits.soft_max_separation); |
| 177 | printf( |
| 178 | "{%f, %f, %f, %f, {%f, %f, %f, %f}, {%f, %f, %f, %f}, {%f, %f, %f, " |
| 179 | "%f}},\n", |
| 180 | limits.upper_claw.lower_hard_limit, |
| 181 | limits.upper_claw.upper_hard_limit, limits.upper_claw.lower_limit, |
| 182 | limits.upper_claw.upper_limit, limits.upper_claw.front.lower_angle, |
| 183 | limits.upper_claw.front.upper_angle, |
| 184 | limits.upper_claw.front.lower_decreasing_angle, |
| 185 | limits.upper_claw.front.upper_decreasing_angle, |
| 186 | limits.upper_claw.calibration.lower_angle, |
| 187 | limits.upper_claw.calibration.upper_angle, |
| 188 | limits.upper_claw.calibration.lower_decreasing_angle, |
| 189 | limits.upper_claw.calibration.upper_decreasing_angle, |
| 190 | limits.upper_claw.back.lower_angle, |
| 191 | limits.upper_claw.back.upper_angle, |
| 192 | limits.upper_claw.back.lower_decreasing_angle, |
| 193 | limits.upper_claw.back.upper_decreasing_angle); |
| 194 | |
| 195 | printf( |
| 196 | "{%f, %f, %f, %f, {%f, %f, %f, %f}, {%f, %f, %f, %f}, {%f, %f, %f, " |
| 197 | "%f}},\n", |
| 198 | limits.lower_claw.lower_hard_limit, |
| 199 | limits.lower_claw.upper_hard_limit, limits.lower_claw.lower_limit, |
| 200 | limits.lower_claw.upper_limit, limits.lower_claw.front.lower_angle, |
| 201 | limits.lower_claw.front.upper_angle, |
| 202 | limits.lower_claw.front.lower_decreasing_angle, |
| 203 | limits.lower_claw.front.upper_decreasing_angle, |
| 204 | limits.lower_claw.calibration.lower_angle, |
| 205 | limits.lower_claw.calibration.upper_angle, |
| 206 | limits.lower_claw.calibration.lower_decreasing_angle, |
| 207 | limits.lower_claw.calibration.upper_decreasing_angle, |
| 208 | limits.lower_claw.back.lower_angle, |
| 209 | limits.lower_claw.back.upper_angle, |
| 210 | limits.lower_claw.back.lower_decreasing_angle, |
| 211 | limits.lower_claw.back.upper_decreasing_angle); |
| 212 | printf("%f, // claw_unimportant_epsilon\n", |
| 213 | limits.claw_unimportant_epsilon); |
| 214 | printf("%f, // start_fine_tune_pos\n", limits.start_fine_tune_pos); |
| 215 | printf("%f,\n", limits.max_zeroing_voltage); |
| 216 | printf("}\n"); |
| 217 | } |
| 218 | |
| 219 | last_position = *control_loops::claw_queue_group.position; |
| 220 | } |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | } // namespace frc971 |
| 226 | |
| 227 | int main() { |
| 228 | ::aos::Init(); |
| 229 | int returnvalue = ::frc971::Main(); |
| 230 | ::aos::Cleanup(); |
| 231 | return returnvalue; |
| 232 | } |