The 3pi+ turtle robot refuses to turn close to a perfect 90 and when it breaks between forward commands it’s too strong slightly throwing it off course. I’m attaching my code please help
Hello.
It looks like you are trying to turn the robot just by timing how long to power the motors for. That method might be okay for turning to face a general direction, but I expect it to be difficult to turn “a perfect 90°” that way. I suggest you try using sensor feedback from either the encoders or the robot’s inertial sensors for that. There are advantages and disadvantages to each of those approaches, but it looks like you are already using the encoders to control how far your robot goes while moving forward, so that would probably be the easiest option to try first.
By the way, if you try that and want to post updated code, instead of using screenshots of the program, I recommend you just copy and paste the code into your post. Then add a line with ```
before and after it to format it in the post.
- Patrick
Hey Patrick Sorry for the late response, I recoded the bot but the new problem I’m having is that the left turn overturns and the right one underturns and I cant seem to fix it.
#include <Pololu3piPlus32U4.h>
#include <PololuMenu.h>
using namespace Pololu3piPlus32U4;
// Declare peripherals
OLED display;
Buzzer buzzer;
Motors motors;
Encoders encoders;
ButtonB buttonB;
// Define movement parameters
uint16_t maxSpeed = 200; // Slow down for precise control
uint16_t countsPer90Turn = 150; // Encoder counts needed for a 90-degree turn (adjust for your robot)
// Function to move forward a set distance (in encoder counts)
void moveForward(uint16_t counts)
{
encoders.getCountsAndResetLeft();
encoders.getCountsAndResetRight();
motors.setSpeeds(51, 50); // Forward motion
while (encoders.getCountsLeft() < counts && encoders.getCountsRight() < counts)
{
// Keep moving forward
}
motors.setSpeeds(0, 0); // Stop motors
}
// Function to turn 90 degrees to the right using encoders
void turnRight90Encoders()
{
encoders.getCountsAndResetLeft();
encoders.getCountsAndResetRight();
motors.setSpeeds(maxSpeed, -maxSpeed); // Rotate in place (left wheel forward, right wheel backward)
while (encoders.getCountsLeft() < countsPer90Turn || encoders.getCountsRight() > -countsPer90Turn)
{
// Wait for the encoders to reach the target counts
}
motors.setSpeeds(0, 0); // Stop motors
}
// Function to turn 90 degrees to the left using encoders
void turnLeft90Encoders()
{
encoders.getCountsAndResetLeft();
encoders.getCountsAndResetRight();
motors.setSpeeds(-maxSpeed, maxSpeed); // Rotate in place (left wheel backward, right wheel forward)
while (encoders.getCountsLeft() > -countsPer90Turn || encoders.getCountsRight() < countsPer90Turn)
{
// Wait for the encoders to reach the target counts
}
motors.setSpeeds(0, 0); // Stop motors
}
void setup()
{
// Welcome sound
buzzer.play(">g32>>c32");
// Display message and wait for button press
display.clear();
display.print(F("Press B to start"));
while (!buttonB.getSingleDebouncedPress());
// Step 1: Move forward 50 cm
delay(500);
moveForward(7.5 / 0.011);
// Step 2: Turn right 90 degrees
delay(900);
turnRight90Encoders();
delay(500);
moveForward(7.5 / 0.011);
// Step 3: Turn left 90 degrees
turnLeft90Encoders();
moveForward(7.5 / 0.011);
delay(500);
turnLeft90Encoders();
moveForward(7.5 / 0.011);
}
void loop()
{
// No loop logic needed for this task
}
I fixed the how the code was formatted in your first reply and deleted the second reply since that made it redundant.
I recommend you either use the display, or tether your 3pi+ to your computer with a USB cable so you can use the Arduino Serial Monitor to output feedback from your sensors so you can compare what you expect to happen, what is actually happening, and what your robot sensors are reporting (i.e. what your robot thinks is happening). That should give you some indications about what is causing your results.
For example, if you look at your encoder counts after a turn and they are clearly overshooting the target or not reaching the target, that suggests there is room for improvement in your turning control method/algorithm. If the encoder counts are reaching the values you expect, then that might suggest some other physical phenomena is at work (e.g. wheel/tire slippage). In that scenario, it might be useful to also incorporate the inertial sensors into your control scheme.
- Patrick