RoboClaw Packet Serial with the Pololu A-Star 32U4

Spread the love

The Pololu A-Star 32U4 controls a RoboClaw motor controller over packet serial using its Serial1 hardware serial port. Wire three jumpers between the boards, set the RoboClaw to packet serial mode in Motion Studio, and the Basicmicro Arduino library handles the communication with a short sketch.

Pololu A-Star 32U4 board wired to a RoboClaw motor controller with serial jumper wires
Figure 1: The serial port of the A-Star wired to the RoboClaw.

Many microcontrollers include hardware for serial communication, and the RoboClaw can be controlled directly over a serial connection. This guide covers using the Pololu A-Star 32U4 to control a RoboClaw motor controller in packet serial mode. Because the A-Star is Arduino compatible, the Basicmicro Arduino library (formerly the RoboClaw Arduino library) makes it easy to write programs that control the RoboClaw.

The procedure works with any RoboClaw model, and the same approach applies to other Arduino compatible boards that have a free hardware serial port.

What You Need

How Do You Set Up the A-Star and Arduino IDE?

  1. If this is the first time an A-Star will be used with a given computer, a small amount of one-time setup is required. The Arduino IDE needs Pololu’s board support package installed, and Windows users can optionally install Pololu’s drivers so the board appears with a proper name in the Device Manager. The “Getting started” section (section 6) of the Pololu A-Star 32U4 User’s Guide covers both steps.
  2. Next, install the Basicmicro Arduino library. In the Arduino IDE go to Sketch > Include Library > Manage Libraries, search for “Basicmicro” and click Install. See Using the RoboClaw Arduino Library for a complete walkthrough of the install.

How Is the RoboClaw Wired to the A-Star?

  1. Wire power and one or two motors to the RoboClaw before wiring the A-Star to it, and use Motion Studio to verify that the controller and motors work properly. The Dual Channel RoboClaw Quick Start Guide covers this process.
  2. Encoders are not required for this example, but if the motors in use have them, Pololu Encoder Wiring covers connecting them to the RoboClaw. Wiring the encoders now allows speed and position commands to be used later.
  3. Ensure that power to the RoboClaw and the A-Star is disconnected before making the serial connections.
  4. Wire the serial connection between the A-Star and the RoboClaw using three male to female jumper wires, as shown in Figure 1. The table below lists the connections to make.
    Function A-Star RoboClaw
    Receive Pin 0 S2 signal pin (pin closest to the edge of the board)
    Transmit Pin 1 S1 signal pin (pin closest to the edge of the board)
    Ground Any ground pin Any ground pin on the pin header
  5. Connect the RoboClaw to the computer with a USB cable and reconnect power to the RoboClaw.

How Is Packet Serial Mode Configured in Motion Studio?

  1. Open Basicmicro Motion Studio and connect the RoboClaw in use. Click on “General Settings” in the left-hand pane. In the area labeled “Setup”, set the Control Mode to Packet Serial. In the area labeled “Serial”, set the Packet Serial Address to 128 and the Baudrate to 38400.
    Motion Studio General Settings screen showing packet serial mode, address, and baudrate settings for the RoboClaw
    Figure 2: Setting the serial settings in Motion Studio.

How Do You Upload and Run the Example Sketch?

  1. Create a new sketch in the Arduino IDE and paste in the complete example below.
    // Include the Basicmicro Arduino library
    #include <Basicmicro.h>
    
    // Packet serial address of the RoboClaw, set in Motion Studio
    #define ADDRESS 128
    
    // Create the Basicmicro object using the A-Star's Serial1 hardware
    // serial port and a 10,000 microsecond communication timeout
    Basicmicro controller(&Serial1, 10000);
    
    void setup() {
      // Start serial communication with the RoboClaw at 38400 baud
      controller.begin(38400);
    }
    
    void loop() {
      // Run motor 1 forward at full power
      controller.DutyM1(ADDRESS, 32767);
      delay(2000);
    
      // Stop motor 1
      controller.DutyM1(ADDRESS, 0);
      delay(2000);
    }
  2. Connect the A-Star board to the computer with a USB cable. In the Arduino IDE, select the A-Star 32U4 under Tools > Board and select the board’s port under Tools > Port.
  3. Upload the sketch to the board. Once the upload completes the sketch begins running. If everything is working properly, the motor attached to channel 1 of the RoboClaw runs in the forward direction at full power for 2 seconds and then stops for 2 seconds before the process repeats.

Code Walkthrough

A walkthrough of the code shows how simple it is to control the RoboClaw from the A-Star.

The first line includes the Basicmicro Arduino library in the sketch.

#include <Basicmicro.h>

The next line creates the Basicmicro object. Two parameters are passed to the constructor. The first is a pointer to the A-Star’s Serial1 hardware serial object. The second is the communication timeout in microseconds, so a value of 10000 sets a 10 millisecond timeout.

Basicmicro controller(&Serial1, 10000);
The A-Star 32U4 uses the same ATmega32U4 microcontroller as the Arduino Leonardo. On these boards Serial is the USB connection to the computer and Serial1 is the hardware serial port on pins 0 and 1. The two are independent, so the USB cable can stay connected while the sketch uses hardware serial. Using Hardware Serial with the RoboClaw Arduino Library lists the hardware serial port names and pins for other Arduino models.

In the setup() function the begin() function of the Basicmicro object is called. The value passed to the function is the baudrate for serial communication and it must match the value set in Motion Studio. This call starts serial communication between the A-Star and the RoboClaw.

controller.begin(38400);

In the loop() function a motor command is carried out twice. The first call to DutyM1() runs motor 1 forward at full power and the second call stops it. The two values passed to the function are the packet serial address of the RoboClaw, defined as 128 to match the Motion Studio setting, and the duty cycle. The duty cycle ranges from -32767 for full power in reverse to 32767 for full power forward, and a value of 0 stops the motor. After each command, delay() pauses the program for two seconds.

controller.DutyM1(ADDRESS, 32767);
delay(2000);

controller.DutyM1(ADDRESS, 0);
delay(2000);

More commands that can be called on the Basicmicro object are covered in the function reference of Using the RoboClaw Arduino Library.

Next Steps

With the RoboClaw running from the A-Star, Using the RoboClaw Arduino Library covers the full set of library functions, and Using Encoders with the Arduino Library shows how to use encoder feedback for speed and position control. Using Hardware Serial with the RoboClaw Arduino Library explains hardware serial options on other Arduino boards.