FLL Oradea – a doua zi

Prezentarea

Calibrarea de senzor de culoare:

#!/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

import time

# 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()


white_max = 74 ##4 ## 38
black_min = 7 ##  ## 5
IntervalNegru_Alb= white_max - black_min # Range=Interval

def Functia_de_Calibrare(sensorT):
    ev3.screen.print("STANGA: Calibrare")
    ev3.screen.print("CENTRU: Run")
    while True:
        if Button.CENTER in ev3.buttons.pressed():
            return
        if Button.LEFT in ev3.buttons.pressed():
            ev3.screen.clear()
            ev3.screen.print("Ok, calibram")
            ev3.speaker.beep()
            break
        time.sleep(0.05)

    ev3.screen.print("Pune pe alb ")
    ev3.screen.print("Apasa CENTRU")
    while True:
        if Button.CENTER in ev3.buttons.pressed():
            white_max = colorS.reflection()
            ev3.speaker.beep()
            break
        time.sleep(0.05)

    ev3.screen.clear()
    ev3.screen.print("Pune pe negru")
    ev3.screen.print("Apasa DREAPTA")
    while True:
        if Button.RIGHT in ev3.buttons.pressed():
            black_min = colorS.reflection()
            ev3.speaker.beep()
            break
        time.sleep(0.05)
    ev3.screen.clear()
    

    IntervalNegru_Alb = white_max - black_min # calculam noul interval 


def readCalibratedColor(): ##sensorT):
    ReflexiaBruta= colorS.reflection()
    CuloareCalibrata = int(100 * float(rReflexiaBruta - black_min)/float(black_white_range))
    return CuloareCalibrata


# Write your program here.

# Initializeaza Senzorul de Culoare 
colorS = ColorSensor(Port.S3)
Functia_de_Calibrare(colorS)

Oprirea la o linie neagră:

#!/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
import time


# 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.

# Initializeaza Senzorul de Culoare 
colorS = ColorSensor(Port.S3)

# Initializeaza motoarele
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)

#Declaram/"construim" un obiect de tip sasiu/DriveBase
robot = DriveBase(left_motor,right_motor,wheel_diameter=55.5,axle_track=104)

ev3.speaker.beep()
robot.straight(400)
ev3.speaker.beep()
# Cata vreme nu am gasit linia neagra mergem inainte

robot.drive(80,0) # 80 mm/s viteza\speed, 0 turning\intoarcere (straight)

while(colorS.reflection() > 7):
    time.sleep(0.01)

# Cand am iesit din bucla while inseamna ca am gasit linia neagra si ne putem opri
robot.drive(0,0)
ev3.speaker.beep()
robot.stop()