upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Member Functions | Protected Attributes

API for the I2C Motor Driver. More...

Detailed Description

This class implements support for the 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 I2C Motor Driver.

For stepper operation, this driver can run in one of two modes - Mode 1, where this driver handles the stepping operation, and Mode 2, where this driver simply sends commands to the Motor Driver, and it handles the stepping operation. Mode2 requires updated (and working) firmware to be loaded onto the device.

The default stepper operation mode is Mode1, which is generally more flexible and is supported on all firmware revisions.

md.jpg

An example showing the use of a DC motor

// Instantiate an I2C Motor Driver on I2C bus 0
upm::MD motors(MD_I2C_BUS, MD_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(MD_DIR_CW, MD_DIR_CW);
motors.setMotorSpeeds(127, 127);
upm_delay(3);
// counter clockwise
cout << "Reversing M1 and M2 for 3 seconds" << endl;
motors.setMotorDirections(MD_DIR_CCW, MD_DIR_CCW);
upm_delay(3);

An example showing the use of a 4-wire stepper

// Instantiate an I2C Motor Driver on I2C bus 0
upm::MD motors(MD_I2C_BUS, MD_DEFAULT_I2C_ADDR);
// This example demonstrates using the MD to drive a stepper motor
// configure it, for this example, we'll assume 200 steps per rev
motors.configStepper(200);
// set for half a rotation
motors.setStepperSteps(100);
// let it go - clockwise rotation, 10 RPM speed
motors.enableStepper(MD_STEP_DIR_CW, 10);
upm_delay(3);
// Now do it backwards...
motors.setStepperSteps(100);
motors.enableStepper(MD_STEP_DIR_CCW, 10);
// now disable
motors.disableStepper();

Public Member Functions

 MD (int bus=MD_I2C_BUS, uint8_t address=MD_DEFAULT_I2C_ADDR)
 
 ~MD ()
 
bool writePacket (MD_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 (MD_DC_DIRECTION_T dirA, MD_DC_DIRECTION_T dirB)
 
bool enableStepper (MD_STEP_DIRECTION_T dir, uint8_t speed)
 
bool disableStepper ()
 
bool setStepperSteps (unsigned int steps)
 
void configStepper (unsigned int stepsPerRev, MD_STEP_MODE_T mode=MD_STEP_MODE1)
 

Protected Attributes

md_context m_md
 

Constructor & Destructor Documentation

MD ( int  bus = MD_I2C_BUS,
uint8_t  address = MD_DEFAULT_I2C_ADDR 
)

MD constructor

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

MD destructor

Here is the call graph for this function:

Member Function Documentation

bool writePacket ( MD_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

Here is the call graph for this function:

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

Here is the call graph for this function:

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

Here is the call graph for this function:

bool setMotorDirections ( MD_DC_DIRECTION_T  dirA,
MD_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

Here is the call graph for this function:

bool enableStepper ( MD_STEP_DIRECTION_T  dir,
uint8_t  speed 
)

To control a stepper motor, sets its direction and speed, and then starts operation. For Mode2, this method will return immediately. For Mode1 (the default) this method returns when the number of steps specified by setStepperSteps() has completed.

Parameters
dirDirection, STEP_DIR_CW or STEP_DIR_CCW
speedMotor speed. Valid range is 1-255. For Mode 1 (default), this specifies the speed in RPM's. For Mode 2, speed is multiplied by 4ms by the board, so higher numbers will mean a slower speed.
Returns
True if successful

Here is the call graph for this function:

bool disableStepper ( )

To control a stepper motor, stops the stepper motor.

Returns
True if successful

Here is the call graph for this function:

bool setStepperSteps ( unsigned int  steps)

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

For Mode1 (the default) steps can be any positive integer.

Parameters
stepsNumber of steps to execute. 255 (only in Mode2) means continuous rotation.
Returns
True if successful

Here is the call graph for this function:

void configStepper ( unsigned int  stepsPerRev,
MD_STEP_MODE_T  mode = MD_STEP_MODE1 
)

Configure the initial Stepper parameters. This should be called before any other stepper method.

Parameters
stepsPerRevThe number of steps required to complete one full revolution.
modeThe stepper operating mode, default STEP_MODE1
Returns
Elapsed milliseconds

Here is the call graph for this function:

Collaboration diagram for MD:
Collaboration graph
[legend]

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