Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the example code compatible with python3 #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.pyc
settings.json

.DS_Store
4 changes: 2 additions & 2 deletions chapter10/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def convert_speed(self, speed):

def set_left(self, speed):
mode, output_speed = self.convert_speed(speed)
self.left_motor.setSpeed(output_speed)
self.left_motor.setSpeed(int(output_speed))
self.left_motor.run(mode)

def set_right(self, speed):
mode, output_speed = self.convert_speed(speed)
self.right_motor.setSpeed(output_speed)
self.right_motor.setSpeed(int(output_speed))
self.right_motor.run(mode)

def stop_motors(self):
Expand Down
3 changes: 2 additions & 1 deletion chapter10/servo_type_position.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from Raspi_MotorHAT.Raspi_PWM_Servo_Driver import PWM
import atexit
from builtins import input

pwm = PWM(0x6f)
# This sets the timebase for it all
Expand Down Expand Up @@ -33,6 +34,6 @@ def stop():
import time

while (True):
position = int(raw_input("Type your position in degrees (90 to -90, 0 is middle): "))
position = int(input("Type your position in degrees (90 to -90, 0 is middle): "))
end_step = convert_degrees_to_pwm(position)
pwm.setPWM(0, 0, end_step)
1 change: 1 addition & 0 deletions chapter11/avoid_behavior.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from robot import Robot, NoDistanceRead
from time import sleep

Expand Down
4 changes: 2 additions & 2 deletions chapter11/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def set_left(self, speed):
if not self.drive_enabled:
return
mode, output_speed = self.convert_speed(speed)
self.left_motor.setSpeed(output_speed)
self.left_motor.setSpeed(int(output_speed))
self.left_motor.run(mode)

def set_right(self, speed):
if not self.drive_enabled:
return
mode, output_speed = self.convert_speed(speed)
self.right_motor.setSpeed(output_speed)
self.right_motor.setSpeed(int(output_speed))
self.right_motor.run(mode)

def stop_motors(self):
Expand Down
1 change: 1 addition & 0 deletions chapter11/simple_avoid_behavior.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from robot import Robot, NoDistanceRead
from time import sleep

Expand Down
11 changes: 6 additions & 5 deletions chapter11/test_hcsr04.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import print_function
import time
from gpiozero import DigitalInputDevice, DigitalOutputDevice

# Setup devices, an input device and an output device, with pin numbers for the sensors.
print "Prepare GPIO pins"
print("Prepare GPIO pins")

# Left sensor
left_trigger = DigitalOutputDevice(17)
Expand All @@ -17,7 +18,7 @@
right_trigger.value = False

# wait a little, to iron out spurious responses.
print "Warm up time"
print("Warm up time")
time.sleep(0.5)

def make_measurement(trig_device, echo_device):
Expand All @@ -37,15 +38,15 @@ def make_measurement(trig_device, echo_device):
pulse_start = time.time()
# We ran out of time here.
if pulse_start > time_out:
print "timed out - missed pulse start"
print("timed out - missed pulse start")
return 100

# Now we wait for the echo_device pin to stop being 1, going from high, to low, the end of the pulse.
pulse_end = time.time()
while echo_device.pin.state == 1:
pulse_end = time.time()
if pulse_end > time_out:
print "timed out - pulse end too long"
print("timed out - pulse end too long")
return 100

# The duration is the time between the start and end of pulse in seconds.
Expand All @@ -61,6 +62,6 @@ def make_measurement(trig_device, echo_device):
# Make our measurements and print them
left_distance = make_measurement(left_trigger, left_echo)
right_distance = make_measurement(right_trigger, right_echo)
print "Left: ", left_distance, "cm", "Right:", right_distance
print("Left: ", left_distance, "cm", "Right:", right_distance)
# Sleep a bit before making another.
time.sleep(0.5)
1 change: 1 addition & 0 deletions chapter12/drive_distance_behavior.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from robot import Robot, EncoderCounter
from pid_controller import PIController
import time
Expand Down
11 changes: 6 additions & 5 deletions chapter12/drive_square_behavior.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from robot import Robot, EncoderCounter
from pid_controller import PIController
import time
Expand Down Expand Up @@ -52,7 +53,7 @@ def drive_distances(bot, left_distance, right_distance, speed=80):
set_secondary(secondary_speed + adjustment)
secondary_encoder.set_direction(math.copysign(1, secondary_speed+adjustment))
# Some debug
print "Primary c:{:.2f} ({:.2f} mm)\tSecondary c:{:.2f} ({:.2f} mm) t:{:.2f} e:{:.2f} s:{:.2f} adjustment: {:.2f}".format(
print("Primary c:{:.2f} ({:.2f} mm)\tSecondary c:{:.2f} ({:.2f} mm) t:{:.2f} e:{:.2f} s:{:.2f} adjustment: {:.2f}".format(
primary_encoder.pulse_count,
primary_encoder.distance_in_mm(),
secondary_encoder.pulse_count,
Expand All @@ -61,11 +62,11 @@ def drive_distances(bot, left_distance, right_distance, speed=80):
error,
secondary_speed,
adjustment
)
))

# Stop the primary if we need to
if abs(primary_encoder.pulse_count) >= abs(primary_distance):
print "primary stop"
print("primary stop")
set_primary(0)
secondary_speed = 0

Expand All @@ -79,11 +80,11 @@ def drive_arc(bot, turn_in_degrees, radius, speed=80):
else:
left_radius = radius + half_width_ticks
right_radius = radius - half_width_ticks
print "Arc left radius {:.2f}, right_radius {:.2f}".format(left_radius, right_radius)
print("Arc left radius {:.2f}, right_radius {:.2f}".format(left_radius, right_radius))
radians = math.radians(abs(turn_in_degrees))
left_distance = int(left_radius * radians)
right_distance = int(right_radius * radians)
print "Arc left distance {}, right_distance {}".format(left_distance, right_distance)
print("Arc left distance {}, right_distance {}".format(left_distance, right_distance))
drive_distances(bot, left_distance, right_distance, speed=speed)

bot = Robot()
Expand Down
4 changes: 2 additions & 2 deletions chapter12/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def set_left(self, speed):
if not self.drive_enabled:
return
mode, output_speed = self.convert_speed(speed)
self.left_motor.setSpeed(output_speed)
self.left_motor.setSpeed(int(output_speed))
self.left_motor.run(mode)

def set_right(self, speed):
if not self.drive_enabled:
return
mode, output_speed = self.convert_speed(speed)
self.right_motor.setSpeed(output_speed)
self.right_motor.setSpeed(int(output_speed))
self.right_motor.run(mode)

def stop_motors(self):
Expand Down
13 changes: 7 additions & 6 deletions chapter12/straight_line_drive.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import print_function
from robot import Robot
from pid_controller import PIController
import time

bot = Robot()
stop_at_time = time.time() + 60
stop_at_time = time.time() + 5

speed = 80
bot.set_left(speed)
Expand All @@ -19,10 +20,10 @@
# Get the speed
adjustment = pid.get_value(error)
right_speed = int(speed + adjustment)
print "left", left, \
"right", right, \
"right_speed:", right_speed, \
"error:", error, \
"adjustment: %.2f" % adjustment
print("left", left,
"right", right,
"right_speed:", right_speed,
"error:", error,
"adjustment: %.2f" % adjustment)
# Appy it to the right motor
bot.set_right(right_speed)
5 changes: 3 additions & 2 deletions chapter12/test_distance_travelled.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from robot import Robot
import time
import math
Expand All @@ -16,6 +17,6 @@ def ticks_to_mm(ticks):
bot.set_right(90)

while time.time() < stop_at_time:
print "Left:", ticks_to_mm(bot.left_encoder.pulse_count), \
"Right:", ticks_to_mm(bot.right_encoder.pulse_count)
print("Left:", ticks_to_mm(bot.left_encoder.pulse_count), \
"Right:", ticks_to_mm(bot.right_encoder.pulse_count))
time.sleep(0.05)
2 changes: 1 addition & 1 deletion chapter12/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ def when_changed(self):
bot.set_right(90)

while time.time() < stop_at_time:
print "Left:", left_encoder.pulse_count, "Right:", right_encoder.pulse_count
print("Left:", left_encoder.pulse_count, "Right:", right_encoder.pulse_count)
time.sleep(0.05)
Loading