Aegidius
 Plüss   Aplulogo
   
serverstatus
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2018, V10.0
 
  TCPCom Module
 
TCP With The Pi2Go Robot
 

 

Pi2Go, a Low-Cost Moving Robot

It is great fun to teach robotics in the classroom or learn the basics of robotics at home. It is strongly recommended to use a moving robot, because it is the archetype of many robot applications in the daily life, like self-driving cars, mars mobiles, home cleaning robots, etc. People take as an evidence that robots contain some kind of a computer that provides the "intelligence" of the system. And this computer has to be programmed to achieve this goal, so computer programming and algorithms is considered as the central part of a robot system.

In contrast to other teaching robotic systems, like the Lego NXT or EV3, the Pi2Go robot from 4tronix and TTS distributed world-wide by many local robot shops is a prebuild system with 2 motors and several typical sensors, button(s) and lamps (Leds). Because in contrast to the Lego system, the Raspberry PI (RPi) and not a custom microcontroller system is used, it can easily be expanded via the I2C/SPI bus and the RPi camera port. Moreover, the RPi runs a fully accessible Linux system with a multitude of libraries and applications.

In the previous site you learned how to access the RPi Linux system, so have a look there. For programming Python and Java with the Pi2Go, a special SD card is available at the RaspiBrick site. It contains a preconfigured Linux with many additional Pi2Go oriented libraries and scripts that simplify the programming, especially when the TigerJython IDE is used.

Robot-to-Robot Communication Over TCP/IP

In the previous session you already learned how to use TCP/IP for the communication between the RPi and a program running on a PC. This practice helps you to develop applications to control the Pi2Go remotely. Because the RaspiBrick distribution contains a pre-built TCP socket server (like the EV3 BrickGate), the remote control will not be covered here.

The focus in this chapter is the communication between two robots. As you imagine, there are many interesting applications where several robots exchange data and information, in other words "talk to each other". As demonstration we show a circular course with a pass-by. If you imagine that the robots represents trains, the pass-by could be at the railway station, where one of the trains remains stopped until the other train arrives.

To detect the railway station two flexible strips of white paper wrapped into a transparency cross the track and trigger the front infrared sensor. The left and right line infrared sensors are used to detect the edge of the track. A very simple roadside follower algorithm is implemented as follows: When the white color of the background is detected, the robot moves on a arc back to the black track.

Python server:

from raspibrick import *
from tcpcom import TCPServer

def onStateChanged(state, msg):
    global isWaiting, start
    if state == TCPServer.LISTENING:
        ledLeft.setColor("red")
        display.showText("stop")
        gear.stop()
        isWaiting = True
    if state == TCPServer.CONNECTED:
        ledLeft.setColor("green")
    if state == TCPServer.MESSAGE:
        isWaiting = False
        start = time.time()
        display.showText("run")

robot = Robot()
display = Display()
ledLeft = Led(LED_LEFT)
ledLeft.setColor("red")
gear = Gear()
gear.setSpeed(25)
irLeft = InfraredSensor(IR_LINE_LEFT)
irRight = InfraredSensor(IR_LINE_RIGHT)
irCenter = InfraredSensor(IR_CENTER)
r = 0.1
port = 5000
server = TCPServer(port, stateChanged = onStateChanged)
start = time.time()
isWaiting = True
while not isEscapeHit():
    if isWaiting:
        continue
    vL = irLeft.getValue()
    vR = irRight.getValue()
    vC = irCenter.getValue()
    if vL == 1 and vR == 0:
        gear.forward()
    elif vL == 0 and vR == 0:   
        gear.leftArc(r)
    elif vL == 1 and vR == 1:
        gear.rightArc(r)
    if vC == 1 and time.time() - start > 5:
        server.sendMessage("go")
        display.showText("stop")
        gear.stop()
        isWaiting = True
                
server.terminate()        
robot.exit()
      

For the client, most of the code remains the same. A common library module could be used to avoid code duplication.

Python client:

from raspibrick import *
from tcpcom import TCPClient
import time

def onStateChanged(state, msg):
    global isWaiting, start
    if state == TCPClient.CONNECTED:
        ledLeft.setColor("green")
    if state == TCPClient.DISCONNECTED:
        ledLeft.setColor("red")
        gear.stop()
    if state == TCPClient.MESSAGE:
        isWaiting = False
        start = time.time()
        display.showText("run")



robot = Robot()
display = Display()
ledLeft = Led(LED_LEFT)
ledLeft.setColor("red")
gear = Gear()
gear.setSpeed(25)
irLeft = InfraredSensor(IR_LINE_LEFT)
irRight = InfraredSensor(IR_LINE_RIGHT)
irCenter = InfraredSensor(IR_CENTER)
r = 0.1
host = "192.168.0.4"
port = 5000
client = TCPClient(host, port, stateChanged = onStateChanged)
rc = client.connect()
isWaiting = False
display.showText("try")
if rc:
    display.showText("run")
    start = time.time()
    while not isEscapeHit():
        if isWaiting:
            continue
        vL = irLeft.getValue()
        vR = irRight.getValue()
        vC = irCenter.getValue()
        if vL == 1 and vR == 0:
            gear.forward()
        elif vL == 0 and vR == 0:   
            gear.leftArc(r)
        elif vL == 1 and vR == 1:
            gear.rightArc(r)
        if vC == 1 and time.time() - start > 5:
            client.sendMessage("go")
            display.showText("stop")
            gear.stop()
            isWaiting = True
    client.disconnect()                
robot.exit()

 

tcpcom-raspi