News    The Project    Technology    RoboSpatium    Contribute    Subject index    Download    Responses    Games    Gadgets    Contact   




<<< Python: Background activity         Apache Webserver >>>

Python: Commandline parameters

The circuit used in this chapter

Switching LEDa and a servo via a GPIO using Python
Figure 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.

Hand parameters to a script from the command line

In addition to reading parameters from a file, as demonstrated in the previous chapter, these can also be handed to a script while started from the commandline. The following example processes parameters for two LEDs and a servo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python  
 
import sys # Needed for handling command line parameters  
from gpiozero import Device, Servo, PWMLED # Needed for PWM control  
 
# The older Raspberry Pi 1 or Pi Zero require to set the pin factory  
from gpiozero.pins.pigpio import PiGPIOFactory  
 
 
# Pin factory for older Raspberry Pi models  
Device.pin_factory = PiGPIOFactory()  
 
argNumber = 0  
 
for arg in sys.argv: # Loop through all command line parameters  
 
    argNumber = argNumber + 1  
 
    if arg == "-h" or arg == "--help" or len(sys.argv) < 2:  
        print ("Script can set GPIO (physical pin numbers) 16 or 22 to pwm signal from 0.0 to 1.0")  
        print ("or set GPIO 12 to servo angle from -1.0 to 1.0")  
        print ("commandline.py -pwm16 <pulse-width>  -pwm22 <pulse-width> -servo12 <servo angle>")  
        sys.exit()  
    elif arg == "-pwm16" or arg == "--pulse-width-pin-16":  
        pulseWidth16 = 0  
        if(argNumber < len(sys.argv)):  
            pulseWidth16 = float(sys.argv[argNumber])  
        if (pulseWidth16 < 0.0 or pulseWidth16 > 1.0):  
            print("Pulse-width 16 " + str(pulseWidth16) + " out of range (0.0 to 1.0)!")  
            sys.exit(2)  
        else:  
            pwm16 = PWMLED("BOARD16", True, 0, 100)  
            pwm16.value = pulseWidth16  
    elif arg == "-pwm22" or arg == "--pulse-width-pin-22":  
        pulseWidth22 = 0  
        if(argNumber < len(sys.argv)):  
            pulseWidth22 = float(sys.argv[argNumber])  
        if (pulseWidth22 < 0.0 or pulseWidth22 > 1.0):  
            print("Pulse-width 22 " + str(pulseWidth22) + " out of range (0.0 to 1.0)!")  
            sys.exit(2)  
        else:  
            pwm22 = PWMLED("BOARD22", True, 0, 100)  
            pwm22.value = pulseWidth22  
    elif arg == "-servo12" or arg == "--servo-angle-pin-12":  
        servoAngle12 = 0  
        if(argNumber < len(sys.argv)):  
            servoAngle12 = float(sys.argv[argNumber])  
        if (servoAngle12 < -1.0 or servoAngle12 > 1.0):  
            print("Servo angle 12 " + str(servoAngle12) + " out of range (-1.0 to 1.0)!")  
            sys.exit(2)  
        else:  
            servo12 = Servo("BOARD12")  
            servo12.value = servoAngle12  
 
    

Save the script as "/home/pi/commandline.py" and make it executable:
chmod a+x background.py

If you call the script, you can hand the servo lever position or the duty cycle of the LEDs as parameter. The following example sets the pulse width of LED 16 to 40%:
./commandline.py -pwm16 0.4

The command to move the servo lever to 45 degrees:
./commandline.py -servo12 0.5

or with the long version:
./commandline.py --servo-angle-pin-12 0.5

Several parameters can also be transferred at the same time:
./commandline.py -servo12 -0.5 -pwm16 0.1 -pwm22 0.3



<<< Python: Background activity         Apache Webserver >>>


News    The Project    Technology    RoboSpatium    Contribute    Subject index    Archives    Download    Responses    Games    Links    Gadgets    Contact    Imprint   





Twitter YouTube Hackaday Patreon TPO