From 0c8c9ad976879f7c90f9915a60845ccb0cdb337d Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 25 Dec 2013 13:25:16 +0100 Subject: initial commit --- examples/magpi-9-cambot2/cambot.py | 117 +++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 examples/magpi-9-cambot2/cambot.py (limited to 'examples/magpi-9-cambot2/cambot.py') diff --git a/examples/magpi-9-cambot2/cambot.py b/examples/magpi-9-cambot2/cambot.py new file mode 100644 index 0000000..f1013ab --- /dev/null +++ b/examples/magpi-9-cambot2/cambot.py @@ -0,0 +1,117 @@ +# This version uses new-style automatic setup/destroy/mapping +# Need to change /etc/webiopi + +# Imports +import webiopi + +# Retrieve GPIO lib +GPIO = webiopi.GPIO + +# -------------------------------------------------- # +# Constants definition # +# -------------------------------------------------- # + +# Left motor GPIOs +L1=17 # H-Bridge 1 +L2=18 # H-Bridge 2 +LS=21 # H-Bridge 1,2EN + +# Right motor GPIOs +R1=23 # H-Bridge 3 +R2=24 # H-Bridge 4 +RS=25 # H-Bridge 3,4EN + +# -------------------------------------------------- # +# Convenient PWM Function # +# -------------------------------------------------- # + +# Set the speed of two motors +def set_speed(speed): + GPIO.pulseRatio(LS, speed) + GPIO.pulseRatio(RS, speed) + +# -------------------------------------------------- # +# Left Motor Functions # +# -------------------------------------------------- # + +def left_stop(): + GPIO.output(L1, GPIO.LOW) + GPIO.output(L2, GPIO.LOW) + +def left_forward(): + GPIO.output(L1, GPIO.HIGH) + GPIO.output(L2, GPIO.LOW) + +def left_backward(): + GPIO.output(L1, GPIO.LOW) + GPIO.output(L2, GPIO.HIGH) + +# -------------------------------------------------- # +# Right Motor Functions # +# -------------------------------------------------- # +def right_stop(): + GPIO.output(R1, GPIO.LOW) + GPIO.output(R2, GPIO.LOW) + +def right_forward(): + GPIO.output(R1, GPIO.HIGH) + GPIO.output(R2, GPIO.LOW) + +def right_backward(): + GPIO.output(R1, GPIO.LOW) + GPIO.output(R2, GPIO.HIGH) + +# -------------------------------------------------- # +# Macro definition part # +# -------------------------------------------------- # +@webiopi.macro +def go_forward(): + left_forward() + right_forward() + +@webiopi.macro +def go_backward(): + left_backward() + right_backward() + +@webiopi.macro +def turn_left(): + left_backward() + right_forward() + +@webiopi.macro +def turn_right(): + left_forward() + right_backward() + +@webiopi.macro +def stop(): + left_stop() + right_stop() + +# Called by WebIOPi at script loading +def setup(): + # Setup GPIOs + GPIO.setFunction(LS, GPIO.PWM) + GPIO.setFunction(L1, GPIO.OUT) + GPIO.setFunction(L2, GPIO.OUT) + + GPIO.setFunction(RS, GPIO.PWM) + GPIO.setFunction(R1, GPIO.OUT) + GPIO.setFunction(R2, GPIO.OUT) + + set_speed(0.5) + stop() + + +# Called by WebIOPi at server shutdown +def destroy(): + # Reset GPIO functions + GPIO.setFunction(LS, GPIO.IN) + GPIO.setFunction(L1, GPIO.IN) + GPIO.setFunction(L2, GPIO.IN) + + GPIO.setFunction(RS, GPIO.IN) + GPIO.setFunction(R1, GPIO.IN) + GPIO.setFunction(R2, GPIO.IN) + -- cgit v1.2.3