Skip to content

Instantly share code, notes, and snippets.

@aadishv
Created May 25, 2025 18:03
Show Gist options
  • Select an option

  • Save aadishv/8b671b67415c62458e2b832cb941d4c7 to your computer and use it in GitHub Desktop.

Select an option

Save aadishv/8b671b67415c62458e2b832cb941d4c7 to your computer and use it in GitHub Desktop.

This was a one-shot conversation with Claude Sonnet 4 on our (private) 3151A C++ codebase. Only the source files (no headers) were linked.

Bug Hunt in VEX Robotics Code

User

...

@src

I challenge you to find a bug in this code. There are more than one. Take your time. Use as many tools as you need and think for as long as you'd like.

Agent

Use Tool: thinking (toolu_01QqLftT9wmcyRDvYXKC2iuo)

Tool Results: toolu_01QqLftT9wmcyRDvYXKC2iuo

Finished thinking.

Agent

I'll systematically examine this VEX robotics codebase to identify bugs. Let me start by looking for common patterns and issues.

Use Tool: grep (toolu_017tsV5qmuMMwffdpKJ9uoUq)

Tool Results: toolu_017tsV5qmuMMwffdpKJ9uoUq

No matches found

Agent

Use Tool: grep (toolu_01Nho6Mj9NW6UAFjrK48pE8q)

Tool Results: toolu_01Nho6Mj9NW6UAFjrK48pE8q

No matches found

Agent

Use Tool: grep (toolu_01SggH8B2h6s6vHMw7Wpnysw)

Tool Results: toolu_01SggH8B2h6s6vHMw7Wpnysw

Found 5 matches:

Matches in 3151A/src/robot.cpp

Agent

Great! I found the first bug. Let me examine this more closely:

Use Tool: read_file (toolu_01CicVC4Fz61Jjsk1A2BBzU2)

Tool Results: toolu_01CicVC4Fz61Jjsk1A2BBzU2

Agent

Bug #1 Found!

In robot.cpp line 371-372:

errorAcceptable = fabs(error) > 100; // is the error acceptable for movement?

This is backwards logic! The variable errorAcceptable is set to true when the error is large (> 100), but logically, an error should be "acceptable" when it's small. Later in the code, when !errorAcceptable is true (meaning the error is small), it stops the mechanism, which suggests the intended logic was reversed.

Now let me look for more bugs:

Use Tool: grep (toolu_01FyPdbdzryhD7xjvDGcVc6F)

Tool Results: toolu_01FyPdbdzryhD7xjvDGcVc6F

No matches found

Agent

Use Tool: grep (toolu_01FC3oYDow14CK2u4VdCesSK)

Tool Results: toolu_01FC3oYDow14CK2u4VdCesSK

No matches found

Agent

Use Tool: grep (toolu_01JNGgFNaZEudC9D9KqLNjbJ)

Tool Results: toolu_01JNGgFNaZEudC9D9KqLNjbJ

No matches found

Agent

Let me search for unreachable code more broadly:

Use Tool: grep (toolu_01BjeK1a78SZK1FkADKkDxaw)

Tool Results: toolu_01BjeK1a78SZK1FkADKkDxaw

No matches found

Agent

Use Tool: grep (toolu_01X24qi12XRajjsbK1QAzxgb)

Tool Results: toolu_01X24qi12XRajjsbK1QAzxgb

Found 1 matches:

Matches in 3151A/src/robot.cpp

Agent

Bug #2 Found!

Use Tool: read_file (toolu_017ZFqXSAisiSP1M8ackfn6q)

Tool Results: toolu_017ZFqXSAisiSP1M8ackfn6q

Agent

In robot.cpp lines 908-909:

return false;
pros::delay(100);

The pros::delay(100); line is unreachable code because it comes after a return statement.

Now let me look for motor control issues:

Use Tool: grep (toolu_01FBsvXSXujt7gCTuFSjZw18)

Tool Results: toolu_01FBsvXSXujt7gCTuFSjZw18

No matches found

Agent

Use Tool: grep (toolu_01BwUfd8FVP3mDDpniRFWsaa)

Tool Results: toolu_01BwUfd8FVP3mDDpniRFWsaa

Found 2 matches:

Agent

Bug #3 Found!

Let me examine these motor control issues:

Use Tool: read_file (toolu_019WXdsPrUjwbyZejA7ZnU1Z)

Tool Results: toolu_019WXdsPrUjwbyZejA7ZnU1Z

Agent

In robot.cpp lines 657-663 in smartClamp():

while (goalDistance.get_distance() > 60) {
    leftMotorGroup.move_velocity(-127);
    rightMotorGroup.move_velocity(-127);
    
    if (pros::millis() - startApproach > 1500) break;
    pros::delay(10);
}

After the timeout break, the motors are still running at -127 velocity but there's no code to stop them before proceeding to the clamp operation.

Use Tool: read_file (toolu_019ZHDdFieHWyiFAohjkB9MR)

Tool Results: toolu_019ZHDdFieHWyiFAohjkB9MR

Agent

Bug #4 Found!

Same issue in clampMobileGoal() in lines 698-710: motors continue running at -127 velocity after the timeout break, with no code to stop them before the clamp operation.

Now let me look for namespace inconsistencies:

Use Tool: grep (toolu_01YcG7fF3JsEXXfBEXPrHYi3)

Tool Results: toolu_01YcG7fF3JsEXXfBEXPrHYi3

Found 2 matches:

Agent

Bug #5 Found!

Use Tool: read_file (toolu_01Y6PDCoiq7ugfE3naS4S6o8)

Tool Results: toolu_01Y6PDCoiq7ugfE3naS4S6o8

Agent

In interaction.cpp lines 238-242, there's inconsistent namespace usage:

  • Line 238: robot::hardware::optical.get_proximity()
  • Line 239: optical.get_hue()
  • Line 241: optical.get_hue()

The code uses the full namespace for get_proximity() but just optical for get_hue(). This will cause a compilation error if optical is not in scope.

Let me look for potential infinite loops:

Use Tool: grep (toolu_01AAvaha3ZoX9iMuZMuGiUvy)

Tool Results: toolu_01AAvaha3ZoX9iMuZMuGiUvy

No matches found

Agent

Use Tool: grep (toolu_014adcCGXXnncyvNiFjVnv7t)

Tool Results: toolu_014adcCGXXnncyvNiFjVnv7t

Found 11 matches:

Agent

Let me examine the potentially problematic infinite loops:

Use Tool: read_file (toolu_01SkqWjYy6VTM1ZaPnK1U8ao)

Tool Results: toolu_01SkqWjYy6VTM1ZaPnK1U8ao

Agent

Bug #6 Found!

In robot.cpp in the rejectRing() function (lines 574-600), there's a potential infinite loop. The outer while (true) loop only breaks if distance.get_distance() >= 45 on line 580. However, there's a logical issue:

  1. Line 576-579: Wait until distance.get_distance() >= 50
  2. Line 580: Check if distance.get_distance() >= 45

Since 50 > 45, if the distance is >= 50, it will definitely be >= 45, so the break should always execute. But if somehow the distance drops between 45-49 range after the first while loop, this could create an infinite loop.

Now let me look for logic issues in the intake task:

Use Tool: read_file (toolu_01JphgYKR19oCz5WjWqevW5o)

Tool Results: toolu_01JphgYKR19oCz5WjWqevW5o

Agent

Bug #7 Found!

In robot.cpp in the intakeTask() function, there's contradictory logic:

Lines 443-446:

if (!currentIntakeTask.has_value() || intakeBreak) {
    printf("continuing\n");
    std::cout << "continuing" << std::endl;
    currentIntakeTask.reset();
    continue;
}

Lines 447-452:

if (!intakeBreak) {     //(heldRing == NONE)
    intakeMotor.move(127);
    intakeBottom.move(127);
} else {
    intakeMotor.move(0);
    intakeBottom.move(0);
}

The logic is flawed: if intakeBreak is true in the first check, the function continues (skips the rest). But if it wasn't true initially, there's no guarantee it's still false when the second check happens, making the second check potentially redundant or causing inconsistent behavior.

Let me look for one more type of bug - let me check for conditional compilation issues:

Use Tool: grep (toolu_01VVVp8omqhXg2HECGvfC5J7)

Tool Results: toolu_01VVVp8omqhXg2HECGvfC5J7

Agent

Let me examine a potential issue in the rejectTask:

Use Tool: read_file (toolu_01Lh5kGwQZmYmc2Mh2aUsv5e)

Tool Results: toolu_01Lh5kGwQZmYmc2Mh2aUsv5e

Agent

Bug #8 Found!

In robot.cpp in the rejectTask() function (lines 614-615), there's a logic error:

double previous_position = intakeMotor.get_position();
if (intakeMotor.get_position() - previous_position > 15) {

The previous_position is set to the current position, and then immediately compared to the current position. Since these are the same value, intakeMotor.get_position() - previous_position will always be 0, so the condition > 15 will never be true. This suggests the code should either:

  1. Store the previous position outside the conditional block, or
  2. Use a different logic to determine when to reject

Summary

I found 8 bugs in this VEX robotics codebase:

  1. Logic Error in wallStakeTask(): errorAcceptable is set backwards - it's true when error is large instead of small
  2. Unreachable Code: pros::delay(100) after return false in deviceCheck()
  3. Motor Control Bug in smartClamp(): Motors continue running after timeout break
  4. Motor Control Bug in clampMobileGoal(): Same issue - motors not stopped after timeout
  5. Namespace Inconsistency: Mixed usage of robot::hardware::optical and optical in interaction.cpp
  6. Potential Infinite Loop: Edge case in rejectRing() function logic
  7. Contradictory Logic: Redundant intakeBreak checks in intakeTask()
  8. Impossible Condition: previous_position immediately compared to current position in rejectTask()

These bugs range from compilation issues to runtime logic errors that could cause unpredictable robot behavior during competition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment