def read_colors(motor: Motor, color: ColorSensor): detected = color.color() detected_colors = [] global scan_done global scan_started while not len(detected_colors) == 4: detected_colors.clear() scan_started = False scan_done = False scan.start() while (not (scan_started)): pass while (not (scan_done)): detected = color.color() if (detected in POSSIBLE_COLORS and detected not in detected_colors): detected_colors.append(detected) if len(detected_colors) < 4: reset(motor) return detected_colors
def drehe_bis_dizzy(): """ become dizzy by rotating based on color sensor input """ farbsensor = ColorSensor(Port.S3) ev3.screen.load_image(ImageFile.DIZZY) while (farbsensor.color() != Color.GREEN): time.sleep(0.1) db.drive(speed=0, turn_rate=5) while (farbsensor.color() != Color.RED): time.sleep(0.1) db.stop()
def displayColorValues(port): "Continously prints color values at given port" print("displayColorValues") # creat the sensor object from the ColorSensor class sensor = ColorSensor(port) i = 0 while True: # have four different ways of using this # sensor! color = sensor.color() print(i) i += 1 # Color.BLACK, Color.BLUE, Color.GREEN, Color.YELLOW, Color.RED, Color.WHITE, Color.BROWN or None if color == Color.BLACK: c = "Black" elif color == Color.BLUE: c = "Blue" elif color == Color.GREEN: c = "Green" elif color == Color.YELLOW: c = "Yellow" elif color == Color.RED: c = "Red" elif color == Color.WHITE: c = "White" else: c = "Unknown" print("color = ", c) wait(1000)
def main(): # sound test log.info('test beep') brick.sound.beep() # color sensor test sensor_color = ColorSensor(ROBOT['sensor_color_port']) log.debug('color sensor: color=%s' % sensor_color.color()) # gyro sensor test sensor_gyro = GyroSensor(ROBOT['sensor_gyro_port']) log.debug('gyro sensor: speed=%d, angle=%d' % (sensor_gyro.speed(), sensor_gyro.angle()))
def run_on_color(): color_sensor = ColorSensor(Port.S1) color = color_sensor.color() print(color) data.log('Color detected', color) rgb_value = color_sensor.rgb() print('Color detected RGB', rgb_value) data.log('Color detected RGB', rgb_value) ambient = color_sensor.ambient() print('Color detected ambient', ambient) data.log('Color detected ambient', ambient) reflection = color_sensor.reflection() print('Color detected reflection', reflection) data.log('Color detected reflection', reflection) #reset gyro before start of program gyro_sensor.reset_angle(0) if (color == Color.WHITE): ev3.speaker.say('white') print("Jolene Run") jolene_run() #jolene_test() elif (color == Color.BLUE): #ev3.speaker.say('blue') print("Jason Run") jason_start_slide() # #jolene_test() elif (color == Color.RED): #ev3.speaker.say('red') print("Sophie Run") sophie_run() elif (color == Color.BLACK): jason_step_counter() elif (color == Color.GREEN): sophie_bench() else: rgb_value = color_sensor.rgb() print(rgb_value) #gyro_test() jolene_run()
def displayLightValue(port): "Continously prints all values from color sensor at given port" # creat the sensor object from the ColorSensor class sensor = ColorSensor(port) while True: # have four different ways of using this # sensor! color = sensor.color() reflection = sensor.reflection() ambient = sensor.ambient() rgb = sensor.rgb() print("color: ", color) print("reflection: ", reflection) print("ambient: ", ambient) print("rgb: ", rgb) wait(1000)
# This loop scans the colors of the objects. It repeats until 8 objects # are scanned and placed in the chute. This is done by repeating the loop # while the length of the list is still less than 8. while len(color_list) < 8: # Show an arrow that points to the color sensor. ev3.screen.load_image(ImageFile.RIGHT) # Show how many colored objects we have already scanned. ev3.screen.print(len(color_list)) # Wait for the center button to be pressed or a color to be scanned. while True: # Store True if the center button is pressed or False if not. pressed = Button.CENTER in ev3.buttons.pressed() # Store the color measured by the Color Sensor. color = color_sensor.color() # If the center button is pressed or a color is detected, # break out of the loop. if pressed or color in POSSIBLE_COLORS: break if pressed: # If the button was pressed, end the loop early. We will no longer # wait for any remaining objects to be scanned and added to the # chute. break # Otherwise, a color was scanned. So we add (append) it to the list. ev3.speaker.beep(1000, 100) color_list.append(color)
def RedMission(): # Red Run (Bench Mission (including backrest removal)) # UP BUTTON #!/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, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) wheel_diameter = 56 axle_track = 115 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) Medium_Motor = Motor(Port.A) Large_Motor = Motor(Port.D) leftcolorsensor = ColorSensor(Port.S3) rightcolorsensor = ColorSensor(Port.S2) robot.settings(500) robot.straight(290) robot.stop(Stop.BRAKE) robot.settings(700,400,700,400) robot.turn(110) robot.stop(Stop.BRAKE) while True: robot.drive(200,0) if leftcolorsensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep(3) robot.turn(-110) robot.straight(80) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = rightcolorsensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(rightcolorsensor.color()) if robot.distance() >= 100: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() while True: # Calculate the deviation from the threshold. deviation = rightcolorsensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(rightcolorsensor.color()) if leftcolorsensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.turn(-25) robot.straight(230) Large_Motor.run_angle(50,100,then = Stop.HOLD, wait = True) robot.straight(-60) robot.turn(35) robot.straight(-10) Large_Motor.run_angle(50,-50,then = Stop.HOLD, wait = True) robot.straight(-85) robot.turn(-85) robot.straight(500) robot.turn(-20) robot.straight(250) Large_Motor.run_angle(50,-70,then = Stop.HOLD, wait = False) robot.turn(110) robot.stop(Stop.BRAKE)
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align) from pybricks.robotics import DriveBase import time # Write your program here brick.sound.beep() motorA = Motor(Port.A) motorD = Motor(Port.D) robot = DriveBase(motorA, motorD, 56, 114) cs = ColorSensor(Port.S4) #drive speed at 450 mm per second with 0 deg per second turn rate robot.drive(450, 0) while cs.color() != Color.YELLOW: time.sleep(0.01) robot.stop(Stop.BRAKE)
class Kraz3(RemoteControlledTank): WHEEL_DIAMETER = 24 AXLE_TRACK = 100 def __init__(self, left_motor_port: Port = Port.C, right_motor_port: Port = Port.B, wiggle_motor_port: Port = Port.A, polarity: str = 'inversed', touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__(wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, left_motor_port=left_motor_port, right_motor_port=right_motor_port, polarity=polarity, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.ev3_brick = EV3Brick() self.wiggle_motor = Motor(port=wiggle_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) def kungfu_manoeuvre_whenever_touched_or_remote_controlled(self): """ Kung-Fu manoeuvre via Touch Sensor and Remote Control of head and arms """ while True: if self.touch_sensor.pressed(): self.ev3_brick.speaker.play_file(file=SoundFile.KUNG_FU) self.wiggle_motor.run_angle(speed=500, rotation_angle=360, then=Stop.HOLD, wait=True) elif Button.BEACON in \ self.ir_sensor.buttons(channel=self.ir_beacon_channel): self.wiggle_motor.run(speed=111) else: self.wiggle_motor.stop() wait(10) def keep_reacting_to_colors(self): while True: detected_color = self.color_sensor.color() if detected_color == Color.YELLOW: self.ev3_brick.speaker.play_file(file=SoundFile.YELLOW) self.wiggle_motor.run_angle(speed=-860, rotation_angle=360, then=Stop.HOLD, wait=True) self.ev3_brick.speaker.play_file(file=SoundFile.UH_OH) wait(500) self.ev3_brick.speaker.play_file(file=SoundFile.SNEEZING) wait(500) elif detected_color == Color.RED: self.ev3_brick.speaker.play_file(file=SoundFile.SHOUTING) for _ in range(randint(1, 6)): self.ev3_brick.speaker.play_file(file=SoundFile.SMACK) self.ev3_brick.light.on(color=Color.RED) self.wiggle_motor.run_angle(speed=170, rotation_angle=360, then=Stop.HOLD, wait=True) self.ev3_brick.speaker.play_file(file=SoundFile.LEGO) self.ev3_brick.speaker.play_file(file=SoundFile.MINDSTORMS) self.ev3_brick.light.off() elif detected_color == Color.BROWN: self.ev3_brick.speaker.play_file(file=SoundFile.BROWN) wait(1000) self.wiggle_motor.run_angle(speed=-200, rotation_angle=360, then=Stop.HOLD, wait=True) self.ev3_brick.speaker.play_file(file=SoundFile.CRYING) elif detected_color == Color.GREEN: self.ev3_brick.speaker.play_file(file=SoundFile.GREEN) self.wiggle_motor.run_angle(speed=-400, rotation_angle=360, then=Stop.HOLD, wait=True) self.ev3_brick.speaker.play_file(file=SoundFile.YES) wait(1000) elif detected_color == Color.BLUE: self.ev3_brick.speaker.play_file(file=SoundFile.BLUE) self.ev3_brick.speaker.play_file(file=SoundFile.FANTASTIC) self.ev3_brick.speaker.play_file(file=SoundFile.GOOD_JOB) self.wiggle_motor.run_angle(speed=750, rotation_angle=360, then=Stop.HOLD, wait=True) self.ev3_brick.speaker.play_file(file=SoundFile.MAGIC_WAND) wait(10)
class Richard_Skills: def __init__(self, robot): self.robot = robot self.color_sensor_left = ColorSensor(Port.S3) self.color_sensor_right = ColorSensor(Port.S4) def tell_me_about_your_skills(self): print("SKILLS - I can Dance, Wiggle, Big Wiggle, and Shuffle") print("SKILLS - I have access to information like wheel diameter, see", self.robot.wheel_diameter) print() def dance(self, number_of_dances): print("SKILLS - Perform", number_of_dances, "dance moves.") y = 1 while y <= number_of_dances: self.robot.turn(360) y = y + 1 def wiggle(self, number_of_wiggles): print("SKILLS - Wiggle", number_of_wiggles, "times.") z = 1 while z <= number_of_wiggles: self.robot.turn(45) self.robot.turn(-45) z = z + 1 def shuffle(self, number_of_shuffles): print("SKILLS - Shuffle", number_of_shuffles, "times.") a = 1 while a <= number_of_shuffles: self.robot.move_forward(50) self.robot.move_backwards(50) a = a + 1 def big_wiggle(self, number_of_big_wiggles): print("SKILLS - Big Wiggle", number_of_big_wiggles, "times") b = 1 while b <= number_of_big_wiggles: self.robot.turn(45) self.robot.turn(-15) b = b + 1 def detect_color(self): print("I can see left", self.color_sensor_left.color(), " and right", self.color_sensor_right.color(), ".") def detect_wall(self, distance_mm, angle_turn): ultrasonic_sensor = UltrasonicSensor(Port.S1) print("If I get too close to the wall, I will turn.") if self.ultrasonic_sensor.distance() <= distance_mm: print("Too close!") self.robot.turn(angle_turn) else: print("There is some room...") def under_bar_and_dance(): Crabot.move_forward(950) Crabot.turn(90) Crabot.move_forward(400) Crabot.turn(45) Crabot.move_forward(390) def speed_and_power(distance): Crabot.gyro_drive(200000000000000000, 0, distance)
class CSensor: def __init__(self): self.sensor = ColorSensor(Port.S3) self.dominantColorTab = [] #List used for dominantSortingColor self.logColors = [] #List used to store recently found colors def color(self): """Return the color detected by the native function of the sensor""" return self.sensor.color() def rgb(self): """Return the sensor RGB values""" rgb = self.sensor.rgb() r = (rgb[0] / 100) * 255 g = (rgb[1] / 100) * 255 b = (rgb[2] / 100) * 255 return r, g, b #Convert RGB values into HSV values def hsv(self): """Convert RGB values into HSV values""" r, g, b = rgb[0] / 255.0, rgb[1] / 255.0, rgb[2] / 255.0 mx = max(r, g, b) mn = min(r, g, b) df = mx - mn if mx == mn: h = 0 elif mx == r: h = (60 * ((g - b) / df) + 360) % 360 elif mx == g: h = (60 * ((b - r) / df) + 120) % 360 elif mx == b: h = (60 * ((r - g) / df) + 240) % 360 if mx == 0: s = 0 else: s = df / mx v = mx return h, s, v def rgb_to_hls(self, r, g, b): """Convert rgb values to hsl values""" if (max(r, g, b) == 0): maxc = 0.002 else: maxc = max(r, g, b) if (min(r, g, b) == 0): minc = 0.001 else: minc = min(r, g, b) l = (minc + maxc) / 2.0 if minc == maxc: return 0.0, l, 0.0 if l <= 0.5: s = (maxc - minc) / (maxc + minc) else: s = (maxc - minc) / (2.0 - maxc - minc) rc = (maxc - r) / (maxc - minc) gc = (maxc - g) / (maxc - minc) bc = (maxc - b) / (maxc - minc) if r == maxc: h = bc - gc elif g == maxc: h = 2.0 + rc - bc else: h = 4.0 + gc - rc h = (h / 6.0) % 1.0 return h, l, s def color_difference(self, color1, color2): """Return a list of all the differences of colors in other lists ([diff,diff2,diff3,etc])""" return sum([ abs(component1 - component2) for component1, component2 in zip(color1, color2) ]) def dominantColor3(self): """Return the number value of BLUE, BLACK or WHITE > This function is used for following the path This function compare and take the nearest color """ rgb = self.rgb() #Color references TARGET_COLORS = { "BLUE": (23, 53, 210, 117), "BLACK": (9, 9, 7, 9), "WHITE": (167, 144, 255, 199) } SWITCHER_COLOR = { "BLUE": Color.BLUE, "BLACK": Color.BLACK, "WHITE": Color.WHITE } rgb_list = list(rgb) hsl_color = self.rgb_to_hls(rgb[0], rgb[1], rgb[2]) rgb_list.append(hsl_color[1]) my_color = tuple(rgb_list) differences = [[ self.color_difference(my_color, target_value), target_name ] for target_name, target_value in TARGET_COLORS.items()] differences.sort() my_color_name = differences[0][1] return SWITCHER_COLOR.get(my_color_name, -1) #Same function as dominantColor3 but white red and green def dominantColor4(self): """Same function as dominantColor3 but white red and green""" rgb = self.rgb() TARGET_COLORS = { "RED": (180, 40, 30, 109), "GREEN": (92, 154, 50, 102), "BLUE": (23, 53, 210, 117), "BLACK": (9, 9, 7, 9), "WHITE": (167, 144, 255, 199) } rgb_list = list(rgb) hsl_color = self.rgb_to_hls(rgb[0], rgb[1], rgb[2]) rgb_list.append(hsl_color[1]) my_color = tuple(rgb_list) differences = [[ self.color_difference(my_color, target_value), target_name ] for target_name, target_value in TARGET_COLORS.items()] differences.sort() my_color_name = differences[0][1] return my_color_name def dominantSortingColor(self): """Use dominantColor4 to take a list of 5 colors and choose the most viewed color in this list""" my_color = self.dominantColor4() wait(15) self.dominantColorTab.append(my_color) if (len(self.dominantColorTab) == 5): setlist = set(sorted(self.dominantColorTab)) b = [self.dominantColorTab.count(el) for el in setlist] pos = b.index(max(b)) new_list = list(setlist) self.dominantColorTab.clear() return new_list[pos] else: return "N/A" #Same as dominantSortingColor but return the number of the color def dominantSortingColor2(self): """Same as dominantSortingColor but return the number of the color""" my_color = self.dominantColor4() self.dominantColorTab.append(my_color) SWITCHER_COLOR = { "RED": Color.RED, "GREEN": Color.GREEN, "BLUE": Color.BLUE, "BLACK": Color.BLACK, "WHITE": Color.WHITE } if (len(self.dominantColorTab) == 5): setlist = set(sorted(self.dominantColorTab)) b = [self.dominantColorTab.count(el) for el in setlist] pos = b.index(max(b)) new_list = list(setlist) self.dominantColorTab.clear() wait(10) return SWITCHER_COLOR.get(new_list[pos], -1) else: wait(10) return "N/A" #-- COLOR FINDING WITH PROBABILITIES --# def updateColorProbability(self): """Update the list of recently found colors There may be COLOR_PROBABILITY_TIME_LIMIT colors in the log list at the same time.""" c = self.dominantColor3() while (self.logColors and (len(self.logColors) > COLOR_PROBABILITY_TIME_LIMIT)): self.logColors.pop(0) self.logColors.append(c) def greenColorProbability(self): """Return the percentage of green colors stored in the log list""" return float(self.logColors.count(Color.GREEN)) / float( len(self.logColors)) def redColorProbability(self): """Return the percentage of red colors stored in the log list""" return float(self.logColors.count(Color.RED)) / float( len(self.logColors)) def isRedOrGreen(self): """Find if the current color is Red, Green or none of them. The Green or Red color are confirmed only if the current probability is above, respectively, COLOR_GREEN_PROBA_THRESH and COLOR_RED_PROBA_THRESH""" c = self.dominantColor3() if (c == Color.GREEN and self.greenColorProbability() > COLOR_GREEN_PROBA_THRESH): return Color.GREEN elif (c == Color.RED and self.redColorProbability() > COLOR_RED_PROBA_THRESH): return Color.RED else: return None
left_color = ColorSensor(Port.S1) right_color = ColorSensor(Port.S4) my_distance = UltrasonicSensor(Port.S2) on_table = True while True: while on_table: right_motor.run(700) left_motor.run(700) if my_distance.distance() < 200: lifter.run_angle(500, 200) lifter.run_angle(500, -200) print(my_distance.distance()) if left_color.color() == None or right_color.color() == None: on_table = False if not on_table: right_motor.stop(Stop.BRAKE) left_motor.stop(Stop.BRAKE) wait(50) while left_color.color() != None: left_motor.run(200) right_motor.run(-200) while right_color.color() != None: right_motor.run(200) left_motor.run(-200) right_motor.run(-400) left_motor.run(-400)
def optical_sensor_detect(): global color_detect_loop_fast_flag global color_detected global color_detected_2 global color_detected_name global color_detected_name_2 global color_rgb_int global color_rgb_int_2 global color_rgb_int_ave global color_rgb_int_ave_2 global optical_sensor_run global optical_sensor_run_2 global color_sound_on global color_sound_on_2 global cs global cs_2 global cs_error global cs_error_2 error_fix_wait = 3000 if optical_sensor_run: try: if cs_error: #Wait some and then try to connect again wait(error_fix_wait) cs = ColorSensor(ColorSensorPort) cs_error = False if color_detect_loop_fast_flag: color_rgb_int_ave = cs.reflection() else: color_rgb_int = cs.rgb() color_rgb_int_ave = sum(color_rgb_int) / 3 color_detected = cs.color() color_detected_name = color_name(color_detected) if color_sound_on: color_sound(color_detected, 1) except: cs_error = True print("Color Sensor 1 Error") sound_tools.play_file(SoundFile.COLOR) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.ONE) if optical_sensor_run_2: try: if cs_error_2: #Wait some and then try to connect again wait(error_fix_wait) cs_2 = ColorSensor(ColorSensorPort_2) cs_error_2 = False if color_detect_loop_fast_flag: color_rgb_int_ave_2 = cs_2.reflection() else: color_rgb_int_2 = cs_2.rgb() color_rgb_int_ave_2 = sum(color_rgb_int_2) / 3 color_detected_2 = cs_2.color() color_detected_name_2 = color_name(color_detected_2) if color_sound_on_2: color_sound(color_detected_2, 2) except: cs_error_2 = True print("Color Sensor 2 Error") sound_tools.play_file(SoundFile.COLOR) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.TWO)
robot.drive(-10,0) stop1() robot.stop() elif motor_avarge()<0: while motor_avarge()<0: robot.drive(-10,0) stop1() robot.stop() while motor_avarge()>=-1300: robot.drive(-2000,0) stop1() robot.stop() reset_all() while True: if first_cls.color() == Color.RED: while not Button.CENTER in brick.buttons(): reset_all() while motor_avarge()<=1400: PID(150,0,5,1,1) stop1() robot.stop() reset_all() time.sleep(1) while motor_avarge()>=-270: PID(-400,0,1,1,1) stop1() robot.stop() while True: PID(-500,-210,1,1,1) stop1()
from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. ev3 = EV3Brick() # Write your program here ev3.speaker.set_speech_options('pt-br','m3') ev3.speaker.beep() sleft = ColorSensor(Port.S2) sright = ColorSensor(Port.S3) corLeft= sleft.color() corRight=sright.color() ev3.speaker.set_speech_options('pt-br','m3') ev3.speaker.say("Sensor Esquerdo") if(corLeft == Color.RED): ev3.speaker.say("Vermelho") elif(corLeft == Color.YELLOW): ev3.speaker.say("Amarelo") elif(corLeft == Color.GREEN): ev3.speaker.say("Verde") elif(corLeft == Color.BLUE): ev3.speaker.say("Azul") else: ev3.speaker.say("Cor desconhecida")
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, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() # Write your program here. ev3.speaker.beep() #컬러센서를 초기화 cs = ColorSensor(Port.S3) #색상을 읽는다. Color.RED, Color.BLUE .. color = cs.color() #반사광값을 읽는다. 0 ~ 100% reflection = cs.reflection() #주변광값을 읽는다. 0 ~ 100% ambient = cs.ambient() print(color) print(reflection) print(ambient)
# Initialize the Color Sensor. It is used to detect the color of the objects. color_sensor = ColorSensor(Port.S3) brick.display.clear() # Checks if the sensor and motor are connected to the robot assert color_sensor assert motor speaker.beep(frequency = 670, duration = 100) # This is the main loop. It waits for you to scan and insert 8 colored objects. # Then it sorts them by color. Then the process starts over and you can scan # and insert the next set of colored objects. while True: # Store the color measured by the Color Sensor. color = color_sensor.color() if color in POSSIBLE_COLORS: if color == Color.RED: run_time(500, 1000, then = Stop.HOLD, wait = True) if color == Color.GREEN: run_time(500, 1000, then = Stop.HOLD, wait = True) # Doesn't sense yellow well if color == Color.YELLOW: run_time(500, 1000, then = Stop.HOLD, wait = True) if color == Color.BLUE: run_time(500, 1000, then = Stop.HOLD, wait = True) # Determine if the color read is among POSSIBLE_COLORS if color in POSSIBLE_COLORS: # if yes, beep the brick brick.sound.beep(1000, 100, 100)
# Initialize the drive base. robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104) ev3.screen.load_image(ImageFile.ANGRY) ev3.light.on(Color.YELLOW) flag = 0 while True: robot.drive(500, 0) if (touch_sensor.pressed()): robot.stop() flag = 1 ev3.light.on(Color.RED) break if (color_sensor_front.color() == Color.BLACK and color_sensor_back.color() != Color.BLACK): robot.stop() ev3.speaker.beep(1) robot.turn(180) elif (color_sensor_back.color() == Color.BLACK and color_sensor_front.color() == Color.BLACK): robot.stop() ev3.screen.load_image(ImageFile.KNOCKED_OUT) ev3.light.on(Color.RED) ev3.speaker.say('Ono lost') break if (flag == 1): robot.straight(1000)
from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() # Write your program here. ev3.speaker.beep() left= Motor(Port.B) right = Motor(Port.C) robot = DriveBase(left, right, 55.5, 104) cs = ColorSensor(Port.S3) robot.drive(150,0) #붉은색이 아닌 경우 -> 계속 직진한다. while cs.color() != Color.RED: pass #모터를 멈춘다. robot.stop()
def BlueMission(): #!/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, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) #go front towards the step counter robot.settings(250) robot.straight(650) robot.stop(Stop.BRAKE) wait(20) #makes the robot go slower robot.settings(40) #slowly pushes the step counter by going back and front 2 times robot.straight(140) robot.stop(Stop.BRAKE) robot.straight(-45) robot.stop(Stop.BRAKE) robot.straight(120) robot.stop(Stop.BRAKE) robot.straight(-30) robot.stop(Stop.BRAKE) #the robot then turns and goes backwards robot.settings(250,500,250,500) robot.turn(45) robot.straight(-100) # the robot then goes back until the right color sensor detects back while True: robot.drive(-115,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #the large motor attatchment comes down at the same time the robot takes a turn towards the black line underneath the pull up bar large_motor.run_angle(50,200,then=Stop.HOLD, wait=False) left_motor.run_angle(50,-300,then=Stop.HOLD, wait=True) #the robot then goes straight towards that line robot.straight(120) robot.stop(Stop.BRAKE) #robot continues to go forwards until the left color sensor detects black while True: robot.drive(115,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break right_motor.run_angle(50,150,then=Stop.HOLD, wait=True) #the robot then turns with the right motor until it detects black while True: right_motor.run(85) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #follows the line underneath the pull up bar until the leftsensor detects black #follows the line underneath the pull up bar until the leftsensor detects black BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor1.color()) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #the robot then turns towards the boccia aim and moves straight to push it towards the target and finishes the misison robot.turn(25) robot.straight(250) robot.straight(-50) # this is importate kekekekee large_motor.run_angle(75,-65) robot.straight(-60) while True: robot.drive(-70,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) ev3.speaker.beep() break while True: left_motor.run(-50) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) ev3.speaker.beep() break left_motor.run_angle(50, -20) right_motor.run_angle(50, 20) robot.settings(200) robot.straight(60) robot.turn(-137) #this is also importante jekeke large_motor.run_angle(50,80) robot.straight(143) large_motor.run_angle(550, -120) robot.straight(-40) large_motor.run_angle(550, 120) robot.straight(40) large_motor.run_angle(550, -120) large_motor.run_angle(300, 30, then=Stop.HOLD, wait=True) #robot.straight(40) large_motor.run_angle(300, -100, then=Stop.HOLD, wait=True) #goes to collect the health unit near the basketball (goes back to base) robot.straight(-200) robot.turn(40) robot.straight(556) robot.straight(50) robot.stop(Stop.BRAKE) while True: left_motor.run(50) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) ev3.speaker.beep() break robot.stop(Stop.BRAKE) robot.reset() # Calculate the light threshold. Choose values based on your measurements. BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every # percentage point of light deviating from the threshold, we set the turn # rate of the drivebase to 1.2 degrees per second. # For example, if the light value deviates from the threshold by 10, the robot # steers at 10*1.2 = 12 degrees per second. PROPORTIONAL_GAIN = 1.2 runWhile = True # Start following the line endlessly. while runWhile: # Calculate the deviation from the threshold. deviation = line_sensor1.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) if robot.distance() >= 210: runWhile = False break robot.stop(Stop.BRAKE) robot.turn(5.244312) robot.straight(700) #the robot then goes back until the right color sensor detects back ''' while True: if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #robot.straight(980) ''' robot.stop(Stop.BRAKE)
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.parameters import (Port, Button) from pybricks.tools import print, wait from pybricks.ev3devices import ColorSensor #Fuege den Tools Ordner zum PYTHONPATH hinzu. Nicht notwendig wenn TOF.py im selben Ordner ist import sys sys.path.append("/home/robot/LEGORoboticsPython/Tools") from TOF import TOF #Port des Seonsors festlegen tof = TOF(Port.S2) c = ColorSensor(Port.S3) while not Button.DOWN in brick.buttons(): result = 'TOF: ' + str(tof.distance()) print(result) print("Color" + str(c.color())) tof.close()
class Catapult(IRBeaconRemoteControlledTank, EV3Brick): WHEEL_DIAMETER = 23 AXLE_TRACK = 65 def __init__(self, left_motor_port: Port = Port.B, right_motor_port: Port = Port.C, catapult_motor_port: Port = Port.A, touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3, ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1): super().__init__(wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK, left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.catapult_motor = Motor(port=catapult_motor_port, positive_direction=Direction.CLOCKWISE) self.touch_sensor = TouchSensor(port=touch_sensor_port) self.color_sensor = ColorSensor(port=color_sensor_port) self.ir_sensor = InfraredSensor(port=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel def scan_colors(self): while True: if self.color_sensor.color() == Color.YELLOW: pass elif self.color_sensor.color() == Color.WHITE: self.speaker.play_file(file=SoundFile.GOOD) def make_noise_when_touched(self): while True: if self.touch_sensor.pressed(): self.speaker.play_file(file=SoundFile.OUCH) def throw_by_ir_beacon(self): while True: if Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): self.catapult_motor.run_angle(speed=3500, rotation_angle=-150, then=Stop.HOLD, wait=True) self.catapult_motor.run_angle(speed=3500, rotation_angle=150, then=Stop.HOLD, wait=True) while Button.BEACON in self.ir_sensor.buttons( channel=self.ir_beacon_channel): pass def main(self): self.speaker.play_file(file=SoundFile.YES) Process(target=self.make_noise_when_touched).start() Process(target=self.throw_by_ir_beacon).start() Process(target=self.scan_colors).start() self.keep_driving_by_ir_beacon(speed=1000)
# Initialize the color sensor. line_sensor = ColorSensor(Port.S1) # Initialize the drive base. robot = DriveBase(left_motor, right_motor, wheel_diameter=WHEEL_SIZE, axle_track=AXLE_TRACK) #To calibrate color sensors. amb = line_sensor.ambient() print("ambient is:" + str(amb)) ref = line_sensor.reflection() print("reflection is:" + str(ref)) col = line_sensor.color() print("col is:" + str(col)) # Calculate the light threshold. Choose values based on your measurements. BLACK = 8 WHITE = 95 # outside in the garage, white is 75 WHITE = 75 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = -170 # Set the gain of the proportional line controller. This means that for every
def BlueMission(): #!/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, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) #go front towards the step counter robot.straight(650) robot.stop(Stop.BRAKE) wait(20) #makes the robot go slower robot.settings(40) #slowly pushes the step counter by going back and front 2 times robot.straight(140) robot.stop(Stop.BRAKE) robot.straight(-45) robot.stop(Stop.BRAKE) robot.straight(120) robot.stop(Stop.BRAKE) robot.settings(100) robot.straight(-30) robot.stop(Stop.BRAKE) #the robot then turns and goes backwards robot.turn(45) robot.straight(-100) # the robot then goes back until the right color sensor detects back while True: robot.drive(-30, 0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #the large motor attatchment comes down at the same time the robot takes a turn towards the black line underneath the pull up bar large_motor.run_angle(50, 170, then=Stop.HOLD, wait=False) left_motor.run_angle(50, -300, then=Stop.HOLD, wait=True) #the robot then goes straight towards that line robot.straight(120) robot.stop(Stop.BRAKE) #robot continues to go forwards until the left color sensor detects black while True: robot.drive(30, 0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break right_motor.run_angle(50, 150, then=Stop.HOLD, wait=True) #the robot then turns with the right motor until it detects black while True: right_motor.run(85) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #follows the line underneath the pull up bar until the leftsensor detects black BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor.color()) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #the robot then turns towards the boccia aim and moves straight to push it towards the target and finishes the misison robot.straight(100) #after line following, it goes straight for 100 mm robot.turn(50) robot.straight(100) robot.straight(-30) large_motor.run_angle(100, -65) robot.straight(-60) #the robot then takes a turn (at the same time bringing the attatchment down) towards the slide mission and completes the mission large_motor.run_angle(50, 80, then=Stop.HOLD, wait=False) robot.turn(-195) robot.straight(165) large_motor.run_angle(300, -120, then=Stop.HOLD, wait=True) robot.straight(-30) large_motor.run_angle(200, 120, then=Stop.HOLD, wait=True) ## The robot moves straight towards the mission, getting ready to attempt to push the slide figures off once more. (In case it didn't work before.) robot.straight(30) large_motor.run_angle(300, -120, then=Stop.HOLD, wait=True) robot.straight(-50) '''
ts = TouchSensor(Port.S1).pressed() ''' return grabbed return x return back return green return BREAK grabbed = 0 BREAK = "no" time.sleep(1) ts = TouchSensor(Port.S1).pressed() back = "no" while ts == True: x = cl.color() print(x) if x == Color.GREEN: grabbed = 0 BREAK = "no" back = "no" ev3.screen.clear() ev3.screen.draw_text(40, 50, "GREEN") wait(2) ev3.speaker.play_file(SoundFile.GREEN) robot.straight(160) robot.turn(-90) robot.straight(100) robot.turn(90) robot.straight(20) green = "yes"
def BlueMission(): # Blue Run (Step Counter, Pull-Up Bar, Boccia Aim, Slide, Health Unit - 1) # DOWN BUTTON #!/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, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. #define your variables ev3 = EV3Brick() left_motor = Motor(Port.C) right_motor = Motor(Port.B) medium_motor = Motor(Port.A) large_motor = Motor(Port.D) wheel_diameter = 56 axle_track = 115 line_sensor = ColorSensor(Port.S2) line_sensor1 = ColorSensor(Port.S3) robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) # Go towards the step counter mission from base robot.settings(800) # Speed Change robot.straight(650) robot.stop(Stop.BRAKE) wait(20) # Slow the robot down to succesfully push the step counter. robot.settings(200) # Slowly pushes the step counter by going backward and forward a couple times to increase reliability. robot.straight(230) robot.straight(-20) robot.straight(50) robot.stop(Stop.BRAKE) #robot.straight(-45) #robot.stop(Stop.BRAKE) #robot.straight(120) #robot.stop(Stop.BRAKE) robot.straight(-60) robot.stop(Stop.BRAKE) # The robot then turns and goes backwards until the right color sensor detects black. #robot.settings(250,300,250,300) robot.turn(45) robot.straight(-100) while True: robot.drive(-100,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break #The large motor attatchment comes down at the same time the robot takes a turn towards #the black line underneath the pull up bar left_motor.run_angle(50,-300,then=Stop.HOLD, wait=True) # The robot then goes straight towards the line under the pull-up bar. robot.straight(120) robot.stop(Stop.BRAKE) # Robot continues to go forwards until the left color sensor detects black. while True: robot.drive(115,0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break right_motor.run_angle(100,150,then=Stop.HOLD, wait=True) # The robot turns using the right motor until it detects black. while True: right_motor.run(100) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.straight(-90) large_motor.run_angle(100,150,then=Stop.HOLD, wait=True) robot.stop(Stop.BRAKE) robot.stop(Stop.BRAKE) while True: right_motor.run(40) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep() BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor1.color()) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) robot.stop(Stop.BRAKE) large_motor.run_angle(-150, 150, then=Stop.HOLD, wait=False) robot.turn(20) robot.stop(Stop.BRAKE) robot.settings(800) robot.straight(280) ev3.speaker.beep(3) while True: robot.drive(-115,0) if line_sensor.color() == Color.BLACK: ev3.speaker.beep(10) robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) # robot.straight(-10) robot.stop(Stop.BRAKE) robot.turn(50) # left_motor.run_angle(100, 150) ''' large_motor.run_angle(30,-20,then=Stop.HOLD, wait=False) robot.turn(10) robot.stop(Stop.BRAKE) large_motor.run_angle(100, -50, then=Stop.HOLD, wait=False) robot.turn(90) robot.stop(Stop.BRAKE) ''' BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = line_sensor.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor.color()) if robot.distance() >= 500: robot.stop(Stop.BRAKE) break ev3.speaker.beep(3) while True: robot.drive(40,0) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep() robot.straight(30) robot.turn(-100) robot.straight(70) large_motor.run_angle(600,150,then=Stop.HOLD, wait=True) while True: robot.drive(-50, 0) if line_sensor1.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) right_motor.run_angle(600,500,then=Stop.HOLD,wait=True) robot.straight(20) BLACK = 9 WHITE = 85 threshold = (BLACK + WHITE) / 2 # Set the drive speed at 100 millimeters per second. DRIVE_SPEED = 100 # Set the gain of the proportional line controller. This means that for every PROPORTIONAL_GAIN = 1.2 runWhile = True robot.reset() ev3.speaker.beep() while True: # Calculate the deviation from the threshold. deviation = line_sensor1.reflection() - threshold # Calculate the turn rate. turn_rate = PROPORTIONAL_GAIN * deviation # Set the drive base speed and turn rate. robot.drive(DRIVE_SPEED, turn_rate) wait(10) print(line_sensor1.color()) if robot.distance() >= 580: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) while True: robot.drive(50, 0) if line_sensor.color() == Color.BLACK: robot.stop(Stop.BRAKE) break robot.stop(Stop.BRAKE) ev3.speaker.beep(3) robot.turn(-45) robot.stop(Stop.BRAKE) robot.straight(30) large_motor.run_angle(1000,-150,then=Stop.HOLD, wait=True) robot.straight(-40) large_motor.run_angle(1000,150,then=Stop.HOLD, wait=True) robot.straight(40) large_motor.run_angle(1000,-150,then=Stop.HOLD, wait=True) robot.straight(-115) robot.turn(95) robot.straight(420) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100) robot.turn(-100) robot.turn(100)
return [-1, 1][random.randrange(2)] def check_container_full(): return color_sensor_ramp.color() != calibration_ramp global last_direction_change last_direction_change = 0 # reset axes motor_arm.run_until_stalled(100, Stop.COAST, 30) motor_arm.reset_angle(0) motor_grabber.run_until_stalled(100, Stop.COAST, 30) motor_grabber.reset_angle(0) calibration_surface = color_sensor_floor.color() calibration_ramp = color_sensor_ramp.color() while ball_count < 4: global last_direction_change collision_avoidance() check_ball() if check_container_full(): break # run every 1s if watch.time() - last_direction_change > random_direction_change_interval: last_direction_change = watch.time() random_direction = random.randint(0, 91) robot.drive_time(drive_speed,
import time Vmoottori = Motor(Port.B, Direction.CLOCKWISE) Omoottori = Motor(Port.C, Direction.CLOCKWISE) variSensori = ColorSensor(Port.S2) # Play a sound #brick.sound.file("Taistelujaska.wav") va = True reitilla = False i = 0 laskuri = 0 while i < 50000: vari = variSensori.color() Vmoottori.run(-300) Omoottori.run(-300) time.sleep(0.05) if vari == 6: Vmoottori.run(200) Omoottori.run(-200) time.sleep(0.2) Vmoottori.run(-200) Omoottori.run(-200) time.sleep(1) Vmoottori.run(-200) Omoottori.run(200) time.sleep(0.5) i += 1 laskuri += 1
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, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Initialize the motors. left_motor = Motor(Port.B) right_motor = Motor(Port.C) # Initialize the color sensor. line_sensor = ColorSensor(Port.S2) #initialize brick variables AV1 = DriveBase(left_motor, right_motor, 30, 100) ev3 = EV3Brick() # Write your program here. while line_sensor.color() != Color.BLACK: #ev3.speaker.play_notes(['C4/4', 'C4/4', 'G4/4', 'G4/4']) AV1.straight(10) #ev3.speaker.beep(frequency=1000, duration=500) # Write your program here. ev3.speaker.say("All Done!")