upm  0.3.2
Sensor/Actuator repository for libmraa (v0.7.2)
Public Types | Public Member Functions

API for the Grove I2C Motor Driver. More...

Detailed Description

This class implements support for the Grove I2C Motor Driver. This device can support a single 4-wire stepper motor, or two 2-wire DC motors. The device contains an Atmel* ATmega8L microcontroller that manages an L298N H-bridge driver chip.

This device supports an I2C bus speed of 100Khz only.

The module does not provide any telemetry or status - it only accepts I2C commands for its various operations.

This module was tested with version 1.3 of the Grove I2C Motor Driver

grovemd.jpg
// Instantiate an I2C Grove Motor Driver on I2C bus 0
upm::GroveMD *motors = new upm::GroveMD(GROVEMD_I2C_BUS,
GROVEMD_DEFAULT_I2C_ADDR);
// set direction to CW and set speed to 50%
cout << "Spin M1 and M2 at half speed for 3 seconds" << endl;
motors->setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW);
motors->setMotorSpeeds(127, 127);
sleep(3);
// counter clockwise
cout << "Reversing M1 and M2 for 3 seconds" << endl;
motors->setMotorDirections(upm::GroveMD::DIR_CCW, upm::GroveMD::DIR_CCW);
sleep(3);

Public Types

enum  REG_T {
  SET_SPEED = 0x82, SET_PWM_FREQ = 0x84, SET_DIRECTION = 0xaa, SET_MOTOR_A = 0xa1,
  SET_MOTOR_B = 0xa5, STEPPER_ENABLE = 0x1a, STEPPER_DISABLE = 0x1b, STEPPER_NUM_STEPS = 0x1c
}
 
enum  STEP_DIRECTION_T { STEP_DIR_CCW = 0x0a, STEP_DIR_CW = 0x05 }
 
enum  DC_DIRECTION_T { DIR_CCW = 0x02, DIR_CW = 0x01 }
 

Public Member Functions

 GroveMD (int bus=GROVEMD_I2C_BUS, uint8_t address=GROVEMD_DEFAULT_I2C_ADDR)
 
 ~GroveMD ()
 
bool writePacket (REG_T reg, uint8_t data1, uint8_t data2)
 
bool setMotorSpeeds (uint8_t speedA, uint8_t speedB)
 
bool setPWMFrequencyPrescale (uint8_t freq=0x03)
 
bool setMotorDirections (DC_DIRECTION_T dirA, DC_DIRECTION_T dirB)
 
bool enableStepper (STEP_DIRECTION_T dir, uint8_t speed)
 
bool disableStepper ()
 
bool setStepperSteps (uint8_t steps)
 

Constructor & Destructor Documentation

GroveMD ( int  bus = GROVEMD_I2C_BUS,
uint8_t  address = GROVEMD_DEFAULT_I2C_ADDR 
)

GroveMD constructor

Parameters
busI2C bus to use
addressI2C address to use
~GroveMD ( )

GroveMD destructor

Member Function Documentation

bool writePacket ( REG_T  reg,
uint8_t  data1,
uint8_t  data2 
)

Composes and writes a 3-byte packet to the controller

Parameters
regRegister location
data1First byte of data
data2Second byte of data
Returns
True if successful
bool setMotorSpeeds ( uint8_t  speedA,
uint8_t  speedB 
)

To control DC motors, sets the speed of motors A & B. Valid values are 0-255.

Parameters
speedASpeed of motor A
speedBSpeed of motor B
Returns
True if successful
bool setPWMFrequencyPrescale ( uint8_t  freq = 0x03)

To control DC motors, sets the PWM frequency prescale factor. Note: this register is not ducumented other than to say the default value is 0x03. Presumably, this is the timer prescale factor used on the ATMega MCU timer driving the PWM.

Parameters
freqPWM prescale frequency; default is 0x03
Returns
True if successful
bool setMotorDirections ( DC_DIRECTION_T  dirA,
DC_DIRECTION_T  dirB 
)

To control DC motors, sets the directions of motors A & B

Parameters
dirADirection for motor A, DIR_CW or DIR_CCW
dirBDirection for motor B, DIR_CW or DIR_CCW
Returns
True if successful
bool enableStepper ( STEP_DIRECTION_T  dir,
uint8_t  speed 
)

To control a stepper motor, sets its direction and speed, and then enables it.

Parameters
dirDirection, STEP_DIR_CW or STEP_DIR_CCW
speedMotor speed. Valid range is 1-255, higher is slower.
Returns
True if successful
bool disableStepper ( )

To control a stepper motor, stops the stepper motor.

Returns
True if successful
bool setStepperSteps ( uint8_t  steps)

To control a stepper motor, specifies the number of steps to execute. Valid values are 1-255, 255 means continuous rotation.

Parameters
stepsNumber of steps to execute. 255 means continuous rotation.
Returns
True if successful

The documentation for this class was generated from the following files: