From df06dc59cc0a01807e6e8434849cc8acb8761e91 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 19 May 2020 15:19:18 +0200 Subject: [PATCH] ev3/snippets: bluetooth: update rover example --- .../ev3/bluetooth_read_spike/connection.py | 6 +++- snippets/ev3/bluetooth_read_spike/main.py | 3 +- snippets/ev3/bluetooth_read_spike/rover.py | 28 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 snippets/ev3/bluetooth_read_spike/rover.py diff --git a/snippets/ev3/bluetooth_read_spike/connection.py b/snippets/ev3/bluetooth_read_spike/connection.py index 6e5efce..d30eb74 100644 --- a/snippets/ev3/bluetooth_read_spike/connection.py +++ b/snippets/ev3/bluetooth_read_spike/connection.py @@ -28,7 +28,11 @@ def get_bluetooth_rfcomm_socket(address, channel): class SpikePrimeStreamReader(): def __init__(self, address): - self.sock = get_bluetooth_rfcomm_socket(address, 1) + try: + self.sock = get_bluetooth_rfcomm_socket(address, 1) + except OSError as e: + print("Turn on Bluetooth on the EV3 and on SPIKE.") + raise e self._values = None diff --git a/snippets/ev3/bluetooth_read_spike/main.py b/snippets/ev3/bluetooth_read_spike/main.py index e16fc5a..901f215 100644 --- a/snippets/ev3/bluetooth_read_spike/main.py +++ b/snippets/ev3/bluetooth_read_spike/main.py @@ -13,6 +13,5 @@ spike = SpikePrimeStreamReader('F4:84:4C:AA:C8:A4') # Now you can simply read values! for i in range(100): - print(spike.orientation()) - print(spike.device('B')) + print(spike.values()) wait(100) diff --git a/snippets/ev3/bluetooth_read_spike/rover.py b/snippets/ev3/bluetooth_read_spike/rover.py new file mode 100644 index 0000000..f6f8dc2 --- /dev/null +++ b/snippets/ev3/bluetooth_read_spike/rover.py @@ -0,0 +1,28 @@ +#!/usr/bin/env pybricks-micropython +from pybricks.hubs import EV3Brick +from pybricks.tools import wait +from pybricks.ev3devices import Motor +from pybricks.robotics import DriveBase +from pybricks.parameters import Port + +from connection import SpikePrimeStreamReader + +# Beep! +ev3 = EV3Brick() +ev3.speaker.beep() + +# Create the connection. See README.md to find the address for your SPIKE hub. +spike = SpikePrimeStreamReader('F4:84:4C:AA:C8:A4') + +# Initialize the motors and drive base +left_motor = Motor(Port.B) +right_motor = Motor(Port.C) +robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104) + +while True: + # Read the orientation + yaw, pitch, roll = spike.orientation() + + # Set speed and turn rate based on orientation + robot.drive(-pitch*6, roll*2) + wait(20)