5.b. Serial Command Protocols
The TReX serial command protocol is fairly straightforward. Communication is achieved by sending command packets consisting of a single command byte followed by any data bytes that command requires. Command bytes always have their most significant bits set (i.e. range from 128 – 255) while data bytes always have their most significant bits cleared (i.e. range from 0 – 127). This means that each data byte can only transmit seven bits of information.
One significant improvement over other Pololu serial controllers is the TReX’s ability to gracefully handle serial errors (such as bad commands, incorrectly formatted commands, or even hardware-level serial errors). The TReX has a configuration parameter that, if set, will shut down the motors if a serial error occurs, but the TReX itself will continue running and accepting commands. This is a safety precaution taken in case the serial error occurred during a command that was intended to stop the motors.
The TReX will respond to two sub-protocols:
This is the simpler and more compact of the two protocols; it is the protocol you should use if your TReX is the only device connected to your serial line. The TReX Compact protocol command packet is simply:
command byte (with MSB set), any necessary data bytes
For example, if we want to set the auxiliary motor (command 0xF0) to full speed (127) using the Compact protocol, we would send the following byte sequence:
in hex: 0xF0, 0x7F
in decimal: 240, 127
This protocol is compatible with the serial protocol used by our other serial motor and servo controllers. As such, you can daisy-chain a TReX on a single serial line along with our other serial controllers (including additional TReXes) and, using this protocol, send commands specifically to the desired TReX without confusing the other devices on the line.
The Pololu protocol is to transmit 128 (0x80 in hex) as the first (command) byte, followed by a device-number data byte. The default device number for the TReX is 7, but this is a configuration parameter you can change. Any Pololu device on the line whose device number matches the specified device number will accept the command that follows; all other Pololu devices will ignore the command. The remaining bytes in the command packet are the same as the Compact protocol command packet you would send, with one key difference: the Compact protocol command byte is now a data byte for the command 128 and hence must have its most significant bit cleared. Therefore, the command packet is:
0x80, device # byte, command byte with MSB cleared, any necessary data bytes
For example, if we want to set the auxiliary motor (command 0xF0) to full speed (127) for a TReX with device number 7, we would send the following byte sequence:
in hex: 0x80, 0x07, 0x70, 0x7F
in decimal: 128, 7, 112, 127
Note that 0x70 is the command 0xF0 with its most significant bit cleared.
The TReX will respond to both the Pololu and Compact protocols on the fly; you do not need to use a jumper or configuration parameter to identify which protocol you will be using.