Receiving feedback from Trex Jr. on Arduino

I’ve been having difficulty figuring out how to query the Trex Jr. for data. I have read the command documentation and the users manual but it’s still a seems a little vague to me.

I used the sample code from the Pololu Simple Motor Controller as an example. However, the difference between that controller and Trex is that for the Simple Controller you send a variable ID which gets printed on the serial monitor and represents the data you are trying to receive. For the trex Jr, there is no variable ID to return.

#include <NewSoftSerial.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

#define QUERY 1
 
// Create an instance of the software serial
// communication object. This represents the
// interface with the TReX Jr device
NewSoftSerial pololu = NewSoftSerial(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);
}
 
  
 //function 
  int readByte()  
  { 
  unsigned long time = millis(); 
  while (!pololu.available()) 
  { 
    if (millis() - time > 500) 
      return -1; 
  } 
  return pololu.read(); 
}  

 unsigned int getVariable(unsigned char variableID) 
{ 
  pololu.print(0x83, BYTE); 
  pololu.print(variableID, BYTE); 
  return readByte();
}  


  
void loop()
{
  
   Serial.println((int)getVariable(QUERY));

  }

I am trying to send the command to return whether serial is in control or not, but it is returning -1 which means that the controller is not outputting any bytes when I send the command byte. I’m not sure how to modify my code for it to return a byte which the serial monitor can read (a 0 or 1 in this case).

Any help would be appreciated.

Hello.

Can you describe how you are connecting your Arduino to the TReX Jr? Did you make sure to put it into serial mode (by removing the mode jumper) and cycle power?

Do you have access to a USB-to-serial adapter? If so, it would be helpful if you could try to communicate with it using the TReX Configurator utility or even the Pololu Serial Transmitter utility. This would help make it clear if the problem is with your code or your connections.

- Ben

I removed all the jumpers except for the BEC jumper. I’m fairly confident my connection is correct because the trex is able to receive commands such as forward, reverse, etc, and the arduino correctly transmits the speed settings to the controller.

Unfortunately, I don’t have a USB-to-serial connection on hand, so I can’t connect the controller to the computer in that way. I suspect that the problem is with the code, I’m not quite sure how to get the serial monitor to display the results of the data query commands I send to the trex. I tried creating a variable and having it store it’s output to the variable such as: Serial.println((int)getVariable(something)); but it returns -1 meaning there is no data to output from the controller.

It is very helpful to know that your connections and code are at least good enough to get a response out of the TReX. However, there could still be a problem with the connections involved in receiving a response from the TReX. Just to rule this out, can you describe your connections to me? Do you have access to an oscilloscope?

- Ben

Thanks for helping me out. My connections are pretty straightforward. I attached the TTL Serial in of the TReX to pin 4 of the arduino (TX via software serial), and serial out to the RX pin 5 of the arduino. The arduino itself is simply connected to comp via the cable. Given my connections are right, I am wondering, is there a byte I send in order to get the trex to begin receiving commands followed by the particular command or just the command itself? For instance to get motor current would I just send the byte 0x8D and wait for a response?

Do your Arduino and TReX Jr have a common ground?

In serial mode, the TReX Jr is constantly waiting for serial commands. Sending the byte 0x8D should be sufficient to get the TReX Jr to return the current. Do you have access to a USB-to-TTL-serial adapter?

- Ben