jesse revised this gist 8 hours ago. Go to revision
1 file changed, 199 insertions
claude_cheat_sheet.md(file created)
| @@ -0,0 +1,199 @@ | |||
| 1 | + | # 🤖 Botball C Programming Cheat Sheet | |
| 2 | + | ||
| 3 | + | ## 📊 Reading Sensors | |
| 4 | + | ||
| 5 | + | ```c | |
| 6 | + | // Digital sensors (touch, bumper) - returns 0 or 1 | |
| 7 | + | digital(port_number); // Example: if(digital(0)) { ... } | |
| 8 | + | ||
| 9 | + | // Analog sensors (light, distance) - returns 0 to 4095 | |
| 10 | + | analog(port_number); // Example: if(analog(0) > 2000) { ... } | |
| 11 | + | ||
| 12 | + | // Motor position (encoders) - returns tick count | |
| 13 | + | get_motor_position_counter(motor_port); // Example: while(get_motor_position_counter(0) < 1000) { ... } | |
| 14 | + | ``` | |
| 15 | + | ||
| 16 | + | ## 🚗 Basic Movement | |
| 17 | + | ||
| 18 | + | ```c | |
| 19 | + | // Start motors | |
| 20 | + | motor(port, speed); // speed: -100 to 100 | |
| 21 | + | // Example: Drive forward | |
| 22 | + | motor(0, 50); // Left motor | |
| 23 | + | motor(1, 50); // Right motor | |
| 24 | + | ||
| 25 | + | // Stop all motors | |
| 26 | + | ao(); | |
| 27 | + | ||
| 28 | + | // Wait (in milliseconds) | |
| 29 | + | msleep(1000); // Waits 1 second | |
| 30 | + | ``` | |
| 31 | + | ||
| 32 | + | ## 🔄 Loops | |
| 33 | + | ||
| 34 | + | ```c | |
| 35 | + | // Count-controlled loop | |
| 36 | + | for(int i = 0; i < 5; i++) { | |
| 37 | + | // Repeats 5 times | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | // Condition-controlled loop | |
| 41 | + | while(digital(0) == 0) { | |
| 42 | + | // Runs until sensor is pressed | |
| 43 | + | } | |
| 44 | + | ||
| 45 | + | // Infinite loop (with break) | |
| 46 | + | while(1) { | |
| 47 | + | if(condition) break; | |
| 48 | + | // Runs forever until break | |
| 49 | + | } | |
| 50 | + | ``` | |
| 51 | + | ||
| 52 | + | ## ⚙️ Functions | |
| 53 | + | ||
| 54 | + | ```c | |
| 55 | + | // Simple function | |
| 56 | + | void drive_forward() { | |
| 57 | + | motor(0, 50); | |
| 58 | + | motor(1, 50); | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | // Function with parameters | |
| 62 | + | void drive_speed(int speed) { | |
| 63 | + | motor(0, speed); | |
| 64 | + | motor(1, speed); | |
| 65 | + | } | |
| 66 | + | ||
| 67 | + | // Function that returns a value | |
| 68 | + | int is_wall_close() { | |
| 69 | + | return (analog(0) > 2000); | |
| 70 | + | } | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | ## 📏 Common Movement Patterns | |
| 74 | + | ||
| 75 | + | ```c | |
| 76 | + | // Drive for specific time | |
| 77 | + | void drive_time(int speed, int ms) { | |
| 78 | + | motor(0, speed); | |
| 79 | + | motor(1, speed); | |
| 80 | + | msleep(ms); | |
| 81 | + | ao(); | |
| 82 | + | } | |
| 83 | + | ||
| 84 | + | // Turn right 90 degrees | |
| 85 | + | void turn_right() { | |
| 86 | + | motor(0, 50); // Left forward | |
| 87 | + | motor(1, -50); // Right backward | |
| 88 | + | msleep(800); // Adjust time! | |
| 89 | + | ao(); | |
| 90 | + | } | |
| 91 | + | ||
| 92 | + | // Square up against wall | |
| 93 | + | void square_up() { | |
| 94 | + | while(!digital(0) || !digital(1)) { // Until both bumpers hit | |
| 95 | + | motor(0, 30); | |
| 96 | + | motor(1, 30); | |
| 97 | + | } | |
| 98 | + | ao(); | |
| 99 | + | } | |
| 100 | + | ``` | |
| 101 | + | ||
| 102 | + | ## 🎮 Common Sensor Patterns | |
| 103 | + | ||
| 104 | + | ```c | |
| 105 | + | // Wait for light to start | |
| 106 | + | void wait_for_light() { | |
| 107 | + | while(analog(0) > 200) { | |
| 108 | + | msleep(50); | |
| 109 | + | } | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | // Follow a line | |
| 113 | + | void follow_line() { | |
| 114 | + | if(analog(0) > 2000) { // If sensor sees line | |
| 115 | + | motor(0, 30); // Slow left motor | |
| 116 | + | motor(1, 50); // Fast right motor | |
| 117 | + | } else { | |
| 118 | + | motor(0, 50); // Fast left motor | |
| 119 | + | motor(1, 30); // Slow right motor | |
| 120 | + | } | |
| 121 | + | } | |
| 122 | + | ||
| 123 | + | // Stop at black line | |
| 124 | + | void drive_until_line() { | |
| 125 | + | while(analog(0) < 2000) { // While not on line | |
| 126 | + | motor(0, 50); | |
| 127 | + | motor(1, 50); | |
| 128 | + | } | |
| 129 | + | ao(); | |
| 130 | + | } | |
| 131 | + | ``` | |
| 132 | + | ||
| 133 | + | ## 🔢 Variables & Constants | |
| 134 | + | ||
| 135 | + | ```c | |
| 136 | + | // Define constants (at top of program) | |
| 137 | + | #define LEFT_MOTOR 0 | |
| 138 | + | #define RIGHT_MOTOR 1 | |
| 139 | + | #define LINE_THRESHOLD 2000 | |
| 140 | + | ||
| 141 | + | // Variables for tracking | |
| 142 | + | int cans_collected = 0; | |
| 143 | + | float current_speed = 50.0; | |
| 144 | + | ||
| 145 | + | // Arrays for multiple values | |
| 146 | + | int sensor_readings[3]; | |
| 147 | + | sensor_readings[0] = analog(0); | |
| 148 | + | ``` | |
| 149 | + | ||
| 150 | + | ## 💡 Tips & Tricks | |
| 151 | + | ||
| 152 | + | 1. **Smooth Movement** | |
| 153 | + | - Start slow, speed up, slow down before stopping | |
| 154 | + | - Use small changes in speed (increment by 5-10) | |
| 155 | + | - Check encoder values for straight driving | |
| 156 | + | ||
| 157 | + | 2. **Reliable Sensors** | |
| 158 | + | - Take multiple readings and average them | |
| 159 | + | - Use thresholds with a buffer zone | |
| 160 | + | - Always check sensors before moving | |
| 161 | + | ||
| 162 | + | 3. **Good Programming Habits** | |
| 163 | + | - Comment your code! | |
| 164 | + | - Use meaningful variable names | |
| 165 | + | - Test small parts before big programs | |
| 166 | + | - Back up your code often | |
| 167 | + | ||
| 168 | + | 4. **Debugging** | |
| 169 | + | - Use `printf()` to show values | |
| 170 | + | - Add delays to watch robot behavior | |
| 171 | + | - Test sensors before competition | |
| 172 | + | - Check battery levels | |
| 173 | + | ||
| 174 | + | ## 🏃♂️ Quick Program Template | |
| 175 | + | ||
| 176 | + | ```c | |
| 177 | + | #include <kipr/wombat.h> | |
| 178 | + | ||
| 179 | + | // Constants | |
| 180 | + | #define LEFT_MOTOR 0 | |
| 181 | + | #define RIGHT_MOTOR 1 | |
| 182 | + | #define SENSOR_PORT 0 | |
| 183 | + | ||
| 184 | + | // Functions | |
| 185 | + | void setup() { | |
| 186 | + | wait_for_light(0); | |
| 187 | + | shut_down_in(119); // Safety shutdown | |
| 188 | + | } | |
| 189 | + | ||
| 190 | + | // Main program | |
| 191 | + | int main() { | |
| 192 | + | setup(); | |
| 193 | + | ||
| 194 | + | // Your code here | |
| 195 | + | ||
| 196 | + | ao(); // Always stop motors at end | |
| 197 | + | return 0; | |
| 198 | + | } | |
| 199 | + | ``` | |
Newer
Older