Before the end of this year, I wanted to enrich my uPLibrary a new managed driver for the dual motor driver TB6612FNG that I used earlier this year in a demo at the Microsoft Embedded Conference 2013 on a robot equipped with a Netduino Plus and piloted through the Kinect (thanks to Clement Giorio).

The dual motor driver

This Toshiba motor driver allows to drive two DC motors (Direct Current) with a maximum power of 15 V and with four operating modes: CW (ClockWise), CCW (CounterClockWise), short brake and stop. Also, the speed of each motor can be varied through a PWM (Pulse Width Modulation) by changing the duty cycle.

09457-01b

On the Sparkfun website a useful breakout board that mounts the motor control and makes it easily accessible pins is available.

The managed driver

The managed driver that allows the use of dual motor driver is represented by the TB6612FNG class that provides two contructors through which you can decide whether to use just one or both engines. For each engine is called a channel (A and B) characterized by:

  • 2 input signals through which you can set the four modes listed above (see table datasheet);
  • 1 PWM signal for speed variation;

The first two signals are represented by two instances of the OutputPort class for each channel and the last signal is represented by an instance of the PWM class.

The constructor makes provides the use of channel A only while the more complex prepares the object to the use of both motors (also recalling the simpler constructor).

   1: public TB6612FNG(Cpu.Pin aIn1, Cpu.Pin aIn2, Cpu.PWMChannel aPwmChannel,
   2:                  Cpu.Pin bIn1, Cpu.Pin bIn2, Cpu.PWMChannel bPwmChannel, 
   3:                  Cpu.Pin standby = Cpu.Pin.GPIO_NONE)
   4:     : this(aIn1, aIn2, aPwmChannel, standby)
   5: {
   6:     this.bIn1Port = new OutputPort(bIn1, false);
   7:     this.bIn2Port = new OutputPort(bIn2, false);
   8:  
   9:     this.bPwm = new PWM(bPwmChannel, DEFAULT_PWM_FREQUENCY, 0, false);
  10:     
  11:     this.IsMotorEnabledB = true;
  12:     this.MotorModeB = MotorMode.Stop;
  13:  
  14:     this.bPwm.Start();
  15: }

The application that uses the managed driver can interact with the dual motor driver through some properties (and not methods) to enable them, set the operating mode and speed. If we refer to the channel A we have respectively the following properties IsMotorEnabledA, MotorModeA and MotorSpeedA . Each of these properties acting on pins dedicated to OutputPort above (for the operation mode) and on the PWM pin (for speed, with a value from 0 to 100).

   1: /// <summary>
   2: /// Mode for Motor A
   3: /// </summary>
   4: public MotorMode MotorModeA
   5: {
   6:     get
   7:     {
   8:         if (this.IsMotorEnabledA)
   9:             return this.motorModeA;
  10:         else
  11:             throw new ApplicationException("Motor A is disabled !!");
  12:     }
  13:     set
  14:     {
  15:         switch (value)
  16:         {
  17:             case MotorMode.CCW:
  18:                 this.motorModeA = MotorMode.CCW;
  19:                 this.aIn1Port.Write(false);
  20:                 this.aIn2Port.Write(true);
  21:                 break;
  22:             case MotorMode.CW:
  23:                 this.motorModeA = MotorMode.CW;
  24:                 this.aIn1Port.Write(true);
  25:                 this.aIn2Port.Write(false);
  26:                 break;
  27:             case MotorMode.ShortBrake:
  28:                 this.motorModeA = MotorMode.ShortBrake;
  29:                 this.aIn1Port.Write(true);
  30:                 this.aIn2Port.Write(true);
  31:                 break;
  32:             case MotorMode.Stop:
  33:                 this.motorModeA = MotorMode.Stop;
  34:                 this.aIn1Port.Write(false);
  35:                 this.aIn2Port.Write(false);
  36:                 break;
  37:             default:
  38:                 break;
  39:         }
  40:     }
  41: }
  42:  
  43: /// <summary>
  44: /// Speed for Motor A
  45: /// </summary>
  46: public int MotorSpeedA
  47: {
  48:     get
  49:     {
  50:         if (this.IsMotorEnabledA)
  51:             return this.motorSpeedA;
  52:         else
  53:             throw new ApplicationException("Motor A is disabled !!");
  54:     }
  55:     set
  56:     {
  57:         if ((value >= 0) && (value <= 100))
  58:             this.motorSpeedA = value;
  59:         this.aPwm.DutyCycle = (double)this.motorSpeedA / 100;
  60:     }
  61: }

For example, in the case of a robot with two wheels and therefore two motors driven by this driver, we could develop a method that active both in the same direction to advance or retract the robot, or a method that active both in opposite directions to make it turn.

   1: // both motors in counter clockwise mode
   2: this.tb6612fng.MotorModeA = MotorMode.CCW;
   3: this.tb6612fng.MotorModeB = MotorMode.CCW;
   4:  
   5: //...
   6:  
   7: this.tb6612fng.MotorModeA = MotorMode.CW;
   8: this.tb6612fng.MotorModeB = MotorMode.CCW;
   9:  

The library is at version 2.2 and it is available on Nuget and on CodePlex. Unfortunately, the dual motor driver support is guaranteed for .NET Micro Framework 4.2 and above but the version 4.1 is excluded, because it is not available the PWM class.