🧠Algorithms & Strategy
Our robot utilizes a linear sequential strategy driven by sensor feedback loops. Below is the high-level logic flow for the ‘Test Left’ autonomous retrieval routine.
1. Logic Flowchart
This diagram represents the decision-making process executed inside the main() loop.
2. Attacker Strategy Source Code
Goal: Locate the ores, secure it with the claw, pull the claw up and put it into the box using Gyro-stabilized movement.
/* PSEUDO-CODE: ATTACKER LOGIC */
// ==================== CONFIGURATION ====================
CONSTANT MAX_SPEED = (Motor Limit)
CONSTANT ARM_UP = 1
CONSTANT ARM_DOWN = 0
CONSTANT CLAMP_CLOSE = 1
CONSTANT CLAMP_OPEN = 0
// ==================== MAIN PROGRAM ====================
FUNCTION Main() {
Initialize_EV3_Systems()
// Setup Sensors
Set_Color_Mode("REFLECT")
Set_Gyro_Mode("ANGLE")
// Hardware Gyro Reset (Toggle modes to zero out drift)
Switch_Gyro_Mode("RATE")
Wait(0.1 seconds)
Switch_Gyro_Mode("ANGLE")
// Execute the main mission strategy
Run_Test_Left()
}
// ==================== STRATEGY LOGIC ====================
FUNCTION Run_Test_Left() {
// --- OBJECT 1 ---
Drive_Straight_Until_Black_Line(Threshold: 25, Speed: 60%)
Claw_Control(CLAMP_CLOSE)
Arm_Control(ARM_UP)
Drive_Backward_Distance(65 cm)
Turn_Gyro(-90 degrees) // Left
Drive_Forward_Distance(6 cm)
Turn_Gyro(-89 degrees) // Adjustment
Drive_Forward_Distance(6 cm)
Claw_Control(CLAMP_OPEN)
// --- OBJECT 2 ---
Turn_Gyro(90 degrees)
Drive_Backward_Distance(40 cm)
Arm_Control(ARM_DOWN)
Turn_Gyro(90 degrees)
Drive_Straight_Until_Black_Line(Threshold: 25, Speed: 70%)
Drive_Backward_Distance(2 cm)
Claw_Control(CLAMP_CLOSE)
Arm_Control(ARM_UP)
Turn_Gyro(180 degrees)
Drive_Forward_Distance(35 cm)
Turn_Gyro(90 degrees)
Drive_Forward_Distance(8 cm)
Turn_Gyro(-90 degrees)
Drive_Forward_Distance(8 cm)
Claw_Control(CLAMP_OPEN)
// --- OBJECT 3 ---
Turn_Gyro(90 degrees)
Drive_Backward_Distance(70 cm)
Arm_Control(ARM_DOWN)
Turn_Gyro(90 degrees)
Drive_Straight_Until_Black_Line(Threshold: 25, Speed: 70%)
Drive_Backward_Distance(2 cm)
Claw_Control(CLAMP_CLOSE)
Arm_Control(ARM_UP)
Turn_Gyro(180 degrees)
Drive_Forward_Distance(35 cm)
Turn_Gyro(90 degrees)
Drive_Forward_Distance(8 cm)
Turn_Gyro(-90 degrees)
Drive_Forward_Distance(8 cm)
Claw_Control(CLAMP_OPEN)
// --- OBJECT 4 ---
Turn_Gyro(180 degrees)
Arm_Control(ARM_DOWN)
Drive_Straight_Until_Black_Line(Threshold: 25, Speed: 70%)
Claw_Control(CLAMP_CLOSE)
Arm_Control(ARM_UP)
Turn_Gyro(180 degrees)
Drive_Forward_Distance(8 cm)
Claw_Control(CLAMP_OPEN)
// --- OBJECT 5 ---
Turn_Gyro(180 degrees)
Arm_Control(ARM_DOWN)
Drive_Straight_Until_Black_Line(Threshold: 25, Speed: 70%)
Claw_Control(CLAMP_CLOSE)
Arm_Control(ARM_UP)
Turn_Gyro(180 degrees)
Drive_Forward_Distance(8 cm)
Claw_Control(CLAMP_OPEN)
// --- END GAME / CELEBRATION ---
Turn_Gyro(180 degrees)
Arm_Control(ARM_DOWN)
Drive_Forward_Distance(10 cm)
Turn_Gyro(90 degrees)
Drive_Straight_Until_Black_Line(Threshold: 25)
Claw_Control(CLAMP_CLOSE)
Arm_Control(ARM_UP)
Turn_Gyro(180 degrees)
Drive_Forward_Distance(8 cm)
Turn_Gyro(-90 degrees)
Drive_Forward_Distance(8 cm)
Claw_Control(CLAMP_OPEN)
}
// ==================== HELPER DEFINITIONS ====================
FUNCTION Drive_Straight_Until_Black_Line(threshold, speed) {
// Uses Gyro to keep straight (PID)
// Checks Color Sensor Reflected Light
WHILE (Color_Sensor_Value < threshold) {
Adjust_Motor_Speeds_Based_On_Gyro_Drift()
}
Stop_Motors()
}
FUNCTION Turn_Gyro(degrees) {
Calculate_Target_Angle()
// Coarse turn (fast)
WHILE (Current_Angle != Target_Angle) {
Rotate_Motors_Opposite_Directions()
}
// Fine tune (slow) to fix overshoot
WHILE (Current_Angle != Target_Angle) {
Micro_Adjust_Motors()
}
Stop_Motors()
}