On coding Rust and tb6612fng crates

Hello!
I am currently looking to create a motor car using Arduino-UNO R3 and the Rust language with two motors managed by a tb6612fng motor driver.
For this purpose I have adopted the crate currently called “tb6612fng-rs”.
I was able to make the two motors rotate forward, backward, stop, and brake at the same time, but when I tried to give the left and right motors commands to rotate forward and backward respectively and switch the direction of travel, the motors stopped moving.
What could be the cause in this case?

I would appreciate it if you could help me.

Translated with DeepL.com (free version)

I’m not familiar with that particular system, but it will probably be hard for anyone to help with just this description. Can you share your code that isn’t working right?

2 Likes

Sorry. And thank you.
I am attaching the URL of the program in question I created and the documentation of the crate I am using

/*motor.rs*/

use arduino_hal as ah;
use tb6612fng as tb;

use ah::hal::port::{mode::{Output, PwmOutput},
    Pin, PD3, PD4, PD7, PD5, PB0, PD6, PB3};
use ah::simple_pwm::Timer0Pwm;
use tb::{Motor,Tb6612fng, Tb6612fngError, DriveCommand::{Forward,Backward,Brake,Stop}};

type LeftMotorPin1  = Pin<Output, PD4>;
type LeftMotorPin2  = Pin<Output, PD3>;
type LeftPwmPin     = Pin<PwmOutput<Timer0Pwm>, PD5>;
type RightMotorPin1 = Pin<Output, PB0>;
type RightMotorPin2 = Pin<Output, PD7>;
type RightPwmPin    = Pin<PwmOutput<Timer0Pwm>, PD6>;
type StandByPin     = Pin<Output, PB3>;

const DEFAULT_SPEED:u8 = 80;

pub struct MotorControl{
    motor_driver: Tb6612fng<
    LeftMotorPin1, LeftMotorPin2, LeftPwmPin,
    RightMotorPin1, RightMotorPin2, RightPwmPin,
    StandByPin>
}
impl MotorControl{
    pub fn new(
        ain1:LeftMotorPin1, ain2:LeftMotorPin2, pwma:LeftPwmPin,
        bin1:RightMotorPin1, bin2:RightMotorPin2, pwmb:RightPwmPin,
        stby:StandByPin
    ) -> Result<Self, Tb6612fngError<StandByPin>>{
        let motor_left = 
            Motor::new(ain1, ain2, pwma).unwrap();
        let motor_right = 
            Motor::new(bin1, bin2, pwmb).unwrap();
        let motor_driver = 
            Tb6612fng::new(motor_left, motor_right,stby).unwrap();
        return Ok(MotorControl{motor_driver})
    }

    pub fn set_stby(&mut self){
        let _ = self.motor_driver.enable_standby().unwrap();
    }

    pub fn ready_stby(&mut self){
        let _ = self.motor_driver.disable_standby().unwrap();
    }

    pub fn forward(&mut self){
        self.ready_stby();
        let _ = self.motor_driver.motor_a
            .drive(Forward(DEFAULT_SPEED)).unwrap();
        let _ = self.motor_driver.motor_b
            .drive(Forward(DEFAULT_SPEED)).unwrap();
    }

    pub fn leftward(&mut self){
        self.ready_stby();
        let _ = self.motor_driver.motor_a
            .drive(Backward(DEFAULT_SPEED)).unwrap();
        let _ = self.motor_driver.motor_b
            .drive(Forward(DEFAULT_SPEED)).unwrap();
    }

    pub fn rightward(&mut self){
        self.ready_stby();
        let _ = self.motor_driver.motor_a
            .drive(Forward(DEFAULT_SPEED)).unwrap();
        let _ = self.motor_driver.motor_b
            .drive(Backward(DEFAULT_SPEED)).unwrap();
    }

    pub fn stop(&mut self){
        self.ready_stby();
        let _ = self.motor_driver.motor_a
        .drive(Stop).unwrap();
    let _ = self.motor_driver.motor_b
        .drive(Stop).unwrap();
    }

    pub fn brake(&mut self){
        self.ready_stby();
        let _ = self.motor_driver.motor_a
        .drive(Brake).unwrap();
    let _ = self.motor_driver.motor_b
        .drive(Brake).unwrap();
    }
}

tb6612fng-rs
URL : http s://docs.rs/tb6612fng/1.0.0/tb6612fng/struct.Tb6612fng.html

I don’t see anything wrong with the code at first glance. I might be missing something, or the problem might be in the hardware someplace.

The symptoms you describe make me suspect that there might be an unintentional linkage between the two motors (either electrical or mechanical). Some things you can try to hopefully isolate the problem:

  • Does the problem happen when the wheels are not touching the ground?
  • What happens if you command one motor to drive and the other to coast (Stop)?
  • What happens if you unplug one of the motors and run the code that you have?

Describe the results of your trial.

Q. Does the problem occur when the wheels are not on the ground?
A.Yes. Similarly, the forward process of both motors will work, but not during the turning process on either side.

Q.What happens when one motor is driven and the other motor is coasting (stopped)?
A. Only the motor you want to drive will move. The other motor is stopped.

Since both motors are running, it cannot be considered a wiring or power problem.
Is it possible that the problem could be due to another instruction conflict?

This rules out any mechanical issues that I can think of: It’s not ground friction overcoming the motor power and the wheels/motors are physically able to move independently.

It still could be. My current suspicion is that there’s something like a short circuit between the two channels of the motor controller:

  • When they’re both commanded to do the same thing, there is no potential difference across the short, so there is no current flow and everything works correctly.
  • When one motor is commanded to coast, the motor controller effectively disconnects that channel by giving it a high resistance, which also prevents current through the short, allowing the other motor to work properly.
  • When the two motors are commanded to opposite directions, however, the short is placed in parallel with the motor coils. Assuming it has a significantly lower resistance than the motors themselves, all the current will run through the short instead of the motors.

This is still just a guess, but it fits all the facts I have at the moment. It’s also probably biased by my electronics/hardware background.


It could be something like that, but I’m not familiar enough with the particular setup you’re using to say what it might be. If this is a software problem, my guess would be that there’s a configuration issue, like swapping a pair of control pin definitions.

Thank you very much.

I will scrutinize the hardware section again.

Thank you so much for the past few days!