I recently purchased a TReX Dual controller and I spent this evening writing some code to figure out how to interact with it via serial from my Arduino Uno. I figured that the example might be useful to others. It just sends commands to the TReX to check signature and modes. It can be used as a springboard to other commands.
enjoy,
-Mark
/*
This is code the demonstrates interaction with the
TReX Dual Motor Controller from Pololu:
https://www.pololu.com/product/777
Command Reference:
https://www.pololu.com/file/0J1/TReX_Commands_v1.2.pdf
The circuit setup that this code has been successfully tested
with is:
Pololu G -> Arduino GND
Pololu S0 -> Arduino Pin 5 (configured as Serial RX)
Pololu S1 -> Arduino Pin 4 (configured as Serial TX)
Separate power is given to the Arduino (via USB cable) and
the TReX controller (via the motor VIN and GND connectors).
All jumpers were removed.
This code is free, use it however you want, but no guarantees
or warranties are given or implied. Uti periculo tuo.
*/
#include <Stream.h>
// 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 TREX_TX_PIN 4
#define TREX_RX_PIN 5
// Create an instance of the software serial
// communication object. This represents the
// interface with the TReX device
SoftwareSerial trex(TREX_RX_PIN, TREX_TX_PIN);
// Main application entry point
void setup() {
// Define the appropriate input/output pins
pinMode(TREX_RX_PIN, INPUT);
pinMode(TREX_TX_PIN, OUTPUT);
// Setup serial to use for debug messages
Serial.begin(9600);
// Setup serial for communicating with the trex interface
trex.begin(19200);
Serial.println("Setup complete.");
}
void loop() {
getSignature();
getMode();
getSerialIsMaster();
delay(3000);
}
void getSignature() {
Serial.println("Getting signature...");
// We expect 7 characters back and we add a null to terminate the string
char signature[8] = "xxxxxxx";
uint8_t index = 0;
trex.write(0x81);
delay(10);
while (trex.available() && index < 8) {
signature[index++] = trex.read();
}
Serial.println(signature);
}
void getMode() {
Serial.println("Getting mode...");
char mode;
trex.write(0x82);
delay(10);
while (trex.available()) {
mode = trex.read();
}
Serial.println(mode);
}
void getSerialIsMaster() {
Serial.println("Getting serial is master...");
boolean isMaster;
trex.write(0x83);
delay(10);
while (trex.available()) {
isMaster = trex.read();
}
Serial.println(isMaster);
}