from http://www.pjrc.com/teensy/td_libs_Encoder.html i got a nice 4X encoding library to include and i successfully use this library to determine directions of the motor rotations, and by manually rotating the wheel attached to my motor, completing 1 CW revolution via the wheel, gives +360 count to the program, and i connected CHA and CHB to 2 interrupt pins so the positions i get from encoder is very accurate...
problem comes when i want the robot to move forward another 10cm after it meets the junction, and then turn right 90degrees so that it will be in the right track
even with accurate counts from the encoder i did not successfully control the robot to move in such way...i just need it to move for 180counts then exit the while loop, but it only stops after it moves for over 600counts (meaning more than 2 revolutions) the robot travelled too far from what i needed...at first i thought it was because of my robot momentum but when i left the wheels moving without touching the ground the result is still the same.. happens that the loop that i write, when the robot applies PWM to the motor driver, it exceeds the encoder counter way too much before it exits the loop..
i have written getEncoder() to get encoder values, resetEncoder() to reset encoder values when it arrive junction, tempStop() to stop the motor driver and goStraight to make it go another 10cm from junction, the
- CODE: SELECT_ALL_CODE
float lengthPerCount = (80 * 3.1416) / 360;
// π X Diameter of wheel divide by 360(<- 1 revolution of wheel gives 360 counts, so i find distance travelled if count increase by 1)
float offsetLength = 125; //this is the distance i need to travel From Junction Distance, in mm
long needCount1 = offsetLength / lengthPerCount; //so i set loop conditions using the extra count needed to travel 125mm
====
void goStraight () //GO STRAIGHT FOR OFFSET 125MM//
{ resetEncoder();
getEncoder(); //get the new encoder value and put it in positionLeft and positionRight, and this is 0 before it loops
while(positionLeft < needCount1 || positionRight < needCount1)
{
getEncoder();
digitalWrite (M1, LOW); //apply pwm to motor driver making it to move forward
digitalWrite (M2, HIGH);
analogWrite(E1,110);
analogWrite(E2,110);
}
tempStop(); //stop after it finished travel 125mm
delay(1000);
//goRight();
}
the goRight() function is written in the same manner but implementing it will make the robot rotate itself for 2 circles instead of 90 degrees...
its just a simple go and turn but i failed already... figuring about the loops for days already...what can i do to improve this condition? or do i need to apply PID algorithm? the code from http://www.cytron.com.my/viewProduct.php?pcode=SPG30E-30K&name=DC%20Geared%20Motor%20with%20Encoder was not for arduino...