Sony Arouje

a programmer's log

Posts Tagged ‘pi tracker

Pi Tracker–Code running in Raspberry Pi

with one comment

In my last post you can see the Tracker in action. This post I will explain the code running in Raspberry pi that controls the Tracker. I use Wiring Pi to control the  GPIO instead of the default RPI.GPIO. The intention of using Wiring pi is because Software PWM is easy to implement. Any way after the first run of the tracker I realized that my tracker is slow moving one and no need to control the speed, so I haven’t used Software PWM here.

Installing Wiring Pi for python

Command  prompt type the below commands

  1. sudo apt-get install python-dev python-pip
  2. sudo pip install wiringpi2

Let’s check whether wiringPi installed correctly

  1. sudo python
  2. import wiringpi2
  3. wiringpi2.piBoardRev()

if it returns a value then wiring Pi installed successfully.

Pi Tracker php code running in Raspberry pi

Motor.py

import wiringpi2 as io
from time import sleep
class Motor:
        def __init__(self, gpio_a, gpio_b):
                self.Gpio_a=gpio_a
                self.Gpio_b=gpio_b
                self.activeGpio=gpio_a
        def setup(self):
                io.wiringPiSetupGpio()
                io.pinMode(self.Gpio_a,1)
                io.pinMode(self.Gpio_b,1)
        def forward(self):
                self.stop()
                self.activeGpio=self.Gpio_a
        def reverse(self):
                self.stop()
                self.activeGpio=self.Gpio_b
    
        def acclerate(self,speed):
                io.digitalWrite(self.activeGpio,1)
                #speed param is not used here.
        def stop(self):
                io.digitalWrite(self.Gpio_a,0)
                io.digitalWrite(self.Gpio_b,0)

Engine.py

from Motor import Motor
class Engine:
        motor_a=Motor(17,27)
        motor_b=Motor(23,24)
        def __init__(self):
                Engine.motor_a.setup()
                Engine.motor_b.setup()
        def steer(self, command):
                direction,speed=command.split(",")
                if(direction=="CLOSE"):
                        self.stop()
                elif (direction=="U"):
                        self.forward()
                elif (direction=="L"):
                        self.left()
                elif(direction=="R"):
                        self.right()
                else:
                        self.reverse()
        def forward(self):
                Engine.motor_a.forward()
                Engine.motor_b.forward()
                self.acclerate()
        def reverse(self):
                Engine.motor_a.reverse()
                Engine.motor_b.reverse()
                self.acclerate()
        def right(self):
                Engine.motor_b.reverse()
                Engine.motor_a.forward()
                self.acclerate()
        def left(self):
                Engine.motor_a.reverse()
                Engine.motor_b.forward()
                self.acclerate()
        def acclerate(self):
                Engine.motor_a.acclerate(10)
                Engine.motor_b.acclerate(10)
        def stop(self):
                Engine.motor_a.stop()
                Engine.motor_b.stop()
                print "engine stopped"

trackerRemoteListener.py

import socket
from Engine import Engine
TCP_IP = '10.0.0.10'
TCP_PORT =5005
BUFFER_SIZE = 100
engine= Engine()
engine.stop()
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
while True:
        try:
                data = conn.recv(BUFFER_SIZE)
                print "received data: ",data
                engine.steer(data)
                if not data:
                        engine.stop()
                        conn,addr=s.accept()
        except KeyboardInterrupt:
                conn.close()
                engine.stop()
                print "connection closing"
        except Exception as err:
                conn.close()
                s.listen(1)
                conn,addr=s.accept()
                #s.listen(1)
                print "error occured"
                pass
conn.close()

How it works

Motor.py class represent a motor in the Tracker. It accepts two parameters and it should be a valid GPIO pin to which the motor is connected. For e.g Motor_a is connected to GPIO 17 and 27. If we give write 1 to 17 and 0 to 27 then Motor_a will run forward, write 1 to 27 and 0 to 17 Motor_a run reverse.

Pi Tracker uses two motors to drive, Engine.py controls these two motors. As you can see in the code Engine.py creates two Motor instance. It also has functions to steer the tracker in different directions.

trackerRemoteListener.py is the socket program that listens to the command from external device to control the tracker. To control this tracker I used an Android tab that sends the command to this listener.

Hope the code is self explanatory, if not post your questions in the comment section.

 

Happy coding…

Written by Sony Arouje

June 24, 2014 at 11:55 am

Posted in Raspberry Pi

Tagged with , , ,

%d bloggers like this: