def __init__(self, loop: asyncio.AbstractEventLoop, datadir = "data"): self.datadir = datadir self.rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) self.oakD = OakD() self.oakD.startDevice() self.datadir = f"v2-{int(time.time())}" os.mkdir(self.datadir)
def __init__(self, loop: asyncio.AbstractEventLoop, datadir: str = "data"): self.datadir = datadir self.rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) self.camera = PiCamera(resolution=(320, 240), framerate=30) self.gps = gps(mode=WATCH_ENABLE | WATCH_NEWSTYLE) self.busy = False try: self.interp = tf.lite.Interpreter("steer.tflite") self.interp.allocate_tensors() except Exception: pass
async def init_sphero(self): # init the rvr # TODO fix asyncio problems and use the asyncio version logging.info("RVR: connecting") self.dal = SerialAsyncDal(asyncio.get_running_loop()) self.rvr = SpheroRvrAsync(self.dal) # fix rvr blocking all default logging after init... null_handler = [ h for h in logging.getLogger().handlers if h.name == "null_handler" ][0] logging.getLogger().removeHandler(null_handler) logging.info("RVR: waking up") await self.rvr.wake() logging.info("RVR: resetting yaw") await self.rvr.reset_yaw() logging.info("RVR: init done")
import os import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal loop = asyncio.get_event_loop() rvr = SpheroRvrAsync( dal=SerialAsyncDal( loop ) ) async def main(): """ This program has RVR drive around in different directions using the function drive_with_yaw_normalized. This is a newer command, and uses some different conventions from what you may be accustomed to if you have used the initial release of this SDK. In Sphero conventions, "heading" is CCW positive, matching a compass, while "yaw" is CW positive, as a rotation around the (vertical) Z axis of the robot following the right hand rule. This yaw convention was selected to match the ISO 8855 standard: "Road Vehicles - Vehicle dynamics and road-holding ability -- Vocabulary" Because of this difference between heading and yaw, headings will need to be converted into yaw angles IF you want to switch commands in existing projects.
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal from sphero_sdk import RawMotorModesEnum loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) async def main(): """ This program demonstrates how to get motor thermal protection status, in the event RVR's motors have already been stopped to prevent overheating. This can be used to check if the motors are no longer in a thermal protection state. RVR does not need to be awake in order to run this operation. """ response = await rvr.get_motor_thermal_protection_status() print('Thermal protection status response', response) # Delay to allow the callback to be invoked await asyncio.sleep(2) if __name__ == '__main__': try: loop.run_until_complete(main())
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import Colors from sphero_sdk import RvrLedGroups from sphero_sdk import RestfulAsyncDal loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=RestfulAsyncDal( domain='0.0.0.0', # Add your raspberry-pi's IP address here port=2010 # The port opened by the npm server is always 2010 )) async def main(): """ This program demonstrates how to set multiple LEDs. """ await rvr.wake() # Give RVR time to wake up await asyncio.sleep(2) await rvr.set_all_leds(led_group=RvrLedGroups.all_lights.value, led_brightness_values=[ color for _ in range(10)
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal from sphero_sdk import RvrStreamingServices loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) async def imu_handler(imu_data): print('IMU data response: ', imu_data) async def color_detected_handler(color_detected_data): print('Color detection data response: ', color_detected_data) async def main(): """ This program demonstrates how to disable all notifications and active commands. """ await rvr.wake() # Give RVR time to wake up
import rclpy from rclpy.node import Node from std_msgs.msg import String from std_msgs.msg import Int8MultiArray from geometry_msgs.msg import Vector3 import time import json debug = True loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) # sensor variable initialization imu_global = {} color_global = {} accelerometer_global = {} ambient_global = {} encoder_global = {} received = 0x00 # received byte - fully received at 0x1f # send stuff variable initialization # sensor publisher class MinimalPublisher(Node):
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) # Flag used to indicate that calibration is complete calibration_completed = False # Handler for completion of calibration async def on_calibration_complete_notify_handler(response): global calibration_completed print('Calibration complete, response:', response) calibration_completed = True async def main(): """ This program demonstrates the magnetometer calibration to find north. """ global calibration_completed
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import RestfulAsyncDal from sphero_sdk import RvrStreamingServices loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=RestfulAsyncDal( domain='0.0.0.0', # Add your raspberry-pi's IP address here port=2010 # The port opened by the npm server is always 2010 )) async def color_detected_handler(color_detected_data): print('Color detection data response: ', color_detected_data) async def main(): """ This program uses the color sensor on RVR (located on the down side of RVR, facing the floor) to report colors detected. To exit program, press <CTRL-C> """ await rvr.wake() # Give RVR time to wake up await asyncio.sleep(2)
def __init__( self, loop ): self.rvr = SpheroRvrAsync( dal=SerialAsyncDal( loop ) )
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import RestfulAsyncDal loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=RestfulAsyncDal( domain='0.0.0.0', # Add your raspberry-pi's IP address here port=2010 # The port opened by the npm server is always 2010 )) async def infrared_message_received_handler(response): print('Response data for IR message received:', response) async def main(): """ This program sets up RVR to communicate with another robot, e.g. BOLT, capable of infrared communication. To try this out, write a script for your other robot that a) broadcasts on the corresponding channel that RVR is set to listen to [in this case channel 0] and b) listens on the channel which RVR sends messages on [in this case channel 3] """ await rvr.wake()
from geometry_msgs.msg import PoseWithCovariance from geometry_msgs.msg import Pose from geometry_msgs.msg import TwistWithCovariance from geometry_msgs.msg import Twist from geometry_msgs.msg import Point from geometry_msgs.msg import Quaternion from geometry_msgs.msg import Vector3 from nav_msgs.msg import Odometry from std_msgs.msg import Header MAX_SENSOR_VALUE = 2**31 loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop # device='/dev/ttyTHS1', )) logging.basicConfig(level=logging.DEBUG) num_msgs_received = {} position = Point() orientation = Quaternion() pose = Pose( position=position, orientation=orientation, ) pose_with_covariance = PoseWithCovariance(pose=pose, ) linear = Vector3()
import os import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal from sphero_sdk import InfraredCodes from sphero_sdk import RawMotorModesEnum loop = asyncio.get_event_loop() rvr = SpheroRvrAsync( dal=SerialAsyncDal( loop ) ) async def main(): """ This program sets up RVR to communicate with another robot, e.g. BOLT, capable of infrared communication. """ await rvr.wake() # Give RVR time to wake up await asyncio.sleep(2) await rvr.start_robot_to_robot_infrared_broadcasting( far_code=InfraredCodes.one.value,
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import RestfulAsyncDal from sphero_sdk import RvrStreamingServices loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=RestfulAsyncDal( domain='0.0.0.0', # Add your raspberry-pi's IP address here port=2010)) async def imu_handler(imu_data): print('IMU data response: ', imu_data) async def ambient_light_handler(ambient_light_data): print('Ambient data response:', ambient_light_data) async def velocity_handler(velocity_data): print('Velocity data response:', velocity_data) async def main(): """ This program has RVR drive around in different directions using the function raw_motors.
import sys import os sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))) import RPi.GPIO as GPIO import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal import time loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) GPIO.setmode(GPIO.BCM) right_trigger = 20 right_echo = 21 left_trigger = 27 left_echo = 18 right_slesh = 15 left_slesh = 15 GPIO.setup(left_trigger, GPIO.OUT) GPIO.setup(left_echo, GPIO.IN) GPIO.setup(right_trigger, GPIO.OUT) GPIO.setup(right_echo, GPIO.IN) def distance_left():
import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import asyncio from sphero_sdk import SpheroRvrAsync from sphero_sdk import SerialAsyncDal from sphero_sdk import RvrStreamingServices loop = asyncio.get_event_loop() rvr = SpheroRvrAsync(dal=SerialAsyncDal(loop)) async def color_detected_handler(color_detected_data): print('Color detection data response: ', color_detected_data) async def main(): """ This program demonstrates how to use the color sensor on RVR (located on the down side of RVR, facing the floor) to report colors detected. To exit program, press <CTRL-C> """ await rvr.wake() # Give RVR time to wake up await asyncio.sleep(2) await rvr.enable_color_detection(is_enabled=True) await rvr.sensor_control.add_sensor_data_handler(