But there is no mention of fabs() in the Arduino reference docs.
https://www.arduino.cc/reference/en/language/functions/math/fabs/ gives an error. Maybe this should be added. Unless it's not officially supported?
Delta_G:
And that's why it is important to post a complete piece of code and not just the one dumb line you can't tell anything is wrong from.
My apologies... but i had only started writing the function at that time. I am trying to calculate the speed of rotation of my robot using the encoders on both wheels. I am sending the commands via serial monitor to turn the robot right or left. Here is what i have so far:
int left, right, turnout = 0; //global variables
//the turn function. It uses the encoder pulse values from both DC motors to calculate current
//speed of rotation and then based on the user command, turns left or right, and outputs the
//appropriate PWM value to send to each motor.
void turn()
{
float turnspeed = 0;
float rotationratio = 0;
int turnmin = -5, turnmax = 5;
if (left == 1 || right == 1)
{
turnspeed = (pulseright + pulseleft); //current turn speed; variables of type int; pulseright and pulseleft have polarities.
turnspeed = abs(turnspeed); //should return absolute float value of turn speed
rotationratio = 5 / turnspeed; //set the speed of rotation
if (rotationratio <= 5)
{
rotationratio = 0.5;
}
if (rotationratio > 5) //this implies that turnspeed < 1
{
rotationratio = 5;
}
}
if (left ==1)
{
turnout += rotationratio;
}
else if (right == 1 )
{
turnout -= rotationratio;
}
else turnout = 0;
if (turnout > turnmax)
turnout = turnmax; //the max value setting of amplitude
if (turnout < turnmin)
turnout = turnmin; //the min value setting of amplitude
Turn_pwm = turnout; //PWM value sent to both motors to initiate the rotation of the robot
}
When i tried turnspeed = fabs(turnspeed);
it doesn't work, for some reason.