Introduction to Python, constants and print statements – Driving Lesson #2

  • Pre-driving checks

If you are here you should have set up your Raspberry Pi,  understand how to connect your Pi-Car to your Raspberry Pi and know how to use IDLE or another editor to write Python code and run it. If not don’t worry:

For more information on setting up your Raspberry Pi click here.

For more information on how to connect your Pi-Car to your Raspberry Pi click here.

For more information on how to start-up and use the IDLE3 editor to write and run Python code click here.

We would also recommend that you have completed the Pi-Cars Driving Lesson #1 where you do a quick test with Pi-Car to check everything is working and you can see how to actually write a Python program and then run it.

  • What’s in this driving lesson?

By now you will have written a few lines of code and run it to make the wheels move on the Pi-Car but you may not have understood much. This lesson introduces a bit more about what a program is and the concept of constants.

  • A little theory

Much like real driving, we do need some theory in order to understand how to drive a Pi-Car and get the most out of it. I am sure lots of you will just skip this section and get onto the actual driving part, however, do come back and read on if you are at all unsure as you do need to understand the concepts here in order to be able to program.

Some of the concepts in programming and you might not understand how important they are but keep with it, as you start using them you will see why they are important.

What is a program?

A program is a set of instructions to the Raspberry Pi to get it to do something. The program must be written in a language – a language which has certain rules as to how it is written. The language we are going to use is called Python.

All we need to know at the moment is that the Raspberry Pi understands Python. This is just the same as you or I can understand English. So if somebody gives us some instructions in English we can carry them out. Likewise with the Raspberry Pi if it is given  some instructions in Python it can understand them and carry them out. This happens when the program is run.

It is also important to realise that once the program has been run it will try and carry out every instruction, one line after another in the order you had written them down in the program. It will only stop when it has no more instructions to carry out.

Much like speaking a language, when you write a programming language there are certain building blocks that you use which let you do powerful things with a program and also help you organise it. The first of these building blocks we will introduce are called constants.

Constants and how they can help

As you may have seen from the previous bit of code that got the Pi-Car going, it was actually quite difficult to understand. The first thing that can help us with this are called constants.

The best way of explaining a constant is by adding them to the code we created in driving lesson #1.

  • Lets get driving…

Adding constants

Here is the code created in the first driving lesson:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(12,GPIO.OUT)

GPIO.output(12,1)

time.sleep(3)

GPIO.output(12,0)

Chances are you probably cannot remember what all of this means and what it makes the Pi-Car do, so lets add some constants to make it easier to understand. Change your code so it looks like below:

import time, RPi.GPIO as GPIO

ON = 1
OFF = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(12,GPIO.OUT)

GPIO.output(12,ON)

time.sleep(3)

GPIO.output(12,OFF)

With the changes you made above it is a good idea to run the program again to check that you have it right. As before, if connected to the Pi-Car it should move forwards, wait for 3 seconds and the stop. If you cannot remember how you actually write Python code on the Raspberry Pi and then run it click here.

Lets have a look at what we added in the program above. Adding the lines:

ON = 1
OFF = 0

means that you are instructing the program to create a constant called ON and another constant called OFF. The ‘=’ sign is used to give each constant a value. In programming you have to have it this way round with the name on the left hand-side followed by the ‘=’ symbol and and the value you want to set it to on the right hand-side.

Once your constants are created you can then use them later in the same program:

GPIO.output(12,ON)

time.sleep(3)

GPIO.output(12,OFF)

This makes it much easier to understand, you can quickly see that you are turning GPIO pin 12 on and off (which in turn moves the Pi-Car forwards and then stops it moving forwards).

Adding more constants

It may not appear that important to be able to do this at the moment but when your programs get bigger it is a great help to be able to understand what is happening. We can see this in action by adding more instructions to the program to move the Pi-Car in other directions. We do this by turning on and off other GPIO pins.

In a Pi-Car the way the GPIO pins result in a particular command to the Pi-Car is as follows:

  • GPIO pin 11 – backwards
  • GPIO pin 12 – forwards
  • GPIO pin 13 – left
  • GPIO pin 15 – right

Rather than always having to put the GPIO pin number into the code, these are perfect candidates to make into constants. Once they are we do not need to keep on remembering that 13 is left every time we want to use it, or that 12 is forwards. It makes our life that bit easier which is what engineering is all about!

import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
BACKWARDS = 11
FORWARDS = 12
LEFT = 13
RIGHT = 15

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.output(FORWARDS,ON)
time.sleep(3)
GPIO.output(FORWARDS,OFF)

Again with the changes you have made above run the code again to check that it still works. Your Pi-Car should still just move forwards, wait for 3 seconds and stop moving forwards.

As we did before we create our constants with the names on the left, following by an ‘=’ symbol and then the value we want to give it:

BACKWARDS = 11
FORWARDS = 12
LEFT = 13
RIGHT = 15

With the constants created we can then use them later in the program instead of writing the number:

GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.output(FORWARDS,ON)
time.sleep(3)
GPIO.output(FORWARDS,OFF)

This helps us when we try and understand the program and also it means we will not make an error typing the number in.

With our constants defined it is now quite easy to write the instructions that make the Pi-Car goes in other directions:

import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS = 12
BACKWARDS = 11
LEFT = 13
RIGHT = 15

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
time.sleep(3)
GPIO.output(FORWARDS,OFF)

GPIO.output(BACKWARDS,ON)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)

GPIO.output(LEFT,ON)
time.sleep(3)
GPIO.output(LEFT,OFF)

GPIO.output(RIGHT,ON)
time.sleep(3)
GPIO.output(RIGHT,OFF)

It is now quite easy to understand what the code above is trying to do, so run the Python code again and check that your Pi-Car does what you asked it to.

Moving the Pi-Car left and right

So we can now activate all the functions of the Pi-Car from the Raspberry Pi but it is still quite basic. For instance you will have noticed that when you ran the program above the Pi-Car wheels just turned left and then right rather than moving left and right because forwards was not on.

In order to make the Pi-Car actually move left for example you will need to do the following:

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)

Getting the program to talk to us

You may have found that when you tried to do run the programs above they did not work as you hoped. You may also not always want to chase after your Pi-Car all the time as you are writing your program and want another way of knowing what it is doing.

So how can we find this out? The best way is to have the Python program tell you what it is doing. The way to do this with Python is to use the print statement. The print statement could say

print ("moving forwards")

Or:

print ("moving backwards")

We simply need to add this instruction to the program and Python will understand it and print to the screen where Python is running.

import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
print ("Moving forwards")
time.sleep(3)
GPIO.output(FORWARDS,OFF)

GPIO.output(BACKWARDS,ON)
print ("Moving backwards")
time.sleep(3)
GPIO.output(BACKWARDS,OFF)

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left")
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)

GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right")
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)

Try and run the program and you should see the text print to screen at the same time as your Pi-Car moves around. All you have to remember is to put what you want to print inside brackets and between the speech marks – ” “.

Another thing you can do is print out the value of the constants you created. For example maybe you got your GPIO pin numbers muddled up. You would be able to find this out by printing out the value of the constants you are using for FORWARDS, BACKWARDS and the others:

import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS)
time.sleep(3)
GPIO.output(FORWARDS,OFF)
GPIO.output(BACKWARDS,ON)
print ("Moving backwards after turning on backwards GPIO pin no = ",BACKWARDS)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left after turning on left GPIO pin no = ",LEFT)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)

GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right after turning on right GPIO pin no = ",RIGHT)
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)

When you run the program this time you should see the GPIO pin numbers print out. Notice here that you put the constant name outside of the speech marks:

print ("Moving right after turning on right GPIO pin no = ",RIGHT)

If you put it inside the speech marks the Raspberry Pi would think you wanted to print out the word RIGHT. Because you put the ‘,’ symbol this tells the Raspberry Pi you want to add it to the end of the string.

  • Driving Lesson review

Congratulations you made it through, hopefully you did actually crash your Pi-Car a few times as it means you managed to get it moving. In this lesson you leant two main things:

Constants:

How to use

1. Put the name on the left and the value on the right, choose a suitable name:

ON = 1

2. Create them before you can use them in your program.

Why do they help?

1. It makes it easier to read your program.

2. If you use the same value in lots of different places it will make sure that it always is the same. Also if you want to change it you can just change it in one place.

  • Print statement:

How to use

1. Add the word print followed by what you want to print out in speech marks:

print ("moving forwards")

2. If you want to print out the value of a constant add the name of the constant and make sure it is not inside the speech marks:

print ("Moving right after turning on right GPIO pin no = ",RIGHT)
  • Gotcha’s

In any engineering things rarely work first time so don’t worry. Hopefully the list below will help. If not email us at info@pi-cars.com and leave a comment providing your code and what you tried to do, the solution will be obvious as soon as you see it!

  1. I keep on getting invalid syntax after I try and run the code? The code listed above will run by typing sudo python filename in the command prompt and also when run from IDLE3 but from IDLE it will complain of a syntax error. This is because of IDLE wants the print statement without the brackets, like print “message” rather than print (“message”)
  2. How to I edit and run the code? You can write Python code into any text editor, you can open up Leaf that comes with the Raspberry Pi, or you can open a terminal and type sudo nano which will open a text editor. Our tutorials use IDLE3 as this provides tips on syntax and you can also run it from there. If you use a text editor you will have to run the code by opening a terminal and typing sudo python filename.py
  3. It tells me to try root what is that!? Accessing the GPIO pins currently needs you to have a bit of extra power than a normal user. To do this if you run your Python file from the command line you will need to type ‘sudo python filename.py’ rather than just python. The sudo word gives you the extra power. If you are using IDLE3 you need to open a terminal and type ‘sudo idle3’ rather than double clicking on it. A bit of a pain which will be fixed soon!

Next -> Driving Lesson #3 Variables

Previous -> Driving Lesson #1 Pi-Cars and Python

PDF Version

PDF version of this article – Introduction to Python, constants and print statements – Driving Lesson #2 _ Pi-Cars

Advertisements

About Barnaby Kent

http://www.myactionreplay.com
This entry was posted in Under the bonnet and tagged , , , , , , , . Bookmark the permalink.

3 Responses to Introduction to Python, constants and print statements – Driving Lesson #2

  1. Pingback: Variables and while loops – Driving Lesson #3 | Pi-Cars

  2. Pingback: Python if statements – Driving Lesson #4 | Pi-Cars

  3. Pingback: Python while loops – Driving lesson #5 | Pi-Cars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s