Permalink
Browse files

added python3 examples page

  • Loading branch information...
simonmonk committed Nov 20, 2017
1 parent 6272b13 commit b1bfddfeea58c2275cf5068a268ed765ed3e453c
View
@@ -0,0 +1,178 @@
# rrb3.py Library
import RPi.GPIO as GPIO
import time
class RRB3:
MOTOR_DELAY = 0.2
RIGHT_PWM_PIN = 14
RIGHT_1_PIN = 10
RIGHT_2_PIN = 25
LEFT_PWM_PIN = 24
LEFT_1_PIN = 17
LEFT_2_PIN = 4
SW1_PIN = 11
SW2_PIN = 9
LED1_PIN = 8
LED2_PIN = 7
OC1_PIN = 22
OC2_PIN = 27
OC2_PIN_R1 = 21
OC2_PIN_R2 = 27
TRIGGER_PIN = 18
ECHO_PIN = 23
left_pwm = 0
right_pwm = 0
pwm_scale = 0
old_left_dir = -1
old_right_dir = -1
def __init__(self, battery_voltage=9.0, motor_voltage=6.0, revision=2):
self.pwm_scale = float(motor_voltage) / float(battery_voltage)
if self.pwm_scale > 1:
print("WARNING: Motor voltage is higher than battery votage. Motor may run slow.")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.LEFT_PWM_PIN, GPIO.OUT)
self.left_pwm = GPIO.PWM(self.LEFT_PWM_PIN, 500)
self.left_pwm.start(0)
GPIO.setup(self.LEFT_1_PIN, GPIO.OUT)
GPIO.setup(self.LEFT_2_PIN, GPIO.OUT)
GPIO.setup(self.RIGHT_PWM_PIN, GPIO.OUT)
self.right_pwm = GPIO.PWM(self.RIGHT_PWM_PIN, 500)
self.right_pwm.start(0)
GPIO.setup(self.RIGHT_1_PIN, GPIO.OUT)
GPIO.setup(self.RIGHT_2_PIN, GPIO.OUT)
GPIO.setup(self.LED1_PIN, GPIO.OUT)
GPIO.setup(self.LED2_PIN, GPIO.OUT)
GPIO.setup(self.OC1_PIN, GPIO.OUT)
if revision == 1:
self.OC2_PIN = self.OC2_PIN_R1
else:
self.OC2_PIN = self.OC2_PIN_R2
GPIO.setup(self.OC2_PIN_R2, GPIO.OUT)
GPIO.setup(self.SW1_PIN, GPIO.IN)
GPIO.setup(self.SW2_PIN, GPIO.IN)
GPIO.setup(self.TRIGGER_PIN, GPIO.OUT)
GPIO.setup(self.ECHO_PIN, GPIO.IN)
def set_motors(self, left_pwm, left_dir, right_pwm, right_dir):
if self.old_left_dir != left_dir or self.old_right_dir != right_dir:
self.set_driver_pins(0, 0, 0, 0) # stop motors between sudden changes of direction
time.sleep(self.MOTOR_DELAY)
self.set_driver_pins(left_pwm, left_dir, right_pwm, right_dir)
self.old_left_dir = left_dir
self.old_right_dir = right_dir
def set_driver_pins(self, left_pwm, left_dir, right_pwm, right_dir):
self.left_pwm.ChangeDutyCycle(left_pwm * 100 * self.pwm_scale)
GPIO.output(self.LEFT_1_PIN, left_dir)
GPIO.output(self.LEFT_2_PIN, not left_dir)
self.right_pwm.ChangeDutyCycle(right_pwm * 100 * self.pwm_scale)
GPIO.output(self.RIGHT_1_PIN, right_dir)
GPIO.output(self.RIGHT_2_PIN, not right_dir)
def forward(self, seconds=0, speed=1.0):
self.set_motors(speed, 0, speed, 0)
if seconds > 0:
time.sleep(seconds)
self.stop()
def stop(self):
self.set_motors(0, 0, 0, 0)
def reverse(self, seconds=0, speed=1.0):
self.set_motors(speed, 1, speed, 1)
if seconds > 0:
time.sleep(seconds)
self.stop()
def left(self, seconds=0, speed=0.5):
self.set_motors(speed, 0, speed, 1)
if seconds > 0:
time.sleep(seconds)
self.stop()
def right(self, seconds=0, speed=0.5):
self.set_motors(speed, 1, speed, 0)
if seconds > 0:
time.sleep(seconds)
self.stop()
def step_forward(self, delay, num_steps):
for i in range(0, num_steps):
self.set_driver_pins(1, 1, 1, 0)
time.sleep(delay)
self.set_driver_pins(1, 1, 1, 1)
time.sleep(delay)
self.set_driver_pins(1, 0, 1, 1)
time.sleep(delay)
self.set_driver_pins(1, 0, 1, 0)
time.sleep(delay)
self.set_driver_pins(0, 0, 0, 0)
def step_reverse(self, delay, num_steps):
for i in range(0, num_steps):
self.set_driver_pins(1, 0, 1, 0)
time.sleep(delay)
self.set_driver_pins(1, 0, 1, 1)
time.sleep(delay)
self.set_driver_pins(1, 1, 1, 1)
time.sleep(delay)
self.set_driver_pins(1, 1, 1, 0)
time.sleep(delay)
self.set_driver_pins(0, 0, 0, 0)
def sw1_closed(self):
return not GPIO.input(self.SW1_PIN)
def sw2_closed(self):
return not GPIO.input(self.SW2_PIN)
def set_led1(self, state):
GPIO.output(self.LED1_PIN, state)
def set_led2(self, state):
GPIO.output(self.LED2_PIN, state)
def set_oc1(self, state):
GPIO.output(self.OC1_PIN, state)
def set_oc2(self, state):
GPIO.output(self.OC2_PIN, state)
def _send_trigger_pulse(self):
GPIO.output(self.TRIGGER_PIN, True)
time.sleep(0.0001)
GPIO.output(self.TRIGGER_PIN, False)
def _wait_for_echo(self, value, timeout):
count = timeout
while GPIO.input(self.ECHO_PIN) != value and count > 0:
count -= 1
def get_distance(self):
self._send_trigger_pulse()
self._wait_for_echo(True, 10000)
start = time.time()
self._wait_for_echo(False, 10000)
finish = time.time()
pulse_len = finish - start
distance_cm = pulse_len / 0.000058
return distance_cm
def cleanup(self):
GPIO.cleanup()
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -0,0 +1,61 @@
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style>
.controls {
width: 150px;
font-size: 32pt;
text-align: center;
padding: 15px;
background-color: green;
color: white;
}
</style>
<script>
function sendCommand(command)
{
$.get('/', {command: command});
}
function keyPress(event){
code = event.keyCode;
if (code == 119) {
sendCommand('f');
}
else if (code == 97) {
sendCommand('l');
}
else if (code == 115) {
sendCommand('s');
}
else if (code == 100) {
sendCommand('r');
}
else if (code == 122) {
sendCommand('b');
}
}
$(document).keypress(keyPress);
</script>
</head>
<body>
<h1>Web Rover</h1>
<table align="center">
<tr><td></td><td class="controls" onClick="sendCommand('f');">W</td><td></td></tr>
<tr><td class="controls" onClick="sendCommand('l');">A</td>
<td class="controls" onClick="sendCommand('s');">S</td>
<td class="controls" onClick="sendCommand('r');">D</td>
</tr>
<tr><td></td><td class="controls" onClick="sendCommand('b');">Z</td><td></td></tr>
</table>
</body>
</html>
@@ -0,0 +1,40 @@
# Attach: SR-04 Range finder, switch on SW1, and of course motors.
# The switch SW2 stops and starts the robot
from rrb3 import *
import time, random
BATTERY_VOLTS = 9
MOTOR_VOLTS = 6
rr = RRB3(BATTERY_VOLTS, MOTOR_VOLTS)
# if you dont have a switch, change the value below to True
running = False
def turn_randomly():
turn_time = random.randint(1, 3)
if random.randint(1, 2) == 1:
rr.left(turn_time, 0.5) # turn at half speed
else:
rr.right(turn_time, 0.5)
rr.stop()
try:
while True:
distance = rr.get_distance()
print(distance)
if distance < 50 and running:
turn_randomly()
if running:
rr.forward(0)
if rr.sw2_closed():
running = not running
if not running:
rr.stop()
time.sleep(0.2)
finally:
print("Exiting")
rr.cleanup()
@@ -0,0 +1,14 @@
# 04_distance.py
# Uses the ultrasonic rangefinder to measure distance
from rrb3 import *
import time
rr = RRB3()
print("Press CTRL-c to quit the program")
while True:
dist = rr.get_distance()
print(dist)
time.sleep(0.5)
@@ -0,0 +1,31 @@
# 04_movement.py
# Uses the ultrasonic rangefinder to detect movement
from rrb3 import *
import time
threshold = 10
rr = RRB3()
reference = rr.get_distance()
rr.set_led1(0)
rr.set_led2(0)
print("alarm activated")
print("Press CTRL-c to quit the program")
while True:
time.sleep(0.3)
reading = rr.get_distance()
difference = abs(reading - reference)
if difference > threshold:
print("Movement detected")
for a in range(5):
rr.set_led1(1)
rr.set_led2(1)
time.sleep(0.3)
rr.set_led1(0)
rr.set_led2(0)
time.sleep(0.3)
reference = reading
@@ -0,0 +1,67 @@
# 08_manual_robot.py
# Use the arrow keys to direct the robot
from rrb3 import *
import sys
import tty
import termios
rr = RRB3(9.0, 6.0) # battery, motor
UP = 0
DOWN = 1
RIGHT = 2
LEFT = 3
print("Use the arrow keys to move the robot")
print("Press CTRL-c to quit the program")
# These functions allow the program to read your keyboard
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if ch == '0x03':
raise KeyboardInterrupt
return ch
def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
return c1
c3 = getchar()
return ord(c3) - 65 # 0=Up, 1=Down, 2=Right, 3=Left arrows
# This will control the movement of your robot and display on your screen
try:
while True:
keyp = readkey()
if keyp == UP:
rr.forward(1)
print 'forward'
elif keyp == DOWN:
rr.reverse(1)
print 'backward'
elif keyp == RIGHT:
rr.right(1)
print 'clockwise'
elif keyp == LEFT:
rr.left(1)
print 'anti clockwise'
elif keyp == LEFT:
rr.left(1)
print 'anti clockwise'
elif ord(keyp) == 3:
break
except KeyboardInterrupt:
GPIO.cleanup()
Oops, something went wrong.

0 comments on commit b1bfddf

Please sign in to comment.