Pololu TReX Jr Motor Controller

modified on 6 April 2010 at 16:21 - 595 views

From RoboWiki

Jump to: navigation, search

The Pololu TReX Jr Motor Controller is a powerful multi-functional motor controller ideal for table-top robotics. Though it is larger than the dual serial motor controller from Pololu, it is much more sturdy and can drive a range of powerful DC motors. The club supports this device as our official motor controller.

It is important to note that not all jumpers are needed on the device, and that several may be removed without issues. Please refer to the official user's guide for which jumpers may be removed.

The Pololu TReX Jr Motor Controller

Contents

[hide]

[edit] Communication Protocol

The TReX Jr motor controller can be controlled via three different means: standard radio-control pulses, variable analog voltage, and by serial communication. This tutorial focuses on serial (RS-232) communication, since serial communication is quick and easy to do with an Arduino. You can, using the right cable and drivers, directly communicate between a computer and TReX Jr, though this is well out of this article's scope.

[edit] Wiring to the Arduino

Wiring the TReX Jr to the Arduino is quick and simple! To the left is an image of the input/output connections of the motor controller. The only needed cables are the input/output wires used for the serial communications, as well as a tied ground. Select two pins on your Arduino as the serial communication pairs. Then, wire these two cables from those two pins to the appropriate SO and SI TTL pins. Then, a common ground must be wired, such that the serial ground on the motor controller is wired to the ground on the Arduino.

The Pololu TReX Jr Motor Controller input/output connection pins. Click for a larger image!

[edit] Jump Pins

Jumpers are the little blue components that jumps (connects) two pins, which are common for configuring hardware. Based on certain jumper locations for the TReX Jr Motor Controller, you can set it to receive RC analog inputs, or different types of serial inputs. For our usage, in which we only connect this device to the Arduino, we have the following settings:

The Pololu TReX Jr Motor Controller pin setup. Remove the red and green jumpers.

Using the image to the right, remove the red jumper (which is used for RC control), then remove the green jumper (which sets RC control voltage properties), and the last blue jumper can be left alone or removed. If there are any jumpers on the COM, G, or TTL pins (which are the column of 5 pins), remove them.

[edit] Controller LEDs

The device contains several LEDs that can help you debug and track the current state of the controller. There are eight total LEDs. For each motor there is a red and green pair that show the current speed and direction of the motor. The bright blue LED, located towards a corner of the device, indicates that the device is communicating correctly with whichever interface is being used. The red and green LED pairs found between the two center capacitors are the communication status lights. The green LED flickers for each communication, while the red shows the current communication state.

Much more information about the LEDs can be found on the official user's guide. This document also contains debugging information.

[edit] Motor Power

This controller supports between 5 to 24 V. Most club DC motors work on 7.2 volts, and should thus be the target use of this controller. The internal on-board logic runs at 5V, though does not to be supplied (though the grounds between the controller and the Arduino should be tied).

[edit] Controller Communication

Though there are several methods of communication with this motor controller, the easiest is by serial communication via the compact pololu protocol. The compact pololu protocol is based on a series of instructions that are single byte values that are sent via TTL. Some instructions may be given parameters, such as motor index or target speed, which are sent as a second byte. A full list of commands can be found here or within the official user's guide. Note that the default communication speed for this device is set at 19200 baud (bits per second), which the Arduino supports.

A second protocol, the standard "pololu" protocol, may be used, but is much more complicated and requires multi-byte message lengths. Read more about these communication methods in the official user's guide.

[edit] Sample Arduino Code

The following code is an example that can be loaded onto any Arduino that has a connection to the TReX Jr motor controller. The default communication pins are that of 4 (serial out) and 5 (serial in). The core of the example is found within the SetSpeed(...) function, which sets the target motor both speed and direction.

/*******************************************\
 Pololu TReX Jr Motor Controller code
 
 Control a Pololu TReX Jr motor controller
 using Pololu's "compact" protocol via RS-232
 communication lines. Note that the protocol
 requires us to wait for the echo, though this
 is not done in this code for simplicities sake.
 
 Jeremy Bridon
 jgbridon@gmail.com
\********************************************/
 
// Included for serial communication
#include <SoftwareSerial.h>
 
// Define pins you're using for serial communication
// Do not use pins 0 or 1 as they are reserved for
// standard I/O and programming
#define TXPIN 4
#define RXPIN 5
 
// Create an instance of the software serial
// communication object. This represents the
// interface with the TReX Jr device
SoftwareSerial pololu(RXPIN, TXPIN);
 
// Main application entry point 
void setup()
{
  // Define the appropriate input/output pins
  pinMode(RXPIN, INPUT);
  pinMode(TXPIN, OUTPUT);
 
  // Begin communicating with the pololu interface
  Serial.begin(9600);
  pololu.begin(19200);
}
 
// Main application loop
void loop()
{
  // Loop through 127 to 0, forward
  for(int i = 127; i >= 0; i--)
  {
    // Say that we are setting our speed
    Serial.print("Setting speed to: ");
    Serial.println(i, DEC);
 
    // Set speed to motor 0 and forward
    SetSpeed(0, true, i);
    delay(100);
  }
 
  // Loop through 0 to 127, backward
  for(int i = 0; i < 128; i++)
  {
    // Say that we are setting our speed
    Serial.print("Setting speed to: ");
    Serial.println(i, DEC);
 
    // Set speed to motor 0 and forward
    SetSpeed(0, true, i);
    delay(100);
  }
}
 
// Set the motor index, direction, and speed
// Motor index should either be a 0 or 1
// Direction should be either true for forward or false for backwards
// Speed should range between 0 and 127 (inclusivly)
void SetSpeed(int MotorIndex, boolean Forward, int Speed)
{
  // Validate motor index
  if(MotorIndex < 0 || MotorIndex > 1)
    return;
 
  // Validate speed
  if(Speed < 0)
    Speed = 0;
  else if(Speed > 127)
    Speed = 127;
 
  // Send the "set" command based on the motor
  // Note that we do not accelerate to the
  // speed, we just instantly set it
  unsigned char SendByte = 0;
  if(MotorIndex == 0)
    SendByte = 0xC2;
  else if(MotorIndex == 1)
    SendByte = 0xCA;
 
  // If we go backwards, the commands are the same
  // but minus one
  if(!Forward)
    SendByte--;
 
  // Send the set speed command byte
  pololu.print(SendByte, BYTE);
 
  // Send the speed data byte
  pololu.print(Speed, BYTE);
}

[edit] External References