News The Project Technology RoboSpatium Contribute Subject index Download Responses Games Gadgets Contact <<< Python: Pulse-width modulation Python: Commandline parameters >>> Python: Background activityThe circuit used in this chapterFigure 1:Two LEDs and a servo are switched in the examples of this chapter. The servo should be a microservo (also called a 9g servo), as these draw less current than standard servos. More about how to connect LEDs to GPIOs is written in the chapter on switching LEDs via GPIOs. Which pins have which function can be found in the chapter on the hardware of the Raspberry Pi. Change parameters without restarting a scriptSo far we always had to restart a Python script in order to be able to change parameters within the program. In this chapter I want to show how you can do this while the program is running. A common way of transferring data to a running program is to read special command files. To do this, the program must constantly check whether a certain file existse and, should that be the case, read the parameters it contains. In the following example the name of the file is "ParameterGPIO.txt" and it must be stored in the directory "/home/pi/":
Store the script in the directory "/home/pi/background.py" and declare it to be executable: chmod a+x background.py To make the program run in background, it must be started with an "&" at the end of the command line: ./background.py& If the program is running in the background, you can transfer parameters to it by generating a file with the name "/home/pi/ParameterGPIO.txt" and writing a command line into it. This can be done with the "echo" command. In order to switch on the LED on pin 16 with 40% pulse width, the needed command line is: echo GPIO16 0.4 > /home/pi/ParameterGPIO.txt To turn the servo lever for 90° clockwise use: echo GPIO12 -1.0 > /home/pi/ParameterGPIO.txt To turn the servo lever 90° counterclockwise use: echo GPIO12 1.0 > /home/pi/ParameterGPIO.txt If you write a non valid parameter for the servo lever into the file, an error message is displayed: echo GPIO12 -2.0 > /home/pi/ParameterGPIO.txt Using correct upper and lower case is important! For example the command: echo Gpio12 -2.0 > /home/pi/ParameterGPIO.txt is simply ignored! You can no longer end the execution of the script by pressing 'ctrl+c', because keyboard inputs are not forwarded to programs running in background. To end the script, you can type the command: killall python ServicesSuch a program running in the background is also known as a service or daemon. In the chapter on pulse width modulation it was already pointed out that the functions of "gpiozero" in Python only work if the corresponding "daemon", which is a program called "pigpiod", is running in the background. That program monitors the file "/dev/pigpio". This means that the LED on pin 16 can also be set to a duty cycle of 40% from the command line without a Python script using the command:echo "p 23 40" > /dev/pigpio Note that this command does not use physical numbering for the GPIOs, but the numbering as displayed by the command: pinout The physical pin 16, to which an LED is connected in our example above, corresponds to GPIO 23 on a Raspberry Pi 1, Model B+. In contrast to our Python script, the pigpiod program can handle a lot more commands and it can access all of the GPIOs on your Raspberry Pi - not only the 3 GPIOs in the example script above. The advantage of our script, however, is that we can very quickly add any commands needed, even those that are not implemented in pigpiod. <<< Python: Pulse-width modulation Python: Commandline parameters >>> News The Project Technology RoboSpatium Contribute Subject index Archives Download Responses Games Links Gadgets Contact Imprint |