Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Raspberry PI and TReX DMC01 with RC and serial


#1

Hello,
First of all I want to state that I am rather new to robotics but I am eager to learn.
My plan is to build a robot truck that can perform various things. More features will be added to it, the more I learn. The plan, as I said is to build a remote controlled robot truck that is able to explore an area and map it using a GPS.

I will list the parts that I already own:

  • chassis - Wild Thumper 4WD
  • motor controller - TReX DMC01
  • raspberry pi 3 B+
  • FlySky FS-i6 transmitter with FS-iA6B reciever
  • 1 LiPo 2S 5000MAh

The main issue right now is controlling the robot.

  1. For starters, I sorta managed to get the thing up and running via RC and just the Trex. I connected the RC receiver to channels 1 and 2 and left the mix mode jumper on.
    On the RC reciever it’s connected to channels 1 and 3, because this way it allows me to control the robot with both sticks of the transmitter (LS - throttle with forward and reverse and RS - left and right). Now I am uncertain if this is a “healthy” way of controlling the robot. My main concern is that, let’s say I set the throttle to 50% forward, if I want to turn using the RS but moving the sticks to maximum, the speed of the motors will be overwritten for the motors on one side, and the motors on the other side will be spinning the other way.

  2. I want to be able to control the robot via the RC, but when I flip a switch on the controller, it should go in to autonomous mode and be controlled by the Raspberry Pi… a feature like “return home” or go into “exploration mode” and avoid obstacles with the help of proximity sensors. Also, If I flip back the switch to be able to control it via RC.

  3. Keeping the “requirements” from point 2, I was wondering if it would be easier to do this with a smartphone acting as a RC controller?

I have read 2 topics on the forum, but I did not understand most of it :slight_smile:



#2

Hello.

It sounds like you are controlling your motors using both joysticks on your RC transmitter. Mix mode is typically used to allow a single stick to control both motors, with one axis being the throttle and the other axis being the steering, but I do not see any problem with using separate sticks. For your example, setting the throttle to 50% and the steering to 100% should result in one motor running forward at 100% (throttle + steering) and the other motor running in reverse at -50% (throttle - steering). If you want to better understand how mixing mode works on the TReX, you might consider reading the “Driving a Motor” subsection under the “Connecting an RC Receiver” section in the Simple Motor Controller User’s Guide. The Simple Motor Controller User’s Guide is newer, so it does a better job at explaining how mixing mode works than the TReX user’s guide.

As for your second question, you could use the serial override feature on channel 5 of the TReX board, as mentioned in the other topics you linked. You can find more information on that feature in the TReX User’s Guide under the “RC/Analog in Detail” section.

Regarding your last question, using a smartphone might require a very different setup. You might find it helpful to look at this example project, which shows how to setup a web interface to control a robot with our A-Star Robot Controllers with Raspberry Pi Bridge. Unfortunately, the A* Robot Controllers would be underpowered for the Wild Thumper chassis’s motors, so you can try adapting that approach for the TReX motor controller.

- Amanda


#3

Ok, thanks for the info. I have made some progress and managed to connect the Trex to the Pi via serial TTL and managed a few basic commands…tho, there are some issues.

I looked up the Trex commands - https://www.pololu.com/file/0J1/TReX_Commands_v1.2.pdf but I don’t understand some of the stuff there and some of them might be wrong?

For starters, I managed to write a script for forward and reverse movement.

import serial
import time

ser = serial.Serial('/dev/ttyS0', 19200, timeout = 1)

def forward(speed):
	ser.write(chr(0xCA))		#right motors forward
	ser.write(chr(speed))
	ser.write(chr(0xC1))		#left motors forward
	ser.write(chr(speed))

def reverse(speed):
	ser.write(chr(0xC9))		#right motors reverse
	ser.write(chr(speed))
	ser.write(chr(0xC2))		#left motors reverse
	ser.write(chr(speed))

forward(40)
time.sleep(3)
forward(0)

#time.sleep(1)
ser.read()

#reverse(30)
#time.sleep(2)
#reverse(0)

From my tests…the motor command bytes are something like this:
0xC1 Left motors forward
0xC5 Left motors forward

0xC2 Left motors reverse
0xC6 Left motors reverse

0xCA Right motors forward
0xCE Right motors forward

0xC9 Right motors reverse
0xCD Right motors reverse

I know…baby, steps…but I am learning.
Tho, most of the other commands I don’t seem to be able to understand.

Next part is to learn how to read data from the motor controller. Any hints?


#4

Why do you think some of the commands are wrong and which ones are you referring to? From your code and the command bytes you used in your test, I suspect that your left motor is connected differently than your right motor. Connecting power to the motor in one orientation makes the motor spin one way (e.g. clockwise), and reversing the power makes the motor spin the other way (e.g. counter-clockwise).

As for reading data from the TReX, in your code, it looks like you are trying to read form the TReX after setting both motors’ speed and direction. The TReX command documentation specifically states that the set motor 1 and 2 command returns nothing, which is why ser.read() is not working. The data-querying commands on the first page of the command documentation you posted are the only ones that return a value.

- Amanda


#5

Hello @ithanium2! I also have problem with TReX DMC01 and Raspberry Pi 3 B+.
Can you share you configuration, photos or instruction for connecting them if possible?


#6

@AmandaS As you may see it will be grate and simplify usage of yours wonderful products if Pololu additionally add code examples and wiring scheme’s for popular platform like Raspberry Pi or Arduino.


#7

Well, there isn’t really much that I can share to be honest.
The motor commands that seem to work for me are the one posted above. I still have no clue how to read the data from the motor controller and what some commands do and how they work (Set motor or Accelerate motor).

As for the setup itself…at the moment I ditched the RC part and I am controlling the robot with smartphone via bluetooth, using bluedot - http://bluedot.readthedocs.io/en/latest/gettingstarted.html
For me, bluetooth control is just a “temporary fix”…it’s not an option because I also need video streaming. It’s just a learning step, so I can figure out how to implement a solution for controlling the robot via the internet (WiFi but prefferably a mobile broadband connection).

Also, what part of connecting them are you struggling with?


#8

@ithanium2 Hmm. I have pretty mach same problem in general. My vision of the solution is to use WebTRC to streaming video and controlling devise. For RPi there are almost ready library UV4L. Or similar WebRTC articles like this. But use WebRTC may be much harder problem then I thinking now, so be careful with it.

I face wiring and configuration problem (I think so). I try connect to TReX using RxTxGND pins on DMC01 and GND,GPIO14,GPIO15 on RPi side.

TReX_DMC01_com pi3b%20_gpio_uart

But something goes wrong. When I run with /dev/ttyS0 I receive an error. When use /dev/ttyAMA0 no response from DMC01.
Can you share your RPi configuration and how you connect RPi to DMC01?


#9

Wow…you seem to be way more advanced then me :slight_smile:
I did no special configuration on the Pi. Just make sure your wires and jumpers are properly set.
Also…I am afraid you are using the wrong serial pins on the TReX…you need to use the TTL.

Your wiring should be something like this:
Trex Pi
GND ---------------------------------------> GND
SO ---------------------------------------> GPIO15 (RX)
SI ---------------------------------------> GPIO14 (TX)

Also you need to remove the jumper from the RC/Analog Mode pins…with no jumper on those 3 pins, the TReX will be set to work in Serial mode. You will still be able to receive RC signals, but if I understood correctly you will need to process them. Something like this:
RC module —> Trex ----> Pi —> Trex ----> motors.

Hope this helps.


#10

@ithanium2 Thank you wary mach! You wiring schema works from the first run and save me tons of time!
I only have time to run small test for setting speed from your example and it works. :+1:
Next tests need to be done on whole command list to see have interaction between RPi and TReX can be organized in more effective way.

I plan my device will be mostly indoor solution and will use wi-fi to connect to the Internet.
To control it from smartphone or other PC-like device it need somehow be discoverable in the Internet.


#11

@Morpheus I’m glad you fixed your issue; thanks for letting us know. Please note that the Raspberry Pi’s GPIO pins are not 5V tolerant, so you will need to use a bidirectional logic level shifter (like this one) to interface with the Raspberry Pi.

We try to provide all of the necessary information to use the TReX controller, including basic wiring diagrams and serial commands, in the TReX’s user’s guide, which I strongly suggest you read thoroughly before continuing with your project.

@ithanium2
Are you using a logic level shifter between your TReX and Raspberry Pi?

It sounds like you did not read my post explaining that those commands do not return data. Please see the data-querying commands in the TReX’s command documentation guide, which is linked under the “Serial Command List and Documentation” section in the user’s guide. If you are still confused, can you elaborate what serial command(s) you are having trouble understanding?

- Amanda


#12

@Morpheus glad that I could help. Keep us posted and perhaps post some updates when you are making progress with your robot.

@AmandaS thank you for your response.

First of…no I am not using a logic level shifter…why should I? On the documentation page where I read how to connect to the controller with the Pi, there is no mention that I should use one - https://www.pololu.com/docs/0J1/3.b

As for the serial…I have read your post and looked at the commands…but I am not sure how to make them work…it’s abit confusing for me.
Let’s say I would want to get the motor currents…the code would be?

ser.write(chr(0x8F))     #0x8F: Get Motor Currents command
print(ser.read())

Of course…This would work only in Python2…but that’s another story (I figured out the code for Python3 aswell and I will post it here soon).

Now…there are still some things that I do not understand on the serial communication page - https://www.pololu.com/docs/0J1/5

You can use the serial interface for four general purposes: querying the TReX for information (any mode), setting its configuration parameters (serial mode only), sending it motor commands (any mode), and upgrading its firmware (must be in firmware-upgrade mode). Firmware upgrading is addressed in Section 6.

Serial motor commands are accepted in any mode, but they will only immediately affect the motors if the TReX is in serial mode or channel 5 has serial override active. When the TReX is in RC or analog mode and serial override is not active, the most recently received serial motor command for each motor is buffered. These buffered motor commands take effect if serial override becomes active.

I don’t understand what serial override is and how do I enable it.


#13

The Raspberry Pi operates at 3.3V, so to use it safely with the TReX, which uses 5V logic, you would need to use something like a logic-level shifter. The Raspberry Pi GPIO pins are not 5V tolerant and connecting them to a voltage higher than 3.3V will likely damage the board, which might be the reason why you are having trouble reading from the TReX. You could try doing a simple loop-back test by connecting the Raspberry Pi’s RX and TX pins together, opening a serial terminal and typing characters into it to see if the characters are echoed back, to verify that the RX pin is still working.

Your code looks fine for reading the motor current from the TReX, but could you try adding a 10 millisecond delay after calling ser.write and before calling ser.read to see if that helps?

The serial override feature allows you to enable or disable serial control and is controlled by channel 5 on the TReX. You can find out more in the TReX’s user’s guide under the “Channel Functions” sections.

- Amanda