basic program for encoder module


#1

Hello we are a simple class asking for help.we would like to know if anyone has a basic encoder module program that will go about 12 inches forward and stop.


#2

I’m going to provide a basic version that might drift and overshoot 12 inches. I can provide a slightly more complex but more accurate version if you’d like.


#define WHEEL_RADIUS 2;
function travelDistance(float inches) {
    int ticks = (int)((inches / WHEEL_RADIUS ) * ( 180 / PI ));
    while(SensorValue[encoder] < ticks) {
        //run motors 
    }
    //stop motors upon loop exit
}


#3

This version utilizes a brake to prevent overshoot.



void driveFoward(int inches){
	SensorValue[encoder] = 0;
	float distance = inches/(wheel_diameter*PI) * 360;      // replace wheel_diameter with the diameter of your wheels

	while(SensorValue[encoder] <  distance){
		// run motors
	}
	// run motors at some negative speed
	wait1Msec(200);       // you will have to tune the amount of time
	// stop motors
}

driveFoward(12);