def main(): uartComm = UARTDevice(Port.S2, 9600, 100) uartComm.clear() xlimits = [-310, 310] while True: reflection = light.reflection() try: data_raw = uartComm.read(24).decode().replace('B', '') data = ujson.loads(data_raw) except Exception as e: uartComm.clear() print(e) print('Read Failed') data = {'x': 0, 'y': 0} #need this or robot.drive throws an error try: signal = reading(reflection) uartComm.write(signal) print(signal) #prints json signal except Exception as e: print(e) print("Write Failed") #print(data['x'], data['y']) robot.drive(data['y'] / 2, 0) print(horz.angle()) horiz_dist = data['x'] * (360 / 90) constrain(horiz_dist, xlimits) horz.run_target(100, horiz_dist)
#!/usr/bin/env pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.iodevices import UARTDevice from pybricks.parameters import Port from pybricks.media.ev3dev import SoundFile # Initialize the EV3 ev3 = EV3Brick() # Initialize sensor port 2 as a uart device ser = UARTDevice(Port.S2, baudrate=115200) # Write some data ser.write(b'\r\nHello, world!\r\n') # Play a sound while we wait for some data for i in range(3): ev3.speaker.play_file(SoundFile.HELLO) ev3.speaker.play_file(SoundFile.GOOD) ev3.speaker.play_file(SoundFile.MORNING) print("Bytes waiting to be read:", ser.waiting()) # Read all data received while the sound was playing data = ser.read_all() print(data)
uart.clear() while True: while not (xBut.pressed() | yBut.pressed()): wait(100) if uart.waiting() > 0: msg = uart.read(1) uart.clear() #should clear automatically, but needed some help msg = str(msg) if msg[2] == 'T': VibrateMotor() print('|', end='') elif msg[2] == 'F': print('.', end='') elif msg[2] == 'C': CompletionVibrate() while (xBut.pressed() | yBut.pressed()): if xBut.pressed(): uart.write('x') #send uart 'x' #print('wrote x') elif yBut.pressed(): uart.write('y') #send uart 'y' #print('wrote y') print() if uart.waiting() > 0: VibrateMotor() uart.read(uart.waiting()) wait(500)
SoundFile, ImageFile, Align) from pybricks.tools import print, wait, StopWatch from pybricks.robotics import DriveBase from pybricks.iodevices import UARTDevice brick.sound.beep() ymotor = Motor(Port.B, Direction.CLOCKWISE) xmotor = Motor(Port.A, Direction.COUNTERCLOCKWISE) direction = 1 uart = UARTDevice(Port.S3, 9600, timeout=10000) wait(500) uart.write('a') print("Done") curr = "" previous = 'q' uart.write('q') while True: xangle = xmotor.angle() yangle = ymotor.angle() print(str(xangle) + " " + str(yangle)) if yangle > 20 and xangle > 20: curr = 'x' elif yangle > 20 and (xangle < 20 and xangle > -20): curr = 'y' elif yangle > 20 and (xangle < -20): curr = 'z'
baby_car = Motor(Port.A) # # Setup Sensors light_sensor = AnalogSensor(Port.S1, False) light_sensor.voltage() #calibrate light sensor whiteLight = lightCalibration() ev3.speaker.set_volume(100) ev3.speaker.say("Calibration Complete") while True: # ......read in joystick controls angle1, angle2 = parseAngle() #print joystick controlls print("ANGLES: ", angle1, ", ", angle2) #.....drive car baby_car_drive(angle1) big_car_drive(angle2) #.....read sensor lightData = light_sensor.voltage() #wait(10) #.....send sensor data print('light: ' + str(lightData)) isLine = lineDetect(lightData, whiteLight) uart.write(str(isLine)) print('isLine: ' + str(isLine)) #wait(10)
placerwaiting = -120 #angle value for placer in the waiting for brick position (placeholder) redAngleTarget = 300 #angle value for red mover to move red bricks to cup (placeholder) redStartAngle = 0 #rest position angle for red mover trashStartingAngle = 0 #waiting angle for trash mover (placeholder) trashchuckAngle = 50 #moving angle for trash mover (placeholder) while True: #to use: place a brick in the mover and then push the center button #if center button is pushed, move brick to sensing position and send something to pi to tell it to read if button.CENTER in brick.buttons(): placer.run_target(300, placedTarget, Stop.BRAKE, True) #target angle placeholder for now uart.write("r".encode()) #placeholder value if uart.waiting() >= 1: data = uart.read(1) print(data.decode('utf-8')) #if red, put in red bin if data == valIfRed: #might need to change these to run_angles depending on arm style redDudes.run_target(300, redAngleTarget, Stop.BRAKE, True) redDudes.run_target(300, redStartAngle, Stop.BRAKE, True) #reset the red mover arm #if not red, throw brick away if data == valIfNotRed: #might need to change these to run_angles depending on arm style
# Initializing the uart device uart2 = UARTDevice(Port.S1, 9600, timeout=2000) # Initializing all signals motorx = Motor(Port.A) motory = Motor(Port.B) sensor = ColorSensor(Port.S2) while True: text =uart2.read_all() if text == b'R': motorx.run(500) elif text == b'L': motorx.run(-500) elif text == b'N': motorx.run(0) elif text == b'F': motory.run(500) elif text == b'B': motory.run(-500) elif text == b'M': motory.run(0) # Reading out the sensor data and writing it over UART if not (sensor.color()) == Color.WHITE): h = 'W' elif (sensor.color() == Color.WHITE): h = 'H' print(h) uart2.write(h)
#!/usr/bin/env pybricks-micropython #brickrun -r -- pybricks-micropython from pybricks.hubs import EV3Brick from pybricks.tools import wait, StopWatch, DataLog from pybricks.parameters import Color, Port from pybricks.ev3devices import Motor from pybricks.iodevices import AnalogSensor, UARTDevice # Initialize the EV3 ev3 = EV3Brick() ev3.speaker.beep() uart = UARTDevice(Port.S1, 9600, timeout=2000) # short pins 5/6 (blue and yellow) uart.write('Test') wait(10) data = uart.read_all( ) #if you connect Pin 5 & 6 you should see Test on the screen ev3.screen.print(data) uart.write('Test') uart.waiting() # how many bytes on the port uart.read(uart.waiting()) # Turn the light off ev3.light.off()
xangle = motor1.angle() # Uses the Read positions to drive the system print('x=', xangle) print('y=', yangle) if xangle > 18: x = 'R' elif xangle < -18: x = 'L' else: x = 'N' if yangle > 18: y = 'F' elif yangle < -18: y = 'B' else: y = 'M' # Writes the signal uart.write(x) uart.write(y) fback = uart.read_all() # Runs the system if fback == b'W': motor3.run(1000) wait(0.01) elif fback == b'H': motor3.run(0) wait(.01)
while True: hopperMotor.dc(90) conveyorMotor.run(-35) reflect = light.reflection() if count % meanSize: reflectMeanNum += reflect else: reflectMean = reflectMeanNum / meanSize # Average every 10 readings reflectMean = reflectMeanNum / meanSize if reflectMean > 0.5: if wasBrick == 0: sendData = bytes('1', 'utf-8') uart.write(sendData) wasBrick = 1 brickQueue.append(time.time()) reflectMean = 0 reflectMeanNum = 0 else: if wasBrick == 1: wasBrick = 0 # Try to read from serial port try: getData = uart.read(1).decode('utf-8') isRed = True if getData == "0" else False print(isRed) except Exception: #print("Read Failed")