1.7 Development Steps this week. #9 #10

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.
atom-4-logo-png-transparent.png

 

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.

1.JPG

 

  • Variables and expressions.

2.JPG

 

  • Functions

functions.JPGfunctions2.JPG

 

  • Loops

32.JPG31.JPG

 

  • Conditional structures.

conditions1.JPG

  • Classes

classes1.JPG

  • Date and time classes

dates1.JPG

dates2.JPG

 

  • Formatting time output.

formatting1.JPG

formatting2.JPG

  • Timedelta objects

timedeltas1.JPG

timedeltas2.JPG

  • Calendars

calendar1.JPG

calendar2.JPG

  • Reading and writing files.

files1.JPG

files2.JPG

  • OS path

ospath.JPG

ospath2.JPG

ospath3.JPG

  • File system shell.

shell.JPG

shell2.JPG

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.

Annotation 2019-08-24 194708.jpg
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
    Annotation 2019-08-24 201028.jpg

    To turn off the LED we can use the reverse command:

echo "0" > gpio4/value

  • Result
    Annotation 2019-08-24 201055.jpg

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

Annotation 2019-08-24 201541.jpg
Using a Switch / Button

Annotation 2019-08-24 202546.jpg

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.
    Annotation 2019-08-24 211639.jpg

Annotation 2019-08-24 212722.jpg

If I have the button pressed the circuit should become closed and I should receive a value of 1.

  • Result
    Annotation 2019-08-24 211611.jpg
    Annotation 2019-08-24 212802.jpgAnnotation 2019-08-24 213202.jpg

Locating Pins

Annotation 2019-08-24 214727.jpg

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:
Annotation 2019-08-24 220904.jpg

If we were using the GPIO.BCM numbering then it would be:
Annotation 2019-08-24 220920.jpg

We can run the code through the terminal, navigate to and run the script that we just created:

Annotation 2019-08-24 221424.jpg

  • Result.

At the moment the button is not pressed.

Annotation 2019-08-24 221548.jpg

As soon as I press it:
Annotation 2019-08-24 221629.jpg

 

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:

Annotation 2019-08-24 224834.jpg

 

Servo Motors

For the following experiments I will be using the default library called “GpioZero” and HS-311 servo.
Annotation 2019-08-25 161246.jpg
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
    Annotation 2019-08-25 185010.jpg

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

 

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s