#!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile from pybricks.iodevices import UARTDevice # Write your program here ev3 = EV3Brick() ev3.speaker.beep() # define motors y_stick = Motor(Port.A) x_stick = Motor(Port.D) y_stick.reset_angle(0) x_stick.reset_angle(0) # define global variables x_last = 0 y_last = 0 ser = UARTDevice(Port.S2, 115200, timeout = None) def read_sensor(): # read sensor via serial # return value data = ser.read_all() return data def read_joystick(): x_pos = x_stick.angle() y_pos = y_stick.angle() print('actual angles', x_pos, ' ', y_pos) if x_pos > 10: x_dir = b'1' elif x_pos < -10: x_dir = b'2' else: x_dir = b'0' if y_pos > 10: y_dir = b'1' elif y_pos < -10: y_dir = b'2' else: y_dir = b'0' return x_dir, y_dir def haptic_fb(): x_stick.run_target(50, 0) y_stick.run_target(50, 0) def main(): while True: x_dir, y_dir = read_joystick() #print(x_dir, ' ', y_dir) while ser.waiting() < 1: wait(10) scanner_reading =(ser.read_all().decode("utf-8")) print("scanner_reading = ", scanner_reading) if scanner_reading == '1': haptic_fb() if ser.waiting() == 0: ser.write((x_dir)) ser.write((y_dir)) #print(x_dir, ' ', y_dir) ser.read_all() ser.write(b'00') main()