Exemple #1
0
 def show_sensors(iterations):
     """ Show the EV3 sensors, current mode and value """
     sensors = list(list_sensors(address=[INPUT_1, INPUT_2, INPUT_3]))   # , INPUT_4
     for _ in range(iterations):
         for sensor in sensors:
             print("{} {}: {}".format(sensor.address, sensor.mode, sensor.value()))
             sleep(.5)
     sleep(10)
        '''.format(left_cs.reflected_light_intensity,
                   center_cs.reflected_light_intensity,
                   right_cs.reflected_light_intensity))
    if left_cs.reflected_light_intensity > 15 >= center_cs.reflected_light_intensity \
            and right_cs.reflected_light_intensity > 15:
        straight()
    else:
        if not turn_left() and not turn_right(
        ) and center_cs.reflected_light_intensity > 15:
            conn.send(
                '------------------------------ OFF THE LINE ------------------------------'
            )
            turn_right() if last_bl_cs is left_cs else turn_left(
            ) if last_bl_cs is right_cs else straight()


three = [sensor.driver_name
         for sensor in list_sensors()].count('lego-ev3-color') == 3

logger_conn, motor_conn = Pipe(False)
p = Process(target=log_data, args=(logger_conn, logger_cs))
p.start()

while not btn.any():
    follow_line_three(motor_conn) if three else follow_line(motor_conn)

motors.off()
motor_conn.send(None)
p.join()
print('Process joined')
Exemple #3
0
def get_info(old_sensor_addresses, old_motor_addresses, all_info=False):
    info = {"disconnected_devices": []}
    if all_info:
        for group_name, leds in LEDS.led_groups.items():
            info["led:" + group_name] = {
                led.desc.split("_")[0]: led.brightness_pct
                for led in leds
            }
    sensor_addresses = set()
    for sensor in list_sensors("*"):
        try:
            address = sensor.address
            if address.count(":") > 1:
                # addresses for i2c sensors end with ':i2c*', remove this
                address = address[:address.index(":", address.index(":") + 1)]
            if address in old_sensor_addresses:
                old_sensor_addresses.remove(address)
                info[address] = {
                    "values":
                    " ".join(
                        str(sensor.value(i)) for i in range(sensor.num_values))
                }
            else:
                info[address] = {
                    "driver_name":
                    sensor.driver_name,
                    "mode": {
                        "values": sensor.modes,
                        "selected": sensor.mode
                    },
                    "command":
                    sensor.commands,
                    "values":
                    " ".join(
                        str(sensor.value(i))
                        for i in range(sensor.num_values)),
                    #"decimals": sensor.decimals,
                }
            sensor_addresses.add(address)
        except Exception:
            traceback.print_exc()
    info["disconnected_devices"].extend(old_sensor_addresses)
    motor_addresses = set()
    for motor in list_motors("*"):
        try:
            address = motor.address
            if address in old_motor_addresses:
                old_motor_addresses.remove(address)
                info[address] = {"position": motor.position}
            else:
                info[address] = {
                    "driver_name": motor.driver_name,
                    "duty_cycle_sp": motor.duty_cycle_sp,
                    "polarity": motor.polarity,
                    "position": motor.position,
                    "position_sp": motor.position_sp,
                    "speed_sp": motor.speed_sp,
                    "ramp_up_sp": motor.ramp_up_sp,
                    "ramp_down_sp": motor.ramp_down_sp,
                    "stop_action": {
                        "values": motor.stop_actions,
                        "selected": motor.stop_action
                    },
                    "time_sp": motor.time_sp,
                    "command": motor.commands
                }
            motor_addresses.add(address)
        except Exception:
            traceback.print_exc()
    info["disconnected_devices"].extend(old_motor_addresses)
    content = json.dumps(info).encode("utf-8")
    return content, sensor_addresses, motor_addresses
Exemple #4
0
#!/usr/bin/env python3

from ev3dev2.motor import LargeMotor, OUTPUT_B, OUTPUT_C, SpeedPercent, MoveTank, MoveSteering
from ev3dev2.sensor import Sensor, list_sensors
from ev3dev2.sensor.lego import TouchSensor, GyroSensor, ColorSensor
from ev3dev2.led import Leds
import time
import atexit

# Set Up Devices

for s in list_sensors():
    if s.driver_name == 'ht-nxt-compass':
        compass = s
    elif s.driver_name == 'lego-ev3-gyro':
        gyro = GyroSensor(s.address)
    elif s.driver_name == 'ht-nxt-ir-seek-v2':
        ir = s
    elif s.driver_name == 'lego-ev3-color':
        color_sensor = ColorSensor(s.address)
    elif s.driver_name == 'lego-ev3-us':
        sonic = s

tank_drive = MoveTank(OUTPUT_B, OUTPUT_C)
steer_drive = MoveSteering(OUTPUT_B, OUTPUT_C)
leds = Leds()


def stop():
    # stop motors when program exits...
    tank_drive.on(0,0)