L9110 Motor Driver with DFRobot L298p under Arduino and ROS

Hello,
Thank you for reading this.
I have been trying to develop Robot as described here.

However, I am unable to run my motors backward. I am using the same code. However, instead of directly running motors (L9110) with Arduino, I am using DFRobot L298p differential driver as described here. The code looks similar to me.

Even though I am running the same code as mentioned in this tutorial

I can run motors in one direction only i.e.
rostopic pub -1 /wheel_power_left std_msgs/Float32 '{data: 1.0}'

rostopic pub -1 /wheel_power_left std_msgs/Float32 '{data: -1.0}' does not work.

Could you please help me and tell me what could be wrong with rostopic pub -1 /wheel_power_left std_msgs/Float32 '{data: -1.0}'. Thanks

Hi,

First of all , check that the the real robot can move the wheels backwards. Check the PWM and the basic controller commands that should make it move backwards.

If that works, the you should check that the note that has the /wheel_power_left subscriber gets the correct that’s and sends the correct controller command

OK. I have figured this out. The problem was in the c++ code block

void turnWheel( const std_msgs::Float32 &wheel_power,
            unsigned int pwm_pin,
            unsigned int fr_pin ) {
float factor = max(min(wheel_power.data, 1.0f), -1.0f);
if( factor >= 0 ) {
    digitalWrite(fr_pin, LOW);
    analogWrite(pwm_pin, (unsigned int)(255 * factor));
} else {
    digitalWrite(fr_pin, HIGH);
    analogWrite(pwm_pin, (unsigned int)(255 * (1.0f + factor)));
}   

}

So, I changed it to

 void turnWheel( const std_msgs::Float32 &wheel_power,
            unsigned int pwm_pin,
            unsigned int fr_pin ) {
float factor = max(min(wheel_power.data, 1.0f), -1.0f);
if( factor >= 0 ) {
    digitalWrite(fr_pin, LOW);
    analogWrite(pwm_pin, (unsigned int)(255 * factor));
} else {
    digitalWrite(fr_pin, HIGH);
    analogWrite(pwm_pin, (unsigned int)(255 * abs(factor)));
}   

}

Note the difference is in negative side of the code after else {…, instead of using

analogWrite(pwm_pin, (unsigned int)(255 * (1.0f + factor)));

I have changed it to

analogWrite(pwm_pin, (unsigned int)(255 * abs(factor)));

and it worked.
Thank you for writing.