upm  1.3.0
Sensor/Actuator repository for libmraa (v1.7.0)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Public Types | Public Member Functions
L298 Class Reference

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 Dual H-Bridge Motor Driver image provided by SparkFun* under CC BY-NC-SA-3.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 = new upm::L298(3, 4, 7);
cout << "Starting motor at 50% for 3 seconds..." << endl;
cout << "Reversing direction..." << endl;
l298->setDirection(upm::L298::DIR_NONE); // fast stop

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 = new upm::L298(200, 3, 4, 7, 8, 9);
l298->setSpeed(10); // 10 RPMs
cout << "Rotating 1 full revolution at 10 RPM speed." << endl;
// move 200 steps, a full rev
cout << "Sleeping for 2 seconds..." << endl;
cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed."
<< endl;
// release

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.

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

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.

Elapsed milliseconds
void initClock ( )

Resets the clock

void setPeriodMS ( int  ms)

Sets the period in milliseconds

msPeriod in milliseconds
void enable ( bool  enable)

Enables the PWM output for a motor

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.

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

Sets the direction of the motor, clockwise or counterclockwise

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

Steps the stepper motor a specified number of steps

stepsNumber of steps to move the stepper motor

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