Serial Controlled Motor Driver Hookup Guide

Pages
Contributors: MTaylor
Favorited Favorite 3

Arduino Library Reference

Example 2 and 3 use the Arduino IDE and a RedBoard to communicate with the SCMD. This section outlines how to get it and how the functions themselves operate.

Getting the Arduino Library

To get the Arduino library, download from Github, or use the Arduino Library Manager.

Download the Github repository

Visit the GitHub repository to download the most recent version of the library, or click the link below:

Use the library manager or install in the Arduino IDE

For help installing the library, check out our How To Install An Arduino Library tutorial.

If you don't end up using the manager, you'll need to move the SparkFun_Serial_Controlled_Motor_Driver_Arduino_Library folder into a libraries folder within your Arduino sketchbook.

Operating the Library

The library is made such that new motor driver object is constructed without parameters, the user populates the public settings structure, then calls .begin() to start the wire library and apply the communication settings.

Example:

language:c
SCMD myMotorDriver; //This creates an instance of SCMD which will be bound to a single controller.

void setup()
{
    myMotorDriver.settings.commInterface = I2C_MODE; //or SPI_MODE
    myMotorDriver.settings.I2CAddress = 0x5A;
    myMotorDriver.settings.chipSelectPin = 10;
    myMotorDriver.begin();
}

Settings

The main SCMD class has a public member which is named settings. To configure settings, use the format myMotorDriver.settings.I2CAddress = (...); then call .begin() to apply.

settings contains the following members:

  • uint8_t commInterface -- Set equal to I2C_MODE or SPI_MODE
  • uint8_t I2CAddress -- Set to address that controller is configured to in case of I2C usage
  • uint8_t chipSelectPin -- Set to chip select pin used on Arduino in case of SPI

Classes and Structures

There are a few classes used in the library. The main class is called SCMD, which is the object that talks to the motor drivers. There are also a couple structs in use -- SCMDSettings and SCMDDiagnostics. A SCMDSettings object named settings is present within the SCMD class for configuration.

SCMD

SCMD is used to declare a single chain of motor drivers at specified port and modes in settings. The contained functions are described in a later section.

language:c
class SCMD
{
public:
    //settings
    SCMDSettings settings;
    SCMD( void );

    uint8_t begin( void );
    ... (Other functions...)

    uint16_t i2cFaults; //Location to hold i2c faults for alternate
    driver
};

SCMD Settings

SCMDSettings is an type for the settings member of SCMD. It is declared public to be configured by the user.

language:c
struct SCMDSettings
{
public:
    //Main Interface and mode settings
    uint8_t commInterface;
    uint8_t I2CAddress;
    uint8_t chipSelectPin;
};

SCMD Diagnostics

SCMDDiagnostics contains a bunch of 8 bit values of data for use with getDiagnostics and getRemoteDiagnostics. Declared objects are passed as a reference to the diagnostic function and written by the collected data.

language:c
struct SCMDDiagnostics
{
public:
    //Attainable metrics from SCMD
    uint8_t numberOfSlaves = 0;
    uint8_t U_I2C_RD_ERR = 0;
    uint8_t U_I2C_WR_ERR = 0;
    uint8_t U_BUF_DUMPED = 0;
    uint8_t E_I2C_RD_ERR = 0;
    uint8_t E_I2C_WR_ERR = 0;
    uint8_t LOOP_TIME = 0;
    uint8_t SLV_POLL_CNT = 0;
    uint8_t MST_E_ERR = 0;
    uint8_t MST_E_STATUS = 0;
    uint8_t FSAFE_FAULTS = 0;
    uint8_t REG_OOR_CNT = 0;
    uint8_t REG_RO_WRITE_CNT = 0;
};

Functions

uint8_t begin( void );

Call after providing settings to start the wire library, apply the settings, and get the ID word (return value should be 0xA9). Don't progress unless this returns 0xA9!

bool ready( void );

This function checks to see if the SCMD is done booting and is ready to receive commands. Use this after .begin(), and don't progress to your main program until this returns true.

bool busy( void );

This function checks to see if the SCMD busy with an operation. Wait for busy to be clear before sending each configuration commands (not needed for motor drive levels).

void enable( void );

Call after .begin(); to allow PWM signals into the H-bridges. If any outputs are connected as bridged, configure the driver to be bridged before calling .enable();. This prevents the bridges from shorting out each other before configuration.

void disable( void );

Call to remove drive from the H-bridges. All outputs will go low.

void reset( void );

This resets the I2C hardware for Teensy 3 devices using the alternate library, and nothing otherwise.

void setDrive( uint8_t channel, uint8_t direction, uint8_t level );

This sets an output to drive at a level and direction.

  • channel: Motor number, 0 through 33.
  • direction: 1 or 0 for forward or backwards.
  • level: 0 to 255 for drive strength.

void inversionMode( uint8_t motorNum, uint8_t polarity );

This switches the perceived direction of a particular motor.

  • motorNum: Motor number, 0 through 33.
  • polarity: 0 for normal and 1 for inverted direction.

void bridgingMode( uint8_t driverNum, uint8_t bridged );

This connects any board's outputs together controlling both from what was the 'A' position.

  • driverNum: Number of connected SCMD, 0 (controller) to 16.
  • bridged: 0 for normal and 1 for bridged.

void getDiagnostics( SCMDDiagnostics &diagObjectReference );

This returns a diagnostic report from the controller.

  • &diagObjectReference: Pass a local SCMDDiagnostics object that will be written into.

void getRemoteDiagnostics( uint8_t address, SCMDDiagnostics &diagObjectReference );

This returns a diagnostic report from a peripheral.

  • address: address of intended peripheral. This starts at 0x50 for the first peripheral and goes up from there.
  • &diagObjectReference: Pass a local SCMDDiagnostics object that will be written into.

void resetDiagnosticCounts( void );

Clears the diagnostic counts of a controller.

void resetRemoteDiagnosticCounts( uint8_t address );

Clears the diagnostic counts of a peripheral.

  • address: address of intended peripheral. This starts at 0x50 for the first peripheral and goes up from there.

uint8_t readRegister(uint8_t offset);

Returns the contents of a memory location of the controller.

  • offset: Memory address to read.

void writeRegister(uint8_t offset, uint8_t dataToWrite);

Writes data to a memory location of the controller.

  • offset: Memory address to write.
  • dataToWrite: Data to write to that address.

uint8_t readRemoteRegister(uint8_t address, uint8_t offset);

Returns the contents of a memory location of a peripheral.

  • address: address of intended peripheral. This starts at 0x50 for the first peripheral and goes up from there.
  • offset: Memory address to read.

void writeRemoteRegister(uint8_t address, uint8_t offset, uint8_t dataToWrite);

Writes data to a memory location of a peripheral.

  • address: address of intended peripheral. This starts at 0x50 for the first peripheral and goes up from there.
  • offset: Memory address to write.
  • dataToWrite: Data to write to that address.