Last active 7 hours ago

claude_cheat_sheet.md Raw

🤖 Botball C Programming Cheat Sheet

📊 Reading Sensors

// Digital sensors (touch, bumper) - returns 0 or 1
digital(port_number);  // Example: if(digital(0)) { ... }

// Analog sensors (light, distance) - returns 0 to 4095
analog(port_number);   // Example: if(analog(0) > 2000) { ... }

// Motor position (encoders) - returns tick count
get_motor_position_counter(motor_port);  // Example: while(get_motor_position_counter(0) < 1000) { ... }

🚗 Basic Movement

// Start motors
motor(port, speed);    // speed: -100 to 100
// Example: Drive forward
motor(0, 50);         // Left motor
motor(1, 50);         // Right motor

// Stop all motors
ao();

// Wait (in milliseconds)
msleep(1000);         // Waits 1 second

🔄 Loops

// Count-controlled loop
for(int i = 0; i < 5; i++) {
    // Repeats 5 times
}

// Condition-controlled loop
while(digital(0) == 0) {
    // Runs until sensor is pressed
}

// Infinite loop (with break)
while(1) {
    if(condition) break;
    // Runs forever until break
}

⚙️ Functions

// Simple function
void drive_forward() {
    motor(0, 50);
    motor(1, 50);
}

// Function with parameters
void drive_speed(int speed) {
    motor(0, speed);
    motor(1, speed);
}

// Function that returns a value
int is_wall_close() {
    return (analog(0) > 2000);
}

📏 Common Movement Patterns

// Drive for specific time
void drive_time(int speed, int ms) {
    motor(0, speed);
    motor(1, speed);
    msleep(ms);
    ao();
}

// Turn right 90 degrees
void turn_right() {
    motor(0, 50);    // Left forward
    motor(1, -50);   // Right backward
    msleep(800);     // Adjust time!
    ao();
}

// Square up against wall
void square_up() {
    while(!digital(0) || !digital(1)) {  // Until both bumpers hit
        motor(0, 30);
        motor(1, 30);
    }
    ao();
}

🎮 Common Sensor Patterns

// Wait for light to start
void wait_for_light() {
    while(analog(0) > 200) {
        msleep(50);
    }
}

// Follow a line
void follow_line() {
    if(analog(0) > 2000) {      // If sensor sees line
        motor(0, 30);           // Slow left motor
        motor(1, 50);           // Fast right motor
    } else {
        motor(0, 50);           // Fast left motor
        motor(1, 30);           // Slow right motor
    }
}

// Stop at black line
void drive_until_line() {
    while(analog(0) < 2000) {   // While not on line
        motor(0, 50);
        motor(1, 50);
    }
    ao();
}

🔢 Variables & Constants

// Define constants (at top of program)
#define LEFT_MOTOR 0
#define RIGHT_MOTOR 1
#define LINE_THRESHOLD 2000

// Variables for tracking
int cans_collected = 0;
float current_speed = 50.0;

// Arrays for multiple values
int sensor_readings[3];
sensor_readings[0] = analog(0);

💡 Tips & Tricks

  1. Smooth Movement

    • Start slow, speed up, slow down before stopping
    • Use small changes in speed (increment by 5-10)
    • Check encoder values for straight driving
  2. Reliable Sensors

    • Take multiple readings and average them
    • Use thresholds with a buffer zone
    • Always check sensors before moving
  3. Good Programming Habits

    • Comment your code!
    • Use meaningful variable names
    • Test small parts before big programs
    • Back up your code often
  4. Debugging

    • Use printf() to show values
    • Add delays to watch robot behavior
    • Test sensors before competition
    • Check battery levels

🏃‍♂️ Quick Program Template

#include <kipr/wombat.h>

// Constants
#define LEFT_MOTOR 0
#define RIGHT_MOTOR 1
#define SENSOR_PORT 0

// Functions
void setup() {
    wait_for_light(0);
    shut_down_in(119); // Safety shutdown
}

// Main program
int main() {
    setup();
    
    // Your code here
    
    ao(); // Always stop motors at end
    return 0;
}