def test_infrared_sensor(self):
        clean_arena()
        populate_arena([('infrared_sensor', 0, 'in1')])

        s = InfraredSensor()

        self.assertEqual(s.device_index, 0)
        self.assertEqual(s.bin_data_format, 's8')
        self.assertEqual(s.bin_data('<b'), (16, ))
        self.assertEqual(s.num_values, 1)
        self.assertEqual(s.address, 'in1')
        self.assertEqual(s.value(0), 16)
        self.assertEqual(s.mode, "IR-PROX")

        s.mode = "IR-REMOTE"
        self.assertEqual(s.mode, "IR-REMOTE")

        val = s.proximity
        # Our test environment writes to actual files on disk, so while "seek(0) write(...)" works on the real device, it leaves trailing characters from previous writes in tests. "s.mode" returns "IR-PROXTE" here.
        self.assertEqual(s.mode, "IR-PROX")
        self.assertEqual(val, 16)

        val = s.buttons_pressed()
        self.assertEqual(s.mode, "IR-REMOTE")
        self.assertEqual(val, [])
Exemple #2
0
def init_hardware():
    global button, display, leds, drive, infraredSensor

    drive = LargeMotor(OUTPUT_B)
    infraredSensor = InfraredSensor(INPUT_3)
    infraredSensor.mode = InfraredSensor.MODE_IR_PROX
    leds = Leds()

    # ultrasonicSensor = UltrasonicSensor(INPUT_2)
    # ultrasonicSensor.mode = UltrasonicSensor.MODE_US_DIST_CM

    display = Display()
    button = Button()
    button.on_enter = btn_on_enter
Exemple #3
0
def main():
    ''' main function to initialize settings'''

    # initialize tank drive
    tank_drive = MoveTank(OUTPUT_B, OUTPUT_C)

    # initialize infrared sensor in proximity mode
    infrared = InfraredSensor()
    infrared.mode = 'IR-PROX'

    # intialize sounds
    sounds = Sound()

    # call run function
    run(tank_drive, infrared, sounds)
    def test_infrared_sensor(self):
        clean_arena()
        populate_arena([('infrared_sensor', 0, 'in1')])

        s = InfraredSensor()

        self.assertEqual(s.device_index, 0)
        self.assertEqual(s.bin_data_format, 's8')
        self.assertEqual(s.bin_data('<b'), (16, ))
        self.assertEqual(s.num_values, 1)
        self.assertEqual(s.address, 'in1')
        self.assertEqual(s.value(0), 16)
        self.assertEqual(s.mode, "IR-PROX")

        s.mode = "IR-REMOTE"
        self.assertEqual(s.mode, "IR-REMOTE")

        val = s.proximity
        self.assertEqual(s.mode, "IR-PROX")
        self.assertEqual(val, 16)

        val = s.buttons_pressed()
        self.assertEqual(s.mode, "IR-REMOTE")
        self.assertEqual(val, [])
Exemple #5
0
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, MoveSteering, OUTPUT_A, OUTPUT_B, OUTPUT_C, SpeedPercent, MoveTank
from ev3dev2.sound import Sound
from ev3dev2.sensor.lego import InfraredSensor
from time import sleep

# Tank pair
tank_pair = MoveTank(OUTPUT_B, OUTPUT_C)

# Infrared remote
ir = InfraredSensor()
ir.mode = ir.MODE_IR_REMOTE

sound = Sound()
# Play a standard beep on boot up
sound.beep()

while True:
    button_code = ir.value()
    if button_code == ir.TOP_LEFT:
        tank_pair.on(left_speed=100, right_speed=100)
    elif button_code == ir.TOP_LEFT_BOTTOM_LEFT:
        tank_pair.on(left_speed=100, right_speed=0)
    elif button_code == ir.TOP_LEFT_TOP_RIGHT:
        tank_pair.on(left_speed=0, right_speed=100)
    elif button_code == ir.TOP_RIGHT:
        tank_pair.on(left_speed=-100, right_speed=100)
    elif button_code == ir.BOTTOM_RIGHT:
        tank_pair.on(left_speed=-100, right_speed=-100)
    elif button_code == ir.BOTTOM_LEFT:
        tank_pair.on(left_speed=100, right_speed=-100)
Exemple #6
0
from ev3dev2.sensor.lego import ColorSensor, InfraredSensor

pino_ir = '4'
pino_corE = '2'
pino_corD = '3'

fator_ir_distancia = 0.9

ir = InfraredSensor()
ir.mode = 'IR-PROX'

corE = ColorSensor(pino_corE)
corD = ColorSensor(pino_corD)

corE.mode = 'RGB-RAW'
corD.mode = 'RGB-RAW'


def viu_verde(r, g, b):
    lim_inf = 25
    lim_sup = 80
    prop = 1.5
    if (r < lim_sup and g < lim_sup and g > lim_inf):
        if g > r * prop:
            return True
    return False


def le_rgb(sensor):
    return corE.red, corE.green, corE.blue

p3 = LegoPort(INPUT_3)
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/brickpi3.html#brickpi3-in-port-modes
p3.mode = 'ev3-uart'
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
p3.set_device = 'lego-ev3-ir'


# Connect infrared to sensor port
ir = InfraredSensor(INPUT_3)

# allow for some time to load the new drivers
time.sleep(0.5)

ir.mode = 'IR-SEEK'
#ir1ProxVal = 100
#prev_ir1ProxVal = 0
ir1DistVal = 0
prev_ir1DistVal = 0
ir1HeadVal = 0
prev_ir1HeadVal = 0

#ir2ProxVal = 100
#prev_ir2ProxVal = 0
ir2DistVal = 0
prev_ir2DistVal = 0
ir2HeadVal = 0
prev_ir2HeadVal = 0

Exemple #8
0
#!/usr/bin/env python3

from ev3dev2.control.GyroBalancer import GyroBalancer, GracefulShutdown
from ev3dev2.sensor.lego import InfraredSensor

import time

IR_SENSOR = InfraredSensor()
IR_SENSOR.mode = IR_SENSOR.MODE_IR_REMOTE

BALANC3R = GyroBalancer()
BALANC3R.balance()

while True:
    button_code = IR_SENSOR.value()

    if button_code == IR_SENSOR.TOP_LEFT_TOP_RIGHT:
        BALANC3R.move_forward()

    elif button_code == IR_SENSOR.TOP_LEFT_BOTTOM_RIGHT:
        BALANC3R.rotate_right()

    elif button_code == IR_SENSOR.BOTTOM_LEFT_TOP_RIGHT:
        BALANC3R.rotate_left()

    elif button_code == IR_SENSOR.BOTTOM_LEFT_BOTTOM_RIGHT:
        BALANC3R.move_backward()

    else:
        BALANC3R.stop()
Exemple #9
0
        self.wheel_diameter = wheel_diameter
        self.axle_track = axle_track

    def drive(self, millimeters, rotations):
        self.left_motor.on_for_rotations(millimeters, rotations)
        self.right_motor.on_for_rotations(millimeters, rotations)

    def drive_time(self, distance, x, milliseconds):
        self.left_motor.on_for_seconds(milliseconds / 1000)
        self.right_motor.on_for_seconds(milliseconds / 1000)


sound = Sound()
#ultrasonicSensor = UltrasonicSensor()
infraredSensor = InfraredSensor(INPUT_1)
infraredSensor.mode = infraredSensor.MODE_IR_REMOTE
# Play a sound.
sound.beep()
# Initialize the Ultrasonic Sensor. It is used to detect
# obstacles as the robot drives around.
#obstacle_sensor = infraredSensor(INPUT_1)
# Initialize two motors with default settings on Port B and Port C.
# These will be the left and right motors of the drive base.
left_motor = Motor(OUTPUT_B)
right_motor = Motor(OUTPUT_C)
# The wheel diameter of the Robot Educator is 56 millimeters.
wheel_diameter = 56
# The axle track is the distance between the centers of each of the wheels.
# For the Robot Educator this is 114 millimeters.
axle_track = 114
# The DriveBase is composed of two motors, with a wheel on each motor.
Exemple #10
0
"""Make BALANC3R robot stay upright and move in a pattern."""

import logging
import time
from ev3dev2.control.GyroBalancer import GyroBalancer, GracefulShutdown
from ev3dev2.sensor.lego import InfraredSensor

# Logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)5s: %(message)s')
log = logging.getLogger(__name__)
log.info("Starting BALANC3R")

# Infrared remote
remote = InfraredSensor()
remote.mode = remote.MODE_IR_REMOTE

# Balance robot
robot = GyroBalancer()
robot.balance()

try:
    # Wait for top left button on remote to be pressed
    button_code = remote.value()
    while button_code != remote.TOP_LEFT:
        button_code = remote.value()
        time.sleep(0.5)  # Give CPU a rest

    # Move robot in a simple pattern
    robot.rotate_left(seconds=3)
    robot.rotate_right(seconds=3)
Exemple #11
0
from time import sleep
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, LargeMotor, MediumMotor, MoveTank, SpeedPercent, MoveSteering
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3, INPUT_4
from ev3dev2.sensor.lego import InfraredSensor
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.button import Button

CHANNEL_OPPONENT = 2

canRun = False

shooter = MediumMotor(OUTPUT_A)
sensor = InfraredSensor(INPUT_4)
sensor.mode = "IR-SEEK"
tank_drive = MoveSteering(OUTPUT_C, OUTPUT_B)
btn = Button()

# Add logging
logging.basicConfig(level=logging.DEBUG, format="%(lineno)s: %(message)s")
log = logging.getLogger(__name__)


# Functions
def shoot_ball():
    shooter.on_for_rotations(SpeedPercent(100), 3)


# testar
def shoot_ball_forever():
Exemple #12
0
import time
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_D, SpeedPercent, MoveTank, MoveSteering, MediumMotor, OUTPUT_B
from ev3dev2.sensor import INPUT_1, INPUT_4, INPUT_2, INPUT_3
from ev3dev2.sensor.lego import TouchSensor, InfraredSensor, ColorSensor
from ev3dev2.led import Leds
from ev3dev2.button import Button
from ev3dev2.sound import Sound
ON = True
OFF = False

music = Sound()
music.play_file("Confirm.wav")
tank_drive = MoveTank(OUTPUT_A, OUTPUT_D)
steering_drive = MoveSteering(OUTPUT_A, OUTPUT_D)
ir = InfraredSensor()
ir.mode = "IR-PROX"
touch_sensor = TouchSensor()
touch_sensor.mode = "TOUCH"
color_arm = MediumMotor(OUTPUT_B)
display_button = Button()
color_sensor = ColorSensor()


def deploy_color_sensor():
    color_arm.on_for_rotations(SpeedPercent(5), 0.30)
    time.sleep(4.5)
    if color_sensor.color == 1:
        music.speak("I found something black on the surface of Mars")
    elif color_sensor.color == 2:
        music.speak("I found water on the surface of Mars")
    elif color_sensor.color == 3: