Hardware Reference
In-Depth Information
void loop()
{
}
Listing 12-7 uses the struct from the header file in the setup portion of the main sketch. Without using libraries
to hold these values, you must prototype them manually in the main sketch, which makes the code less portable to
other projects. Using libraries unlocks the real power of C/C++, where function definitions, function parameters and
return types can conform to the rules that were defined in your library.
Making a Motor Library
Robots help us get the most out of our movement code. We may have many robots based on the same motor
driver chips, so most motor movement can be done from a generic motor library that targets a set of common pin
compatible motor control chips. For a more in-depth look, we will create a motor library initially based on the L293D
chip. In some cases, like Texas Instruments SN754410, they are pin compatible and need to be modified. However,
if a different pin layout were used for a new shield, then the pins would have to be redefined . This project is a based
on the IEEE Rutgers motor controller shield, https://github.com/erosen/Line-Following-Robot , with two 5-volt
motors. The goal is to convert it to a library that conforms to the Arduino and can be easily distributed for anyone
using either chip. In this example, we will create a motor object using the basic C++ features of a class with both
private and public methods.
A motor controller needs three types of defined pins: motor direction, the pulse width modulation (PWM), and
motor enable. These pins enable the motor behavior—for example: on or off and spin forward or backward. The
motor will spin at a particular rate controlled by voltage approximated by the PWM pins. Since these pins can change
from board to board, we need a way to set a default set of pins and then an override so that custom pins can be used.
For instance, software PWM could be used instead of the physical PWM pins that are indicated by the Arduino type.
The example code we are using in Figure 12-8 already supports many helpful features. You can also write directly
to the pins to make the motors move. To enable a motor, set the direction and move it to write:
digitalWrite(motor1Enable, HIGH);
digitalWrite(motor1Dir, HIGH);
analogWrite(motor1PWM, 128);
Given these basic features, we can control the motor for forward, back, left, right, stop, and various speeds. In a
standard Arduino sketch you would end up cutting and pasting them repeatedly, which is not very sustainable. The
next step is to create some useful functions that help to avoid cutting and pasting, so we do not end up with code
that is difficult to read. The final step is to create a library that organizes these functions so that you can use them in
multiple robot or motor projects. The starting sketch is shown in Listing 12-8.
Listing 12-8. Initial motor controller code
#define motor1Dir 7
#define motor2Dir 8
#define motor1PWM 9
#define motor2PWM 10
#define motor1Enable 11
#define motor2Enable 12
void initMotorDriver()
{
pinMode(motor1Dir, OUTPUT);
pinMode(motor2Dir, OUTPUT);
 
Search WWH ::




Custom Search