sparkfun TB6612 motor driver

Hi,

I was trying to learn how to use Sparkfun's TB6612FNG motor driver. I was going through this tutorial web page: https://learn.sparkfun.com/tutorials/tb6612fng-hookup-guide . The following code could be found under the heading "Library and Example Code".

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

I do not understand the purpose of offsetA and offsetB. Could you please help me with it? What would happen if I change offsetA to "-1"? Thank you.

painterguy: Hi,

I was trying to learn how to use Sparkfun's TB6612FNG motor driver. I was going through this tutorial web page: https://learn.sparkfun.com/tutorials/tb6612fng-hookup-guide . The following code could be found under the heading "Library and Example Code".

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

I do not understand the purpose of offsetA and offsetB. Could you please help me with it? What would happen if I change offsetA to "-1"? Thank you.

this paragraph in the link you shared explains it!:

Here we have a basic library. There are two main parts. First, you can send commands like forward, and it will propel your bot forward. This means the right wheel is going clockwise and the left wheel is going counterclockwise. Which way is clockwise and which is counterclockwise depends on which wire of your motor is connected to which of the inputs. This means the forward function might not actually propel the robot forward the first time. You can swap the motor wires if you want, but that is often not possible. The easier solution is to fix this in the software. Near the top of the example code, you will see two constants labeled offset. You can change this from 1 to -1 to swap the configuration of that motor

if it's not clear enough, this is what I understood from it:

offsetA and offsetB determine the direction of rotation of your motors and only really need to be ajusted (to 1 or -1) if you made a mistake in the wiring of your motor - eg when you execute 'forward' command they 'reverse' - and you do not wish to swap the motor wires round to fix the issue.

I'm guess, 'offsetA' is for 'MotorA' and 'offsetB' is for 'MotorB'

hope that helps....

Thank you!

In a three wheel robot where only two wheels are powered, both wheels need to move in the same direction in order to move forward. But when you look at each wheel separately, one would be rotating in clockwise direction from your perspective and the other in counterclockwise.

I think now I understand why offsetA and offsetB are there.

New Question:
The following code could be found under “examples” folder in the downloaded library folder. The library could be downloaded here:
https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library

I do not think that there is a way to run both wheels forward or backward with different values of PWM or speed. Let me elaborate. In the code, it says, “forward(motor1, motor2, 150)”. What if I need to run motor1 at 150 but motor2 at 148. Could you please help me with it? Thank you.

/******************************************************************************
TestRun.ino
TB6612FNG H-Bridge Motor Driver Example code
Michelle @ SparkFun Electronics
8/20/16
https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library

Uses 2 motors to show examples of the functions in the library.  This causes
a robot to do a little 'jig'.  Each movement has an equal and opposite movement
so assuming your motors are balanced the bot should end up at the same place it
started.

Resources:
TB6612 SparkFun Library

Development environment specifics:
Developed on Arduino 1.6.4
Developed with ROB-9457
******************************************************************************/

// This is the library for the TB6612 that contains the class Motor and all the
// functions
#include <SparkFun_TB6612.h>

// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
// the default pins listed are the ones used on the Redbot (ROB-12097) with
// the exception of STBY which the Redbot controls with a physical switch
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 8
#define PWMA 5
#define PWMB 6
#define STBY 9

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void setup()
{
 //Nothing here
}


void loop()
{
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor1.drive(255,1000);
   motor1.drive(-255,1000);
   motor1.brake();
   delay(1000);
   
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor2.drive(255,1000);
   motor2.drive(-255,1000);
   motor2.brake();
   delay(1000);
   
   //Use of the forward function, which takes as arguements two motors
   //and optionally a speed.  If a negative number is used for speed
   //it will go backwards
   forward(motor1, motor2, 150);
   delay(1000);
   
   //Use of the back function, which takes as arguments two motors 
   //and optionally a speed.  Either a positive number or a negative
   //number for speed will cause it to go backwards
   back(motor1, motor2, -150);
   delay(1000);
   
   //Use of the brake function which takes as arguments two motors.
   //Note that functions do not stop motors on their own.
   brake(motor1, motor2);
   delay(1000);
   
   //Use of the left and right functions which take as arguements two
   //motors and a speed.  This function turns both motors to move in 
   //the appropriate direction.  For turning a single motor use drive.
   left(motor1, motor2, 100);
   delay(1000);
   right(motor1, motor2, 100);
   delay(1000);
   
   //Use of brake again.
   brake(motor1, motor2);
   delay(1000);
   
}