def __init__(self): # Connect the required equipement self.lm = ev3.LargeMotor('outB') self.rm = ev3.LargeMotor('outC') self.mm = ev3.MediumMotor() self.ir = ev3.InfraredSensor() self.ts = ev3.TouchSensor() self.cs = ev3.ColorSensor() self.screen = ev3.Screen() # Check if everything is attached check(self.lm.connected, 'My left leg is missing!') check(self.rm.connected, 'Right leg is not found!') check(self.mm.connected, 'My left arm is not connected!') check(self.ir.connected, 'My eyes, I can not see!') check(self.ts.connected, 'Touch sensor is not attached!') check(self.cs.connected, 'Color sensor is not responding!') # Reset the motors for m in (self.lm, self.rm, self.mm): m.reset() m.position = 0 m.stop_action = 'brake' self.draw_face() quote('initiating')
def tacto(done): infra = ev3.InfraredSensor() assert infra.connected touch = ev3.TouchSensor() assert touch.connected screen = ev3.Screen() sound = ev3.Sound() screen.draw.text((60, 40), 'Caminando') screen.update() while infra.proximity > 30: if done.is_set(): break time.sleep(0.1) done.set() #lo detiene de su locura haha ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED) ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.RED) screen.clear() screen.draw.text((60, 20), 'There is something is front of me') screen.update() while not touch_s.is_pressed: sound.speak("Where should i go next?").wait() time.sleep(0.5) done.set() #lo detiene de su locura haha
def feel(done): ir = ev3.InfraredSensor(); assert ir.connected ts = ev3.TouchSensor(); assert ts.connected screen = ev3.Screen() sound = ev3.Sound() screen.draw.text((60,40), 'Going for a walk') screen.update() while ir.proximity > 30: if done.is_set(): break time.sleep(0.1) done.set() #this will set it running in a circle ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED) ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.RED) screen.clear() screen.draw.text((60,20), 'There is something is front of me') screen.update() while not ts.is_pressed: sound.speak("Where should I go next?").wait() time.sleep(0.5) done.set() #will stop the circle dance
def generate_qr(self, token): print("token ", token) screen = ev3.Screen() #print("Creating QR image") qr = pyqrcode.create(token) qr.png('qrTest.png', scale=4)
def __init__(self): self.running = True # Creates the one and only Screen object and prepares a few Image objects. self.lcd_screen = ev3.Screen() # All of these images are exactly 178 by 128 pixels, the exact screen resolution # They are made by Lego and ship with the Lego Mindstorm EV3 Home Edition software self.eyes = Image.open( "/home/robot/csse120/assets/images/ev3_lego/eyes_neutral.bmp") self.angry_eyes = Image.open( "/home/robot/csse120/assets/images/ev3_lego/eyes_angry.bmp") self.puppy_dog_eyes = Image.open( "/home/robot/csse120/assets/images/ev3_lego/eyes_disappointed.bmp") self.sad_eyes = Image.open( "/home/robot/csse120/assets/images/ev3_lego/eyes_hurt.bmp") self.shifty_eyes = Image.open( "/home/robot/csse120/assets/images/ev3_lego/eyes_pinch_left.bmp") self.progress_0 = Image.open( "/home/robot/csse120/assets/images/ev3_lego/progress_bar_0.bmp") self.progress_50 = Image.open( "/home/robot/csse120/assets/images/ev3_lego/progress_bar_50.bmp") self.progress_100 = Image.open( "/home/robot/csse120/assets/images/ev3_lego/progress_bar_100.bmp") self.teary_eyes = Image.open( "/home/robot/csse120/assets/images/ev3_lego/eyes_tear.bmp")
def screen_example(): """Shows the commands to add a full screen image. There is more you could do with the screen, but not the focus here. Look online to learn more. https://sites.google.com/site/ev3python/learn_ev3_python/screen""" from PIL import Image lcd_screen = ev3.Screen() image = Image.open("/home/robot/csse120/assets/images/ev3_lego/eyes_angry.bmp") lcd_screen.image.paste(image, (0, 0)) lcd_screen.update()
def __init__(self): self.mqtt_client = None self.lcd = ev3.Screen() self.guess = '' self.word = ['p', 'r', 'o', 'j', 'e', 'c', 't'] self.hp = 3 self.index = 0 self.running = False self.robot = robo.Snatch3r() self.touch_sensor = ev3.TouchSensor()
def __init__(self): self.running = True # Creates the Screen object and prepares Image objects. self.lcd_screen = ev3.Screen() # All of these images are exactly 178 by 128 pixels, # the exact screen resolution # They are made by Lego and ship with the Lego Mindstorm # EV3 Home Edition software # I found the in m3_ir_events_with_the_screen and the assets self.eyes = Image.open("/home/robot/csse120/assets" "/images/ev3_lego/eyes_neutral.bmp") self.accept = Image.open("/home/robot/csse120/assets" "/images/ev3_lego/accept.bmp") self.decline = Image.open("/home/robot/csse120" "/assets/images/ev3_lego/Decline.bmp")
def main(): left_motor = ev3.LargeMotor('outC') right_motor = ev3.LargeMotor('outB') lcd = ev3.Screen() ts = ev3.TouchSensor('in2') assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor() us.mode = 'US-DIST-CM' units = us.units # reports 'cm' even though the sensor measures 'mm' errors = [] errors.append(0) while not ts.value(): # Stop program by pressing touch sensor button # US sensor will measure distance to the closest # object in front of it. separation = 20 distance = us.value() / 10 # convert mm to cm error = distance - separation err = errors.pop() de = error - err errors.append(err) errors.append(error) #Calculate the integral of error(sum of errors) sum_of_errors = reduce(lambda x, y: x + y, errors) print(str(distance) + " " + units) print(str(error) + " " + units) lcd.draw.text((10, 10), str(distance) + " " + units) lcd.draw.text((10, 10), str(error) + " " + units) if distance != separation: #This is an inconveniently large distance output = (50 * (error)) + (de) + (sum_of_errors) + 0 if (output > 1000): output = 990 elif (output < -1000): output = -990 left_motor.run_forever(speed_sp=output) right_motor.run_forever(speed_sp=output)
def __init__(self): self.mqtt_client = None self.lcd = ev3.Screen() self.num_active_dice = 5 self.max_die_value = 6 self.consecutive_correct = 0 self.dice_values = [0, 0, 0, 0, 0] self.dice_images = [Image.open('/home/robot/csse120/assets/images/dice/none.bmp'), Image.open('/home/robot/csse120/assets/images/dice/one.bmp'), Image.open('/home/robot/csse120/assets/images/dice/two.bmp'), Image.open('/home/robot/csse120/assets/images/dice/three.bmp'), Image.open('/home/robot/csse120/assets/images/dice/four.bmp'), Image.open('/home/robot/csse120/assets/images/dice/five.bmp'), Image.open('/home/robot/csse120/assets/images/dice/six.bmp'), Image.open('/home/robot/csse120/assets/images/dice/seven.bmp'), Image.open('/home/robot/csse120/assets/images/dice/eight.bmp'), Image.open('/home/robot/csse120/assets/images/dice/nine.bmp')] self.randomly_display_new_dice() self.running = False
def __init__(self): # Connect the required equipement self.lm = ev3.LargeMotor('outB') self.rm = ev3.LargeMotor('outC') self.mm = ev3.MediumMotor() self.ir = ev3.InfraredSensor() self.ts = ev3.TouchSensor() self.cs = ev3.ColorSensor() self.screen = ev3.Screen() # Reset the motors for m in (self.lm, self.rm, self.mm): m.reset() m.position = 0 m.stop_action = 'brake' self.draw_face() quote('initiating')
def approach(): left_motor = ev3.LargeMotor('outC') right_motor = ev3.LargeMotor('outB') lcd = ev3.Screen() ts = ev3.TouchSensor('in2') assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor() us.mode = 'US-DIST-CM' units = us.units # reports 'cm' even though the sensor measures 'mm' separation = 20 constant = 50 while not ts.value(): # Stop program by pressing touch sensor button # US sensor will measure distance to the closest # object in front of it. distance = us.value() / 10 # convert mm to cm error = distance - separation #Calculate the derivative of the errors print(str(distance) + " " + units) print(str(error) + " " + units) lcd.draw.text((10, 10), str(distance) + " " + units) lcd.draw.text((10, 10), str(error) + " " + units) if distance != separation: #This is an inconveniently large distance output = constant * error + 0 if (output > 1000): output = 990 else: output = constant * error + 0 left_motor.run_forever(speed_sp=output) right_motor.run_forever(speed_sp=output) else: Sound.tone(1000, 200).wait() #1000Hz for 0.2s sleep(0.5)
#!/usr/bin/env python3 #print("Loading...") import ev3dev.ev3 as ev3 import ev3dev.fonts import urllib.request import time m = ev3.LargeMotor("outA") m2 = ev3.LargeMotor("outD") scr = ev3.Screen() HEADER_TEXT = "http://lonk.pw/k8" AUTO_RES = None if AUTO_RES != None: HEADER_TEXT = "DEBUG MODE" header_font = ev3dev.fonts.load("lutBS14") scr.draw.text((scr.xres/2-scr.draw.textsize(HEADER_TEXT, font=header_font)[0]/2, 1), HEADER_TEXT, font=header_font) scr.update() code = "" code_font = ev3dev.fonts.load('ncenB24') def draw_code(): scr.draw.rectangle([(2,40), (scr.xres-2,70)], "white") if len(code) > 0:
def __init__(self): self.screen = ev3.Screen() self.board = Board(screen=self.screen)
##!/usr/bin/env python3 import ev3dev.ev3 as ev3 from threading import Thread left_motor = ev3.LargeMotor('outC') right_motor = ev3.LargeMotor('outB') lcd = ev3.Screen() ts = ev3.TouchSensor('in2'); assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor() us.mode='US-DIST-CM' units = us.units # reports 'cm' even though the sensor measures 'mm' def stop(): left_motor.stop() right_motor.stop() def drive_straight(distance): radius = 2.8#take measurement pi = math.pi circumference = 2*pi*radius angle = (distance*360)/circumference left_motor.run_to_rel_pos(position_sp=angle, speed_sp=450 ) right_motor.run_to_rel_pos(position_sp=angle, speed_sp=450) right_motor.wait_while('running') left_motor.wait_while('running') def spin(angle = 360, speed=450): left_motor.run_to_rel_pos(position_sp=angle, speed_sp=450) left_motor.run_to_rel_pos(position_sp= (-1)*angle, speed_sp=450) def turnRight(angle=90, speed=10):
#!/usr/bin/env python3 import ev3dev.ev3 as ev3 import time lcd = ev3.Screen() # The EV3 display rightMotor = ev3.LargeMotor('outB') # The motor connected to the right wheel leftMotor = ev3.LargeMotor('outC') # The motor connected to the left wheel button = ev3.Button() # Any button camera = ev3.Sensor(address=ev3.INPUT_1) # The camera assert camera.connected, "Error while connecting Pixy camera to port 2" lcd = ev3.Screen() ts = ev3.TouchSensor('in2') assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor() us.mode = 'US-DIST-CM' units = us.units # reports 'cm' even though the sensor measures 'mm' CAMERA_WIDTH_PIXELS = 255 CAMERA_HEIGHT_PIXELS = 255 leftMotor = ev3.LargeMotor('outC') rightMotor = ev3.LargeMotor('outB') lcd = ev3.Screen() ts = ev3.TouchSensor('in2') assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor()
def execute(self): global instruction global gyro global ultra global motorLeft global motorRight #Lock/unlock a = 1 << 5 | 24 | 2 << 1 b = 0 if locked: b = 255 bus = smbus.SMBus(6) bus.write_i2c_block_data(0x04, a, [b]) #QR/No QR old_token = None if token: motorLeft.run_forever(speed_sp=0) motorRight.run_forever(speed_sp=0) old_token = token self.generate_qr(token) screen = ev3.Screen() while token: if not old_token == token: motorLeft.run_forever(speed_sp=0) motorRight.run_forever(speed_sp=0) old_token = token self.generate_qr(token) motorLeft.run_forever(speed_sp=0) motorRight.run_forever(speed_sp=0) screen.clear() qrImage = Image.open('qrTest.png') screen.image.paste(qrImage, (0, 0)) screen.update() screen.clear() screen.update() if instruction == None: motorLeft.run_forever(speed_sp=0) motorRight.run_forever(speed_sp=0) return stop = instruction['onOff'] == 0 c = instruction['correction'] if stop: motorLeft.run_forever(speed_sp=0) motorRight.run_forever(speed_sp=0) return forward = instruction['turnAngle'] == 0 remaining = instruction['distance'] if forward: ultra.mode = 'US-DIST-CM' dist = ultra.value() print("DIST:", dist) if dist > 100: motorLeft.run_forever(speed_sp=remaining / 1.5 + 20 - c * 20) motorRight.run_forever(speed_sp=remaining / 1.5 + 20 + c * 20) #motorLeft.run_forever(speed_sp=50) #motorRight.run_forever(speed_sp=50) if dist < 100: motorLeft.run_forever(speed_sp=0) motorRight.run_forever(speed_sp=0) else: targetAngle = instruction['turnAngle'] * 180 / 3.14159 currentAngle = gyro.value() diff = targetAngle - currentAngle #distance to go in degrees #speed = (diff*diff)*(80/(180*180)) * 2 + 10 speed = 20 if abs(diff) < 30: speed = 10 if diff > 0: motorLeft.run_forever(speed_sp=-speed) motorRight.run_forever(speed_sp=speed) else: motorLeft.run_forever(speed_sp=speed) motorRight.run_forever(speed_sp=-speed)
def __init__(self): self.screen = ev3.Screen() self.draw_face()