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)
def VibrateMotor(): joystick.run_angle(1400, 180) def CompletionVibrate(): indicator.run_time(1400, 700) 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')
rightMotor.run(Motor_input) leftMotor.run(Motor_input) if error < 10: print('Over!') reset() break #Main Code travel = waiting() while travel == 'on': #Checking for obstacles if dist.distance() > 150: Motor_input = -150 if dist.distance() <= 150: Motor_input = -10 #Serial Commands from cam if uart.waiting() >= 1: data = uart.read() if data.decode('utf-8') == 'y': print('Fire') wait(1000) release() break #Keeps Motors rolling rightMotor.run(Motor_input) leftMotor.run(Motor_input) cleanUp()
SystemLink.Put_SL('Start27', 'BOOLEAN', 'false') if touch1.pressed() == True: throw_ball() SystemLink.Put_SL('Start28', 'BOOLEAN', 'true') if len(ev3.buttons.pressed()) > 0: print("Catching Ball") wait(3000) motorD.track_target(40) # collect data from serial port and move armw while (1): serial.clear() ang1 = "" ang2 = "" curr_byte = serial.read() #print(curr_byte) # decode instructions once b'a' is seen if curr_byte == b'a': # get angles curr_byte = serial.read() while (curr_byte != b'b'): ang1 += curr_byte.decode('utf-8') curr_byte = serial.read() curr_byte = serial.read() while (curr_byte != b'y' and curr_byte != b'n'): ang2 += curr_byte.decode('utf-8') curr_byte = serial.read() if curr_byte == b'y': # move motor and close gripper
direction = 1 print('????') uart = UARTDevice(Port.S1, 9600, timeout=10000) wait(500) uart.write('a') while uart.waiting() == 0: wait(10) curr = "" previous = 'q' uart.write('q') while True: xangle = xmotor.angle() yangle = ymotor.angle() holder = uart.read(1) #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' elif (yangle < 20 and yangle > -20) and xangle < -20: curr = 's' elif (yangle < -20) and xangle < -20: curr = 't' elif (yangle < -20) and (xangle < 20 and xangle > -20): curr = 'u' elif (yangle < -20) and (xangle > 20): curr = 'v'
from pybricks.robotics import DriveBase from pybricks.iodevices import AnalogSensor, UARTDevice brick.sound.beep() minimotor = Motor(Port.C) leftmotor = Motor(Port.A, Direction.CLOCKWISE) rightmotor = Motor(Port.D, Direction.CLOCKWISE) robot = DriveBase(leftmotor, rightmotor, 40, 200) button = TouchSensor(Port.S1) color = ColorSensor(Port.S2) #sense = AnalogSensor(Port.S3, False) uart = UARTDevice(Port.S3, 9600, timeout=10000) direction = 1 #initial waiting phase: handshake = "f" while uart.waiting() == 0: wait(10) handshake = uart.read(1) print(handshake) while True: if uart.waiting() != 0: print(uart.read(1)) wait(0.1)
#!/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()
fobj = open(filename, 'w') for up in range(20): print(up) watch.reset() watch.resume() while watch.time() < 200: robot.drive(20, 0) robot.drive(0, 0) for side in range(80): watchside.reset() watchside.resume() while watchside.time() < 150: minimotor.run(direction * -50) colorcheck(side, up, direction) direction *= -1 while True: colorcheck() direction = 1 if uart.waiting() != 0: holder = uart.read(1).decode("utf-8") print(holder) if holder == 'q': #forward motorspeed = 0 minispeed = 0 #robot.drive(motorspeed,0) #minimotor.run(minispeed) wait(0.1) fobj.close()
uart = UARTDevice(Port.S4, 9600, timeout=200) paddle = Motor(Port.C) belt = Motor(Port.D) pusher = Motor(Port.B) ev3.speaker.beep() wait(500) paddle_closed = 0 message = '' while True: belt.dc(23) pusher.dc(10) try: # uart.write('PLEASE WORK'.encode()) # wait(50) if(uart.waiting() > 1): message = uart.read(1) # print(message) # print(type(message)) # message = message.decode('utf-8') # not too many options on pybricks micropython print("The Message is: ", message) except: # message = uart.read() print("FAILED MESSAGE: ", message) print(type(message)) if(message == b'2' and paddle_closed == 0): paddle.run_angle(100, 90, stop_type = Stop.HOLD, wait = False) belt.run_time(100, 2500) paddle.run_angle(100, -90, stop_type = Stop.HOLD, wait = False) paddle_closed = 1
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") pass try: if (time.time() > (brickQueue[0] + 9500)): brickQueue.pop(0) if isRed == True: if currPos == 180: bucketMotor.run_target(150, redPos) currPos = redPos else: pass