Download Class Notes for 2 March 2017

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Junior Robotics Term One Class No 2
2 March 2017
This is the second class.
1. Get Pi's connected to PC screen and running.
2. Robot Wifi Password changing
Teach the kids how to change the Wifi access password on their robots. Follow
these steps:
1. Open a terminal.
2. Become root by typing sudo su
3. Change into the /etc/hostapd folder:
cd /etc/hostapd
4. Make a backup of your original hostapd.conf file by typing:
cp hostapd.conf hostapd_backup.conf
5. Edit the original hostapd.conf by typing:
mcedit hostapd.conf
6. Look for the line that starts with wpa_passphrase this is the wifi
password for your robot. Change it to any value along as it
contains ONLY letters and numbers and the - char.
7. Save the changes by pressing F2.
8. Exit the editor by pressing F10.
9. From the command line reset the pi by typing:
shutdown -r now
10. When the Pi boots again you should be able to use your new Wifi
password.
3. Basic Python Program
From section 6.3 of the manual write the basic printing program and run it.
6.3 Displaying Text or Printing in Python
In python the print statement is used to display text and variable values to
the user.
Printing Text:
The line below will print Hello World.
print “Hello World”
NOTE All students in a group should either be at the PC screen attached to the
Pi or connected to it via ssh. Thus there should ideally be one or two students
per session.
1. Open a terminal. Either from the desktop or by ssh'ing in.
2. Run the screen program by typing:
screen
3. Create another 3 more sessions in screen by pressing Ctrl a c three times
in a row.
4. Goto the first screen session by pressing Ctrl a 1
5. At the terminal prompt create a new folder with your name as the folder
name.
eg
mkdir jack
6. Change into the new folder by typing:
cd jack
7. Once there create a new python program file by typing:
mce print_program1.py
8. Once the new program file is open in the editor type in the following:
#!/usr/bin/python
print "Hello World"
9. Press F2 to save the program.
10. To run the program press Ctrl a 2 to go into the second screen terminal.
11. Once there goto into the new folder you created earlier by typing:
cd jack
(ie your name)
12. To run the program type:
python print_program1.py
You should see the program print the text Hello World.
13. Return to editing your program by typing Ctrl a 1
Save your program as print_program2.py by pressing F12 and changing the
file name. Then add the lines below to your program and save it.
#!/usr/bin/python
print "Hello World"
# Setup some new variables
first_name = "Jack"
last_name = "Jill"
age = 25
height = 6.12
# Display the values
print "First name: ",first_name
print "Last name: ",last_name
print "Age: ",age
print "Height: ",height
14. Now run your program and see what you get.
4. Introduce Breadboards
Explain how breadboards work and draw a diagram on the board.
5. Explain Electrostatics and how to get the top off the raspberry pi
Explain the fact that they cant touch the pis internals willy nilly and the idea of
electrostatic discharge. Next explain how to remove the lid from the pi and
connect the wires to the inside.
6. LED On and Off Program
Goto section 7.5 Basic GPIO Programming of the manual.
You will need an LED and a 1K resistor ( colors brown-black-black-brown )
Python Code:
An example of the program code needed to have the pi turn the led on and off is given
below.
# led_on_off1.py
# Turn an LED on GPIO18 port on and off.
# Import the required libraries >>>>>>>>>>>>>>>>>>>>>>>>>
import sys, os, threading, getopt, string, time, math, re from time import sleep
import RPi.GPIO as GPIO
# Setup the GPIO Ports >>>>>>>>>>>>>>>>>>>>>
# Set the GPIO Port Operating Mode
GPIO.setmode(GPIO.BCM)
# Set GPIO18 as an Output >>>>>>>>>>>>>>>>>>>>>
# GPIO.IN > Pin is an input.
GPIO.OUT > Pin is an output
GPIO.setup(18, GPIO.OUT)
# @main >>>>>>>>>>>>>>>>>>>>>>>>> # The main program code.
def main():
print "\n\n<*><*><*><*> Turn a LED on and off....\n"
# Turn it on >>>>>>>>>>
print "Led on.....\n"
GPIO.output(18, True)
sleep(1)
# Turn it off >>>>>>>>>>
print "Led off.....\n"
GPIO.output(18, False)
sleep(1)
# Turn it on >>>>>>>>>>
print "Led on.....\n"
GPIO.output(18, True)
sleep(1)
# Turn it off >>>>>>>>>>
print "Led off.....\n"
GPIO.output(18, False)
sleep(1)
# NOTE This is the start of the program because this is the left most #
statement.
print "Start Program >>>>>>>>>>>>>>>>>>>>>>>>>>>>"
main()
Create a new program file in the Pi and enter in the above code. Save it and run it. If
you have done it correctly the led should turn on and off twice.
Basic Pi Robotics Software Guide ver2e 2 March 2017.odt Page 53
1. Import the standard libraries needed to run the program.
These will be the same for just about all programs.
2. Setup GPIO operating mode.
3. Setup GPIO 18 as an output.
We need to tell the Pi what type of pin we want it to be: input or output.
4. Main Program Code.
This is the code that actually makes up the program and does the work.
5. Run Main Program Code.
When the python program is run this is where it starts.
Exercises:
For practice complete the following exercises.
1. Create a program that will use a for loop to turn the led on and off 10 times.
2. Add a second led to the interface board on GPIO 24 and write a program that will
turn both leds on and off alternately.
3. Write a program that will prompt the user for the number of times they wish to
have the leds turn on and off.
Next Class: