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

API for the L298 Dual H-Bridge Motor Driver. More...

Detailed Description

It was developed using the RobotBase Dual H-Bridge module.

This module can support 2 DC motors, or one 2-phase stepper motor. It requires 3 pins per DC motor (or H-bridge), or 4 pins for the stepper motor (uses both H-bridges).

l298.jpg


L298 Dual H-Bridge Motor Driver image provided by SparkFun* under CC BY 2.0.

Example driving a DC motor

// Instantiate one of the 2 possible DC motors on a L298 Dual
// H-Bridge. For controlling a stepper motor, see the l298-stepper
// example.
upm::L298 l298(3, 4, 7);
cout << "Starting motor at 50% for 3 seconds..." << endl;
l298.setSpeed(50);
l298.setDirection(upm::L298::DIR_CW);
l298.enable(true);
upm_delay(3);
cout << "Reversing direction..." << endl;
l298.setDirection(upm::L298::DIR_NONE); // fast stop
l298.setDirection(upm::L298::DIR_CCW);
upm_delay(3);
l298.setSpeed(0);
l298.enable(false);

Example driving a stepper motor

// Instantiate a Stepper motor on a L298 Dual H-Bridge.
// This was tested with the NEMA-17 12V, 350mA, with 200 steps per rev.
upm::L298 l298(200, 3, 4, 7, 8, 9);
l298.setSpeed(10); // 10 RPMs
l298.setDirection(upm::L298::DIR_CW);
l298.enable(true);
cout << "Rotating 1 full revolution at 10 RPM speed." << endl;
// move 200 steps, a full rev
l298.stepperSteps(200);
cout << "Sleeping for 2 seconds..." << endl;
upm_delay(2);
cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." << endl;
l298.setDirection(upm::L298::DIR_CCW);
l298.stepperSteps(100);
// release
l298.enable(false);

Public Types

enum  L298_DIRECTION_T { DIR_NONE = 0x00, DIR_CW = 0x01, DIR_CCW = 0x02 }
 

Public Member Functions

 L298 (int pwm, int dir1, int dir2)
 
 L298 (int stepsPerRev, int en, int i1, int i2, int i3, int i4)
 
 ~L298 ()
 
uint32_t getMillis ()
 
void initClock ()
 
void setPeriodMS (int ms)
 
void enable (bool enable)
 
void setSpeed (int speed)
 
void setDirection (L298_DIRECTION_T dir)
 
void stepperSteps (unsigned int steps)
 

Member Enumeration Documentation

Enum to specify the direction of a motor

Constructor & Destructor Documentation

L298 ( int  pwm,
int  dir1,
int  dir2 
)

L298 constructor for DC motor(s) connected.

Parameters
pwmDigital pin to use for a DC motor - must be PWM-capable
dir1Digital pin to use for motor direction pin 1
dir2Digital pin to use for motor direction pin 2
L298 ( int  stepsPerRev,
int  en,
int  i1,
int  i2,
int  i3,
int  i4 
)

L298 constructor for a 4-wire stepper motor

Parameters
stepsPerRevNumber of steps per full revolution
enEnables the pin
i1Digital pin to use for stepper input 1
i2Digital pin to use for stepper input 2
i3Digital pin to use for stepper input 3
i4Digital pin to use for stepper input 4
~L298 ( )

L298 destructor

Member Function Documentation

uint32_t getMillis ( )

Returns the number of milliseconds elapsed since initClock() was last called.

Returns
Elapsed milliseconds
void initClock ( )

Resets the clock

void setPeriodMS ( int  ms)

Sets the period in milliseconds

Parameters
msPeriod in milliseconds
void enable ( bool  enable)

Enables the PWM output for a motor

Parameters
enableEnables the PWM output if true, disables if false
void setSpeed ( int  speed)

Sets the speed of a DC or stepper motor. For a DC motor, values can range from 0 (off) to 100 (full speed). For a stepper motor, specify the desired RPM.

Parameters
speedSpeed to set the motor to
void setDirection ( L298_DIRECTION_T  dir)

Sets the direction of the motor, clockwise or counterclockwise

Parameters
dirDirection to set the motor to
void stepperSteps ( unsigned int  steps)

Steps the stepper motor a specified number of steps

Parameters
stepsNumber of steps to move the stepper motor

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