Ejemplo n.º 1
0
 def __init__(self):
     self.left_color_sensor = ColorSensor('in4')
     self.right_color_sensor = ColorSensor('in1')
     self.left_large_motor = LargeMotor('outD')
     self.right_large_motor = LargeMotor('outA')
     self.touch_sensor = TouchSensor('in3')
     self.listeners = []
     self.prev_is_pressed = False
Ejemplo n.º 2
0
    def __init__(self,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 medium_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(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.medium_motor = MediumMotor(address=medium_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel
        self.beacon = RemoteControl(sensor=self.ir_sensor,
                                    channel=ir_beacon_channel)

        self.dis = Screen()
        self.noise = Sound()
    def __init__(
            self,
            left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C,
            bazooka_blast_motor_port: str = OUTPUT_A,
            touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3):
        super().__init__(
            left_motor=left_foot_motor_port, right_motor=right_foot_motor_port,
            polarity=Motor.POLARITY_NORMAL)
        
        self.bazooka_blast_motor = MediumMotor(address=bazooka_blast_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.screen = Screen()
        self.speaker = Sound()
Ejemplo n.º 4
0
    def __init__(
            self,
            back_foot_motor_port: str = OUTPUT_C, front_foot_motor_port: str = OUTPUT_B,
            gear_motor_port: str = OUTPUT_A,
            touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3,
            ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1):
        self.front_foot_motor = LargeMotor(address=front_foot_motor_port)
        self.back_foot_motor = LargeMotor(address=back_foot_motor_port)

        self.gear_motor = MediumMotor(address=gear_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.remote_control = RemoteControl(sensor=self.ir_sensor,
                                            channel=ir_beacon_channel)
Ejemplo n.º 5
0
    def __init__(self,
                 left_foot_track_motor_port: str = OUTPUT_B,
                 right_foot_track_motor_port: str = OUTPUT_C,
                 bazooka_blast_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 driving_ir_beacon_channel: int = 1,
                 shooting_ir_beacon_channel: int = 2):
        self.left_foot_track_motor = LargeMotor(
            address=left_foot_track_motor_port)
        self.right_foot_track_motor = LargeMotor(
            address=right_foot_track_motor_port)

        self.bazooka_blast_motor = MediumMotor(
            address=bazooka_blast_motor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.driving_remote_control = RemoteControl(
            sensor=self.ir_sensor, channel=driving_ir_beacon_channel)
        self.shooting_remote_control = RemoteControl(
            sensor=self.ir_sensor, channel=shooting_ir_beacon_channel)

        self.touch_sensor = TouchSensor(address=INPUT_1)
        self.color_sensor = ColorSensor(address=INPUT_3)

        self.leds = Leds()
        self.screen = Screen()

        assert self.left_foot_track_motor.connected
        assert self.right_foot_track_motor.connected
        assert self.bazooka_blast_motor.connected

        assert self.ir_sensor.connected
        assert self.touch_sensor.connected
        assert self.color_sensor.connected

        # reset the motors
        for motor in (self.left_foot_track_motor, self.right_foot_track_motor,
                      self.bazooka_blast_motor):
            motor.reset()
            motor.position = 0
            motor.stop_action = Motor.STOP_ACTION_BRAKE

        self.draw_face()
Ejemplo n.º 6
0
    def __init__(
            self,
            left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C,
            bazooka_blast_motor_port: str = OUTPUT_A,
            touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3,
            ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1):
        super().__init__(
            left_motor_port=left_foot_motor_port, right_motor_port=right_foot_motor_port,
            ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel)
        
        self.bazooka_blast_motor = MediumMotor(address=bazooka_blast_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.leds = Leds()
        self.screen = Screen()
        self.speaker = Sound()
Ejemplo n.º 7
0
    def __init__(self,
                 turn_motor_port: str = OUTPUT_A,
                 move_motor_port: str = OUTPUT_B,
                 scare_motor_port: str = OUTPUT_D,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.turn_motor = MediumMotor(address=turn_motor_port)
        self.move_motor = LargeMotor(address=move_motor_port)
        self.scare_motor = LargeMotor(address=scare_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.beacon = RemoteControl(sensor=self.ir_sensor,
                                    channel=ir_beacon_channel)

        self.noise = Sound()
    def __init__(
            self,
            left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C,
            bazooka_blast_motor_port: str = OUTPUT_A,
            touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3,
            ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1):
        self.left_foot_motor = LargeMotor(address=left_foot_motor_port)
        self.right_foot_motor = LargeMotor(address=right_foot_motor_port)
        
        self.bazooka_blast_motor = MediumMotor(address=bazooka_blast_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.remote_control = RemoteControl(sensor=self.ir_sensor,
                                            channel=ir_beacon_channel)

        self.screen = Screen()
        self.speaker = Sound()
Ejemplo n.º 9
0
    def __init__(self,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 catapult_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor=left_motor_port,
                         right_motor=right_motor_port,
                         polarity=Motor.POLARITY_NORMAL)

        self.catapult_motor = MediumMotor(address=catapult_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.beacon = RemoteControl(sensor=self.ir_sensor, channel=1)

        self.speaker = Sound()
Ejemplo n.º 10
0
    def __init__(self,
                 sting_motor_port: str = OUTPUT_D,
                 go_motor_port: str = OUTPUT_B,
                 claw_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.sting_motor = LargeMotor(address=sting_motor_port)
        self.go_motor = LargeMotor(address=go_motor_port)
        self.claw_motor = MediumMotor(address=claw_motor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel
        self.beacon = RemoteControl(sensor=self.ir_sensor,
                                    channel=self.ir_beacon_channel)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.dis = Screen()
        self.speaker = Sound()
Ejemplo n.º 11
0
#!/usr/bin/env python3

from ev3dev.ev3 import ColorSensor, INPUT_1, INPUT_2, OUTPUT_A, OUTPUT_B, LargeMotor, Button, GyroSensor, Sound, UltrasonicSensor
from PID import PID
from os import system
from time import sleep
from json import dump, load

# Instanciando sensores
cl_left = ColorSensor(address=INPUT_1)
cl_right = ColorSensor(address=INPUT_2)
gyro = GyroSensor('in3')
sonic = UltrasonicSensor('in4')

# Instanciando motores
m_left = LargeMotor(address=OUTPUT_A)
m_right = LargeMotor(address=OUTPUT_B)

# Verificando se os sensores/motores estão conectados
assert cl_left.connected
assert cl_right.connected
assert gyro.connected
assert sonic.connected
assert m_left.connected
assert m_right.connected

# Definindo modo reflectância
cl_left.mode = 'COL-REFLECT'
cl_right.mode = 'COL-REFLECT'

gyro.mode = 'GYRO-ANG'
Ejemplo n.º 12
0
# -- MOTORS --
mLeft = LargeMotor('outD')
mRight = LargeMotor('outC')

mRight.polarity = 'inversed'
mLeft.polarity = 'inversed'

# -- SENSORS --
sSonic = UltrasonicSensor('in1')
sSonic.mode = UltrasonicSensor.MODE_US_DIST_CM

sGyro = GyroSensor('in2')
sGyro.mode = GyroSensor.MODE_GYRO_ANG

cLeft = ColorSensor('in3')
cRight = ColorSensor('in4')


""" Marks which side of the robot the line is on. """
class MoveDir(IntEnum):
    LINE_LEFT = -1
    LINE_RIGHT = 1

""" Minimum and maximum reflectance the line sensor sees. """
MIN_REFL = 1
MAX_REFL = 60
""" Target value of reflectance for staying on line. """
TARGET_REFL = 50

"""
Ejemplo n.º 13
0
#!/usr/bin/env python3

from ev3dev.ev3 import (Motor, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B,
                        OUTPUT_C, TouchSensor, ColorSensor, INPUT_1, INPUT_3,
                        Screen, Sound)

from multiprocessing import Process
from PIL import Image
from time import time

LEFT_FOOT_MOTOR = LargeMotor(address=OUTPUT_B)
RIGHT_FOOT_MOTOR = LargeMotor(address=OUTPUT_C)
MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A)

COLOR_SENSOR = ColorSensor(address=INPUT_3)
TOUCH_SENSOR = TouchSensor(address=INPUT_1)

SCREEN = Screen()
SPEAKER = Sound()


def run_away_whenever_dark():
    while True:
        if COLOR_SENSOR.ambient_light_intensity < 5:  # 15 not dark enough
            SCREEN.image.paste(
                im=Image.open('/home/robot/image/Middle left.bmp'), box=(0, 0))
            SCREEN.update()

            LEFT_FOOT_MOTOR.run_timed(
                speed_sp=-800,  # degrees per second
                time_sp=1500,  # miliseconds
Ejemplo n.º 14
0
#!/usr/bin/env python3

from time import sleep
from ev3dev.ev3 import LargeMotor, GyroSensor, UltrasonicSensor, ColorSensor, Sound
from os import system

system('setfont Lat15-TerminusBold14')

cl_left = ColorSensor('in1')
cl_right = ColorSensor('in4')
l = LargeMotor('outA')
r = LargeMotor('outC')
gyro = GyroSensor('in2')
sonic = UltrasonicSensor('in3')


gradient = []
count = 0

while 1:
	l.run_forever(speed_sp=-300)
	r.run_forever(speed_sp=-300)

	count += 1

	if(count % 25 == 0):
		count = 1

		gradient.append((cl_left.value(), cl_right.value()))

		if(len(gradient) > 10):
Ejemplo n.º 15
0
from ev3dev.ev3 import ColorSensor
import time

sL = ColorSensor('in1')
sR = ColorSensor('in4')

charcount = 40
mi = 0
ma = 100


def progressbar(val):
    t = (val - mi) / (ma - mi)
    markedcount = round(charcount * t)
    return '#' * markedcount + ' ' * (charcount - markedcount)


def intstr(val):
    return ('   ' + str(val))[-3:]


while True:
    l, r = sL.value(), sR.value()
    print("L:" + intstr(l) + "|" + progressbar(l) + "|, " + "R:" + intstr(r) +
          "|" + progressbar(r) + "|",
          end='\r')
    time.sleep(0.1)
Ejemplo n.º 16
0
# -*- coding: utf-8 -*-

from ev3dev.ev3 import ColorSensor, LargeMotor, Button, GyroSensor, Sound, UltrasonicSensor, MediumMotor
from PID import PID
from os import system
from time import sleep
from json import dump, load
import threading
import time
import math

# Alterando fonte do brick
system('setfont Lat15-TerminusBold14')

# Instanciando sensores
cl_left = ColorSensor('in1')
cl_right = ColorSensor('in4')
gyro = GyroSensor('in2')
sonic = UltrasonicSensor('in3')

# Instanciando motores
m_right = LargeMotor('outD')
m_left = LargeMotor('outA')
m_garra = LargeMotor('outC')

try:
	# Verificando se os sensores/motores estão conectados
	assert cl_left.connected
	assert cl_right.connected
	assert gyro.connected
	assert sonic.connected
Ejemplo n.º 17
0
#!/usr/bin/env python3

from time import sleep
from ev3dev.ev3 import LargeMotor, GyroSensor, UltrasonicSensor, ColorSensor
from os import system

system('setfont Lat15-TerminusBold14')

cl_left = ColorSensor('in1')
cl_right = ColorSensor('in2')
l = LargeMotor('outB')
r = LargeMotor('outA')
gyro = GyroSensor('in3')
sonic = UltrasonicSensor('in4')


def girar(graus):
    pos0 = gyro.value()
    if (graus > 0):
        while gyro.value() < pos0 + graus:
            l.run_forever(speed_sp=-500)
            r.run_forever(speed_sp=250)
    else:
        while gyro.value() > pos0 + graus:
            l.run_forever(speed_sp=250)
            r.run_forever(speed_sp=-500)

    l.stop()
    r.stop()

Ejemplo n.º 18
0
#!/usr/bin/env python3

from ev3dev.ev3 import ColorSensor
from time import sleep

# Connect to sensor
sensor = ColorSensor()

sensor.mode = 'COL-COLOR'

colors = ('unknown', 'black', 'blue', 'green', 'yellow', 'red', 'white',
          'brown')

while True:
    print(colors[sensor.value()])
    sleep(0.5)
Ejemplo n.º 19
0
#!/usr/bin/env python3

from ev3dev.ev3 import LargeMotor, ColorSensor, UltrasonicSensor, InfraredSensor, MediumMotor, INPUT_1, INPUT_2, INPUT_3, INPUT_4

# MOTORES
esq = LargeMotor('outA')
dir = LargeMotor('outB')
motor_garra = LargeMotor('outC')
#motor_sensor = MediumMotor('outD')

# SENSORES
sensor_esq = ColorSensor(address=INPUT_1)
sensor_dir = ColorSensor(address=INPUT_2)
sensor_frente = UltrasonicSensor(address=INPUT_3)
sensor_lado = UltrasonicSensor(address=INPUT_4)

sensor_esq.mode = 'COL-REFLECT'
sensor_dir.mode = 'COL-REFLECT'
Ejemplo n.º 20
0
def main():
    data = None

    if True:
        with open('zemljevid.json') as f:
            data = json.load(f)
    else:
        resource = urllib.request.urlopen(
            'http://192.168.0.200:8080/zemljevid.json')
        content = resource.read()
        content = content.decode("utf-8")
        data = json.loads(content)

    os.system('setfont Lat15-TerminusBold14')
    if os.path.exists("logs"):
        import shutil
        shutil.rmtree("logs")
    os.mkdir("logs")

    mL = LargeMotor('outB')
    mL.stop_action = 'hold'
    mR = LargeMotor('outC')
    mR.stop_action = 'hold'
    global cl
    cl = ColorSensor()
    cl.mode = 'COL-COLOR'
    gy = GyroSensor()
    gy.mode = 'GYRO-RATE'
    gy.mode = 'GYRO-ANG'

    # Give gyro a bit of time to start
    time.sleep(3)

    global start_time
    start_time = time.time()
    debug_print("Start time: ", start_time)

    start = [0, 0]
    locations = []

    for key, item in data.items():
        if key == "start":
            start = item
        else:
            locations.append(item)

    # Sort by distance, TODO might be better to minimize turns by prioritizing victims that are in the same line
    locations = sorted(
        locations,
        key=lambda x: abs(start[0] - x[0]) + abs(start[1] - x[1]),
        reverse=False)
    current_location = start

    def reset_neighbourhood_search():
        global searching_neighbourhood
        nonlocal neighbourhood_locations
        searching_neighbourhood = False
        neighbourhood_locations = []

    global searching_neighbourhood
    searching_neighbourhood = False
    neighbourhood_locations = []

    while locations:
        next_location = locations.pop(0)
        go_to_location(x=next_location[0],
                       y=next_location[1],
                       current_x=current_location[0],
                       current_y=current_location[1],
                       mL=mL,
                       mR=mR,
                       gyro=gy)
        current_location = next_location

        color = cl.value()
        if color == 1:  # BLACK: START, 2 second beep
            debug_print("Color sensor: START")
            beep(1, 2000)
            reset_neighbourhood_search()
            locations = sorted(
                locations,
                key=lambda x: abs(start[0] - x[0]) + abs(start[1] - x[1]),
                reverse=False)
        elif color == 2:  # BLUE: good condition, 1 beep
            debug_print("Color sensor: BLUE")
            beep(1)
            reset_neighbourhood_search()
            locations.insert(0, start)
        elif color == 4:  # YELLOW: critical condition, 2 beeps
            debug_print("Color sensor: YELLOW")
            beep(2)
            reset_neighbourhood_search()
            locations.insert(0, start)
        elif color == 5:  # RED: passed away, 3 beeps
            debug_print("Color sensor: RED")
            beep(3)
            reset_neighbourhood_search()
            #locations.insert(0, start)
            locations = sorted(locations,
                               key=lambda x: abs(current_location[0] - x[0]) +
                               abs(current_location[1] - x[1]),
                               reverse=False)
        else:
            debug_print("Color sensor: UNKNOWN (" + str(color) + ")")
            #locations.insert(0, start)
            if not searching_neighbourhood:
                searching_neighbourhood = True
                radius = 5
                for area in range(1, 20):
                    neighbourhood_locations.append([
                        next_location[0] + radius * area,
                        next_location[1] - radius * area
                    ])
                    neighbourhood_locations.append([
                        next_location[0] + radius * area,
                        next_location[1] + radius * area
                    ])
                    neighbourhood_locations.append([
                        next_location[0] - radius * area,
                        next_location[1] + radius * area
                    ])
                    neighbourhood_locations.append([
                        next_location[0] - radius * area,
                        next_location[1] - radius * area
                    ])
            locations.insert(0, neighbourhood_locations[0])
            neighbourhood_locations = neighbourhood_locations[1:]

    # # Rotate back to original orientation
    # angle = calculate_angle(0, gy.value())
    # rotate_to_angle(angle, mL, mR, gy)

    # for i in range (16):
    #     rotate_to_angle(90, mL, mR, gy)
    #     rotate_to_angle(0, mL, mR, gy)

    #for _ in range (5):
    #rotate_to_angle(89, mL, mR, gy)
    #rotate_to_angle(178, mL, mR, gy)
    #rotate_to_angle(270, mL, mR, gy)
    #rotate_to_angle(180, mL, mR, gy)
    #rotate_to_angle(90, mL, mR, gy)
    #rotate_to_angle(0, mL, mR, gy)

    #rotate_to_angle(-91, mL, mR, gy)
    #rotate_to_angle(-182, mL, mR, gy)
    #rotate_to_angle(-2, mL, mR, gy)

    #rotate_to_angle(89, mL, mR, gy)
    #rotate_to_angle(178, mL, mR, gy)
    #rotate_to_angle(-2, mL, mR, gy)
    #rotate_to_angle(87, mL, mR, gy)
    #rotate_to_angle(176, mL, mR, gy)

    # for i in range (5):
    #     drive_for_centimeters(50, mL, mR, gy, 0)
    #     rotate_to_angle(0, mL, mR, gy)
    #     drive_for_centimeters(-50, mL, mR, gy, 0)
    #     rotate_to_angle(0, mL, mR, gy)

    debug_print("End time: ", time.time())
Ejemplo n.º 21
0
#!/usr/bin/env python3

from ev3dev.ev3 import MediumMotor, LargeMotor, ColorSensor, Button
from Classes.Motores import *
import csv
from time import time, sleep
from os import system, remove
system('setfont Lat15-TerminusBold14')

# Motores
M_PORTA = MediumMotor('outC')
M_ESQUERDO = LargeMotor("outA")
M_DIREITO = LargeMotor("outB")

# Sensores de Cor
CL1 = ColorSensor("in1")
CL2 = ColorSensor("in2")
CL1.mode = "RGB-RAW"
CL2.mode = "RGB-RAW"

velocidade_dir = 350
velocidade_esq = 360
CL1_VERDE = []
CL2_VERDE = []

sensor1_r = []
sensor1_g = []
sensor1_b = []
sensor2_r = []
sensor2_g = []
sensor2_b = []
Ejemplo n.º 22
0
M_PORTA = LargeMotor("outC")
M_ESQUERDO = LargeMotor("outA")
M_DIREITO = LargeMotor("outB")

# Sensores infravermelhos
PROX1 = InfraredSensor("in4")
PROX2 = InfraredSensor("in3")
PROX1.mode = "IR-PROX"
PROX2.mode = "IR-PROX"

# Giroscopio
GYRO = GyroSensor("in1")
GYRO.mode = "GYRO-ANG"

# Sensor de cor
COLOR = ColorSensor("in2")
COLOR.mode = "COL-COLOR"

# Variaveis usadas durante o programa
# Constantes
TEMPO_CURVA_90 = 1700
TEMPO_CURVA_180 = 3400
ANGULO_CURVA_180 = 180
ANGULO_CURVA_90 = 90
ANGULO_DESVIO = 9
VELOCIDADE_CURVA = 400
VELOCIDADE_ATUAL = 350
KP = 2.5
KI = 0
KD = 1
OFFSET = 0
Ejemplo n.º 23
0
COL_VALUE = 50
SPEED = 250
FW = 560
BACK = -140
TURN = 242
 map = []


A = ev3.LargeMotor('outA')
B = ev3.LargeMotor('outB')
C = ev3.Motor('outC')
ts = TouchSensor()
#~ us = UltrasonicSensor()
#~ us.mode='US-DIST-CM'		# Put the US sensor into distance mode.
cl = ColorSensor()
cl.mode='COL-REFLECT'
bt = Button()

#~ gy = GyroSensor()
#~ gy.mode='GYRO-ANG'			# Put the gyro sensor into ANGLE mode.

def led_ready():
	Leds.all_off()

def led_start():
	Leds.set_color(Leds.LEFT,  Leds.GREEN)
	Leds.set_color(Leds.RIGHT, Leds.GREEN)

def led_end():
	Leds.set_color(Leds.LEFT,  Leds.ORANGE)
Ejemplo n.º 24
0
def run(fun):
    # Connect two motors and two (different) light sensors
    mL = LargeMotor('outC')
    mR = LargeMotor('outB')

    sL = ColorSensor('in1')
    sR = ColorSensor('in4')
    gy = GyroSensor('in3')

    # Check if the sensors are connected
    assert sL.connected, "Left ColorSensor is not connected"
    assert sR.connected, "Right ColorSensor is not conected"
    assert gy.connected, "Gyro is not connected"

    gyro = Gyro()
    light_sensors = LightSensors()
    encoder = Encoder()

    # Set the motor mode
    mL.polarity = "normal"  # "inversed"
    mR.polarity = "normal"  # "inversed"
    mL.position = 0
    mR.position = 0

    def stop_motors():
        mL.run_direct()
        mR.run_direct()
        mL.duty_cycle_sp = 0
        mR.duty_cycle_sp = 0

    stop_motors()

    # The example doesn't end on its own.
    # Use CTRL-C to exit it (needs command line).
    # This is a generic way to be informed
    # of this event and then take action.
    def signal_handler(sig, frame):
        stop_motors()
        print('Shut down gracefully')
        exit(0)

    # Install the signal handler for CTRL+C
    signal(SIGINT, signal_handler)
    print('Press Ctrl+C to exit')

    per = {
        'mL': mL,
        'mR': mR,
        'sL': sL,
        'sR': sR,
        'gy': gy
    }

    light_sensors.init(per)
    gyro.init(gy)

    # Endless loop reading sensors and controlling motors.
    last_log = time()
    last_now = time()
    max_dt = 0
    dts = 0
    speed_mL = None
    speed_mR = None

    while True:
        state = {}
        state = light_sensors(per, state)
        state = encoder(per, state)
        state = gyro(per, state)
        state = fun(per, state)

        max_speed = 40 * TICKS_PER_CM
        _speed_mL = state.get('mL', 0)
        if _speed_mL != speed_mL:
            speed_mL = _speed_mL
            mL.run_forever(speed_sp=speed_mL/100 * max_speed)
        _speed_mR = state.get('mR', 0)
        if _speed_mR != speed_mR:
            speed_mR = _speed_mR
            mR.run_forever(speed_sp=speed_mR/100 * max_speed)

        dts += 1
        now = time()
        dt = now - last_now
        last_now = now

        if dt > max_dt: max_dt = dt
        if now - last_log > 5.0:
            last_log = now
            print("avg fps: ", dts / 5.0)
            print('min fps: ', 1 / max_dt)
            max_dt = 0
            dts = 0
Ejemplo n.º 25
0
from ev3dev.ev3 import ColorSensor, Motor, UltrasonicSensor
from .driver import Driver
from .linesensorarray import LineSensorArray
from .colorsensor import ColorSensor


driver              = Driver('outA', 'outB')
arm                 = Motor('outC')
claws               = Motor('outD')
line_sensor_array   = LineSensorArray('in1')
color_sensors       = (ColorSensor('in2'), ColorSensor('in3'))
ultrasonic_sensor   = UltrasonicSensor('in4')
# central_line_sensor = ColorSensor('in4')
Ejemplo n.º 26
0
#!/usr/bin/env python3

from ev3dev.ev3 import UltrasonicSensor, ColorSensor, Button
from os import system
import paho.mqtt.client as mqtt

# Sensores ultrasonicos
ULTRA1 = UltrasonicSensor("in1")
ULTRA2 = UltrasonicSensor("in2")
ULTRA1.mode = "US-DIST-CM"
ULTRA2.mode = "US-DIST-CM"

# Sensores de Cor
CL1 = ColorSensor("in3")
CL2 = ColorSensor("in4")
CL1.mode = "COL-COLOR"
CL2.mode = "COL-COLOR"

DISTANCIA_BONECOS = 20
cor_anterior1 = ""
cor_anterior2 = ""
client = mqtt.Client()
botao = Button()
client.connect("localhost", 1883, 60)
client.loop_start()


def on_disconnect(client, userdata, rc=0):
    client.loop_stop()

Ejemplo n.º 27
0
# WIP

from ev3dev.ev3 import ColorSensor, Button
from time import time
import move

cs = ColorSensor('in1')
cs.mode = 'COL-REFLECT'
t0 = time()
btn = Button()

direction = 1
speed = 150
dt = 500
stop_action = "coast"


def main():

    kp = 1  # proportional gain
    ki = 0  # integral gain
    kd = 0  # derivative gain

    integral = 0
    prev_error = 0

    target = cs.value()

    while time() - t0 < 30:
        if btn.any(): break