Development Step #9 Python
o Approach.
For me personally a lynda course will help me out a lot when it comes to learning a completely new programming language:
I will follow this one:
https://www.lynda.com/Python-tutorials/Learning-Python/661773-2.html
As an editor I will be using Atom.
o Learning Experience.
- Atom setup.
- Lynda course.
Topics include:
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML
When I tried to run the “Helloworld!” example, the console displayed the following error message:
python : The term ‘python’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
After googling it I realized that I need to add manually the python path to the Windows Environment Variables. I followed this tutorial:
o Result.
- Hello world example.
- Variables and expressions.
- Functions
- Loops
- Conditional structures.
- Classes
- Date and time classes
- Formatting time output.
- Timedelta objects
- Calendars
- Reading and writing files.
- OS path
- File system shell.
o Sources.
https://www.lynda.com/Python-tutorials/Learning-Python/661773-2.html
https://stackoverflow.com/questions/11813435/im-trying-to-use-python-in-powershell
https://docs.python.org/3/library/index.html
https://docs.python.org/3/reference/index.html
Development Step #10 GPIO & Servo Motors on Raspberry Pi
o Approach.
sdsds
o Learning Experience.
As a starting point I headed to the course called “Raspberry Pi: GPIO” on Lynda.com. The main topics I am interested in are:
- Locating the GPIO
- Programming with Python
- Identifying GPIO pins
- Using the WiringPi and RPi.GPIO libraries
Controlling an LED.
We will enable the GPIO pin through the terminal. First we need to be super users in order to do that. In stead of typing “sudo” every single line, we can simply enter superuser mode through the following command:
sudo -i
Next step is to change the directory (“cd”) to the directory where the GPIO is located with the following command:
cd /sys/class/gpio
Now we need to assign pin 4.
echo "4" > export
This command takes the value “4” and writes it into a file called export. “echo” stands for print.
Now we need to assign pin 4 as an output pin.
echo "out" > gpio4/direction
To turn the LED on we need to write the following command:
echo "1" > gpio4/value
- Result
To turn off the LED we can use the reverse command:
echo "0" > gpio4/value
- Result
To turn off any values for pin 4, we can write:
echo "4" > unexport
As a safety measure we can also exit the “sudo” mode by typing:
exit sudo
Using a Switch / Button
First I need to assign GPIO pin 4 as input:
sudo -i
cd /sys/class/gpio
echo "4" > export
echo "in" > gpio4/direction
I can check the value of gpio4 with the following command:
cat gpio4/value
At the moment I am not having the button pressed, which means that I should receive a value of “0”.
- Result.
If I have the button pressed the circuit should become closed and I should receive a value of 1.
- Result
Locating Pins
There are several different numerations of the GPIO pins.
- GPIO.BOARD – is the physical location
- GPIO.BCM – is the numbering according to the default python library for Raspberry (RPi.GPIO)
- WiringPi – is related to the 3rd party WiringPi python library
Now I will be using a python code to reproduce the previous exercise. First I will import the RPi.GPIO library:
import RPi.GPIO as GPIO
Now I will select the board numbering method. In this case GPIO.BOARD
GPIO.setmode(GPIO.BOARD)
Now I need to select the input pin. In the previous experiment it was pin 4, but according to the GPIO.BCM system, now I am using the GPIO.BOARD system an the corresponding number is 7.
inputpin = 7
The next line should assign the pin as an input pin and indicate that there is a resistor in use.
GPIO.setup(inputpin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
We can now add a if/else statement reflecting the state of the switch/button, if it is closed/pressed or opened/released.
while True:
if GPIO.input(inputpin):
print ("Switch Closed")
else:
print ("Switch Open")inputpin = 7
The final python script should look like that:
If we were using the GPIO.BCM numbering then it would be:
We can run the code through the terminal, navigate to and run the script that we just created:
- Result.
At the moment the button is not pressed.
As soon as I press it:
WiringPi Library
To install the WiringPi Library the following commands should be executed one after another:
sudo apt-get install git-core
sudo apt-get update
sudo apt-get upgrade
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
To test if the installation went smoothly type:
gpio -v
Now to see all the GPIO numbers type in the terminal:
gpio readall
- Result:
Servo Motors
For the following experiments I will be using the default library called “GpioZero” and HS-311 servo.
I am attaching the power cable to Pin #2, which is 5v. The ground cable to Pin#9 and the signal cable to Pin#7 (GPIO.4). And for the Raspberry I am using a 5V 4A power supply.
The first script I will use should make the servo move between its minimum, middle and maximum positions with a small delay in-between.
from gpiozero import Servo
from time import sleep
myGPIO=17
servo = Servo(myGPIO)
while True:
servo.mid()
print("mid")
sleep(0.5)
servo.min()
print("min")
sleep(1)
servo.mid()
print("mid")
sleep(0.5)
servo.max()
print("max")
sleep(1)
To execute the script I can do it from the terminal by typing:
cd experiments //change to the experiments folder I store my scripts now
python servo1.py
Or by using a Python editor called Thonny, which turned out to be more convenient.
- Result
first script makes use of the Gpiozero defaults. It assumes the servo uses a signal frame width of 20ms. The pulse width for the minimum and maximum rotation is assumed to be 1ms and 2ms.
I found that with the default settings my servo moves in the diapason from 30 degrees to 150 degrees. With the following script I will try to expand that diapason.
from gpiozero import Servo
from time import sleep
myGPIO=4
myCorrection=0.45
maxPW=(2.0+myCorrection)/1000
minPW=(1.0-myCorrection)/1000
servo = Servo(myGPIO,min_pulse_width=minPW,max_pulse_width=maxPW)
while True:
servo.mid()
print("mid")
sleep(0.5)
servo.min()
print("min")
sleep(1)
servo.mid()
print("mid")
sleep(0.5)
servo.max()
print("max")
sleep(1)
- Result
The last script I will experiment with generates a range of “value” numbers to sweep the servo between its maximum and minimum position :
from gpiozero import Servo
from time import sleep
myGPIO=4
myCorrection=0
maxPW=(2.0+myCorrection)/1000
minPW=(1.0-myCorrection)/1000
servo = Servo(myGPIO,min_pulse_width=minPW,max_pulse_width=maxPW)
while True:
print("Set value range -1.0 to +1.0")
for value in range(0,21):
value2=(float(value)-10)/10
servo.value=value2
print(value2)
sleep(0.5)
print("Set value range +1.0 to -1.0")
for value in range(20,-1,-1):
value2=(float(value)-10)/10
servo.value=value2
print(value2)
sleep(0.5)
The first “For” loop generates a set of integers between 0 and 20. The value has 10 subtracted from it to give a range -10 to 10. The value is then finally divided by 10 to give a range of -1 to +1. The original set of values contained 20 integers and we still have 20 steps in the sequence. To increase the number of steps to 40 you would replace 20 with 40 and subtract 20 rather than 10.
The second “For” loop does the same thing but generates a sequence from +1 back to -1.
- Result
o Sources.
https://www.raspberrypi-spy.co.uk/2018/02/basic-servo-use-with-the-raspberry-pi/
https://gpiozero.readthedocs.io/en/stable/api_output.html#servo