Wednesday 2 March 2016

Build a Pi Glove – part 2, creating software

In part one, we covered the creation and the hardware setup of a wearable tech glove formally known as Project New York. This tutorial shows you how to create a program and write the software to add interaction to the glove’s buttons. Once you have created the basic code structure, you can develop new interactions by coding and adding your own functions. The program is coded in Python 2.7 to ensure all the libraries are compatible with the Raspberry Pi hardware and the Raspbian operating system. On completion, the glove will give you spoken instructions, tell you the current time, take a picture with the Pi camera module and play a random selection of music, all from your fingertips.

1. A quick test and recap

Ensure that your glove hardware consists of at least five wires connected to a Pi which is mounted to the glove. A Pi camera is also embedded or attached to the glove. Boot up your Raspberry Pi; this could be a separate Pi. Initially, it is worth running the test program below, to ensure that all the hardware and wires are connected correctly and working properly.

  import RPi.GPIO as GPIO

  ######Set up the GPIO Pins ######

  GPIO.setmode(GPIO.BCM)

  ###sets the pin to high ###

  GPIO.cleanup()

  GPIO.setup(17, GPIO.IN, GPIO.PUD_UP) 

  ##11 on the BOARD

  GPIO.setup(18, GPIO.IN, GPIO.PUD_UP) 

  ##12 on the BOARD

  GPIO.setup(9, GPIO.IN, GPIO.PUD_UP)

  ##21 on the BOARD

  GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)

  ##7 on the BOARD

  GPIO.setwarnings(False)  ##switch off other ports

  while True:

      if GPIO.input(4) == 0:

          print “You pressed button one”

                        

      if GPIO.input(17) == 0:

          print “You pressed button two”

        

      if GPIO.input(9) == 0:

          print “You pressed button three”

      if GPIO.input(18) == 0:

          print “You pressed button four”

2. Install the Python libraries

Assuming all went well with the test, you are set up and ready to build the new program. The good news is that most of the modules that you will use in the program are already pre-installed on the Raspbian operating system. To add the ‘spoken instructions’ feature you will install a module called eSpeak. In the LX Terminal, type:

  sudo apt-get install espeak python-espeak

To play the MP3s, syou will use a program called mpg321. To install this, type:

  sudo apt-get install mpg321

Once installed, restart the Pi.

3. Test eSpeak

eSpeak is a simple, compact, open source software speech synthesiser that uses English and other languages. It works by taking a text string and then converting it into audio. But that’s not all – you can also edit the voice, pitch, volume and other parameters of the speech. Test that it is working by creating a new Python file and using the code espeak.synth (“This is a Test”). Now, when you run the program it will read out the phrase “This is a test”.

  from espeak import espeak

  espeak.synth (“This is a test”)

Build a Pi Glove – part 2, creating software

4. Import modules

The Glove program uses a number of modules for its various functions. These are Python files that contain a number of definitions, variables, functions and classes. Import the modules below into your Python program – these will give your program access to the MP3 player, the Pi camera module, the GPIO pins and eSpeak.

  import time

  import random

  import os

  import sys

  import subprocess

  import picamera

  import RPi.GPIO as GPIO

  from sys import exit

  from espeak import espeak

5. GPIO pull-ups

To register that you have triggered the finger button, we make use of GPIO pull-ups to detect that the two contacts have touched together. The pull-up resistor sets the current to 0 volts. When the two wires connect, the voltage changes and this change in state is recognised, triggering the function which you will assign to each of the buttons. If you have no pull-up or pull-down then the GPIO pin can change state, for instance if there is external interference, and this means that it can trigger your button even if you did not touch it. To set these up, add the following code to your program:

  GPIO.setmode(GPIO.BCM)

  ###sets the pin to high ###

  GPIO.cleanup()

  GPIO.setup(17, GPIO.IN, GPIO.PUD_UP) 

  ##11 on the BOARD SPARE

  GPIO.setup(18, GPIO.IN, GPIO.PUD_UP) 

  ##12 on the BOARD MUSIC PLAYER

  GPIO.setup(9, GPIO.IN, GPIO.PUD_UP)

  ##21 on the BOARD TAKE A PICTURE

  GPIO.setup(4, GPIO.IN, GPIO.PUD_UP)

  ##7 on the BOARD TIME

  GPIO.setwarnings(False)##switch off other ports

6. Add the spoken instructions

Since there is no visual display, you will not know that the program is running or that it is ready. Therefore, at the start of the program it reads out the button number and the function of each. This uses the same code from Step 3, calling eSpeak to convert the text to an audio wave and play it back through a speaker or a pair of headphones. You can customise the introduction and what instructions are given. Use time.sleep(2) to add a slight break between the sentences and make the speech more natural.

  espeak.synth (“Welcome to the PI GLOVE”)

  time.sleep(2)

  espeak.synth (“Please make a selection”)

  time.sleep(2)

  espeak.synth (“Button 1 – tell you the time”)

  time.sleep(2)

  espeak.synth (“Button 2 – take a picture”)

  time.sleep(2)

  espeak.synth (“Button 3 – play some tunes”)

  time.sleep(3)

  espeak.synth (“Please select your button”)

7. Set up the time

At this point you are now ready to set up the function for the first button, which will tell you the time in a similar fashion to the old ‘speaking clock’. This feature means you don’t have to take out and unlock your phone – simply press the button and the current time is read back to you. Line 2 of the code creates and stores the current time as a variable

  current_time = (time.strftime(“%H:%M:%S”))

A second variable, line 3, is used to store the ‘time message’ which is then used by eSpeak to read out the time to you, line 4. Add the code to your program:

  def what_is_the_time():

      #global time

      current_time = (time.strftime(“%H:%M:%S”))

      the_time = “The current time is %s” % current_time

      espeak.synth(the_time)

      time.sleep(2)

Build a Pi Glove – part 2, creating software

8. Set up the camera

The picamera module is pre-installed on the Raspberry Pi, so you are ready to create a function which will trigger the camera and save the picture as a new file called newpic.jpg (line 5). The third line is useful to test that the camera is taking a picture and also to familiarise yourself with where you are pointing the camera. When triggered it will display a preview of what the camera sees on a connected monitor or television.

  def take_a_pic():

      with picamera.PiCamera() as camera:

          camera.start_preview()

          time.sleep(2)

          camera.capture(“newpic.jpg”)

9. Take a selfie

It is possible to perform a test in order to ensure that the camera is working by calling upon the take_a_pic() function. Do this by opening up a new Python window and then add the previous code from Step 8. Save and run the code, and you should see a two-second preview of what the camera sees and then the camera will capture this image, which is then stored in the Pi/Home folder.

10. Save as a new file name

Currently, each time a new picture is taken, it overwrites the previous file. Annoyingly, this means that you will lose the last picture you took. To stop this, create a global variable called File_Number, line 1. This variable is incremented each time a new picture is taken. Create a second variable, called file_name (line 2) – this variable is combined with File_Number to create a new unique file name each time the picture is saved (line 4), preserving your previous pictures. Line 5 ensures that the File_Number value is incremented by one each time a photo is saved.    

  global File_Number ###number if photo

  global file_name ###name of photo

  File_Number = 1

  file_name = “Picture” + str(File_Number) + “.jpg”

              File_Number = File_Number + 1

11. Final camera code

The complete camera code uses a function that combines the features from Steps 8 and 10 to trigger the camera and save the image as a new file with a unique file name each time the two ‘poppers’ connect. Add the code below to a new line underneath your time function.

  def take_a_pice(): ###Takes a picture ###

      global File_Number

      global file_name

      with picamera.PiCamera() as camera:

               time.sleep(0.5)

  file_name = “Picture” + str(File_Number) + “.jpg”

                File_Number = File_Number + 1

Build a Pi Glove – part 2, creating software

12. Save the music

There are two small steps to take before you can enable the music player. First, download a number of MP3s and save the file names as numbers – for example, 1.mp3, 2.mp3, 3.mp3 and so on. For the second step, create a variable at the beginning of your program to store the file names, such as:

  songs_list = [“1”, “2”, “3”, “4”, “5”]

This variable is used to select the song.

  ###Code for MP3 random play list###

  songs_list = [“1”, “2”, “3”, “4”, “5”]

13. The music player

Instead of creating another function for the music playback, the MP3 player is called directly from the GPIO pin 17 pull-up. It makes use of the variable song_list, which holds the file names stored as a list of numbers: 0,1,2,3,4,5. In the Home folder, you’ll have your six music tracks named 0.mp3, 1.mp3, 2.mp3, etc. In order to make this a shuffle-based player, we can use the following line of code:

  os.system(‘mpg321 ’+ (random.choice(songs_list)) + ‘.mp3 &’)

  ##change

… which calls the operating system to load the mpg321 software and select a random number from the play list, and it then loads and plays the corresponding mp3 file.

14. Stop the music

The code in Step 13 will keep the music playing continuously. To stop the music, use the code:

  os.system(‘sudo killall mpg321’)   

Map this code to the button on the glove and, by holding down the button for a little longer, you can cycle through a variable called song_play (line 3), which changes from ‘yes’ to ‘no’. When the variable is ‘no’ (line 9), a conditional is used on line 4 to check the state and then use the following code:

  os.system(‘sudo killall mpg321’)

… in order to stop the music playing (line 8). Listen to the spoken instructions, and you can then time it right to properly end the music. Now we’ve explained what’s going on, add the following code into your program:

  os.system(‘sudo killall mpg321’)

          espeak.synth (“Music Player ”)

          song_play = “yes”

          if song_play == “yes”:

              os.system(‘mpg321 ’+ (random.

choice(songs_list)) + ‘.mp3 &’)

              if GPIO.input(17) == 0:

               #turns off song longer hold

                  os.system(‘sudo killall mpg321’)

                  song_play = “no”

                  espeak.synth (“MP3 player stopped”)

Build a Pi Glove – part 2, creating software

15. Create the button structure

Now you have created three features for your glove, you can start assigning them to the finger buttons, which will trigger each function when they are connected with the thumb button. This is achieved with a simple set of conditionals, like: if GPIO.input(17) == 0:. This checks if a GPIO pull-up is present, and then if so, it runs the assigned function. Add the four conditionals below into your program. Remember to move the music player code from Step 14 so it’s beneath the GPIO pin 17 code.

  while True:

      if GPIO.input(4) == 0:

      if GPIO.input(9) == 0:

      if GPIO.input(17) == 0:

      if GPIO.input(18) == 0:

16. Call the functions

Once you have created your conditionals to test for the button pull-up, the final part of your program is to add the function for each GPIO. For example, add the time function to GPIO pin 4 with:

  if GPIO.input(4) == 0:

          what_is_the_time()

This will run the time function you created in Step 7 each time the pin 4 button is connected to the thumb button. You will also want to add some instructions under each button to inform the user what is happening. For example, when triggering the camera it is useful to know when the picture is going to be taken – have a look at the code example below.

  time.sleep(1)

  espeak.synth(“Preparing the camera”)

  time.sleep(2)

  espeak.synth(“Camera ready, smile”)

  time.sleep(1)

  take_a_pic()

  ###enables the camera def and takes a picture

  espeak.synth(“Picture taken and saved”)

  time.sleep(3)

  espeak.synth(“Press button two to tweet your

picture”)

17. Other functionality

Save and run the program as the root user, then debug the code and test the contacts. Common errors may be incorrect wiring on the GPIO pins, loose wires not in contact with the metal poppers, or the thumb and finger not in contact. Once working, you can now create your own interactions for your glove – for example, turn lights on and off, send an SMS to a mobile phone, control your TV or read text in a text file. 



from Linux User & Developer – the Linux and FOSS mag for a GNU generation http://ift.tt/1Y0AvLJ
via IFTTT

No comments:

Post a Comment

Amazon

Donate

Donate Towards More Raspberry PI's for Projects