Aula Jazmati
Published © MIT

Implementation of mobile robot based on Raspberry Pi -Part 1

The goal of this project is to help you begin programming with Python GUI to control your robots with a Raspberry PI.

AdvancedWork in progress3 hours330
Implementation of mobile robot based on Raspberry Pi -Part 1

Things used in this project

Story

Read more

Schematics

Schematics and circuit diagrams

Code

ROBOT Control code (Python GUI and ROS msg)

Python
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 25 09:14:58 2019

@author: Aula
"""

import rospy
from std_msgs.msg import String
import RPi.GPIO as GPIO
from Tkinter import *
 
window = Tk()
 
window.title("Control GUI")
lbl = Label(window, text="ROBOT Control GUI", font=("Arial Bold", 14),bg="white")
 
lbl.grid(column=5, row=0)
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
pinMotorAL1Forwards = 7
pinMotorAL1Backwards = 8
pinMotorBR1Forwards = 13
pinMotorBR1Backwards = 15
pinMotorAL2Forwards = 40
pinMotorAL2Backwards = 10
pinMotorBR2Forwards = 11
pinMotorBR2Backwards = 12
Frequency = 50
DutyCycle = 50
Stop = 0
GPIO.setup(7, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
pwm1 = GPIO.PWM(pinMotorAL1Forwards, Frequency)
pwm2 = GPIO.PWM(pinMotorAL1Backwards, Frequency)
pwm3 = GPIO.PWM(pinMotorBR1Forwards, Frequency)
pwm4 = GPIO.PWM(pinMotorBR1Backwards, Frequency)
pwm5 = GPIO.PWM(pinMotorAL2Forwards, Frequency)
pwm6 = GPIO.PWM(pinMotorAL2Backwards, Frequency)
pwm7 = GPIO.PWM(pinMotorBR2Forwards, Frequency)
pwm8 = GPIO.PWM(pinMotorBR2Backwards, Frequency) 
pwm1.start(Stop)
pwm2.start(Stop)
pwm3.start(Stop)
pwm4.start(Stop)
pwm5.start(Stop)
pwm6.start(Stop)
pwm7.start(Stop)
pwm8.start(Stop)
def StopMotors():
    print('Stopping')
    pwm1.ChangeDutyCycle(Stop)
    pwm2.ChangeDutyCycle(Stop)
    pwm3.ChangeDutyCycle(Stop)
    pwm4.ChangeDutyCycle(Stop)
    pwm5.ChangeDutyCycle(Stop)
    pwm6.ChangeDutyCycle(Stop)
    pwm7.ChangeDutyCycle(Stop)
    pwm8.ChangeDutyCycle(Stop)
def forwards():
    print('Moving forwards')
    pwm1.ChangeDutyCycle(DutyCycle)
    pwm2.ChangeDutyCycle(Stop)
    pwm3.ChangeDutyCycle(DutyCycle)
    pwm4.ChangeDutyCycle(Stop)
    pwm5.ChangeDutyCycle(DutyCycle)
    pwm6.ChangeDutyCycle(Stop)
    pwm7.ChangeDutyCycle(DutyCycle)
    pwm8.ChangeDutyCycle(Stop)
def backwards():
    print('Moving backwards')
    pwm1.ChangeDutyCycle(Stop)
    pwm2.ChangeDutyCycle(DutyCycle)
    pwm3.ChangeDutyCycle(Stop)
    pwm4.ChangeDutyCycle(DutyCycle)
    pwm5.ChangeDutyCycle(Stop)
    pwm6.ChangeDutyCycle(DutyCycle)
    pwm7.ChangeDutyCycle(Stop)
    pwm8.ChangeDutyCycle(DutyCycle)
def left():
    print('Turning left')
    pwm1.ChangeDutyCycle(20)
    pwm2.ChangeDutyCycle(0)
    pwm3.ChangeDutyCycle(70)
    pwm4.ChangeDutyCycle(0)
    pwm5.ChangeDutyCycle(70)
    pwm6.ChangeDutyCycle(0)
    pwm7.ChangeDutyCycle(20)
    pwm8.ChangeDutyCycle(0)
def right():
    print('Turning right')
    pwm1.ChangeDutyCycle(70)
    pwm2.ChangeDutyCycle(0)
    pwm3.ChangeDutyCycle(20)
    pwm4.ChangeDutyCycle(0)
    pwm5.ChangeDutyCycle(20)
    pwm6.ChangeDutyCycle(0)
    pwm7.ChangeDutyCycle(70)
    pwm8.ChangeDutyCycle(0)
btn1 = Button(window, text="FORWARDS",font=("Arial Bold", 14),bg="red", command= forwards)
 
btn1.grid(column=2, row=2)

btn2 = Button(window, text="LEFT",font=("Arial Bold", 14),bg="yellow", command= left)
 
btn2.grid(column=4, row=4)

btn3 = Button(window, text="RIGHT",font=("Arial Bold", 14),bg="cyan",command= right)
 
btn3.grid(column=6, row=6)

btn4 = Button(window, text="BACKWARDS",font=("Arial Bold", 14),bg="light green",command= backwards)
 
btn4.grid(column=8, row=8)

btn5 = Button(window, text="STOP",font=("Arial Bold", 14),fg="white",bg="blue", command= StopMotors)
 
btn5.grid(column=5, row=10)

window.mainloop()
def CommandCallback(commandMessage):
    command = commandMessage.data
    if command == 'forwards':
        forwards()
    elif command == 'backwards':
        backwards()
    elif command == 'left':
        left()
    elif command == 'right':
        right()
    elif command == 'StopMotors':
        StopMotors()
    else:
        print('Unknown command, stopping instead')
        StopMotors()

rospy.init_node('driver')

rospy.Subscriber('command', String, CommandCallback)

rospy.spin()
print('Shutting down: stopping motors')
StopMotors()

ROBOT Control GUI Test

Python
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 24 19:54:51 2019

@author: Aula-Jazmati
"""

from tkinter import *
 
window = Tk()
 
window.title("Control GUI")
lbl = Label(window, text="ROBOT Control GUI", font=("Arial Bold", 14),bg="white")
 
lbl.grid(column=5, row=0)

def forwards():
 
    print("forwards")
    
def backwards():
 
    print("Backwards")
def left():
 
    print("Left")
    
def right():
 
    print("Right")   
def stop():
 
    print("Stop")
    
btn1 = Button(window, text="FORWARDS",font=("Arial Bold", 14),bg="red", command= forwards)
 
btn1.grid(column=2, row=2)

btn2 = Button(window, text="LEFT",font=("Arial Bold", 14),bg="yellow", command= left)
 
btn2.grid(column=4, row=4)

btn3 = Button(window, text="RIGHT",font=("Arial Bold", 14),bg="cyan",command= right)
 
btn3.grid(column=6, row=6)

btn4 = Button(window, text="BACKWARDS",font=("Arial Bold", 14),bg="light green",command= backwards)
 
btn4.grid(column=8, row=8)

btn5 = Button(window, text="STOP",font=("Arial Bold", 14),fg="white",bg="blue", command= stop)
 
btn5.grid(column=5, row=10)

window.mainloop()

Credits

Aula Jazmati

Aula Jazmati

31 projects • 127 followers
💡

Comments