New Smooth Accelleration/Decelleration Power Curve for Using Sonar Sensor (ROBOTC, EV3)

Our Robotics Team was having trouble with the robots starting and stopping so quickly that there was wheel slippage. This was effecting our gameplay and likely cost us first place last year. This is the code that was generated to help alliviate that issue. This specific code is designed to use the UltraSonic sensor. It is a little buggy, but can be tweaked. I would love to incorportate a variety of the PID algorithm to this elliptical ‘P’ algorithm. Any thought leave a comment below on our facebook page. See also: Distance Based Algorithm


#pragma config(Sensor, S1, touch, sensorEV3_Touch)
#pragma config(Sensor, S2, gyro, sensorEV3_Gyro)
#pragma config(Sensor, S3, color, sensorEV3_Color)
#pragma config(Sensor, S4, sonic, sensorEV3_Ultrasonic)
#pragma config(Motor, motorB, left, tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorC, right, tmotorEV3_Large, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

//Standard dimensions for program
float wheelDiameter = 5.6; // standard EV3 Wheel Diameter in cm.

void resetMotors() {
// Name: resetMotors
// Author: Andy Hatfield
// Desc: Resets the motor encoder for left and right (generally B and C respectively
resetMotorEncoder(left);
resetMotorEncoder(right);
}

void brake () {
// Name: brake
// Author: Andy Hatfield
// Desc: Stops the wheels for a duration of 1/100 sec
setMotorSync(left, right, 0, 0);
sleep(10);
}

void moveStraightToObject(float pwr, float dist) // pwr is max power, dist is from an object in cm and needs wheelDiameter defined globally.
{
// Name: moveStraightToObject
// Author: Andy Hatfield
// Desc: This moveStraight has an elliptical accelleration curve, starts easy and ends easy preventing wheel slippage due to
// starting and stopping too quickly. Positive pwr is forward, negative pwr is also forward, it cannot go backward.
// Notes:
// It is over shooting by less than a cm each time.
// THis assumes that the robot can see the wall or object in front if it. Otherwise it fails to operate.

float toObject; // The distance to the object as originally recorded by sonar.
float center; // mathematical center of the ellipse that we are using to create the power curve.
toObject = getUSDistance(sonic);
center = (toObject - dist) / 2;
float lPwr; // a local Power
float x;
pwr = abs(pwr);
resetMotors();
while (abs(getUSDistance(sonic)) >= dist) {
x = ceil(toObject - abs(getUSDistance(sonic)));
lPwr = 4 + pwr * sqrt(1 - (1 / pow(center, 2)) * pow(x - center, 2));
if(lPwr < 3) {lPwr = 3;}; setMotorSync(left, right, 0, lPwr); } brake(); } task main() { moveStraightToObject(90, 11); }

Leave a Reply

Your email address will not be published. Required fields are marked *

*