# Imports import webiopi # Retrieve GPIO lib GPIO = webiopi.GPIO # -------------------------------------------------- # # Constants definition # # -------------------------------------------------- # # Left motor GPIOs L1=9 # H-Bridge 1 L2=10 # H-Bridge 2 LS=11 # 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 # # -------------------------------------------------- # def go_forward(): left_forward() right_forward() def go_backward(): left_backward() right_backward() def turn_left(): left_backward() right_forward() def turn_right(): left_forward() right_backward() def stop(): left_stop() right_stop() # -------------------------------------------------- # # Initialization part # # -------------------------------------------------- # # 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() # -------------------------------------------------- # # Main server part # # -------------------------------------------------- # # Instantiate the server on the port 8000, it starts immediately in its own thread server = webiopi.Server(port=8000, login="cambot", password="cambot") # Register the macros so you can call it with Javascript and/or REST API server.addMacro(go_forward) server.addMacro(go_backward) server.addMacro(turn_left) server.addMacro(turn_right) server.addMacro(stop) # -------------------------------------------------- # # Loop execution part # # -------------------------------------------------- # # Run our loop until CTRL-C is pressed or SIGTERM received webiopi.runLoop() # -------------------------------------------------- # # Termination part # # -------------------------------------------------- # # Stop the server server.stop() # 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)