hi all…my project is to build a mobile robot that move forward in certain distance…i using 2 modified servo motor (from 180deg to 360 deg rotation)… i want it move in certain distance…let say in 10meter…i have working code for rotating the servo but it rotate in time (means when we set it rotate 5sec, it will rotate then stop). my problem is how i modified the code so it looping the 5 sec rotating into distance? ( let say in 2 sec, the mobile robot move in 1meter)
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
void setup() {
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000); //
stopRobot();
}
// Motion routines for forward, reverse, turns, and stop
You will not be able to judge distance with your current setup. You will have to put encoders on your wheels to capture how many rotations the wheel has gone, then use an algorithm to calculate distance depending on how big the wheel is. The only problem with this setup is it won’t account for wheel spin.
You can also calculate the time it takes to go 10m and change your delay.
You are looking for accuracy so little things can be very important. Personally, I am not a fan of Servo.write which is meant for an angular movement. I prefer, and always use, Servo.writeMicroseconds. This way to can tailor what you want to do to the specific servo you are using. Just keep in mind that for most servos, stay in the range of 700 to 2300.
for my robot, I use a PING sonar sensor and have it reference a nearby object either in front or in back.
Before any locomotion, it has subroutine that spins the sonar sensor 360 degrees, and checks if anything is in front or behind 1st. Also any objects outside a 20 degree window (wedge) fore or aft causes the result to be negative, causing distance tracking/sensing to rely on another method like wheel rotation or another future device.
Mine is mostly slated for “watchdog” duties, so it almost always has a solid object (wall) at its back while monitoring.