示例#1
0
#!/usr/bin/env python3
from sys import stderr
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor.lego import Sensor
from ev3dev2.sensor import INPUT_1, INPUT_2
from ev3dev2.display import Display

lcd = Display()

# Connect Pixy camera
pixy = Sensor()

# Connect TouchSensor
ts = TouchSensor(address=INPUT_1)

# Set mode
pixy.mode = 'ALL'

while not ts.value():
    lcd.clear()
    if pixy.value(0) != 0:  # Object with SIG1 detected
        x = pixy.value(1)
        y = pixy.value(2)
        w = pixy.value(3)
        h = pixy.value(4)
        dx = int(w / 2)  # Half of the width of the rectangle
        dy = int(h / 2)  # Half of the height of the rectangle
        xb = x + int(w / 2)  # X-coordinate of bottom-right corner
        yb = y - int(h / 2)  # Y-coordinate of the bottom-right corner
        lcd.draw.rectangle((dx, dy, xb, yb), fill='black')
        lcd.update()
示例#2
0
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device 

dmesg -w

"""

# set device name and i2c address (in hex)
p1.set_device = 'ms-absolute-imu 0x11'

# allow for some time for setup
time.sleep(0.5)

# Connect sensor to sensor port 1
imu = Sensor(INPUT_1)

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

print(
    "#######################################################################")
print(
    "################   starting in compass mode   #########################")
print(
    "#######################################################################")

imu.mode = 'COMPASS'

compassVal = 0
prev_compassVal = 0
示例#3
0
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
""" https://github.com/ev3dev/ev3dev/issues/640

dlech commented on May 15, 2016
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device """

# set device name and i2c address (in hex)
p2.set_device = 'ht-nxt-compass 0x01'

# allow for some time for setup
time.sleep(0.5)

# Connect ht-nxt-compass sensor to sensor port
cmp = Sensor(INPUT_2)

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

# cmp.mode = 'COMPASS'  ###  Not req'd for this sensor as it only has one mode

compassVal = 0
prev_compassVal = 0
compassGoal = 0  # with ht-compass mounted face-front, this translates to North

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'
    level=logging.INFO,
    format='%(asctime)s:%(levelname)s:%(threadName)s:%(message)s')
log = logging.getLogger(__name__)

port1 = INPUT_7
port4 = INPUT_8

legoPort1 = LegoPort(port1)
legoPort1.mode = 'nxt-i2c'
legoPort1.set_device = 'ms-absolute-imu 0x11'
# legoPort4 = LegoPort(port4)
# legoPort4.mode = 'nxt-i2c'
# legoPort4.set_device = 'ms-absolute-imu 0x11'

print('Pausing 2 seconds...')
time.sleep(2)
print('Done, creating sensor class object for {}, {}'.format(port1, port4))
sensor1 = Sensor(address='{}:i2c17'.format(port1))
# sensor4 = Sensor(address='{}:i2c17'.format(INPUT_4))
target_mode = 'ALL'
sensor1.mode = target_mode
# sensor4.mode = target_mode
while True:
    # log.info('Joint 3 ({}) X/Y/Z angle = {}/{}/{}'.format(INPUT_4, sensor4.value(0), sensor4.value(1), sensor4.value(2)))
    log.info('Joint 4 ({}) X/Y/Z angle = {}/{}/{}'.format(
        port1, sensor1.value(0), sensor1.value(1), sensor1.value(2)))

    time.sleep(0.125)

log.info('Done')
示例#5
0
""" https://github.com/ev3dev/ev3dev/issues/640

dlech commented on May 15, 2016
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device """

# set device name and i2c address (in hex)
p2.set_device = 'ht-nxt-compass 0x01'

# allow for some time for setup
time.sleep(0.5)

# Connect ht-nxt-compass sensor to sensor port
cmp = Sensor(INPUT_2)

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

# cmp.mode = 'COMPASS'  ###  Not req'd for this sensor as it only has one mode

compassVal = 0
prev_compassVal = 0
compassGoal = 0     # with ht-compass mounted face-front, this translates to North



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'
示例#6
0
""" https://github.com/ev3dev/ev3dev/issues/640

dlech commented on May 15, 2016
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device """

# set device name and i2c address (in hex)
p2.set_device = 'ms-absolute-imu 0x11'

# allow for some time for setup
time.sleep(0.5)

# Connect sensor to sensor port 1
imu = Sensor(INPUT_2)

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


print("#######################################################################")
print("###############   starting compass calibration   ######################")
print("#######################################################################")

print("")
print("  Sensor PORT 2  ")
print("")

imu.mode = 'COMPASS'
time.sleep(.5)
示例#7
0
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device 

dmesg -w

"""

# set device name and i2c address (in hex)
p4.set_device = 'lego-ev3-gyro'

# allow for some time for setup
time.sleep(0.5)

# Connect sensor to sensor port 1
gyro = Sensor(INPUT_4)

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


print("#######################################################################")
print("################    starting in gyro mode     #########################")
print("#######################################################################")

gyro.mode = 'GYRO-ANG'

gyroVal = 0
prev_gyroVal = 0

startTime = time.time()
示例#8
0
dmesg -w

"""

# set device name and i2c address (in hex)
p2.set_device = 'mi-xg1300l 0x01'

print("wait 4")

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

# p2 = LegoPort('spi0.1:S2:i2c1')

# Connect sensor to sensor port ?
mi = Sensor(INPUT_2)
# mi = Sensor('spi0.1:S2:i2c1')  # unclear if this change helped in any way or not
# mi = I2cSensor('spi0.1:S2:i2c1')  # unclear if this change helped in any way or not
# mi = I2cSensor(INPUT_2)  # unclear if this change helped in any way or not

print("wait 5")

# allow for some time to stabilze
time.sleep(3)

# mi.poll_ms = 0   # didn't work

# set the mi sensor mode
mi.command = 'RESET'

print("wait 6")
示例#9
0
from ev3dev2.motor import OUTPUT_A, LargeMotor, SpeedPercent
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import Sensor

p1 = LegoPort(INPUT_1)

# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/brickpi3.html#brickpi3-in-port-modes
p1.mode = 'nxt-analog'
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
p1.set_device = 'ht-nxt-gyro'

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

# Connect infrared to any sensor port
gyro = Sensor(INPUT_1)

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

gyroVal = 0
prev_gyroVal = 0

print(
    "#######################################################################")
print(
    "################  starting in proximity mode  #########################")
print(
    "#######################################################################")

startTime = time.time()
示例#10
0
time.sleep(0.5)

# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
p1.set_device = 'nxt-i2c-sensor'

# allow for some time for mode to setup
time.sleep(0.5)

# Connect imu to sensor port 1
# imu = Sensor(INPUT_1)
# imu = Sensor(address='serial0-0:S1:i2c1')
# imu = Sensor(address='serial0-0:S1:i2c17')
# imu = Sensor(address='serial0-0:S1:i2c34')
# imu = Sensor(address='serial0-0:S1:i2c22')
# imu = Sensor('serial0-0:S1:i2c11')
imu = Sensor('serial0-0:S1:i2c17')

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

# Connect imu to sensor port 1
# imu = Sensor(INPUT_1)
imu = Sensor(address='serial0-0:S1:i2c1')

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

# p1.set_device = 'ms-absolute-imu'
# time.sleep(0.5)

irProxVal = 0
示例#11
0
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device 

dmesg -w

"""

# set device name and i2c address (in hex)
p1.set_device = 'ht-nxt-compass 0x01'

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

# Connect sensor to sensor port 1
compass = Sensor(INPUT_1)

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

compassVal = 0
prev_compassVal = 0

print(
    "#######################################################################")
print(
    "####################     read compass     #############################")
print(
    "#######################################################################")

startTime = time.time()
示例#12
0
文件: demo.py 项目: dkinneyBU/EV3
def master_function():
    cl = ColorSensor()
    ts = TouchSensor()
    ir = InfraredSensor()
    sound = Sound()
    steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
    medium_motor = MediumMotor()
    pixy = Sensor(address=INPUT_2)
    # assert pixy.connected, "Error while connecting Pixy camera to port 1"
    # pixy.mode = 'SIG1'
    # lcd = Sensor.Screen()
    lcd = Display()

    def top_left_channel_1_action(state):
        move()

    def bottom_left_channel_1_action(state):
        move()

    def top_right_channel_1_action(state):
        move()

    def bottom_right_channel_1_action(state):
        move()

    def move():
        buttons = ir.buttons_pressed()  # a list
        if len(buttons) == 1:
            medium_motor.off()
            if buttons == ['top_left']:
                steer_pair.on(steering=0, speed=40)
            elif buttons == ['bottom_left']:
                steer_pair.on(steering=0, speed=-40)
            elif buttons == ['top_right']:
                steer_pair.on(steering=100, speed=30)
            elif buttons == ['bottom_right']:
                steer_pair.on(steering=-100, speed=30)
        elif len(buttons) == 2:
            steer_pair.off()
            if buttons == ['top_left', 'top_right']:
                medium_motor.on(speed_pct=10)
            elif buttons == ['bottom_left', 'bottom_right']:
                medium_motor.on(speed_pct=-10)
        else:  # len(buttons)==0
            medium_motor.off()
            steer_pair.off()

    # Associate the event handlers with the functions defined above
    ir.on_channel1_top_left = top_left_channel_1_action
    ir.on_channel1_bottom_left = bottom_left_channel_1_action
    ir.on_channel1_top_right = top_right_channel_1_action
    ir.on_channel1_bottom_right = bottom_right_channel_1_action

    opts = '-a 200 -s 150 -p 70 -v'
    speech_pause = 0
    while not ts.is_pressed:
        # rgb is a tuple containing three integers
        ir.process()
        red = cl.rgb[0]
        green = cl.rgb[1]
        blue = cl.rgb[2]
        intensity = cl.reflected_light_intensity
        print('{4} Red: {0}\tGreen: {1}\tBlue: {2}\tIntensity: {3}'.format(
            str(red), str(green), str(blue), str(intensity), speech_pause),
              file=stderr)
        lcd.clear()
        print(pixy.mode, file=stderr)
        if pixy.value(0) != 0:  # Object with SIG1 detected
            x = pixy.value(1)
            y = pixy.value(2)
            w = pixy.value(3)
            h = pixy.value(4)
            dx = int(w / 2)  # Half of the width of the rectangle
            dy = int(h / 2)  # Half of the height of the rectangle
            xb = x + int(w / 2)  # X-coordinate of bottom-right corner
            yb = y - int(h / 2)  # Y-coordinate of the bottom-right corner
            lcd.draw.rectangle((xa, ya, xb, yb), fill='black')
            lcd.update()
        speech_pause += 1
        if speech_pause == 200:
            # try replacing with ir.distance(), ir.heading()
            # or ir.heading_and_distance()
            distance = ir.heading_and_distance()
            if distance == None:
                # distance() returns None if no beacon detected
                str_en = 'Beacon off?'
            else:
                str_en = 'Beacon heading {0} and distance {1}'.format(
                    distance[0], distance[1])
            sound.speak(str_en, espeak_opts=opts + 'en+f5')
            print(str_en, file=stderr)
            speech_pause = 0
    str_en = 'Terminating program'
    sound.speak(str_en, espeak_opts=opts + 'en+f5')
示例#13
0
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
""" https://github.com/ev3dev/ev3dev/issues/640

dlech commented on May 15, 2016
    You are forgetting the I2C address for set_device

$ echo mx-pps58-nx 0x0C >/sys/class/lego-port/port0/set_device """

# set device name and i2c address (in hex)
p2.set_device = 'ms-absolute-imu 0x11'

# allow for some time for setup
time.sleep(0.5)

# Connect sensor to sensor port 1
imu = Sensor(INPUT_2)

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

imu.mode = 'COMPASS'

compassVal = 0
prev_compassVal = 0

tty.setcbreak(sys.stdin)

mL = LargeMotor(OUTPUT_A)
time.sleep(0.5)

mL.reset()
示例#14
0
#Definerer NTC parametere:
a = ntc()
a.beta = 3984
a.maxADC(5000)

#
#tempS = TemperatureSensor(Port.S4)


#Definerer port 4 til å være analog inngang:
p4 = LegoPort(INPUT_4)
p4.mode = 'nxt-analog'
p4.set_device = 'nxt-analog'

#Definerer at temperatursensoren er koblet til port 4: 
ntc_sensor = Sensor(INPUT_4)

while True:
    
    #Leser av sensorverdi i volt:
    msr = ntc_sensor.value()

    #Konverterer til temperatur:
    t = a.temp_C(msr)

    # Printer temperaturen til EV3 LCD skjerm
    #print('The temperature is: %f deg' %t)
    # Printer temperaturen til VS Code output panel
    print('The temperature is: %f deg' %t, file=stderr)

    time.sleep(0.3)