Chapter 7 – RaPi car control and programing:

This chapter is about the programing of the RaPi car Python control script. The first objective is to control the RaPi car with a keyboard over the WIFI connection with Putty. I also explain the basics how to switch the GPIO pins from the Raspberry Pi. To control the in / out pins is the basis to control the car. Besides the control over the keyboard and the collection of first experiences how the control works best would be the control of the car with a Web-Interface. The Web-Interface will be explained in a further chapter.

I will extend the HowTo papers with additional chapters about the controlling of the RaPi car in the future.

Check site conditions:

I assume the software is installed on the Windows PC as well on the Raspberry Pi. I also assume that everything works fine and no problems or questions are open. This HowTo paper is written for the Raspberry Pi B version. Once again here the list as URL “Raspberry Pi WIFI radio controlled rc vehicle – software installation” of the software modules we need to program the first Python script.

software required:

  1. Raspberry Pi update (Raspberry Pi)
  2. WinSCP (Windows computer)
  3. Samba installation / configuration (HowTo paper: Samba HowTo)
  4. Putty installation for the backend access on the RaPi car (Windows computer)
  5. GPIO Library installation (Raspberry Pi)
  6. W-Lan installation / configuration (HowTo paper: W-Lan HowTo) (Raspberry Pi)
  7. Notepad++ installation on the Windows computer (Windows Rechner)

As a little reminder the picture shows how I wired the GPIO pins with the L298 H-Bridge. This is important to remember for the developing of the Python control script.

Raspberry PI - remote controlled car with a Raspberry Pi GPIO pin settings

Raspberry PI – remote controlled car with a Raspberry Pi GPIO pin settings

Step 1 – Python script creation:

I created a folder “rapi_car” via the samba share on my Raspberry Pi. I will create the Python script in this folder. You can create the script with the editor Notepad++ and give the script a name like “rapi_car.py”. Please use the file extension *.py for the Python script.

Raspberry PI - remote controlled car with a Raspberry Pi samba share

Raspberry PI – remote controlled car with a Raspberry Pi samba share

Attention:

You have to check the rights of the new script file. It is important that you can execute the file with the user you are logged in.

Step 2 – Python script programing:

If you like to use my Python script to start with the programing you can download it here. I will correct extend the script from time to time and update the script here.

Download link: Cardboard Car – Software download page

I would be happy if you have new ideas or suggestions for improvement. Please write me an e-mail or leave a comment at the end of the page. Then we can discuss the idea or improvement online.

E-Mail: RapiCar@raspberry-pi-car.com

This is a video from my Raspberry Pi RC car in action.

 

You will find the latest version of all my programs on my Software download page.

Download link: Cardboard Car – Software download page

 

# author Ingmar Stapel

# version 0.4 BETA

# date 20140802 04:36 AM

import RPi.GPIO as io

io.setmode(io.BCM)

import sys, tty, termios, time

import os

# set variables Motor speed.

motorLspeed = 0

motorRspeed = 0

maxspeed = 40

minspeed = 0

acceleration = 0

speedstep = 5

# set variable actual direction

direction = “”

# set variable for turning on spot

spot = “false”

# Here we configure the PWM settings for

# the four DC motors. It defines the two GPIO

# pins used for the input on the L298 H-Bridge,

# starts the PWM and sets the

# motors’ speed initial to 0

motor1_in1_pin = 27

motor1_in2_pin = 22

io.setup(motor1_in1_pin, io.OUT)

io.setup(motor1_in2_pin, io.OUT)

motor2_in1_pin = 24

motor2_in2_pin = 25

io.setup(motor2_in1_pin, io.OUT)

io.setup(motor2_in2_pin, io.OUT)

# set PWM for motor1 to 0

motorpwm1_in1_pin = 4

motorpwm1_in2_pin = 17

io.setup(motorpwm1_in1_pin, io.OUT)

io.setup(motorpwm1_in2_pin, io.OUT)

motorpwm1 = io.PWM(4,100)

motorpwm1 = io.PWM(17,100)

motorpwm1.start(0)

motorpwm1.ChangeDutyCycle(0)

# set PWM for motor2 to 0

motorpwm2_in1_pin = 18

motorpwm2_in2_pin = 23

io.setup(motorpwm2_in1_pin, io.OUT)

io.setup(motorpwm2_in2_pin, io.OUT)

motorpwm2 = io.PWM(18,100)

motorpwm2 = io.PWM(23,100)

motorpwm2.start(0)

motorpwm2.ChangeDutyCycle(0)

# The catch method can determine which key has been pressed

# by the user on the keyboard.

def getch():

fd = sys.stdin.fileno()

old_settings = termios.tcgetattr(fd)

try:

tty.setraw(sys.stdin.fileno())

ch = sys.stdin.read(1)

finally:

termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

return ch

# Here we define the methods used to determine

# whether a motor needs to spin forward or backwards.

# both pins match, the motor will not turn.

def reverse():

io.output(motor1_in1_pin, True)

io.output(motor1_in2_pin, False)

io.output(motor2_in1_pin, False)

io.output(motor2_in2_pin, True)

def forward():

io.output(motor1_in1_pin, False)

io.output(motor1_in2_pin, True)

io.output(motor2_in1_pin, True)

io.output(motor2_in2_pin, False)

# stop the motors

def stop():

io.output(motor1_in1_pin, False)

io.output(motor1_in2_pin, False)

io.output(motor2_in1_pin, False)

io.output(motor2_in2_pin, False)

motorLspeed = 0

motorRspeed = 0

acceleration = 0

# This functions sets the motor speed.

def setmotorspeed(motorLspeed, motorRspeed, acceleration):

if(acceleration > 0):

forward()

if(acceleration > maxspeed):

acceleration = maxspeed

motorLspeed = acceleration

motorRspeed = acceleration

elif(acceleration == 0):

motorLspeed = acceleration

motorRspeed = acceleration

stop()

elif(acceleration < (maxspeed *-1)):

acceleration = (maxspeed * -1)

else:

reverse()

motorLspeed = (acceleration * -1)

motorRspeed = (acceleration * -1)

motorLspeed, motorRspeed = check_motorpseed(motorLspeed, motorRspeed)

return motorLspeed, motorRspeed, acceleration

# This function is used for left steering

def left(motorLspeed, motorRspeed):

if(motorRspeed < motorLspeed):

motorRspeed = motorRspeed + speedstep

else:

motorLspeed = motorLspeed – speedstep

motorLspeed, motorRspeed = check_motorpseed(motorLspeed, motorRspeed)

return motorLspeed, motorRspeed

#This function is used for right steering

def right(motorLspeed, motorRspeed):

if(motorLspeed < motorRspeed):

motorLspeed = motorLspeed + speedstep

else:

motorRspeed = motorRspeed – speedstep

motorLspeed, motorRspeed = check_motorpseed(motorLspeed, motorRspeed)

return motorLspeed, motorRspeed

# check the motorspeed if it is correct and in max/min range

def check_motorpseed(motorLspeed, motorRspeed):

if (motorLspeed < minspeed): motorLspeed = minspeed if (motorLspeed > maxspeed):

motorLspeed = maxspeed

if (motorRspeed < minspeed): motorRspeed = minspeed if (motorRspeed > maxspeed):

motorRspeed = maxspeed

return motorLspeed, motorRspeed

# Setting the PWM pins to false so the motors will not move

# until the user presses the first key

io.output(motor1_in1_pin, False)

io.output(motor1_in2_pin, False)

io.output(motor2_in1_pin, False)

io.output(motor2_in2_pin, False)

# Instructions for when the user has an interface

print(“w/s: direction”)

print(“a/d: steering”)

print(“q: stops the motors”)

print(“x: exit”)

# Infinite loop

# The loop will not end until the user presses the

# exit key ‘X’ or the program crashes…

while True:

# Keyboard character retrieval method. This method will save

# the pressed key into the variable char

char = getch()

print motorLspeed, motorRspeed, acceleration

# The car will drive forward when the “w” key is pressed

if(char == “w”):

acceleration = acceleration + speedstep

motorLspeed, motorRspeed, acceleration = setmotorspeed(motorLspeed, motorRspeed, acceleration)

motorpwm1.ChangeDutyCycle(motorLspeed)

motorpwm2.ChangeDutyCycle(motorRspeed)

# The car will reverse when the “s” key is pressed

if(char == “s”):

acceleration = acceleration – speedstep

motorLspeed, motorRspeed, acceleration = setmotorspeed(motorLspeed, motorRspeed, acceleration)

motorpwm1.ChangeDutyCycle(motorLspeed)

motorpwm2.ChangeDutyCycle(motorRspeed)

# Stop the motors

if(char == “q”):

motorLspeed = 0

motorRspeed = 0

stop()

# The “d” key will toggle the steering right

if(char == “d”):

motorLspeed, motorRspeed = right(motorLspeed, motorRspeed)

motorpwm1.ChangeDutyCycle(motorLspeed)

motorpwm2.ChangeDutyCycle(motorRspeed)

# The “a” key will toggle the steering left

if(char == “a”):

motorLspeed, motorRspeed = left(motorLspeed, motorRspeed)

motorpwm1.ChangeDutyCycle(motorLspeed)

motorpwm2.ChangeDutyCycle(motorRspeed)

# The “x” key will break the loop and exit the program

if(char == “x”):

print(“Program Ended”)

break

# The keyboard character variable char has to be set blank. We need

# to set it blank to save the next key pressed by the user

char = “”

# Program will clean up all GPIO settings and terminates

io.cleanup()

# End

 

Now you have to login via putty on your RaPi Car and go into the folder where you have created the Python control script.

With the command “python rapi_car.py” you start the script. The output should look like something similar to my screenshot.

Raspberry PI - remote controlled car with a Raspberry Pi PuTTY

Raspberry PI – remote controlled car with a Raspberry Pi PuTTY

Conclusion:

With this little script it is possible to control the RaPi car over your WIFI connection. Of course you have to adapt this script for your belongings. With a next step I try to enhance the control of the RaPi car wiht a HTML surface.

Contents:


Chapter 1: Raspberry Pi WIFI radio controlled rc vehicle – introduction
Chapter 2: Raspberry Pi WIFI radio controlled rc vehicle – component list
Chapter 3: Raspberry Pi WIFI radio controlled rc vehicle – chassis
Chapter 4: Raspberry Pi WIFI radio controlled rc vehicle – wiring
Chapter 5: Raspberry Pi WIFI radio controlled rc vehicle – software installation
Chapter 6: Raspberry Pi WIFI radio controlled rc vehicle – power supply
Chapter 7: Raspberry Pi WIFI radio controlled rc vehicle – programing
Chapter 8: Raspberry Pi WIFI radio controlled rc vehicle – live video streaming
Chapter 9: Raspberry Pi WIFI radio controlled rc vehicle – web-interface and smartphone
Chapter 10: Raspberry Pi WIFI radio controlled rc vehicle – cooling with passive heat sinks
Chapter 11: Raspberry Pi WIFI radio controlled rc vehicle – startup scripts
Chapter 12: Raspberry Pi WIFI radio controlled rc vehicle – cardboard car model
Chapter 13: Raspberry Pi WIFI radio controlled rc vehicle – power consumption
(Visited 3,490 times, 8 visits today)