def __init__(self, port, mode): portStringMap = { Port.S1: "in1", Port.S2: "in2", Port.S3: "in3", Port.S4: "in4", } portString = portStringMap.get(port, 'Invalid Port') self.abs = Sensor(address='ev3-ports:' + portString + ':i2c1') self.abs.mode = mode
class IMU: abs = Sensor() def __init__(self, port, mode): portStringMap = { Port.S1: "in1", Port.S2: "in2", Port.S3: "in3", Port.S4: "in4", } portString = portStringMap.get(port, 'Invalid Port') self.abs = Sensor(address='ev3-ports:' + portString + ':i2c1') self.abs.mode = mode def angle(self): res = [] for i in range(self.abs.num_values): res.append(self.abs.value(i)) return res
def on_message(self, messages): global move_joystick try: print("got messages", messages) for message in json.loads(messages): type_ = message["type"] if type_ == "rc-joystick": if message["leftPort"] != old_joystick_left_port or message[ "rightPort"] != old_joystick_right_port: move_joystick = MoveJoystick(message["leftPort"], message["rightPort"]) if message["x"] == 0 and message["y"] == 0: move_joystick.off(brake=False) else: move_joystick.on(message["x"], message["y"], 1) elif type_ == "rc-motor": if message["port"] in motors: motor = motors[message["port"]] else: motor = motors[message["port"]] = Motor( message["port"]) motor.on(message["speed"] * 100) elif type_ == "sensor": port = message["port"] attributes = message["attributes"] device = Sensor(port) for name, value in attributes.items(): setattr(device, name, value) # send changes to other clients EV3InfoHandler.send_to_all(json.dumps({port: attributes}), {self}) elif type_ == "motor": port = message["port"] attributes = message["attributes"] device = Motor(port) for name, value in attributes.items(): setattr(device, name, value) # send changes to other clients EV3InfoHandler.send_to_all(json.dumps({port: attributes}), {self}) elif type_ == "led": port = message["port"] attributes = message["attributes"] led_group = port.split(":")[1].lower() for color_name, brightness in attributes.items(): LEDS.leds[color_name + "_" + led_group].brightness_pct = float(brightness) # send changes to other clients EV3InfoHandler.send_to_all(json.dumps({port: attributes}), {self}) else: raise ValueError("Unknown message type '" + type_ + "'") except Exception: traceback.print_exc() self.send_to_all("next")
""" Limit speed in range [-1000,1000] """ if speed > 1000: speed = 1000 elif speed < -1000: speed = -1000 return speed # Set LEGO port for Pixy on input port 1 in1 = LegoPort(INPUT_1) in1.mode = 'auto' # Wait 2 secs for the port to get ready sleep(2) # Connect Pixy camera pixy = Sensor(INPUT_1) # Set mode to detect signature 1 only pixy.mode = 'SIG1' # Signatures we're interested in (SIG1) sig = 1 # Connect TouchSensor (to stop script) ts = TouchSensor(INPUT_4) # Connect LargeMotors rmotor = LargeMotor(OUTPUT_A) lmotor = LargeMotor(OUTPUT_B) # Defining constants X_REF = 128 # X-coordinate of referencepoint
from ev3dev2.port import LegoPort # EV3 Display lcd = Display() # Connect TouchSensor ts = TouchSensor(INPUT_4) # Set LEGO port for Pixy on input port 1 in1 = LegoPort(INPUT_1) in1.mode = 'auto' # Wait 2 secs for the port to get ready sleep(2) # Connect Pixy camera pixy = Sensor(INPUT_1) # Set mode to detect signature 1 only pixy.mode = 'SIG1' # Read and display data until TouchSensor is pressed while not ts.value(): # Clear EV3 display lcd.clear() # Read values from Pixy x = pixy.value(1) # X-coordinate of centerpoint of object y = pixy.value(2) # Y-coordinate of centerpoint of object w = pixy.value(3) # Width of rectangle around detected object h = pixy.value(4) # Heigth of rectangle around detected object # scale to resolution of EV3 display: # Resolution Pixy while color tracking; (255x199) # Resolution EV3 display: (178x128)
STEP_LENGTH = (1, 3) # Move in a new direction every 1-3 seconds MOTOR_SPEEDS = (-100, 100) # Motor values are anything between -100 and 100 PRINT_TIME = 5 # Print sensor values every 5 seconds def random_between(a, b): # Returns a random float between a and b: return a + random.random() * (b - a) # Initialise all sensors. lm1 = LargeMotor(address="outB") lm2 = LargeMotor(address="outC") cs = ColorSensor(address="in2") us = UltrasonicSensor(address="in3") ir = Sensor(address="in1", driver_name="ht-nxt-ir-seek-v2") compass = Sensor(address="in4", driver_name="ht-nxt-compass") compass.command = "BEGIN-CAL" compass.command = "END-CAL" # This code moves in random directions, and stores the movements in a circular queue. movement_queue = deque([], maxlen=5) last_step_time = time.time() last_print_time = time.time() current_step_wait = 0 solving_white = False while True: if time.time() - last_step_time > current_step_wait: # Set some new motor speeds, and a wait time. last_step_time = time.time()
STEP_LENGTH = (1, 3) # Move in a new direction every 1-3 seconds MOTOR_SPEEDS = (-100, 100) # Motor values are anything between -100 and 100 PRINT_TIME = 5 # Print sensor values every 5 seconds def random_between(a, b): # Returns a random float between a and b: return a + random.random() * (b - a) # Initialise all sensors. lm1 = LargeMotor(address='outB') lm2 = LargeMotor(address='outC') cs = ColorSensor(address='in2') us = UltrasonicSensor(address='in3') ir = Sensor(address='in1', driver_name='ht-nxt-ir-seek-v2') compass = Sensor(address='in4', driver_name='ht-nxt-compass') compass.command = 'BEGIN-CAL' compass.command = 'END-CAL' # This code moves in random directions, and stores the movements in a circular queue. movement_queue = deque([], maxlen=5) last_step_time = time.time() last_print_time = time.time() current_step_wait = 0 solving_white = False while True: if time.time() - last_step_time > current_step_wait: # Set some new motor speeds, and a wait time. last_step_time = time.time()
from ev3dev2.sensor.lego import TouchSensor from ev3dev2.led import Leds import time leds = Leds() #while True: # leds.set_color("LEFT", "GREEN") # leds.set_color("RIGHT", "GREEN") # time.sleep(1) # leds.set_color("LEFT", "RED") # leds.set_color("RIGHT", "RED") # time.sleep(1) ir = Sensor('in4:i2c8') tank_drive = MoveTank(OUTPUT_B, OUTPUT_C) # drive in a turn for 5 rotations of the outer motor # the first two parameters can be unit classes or percentages. #tank_drive.on_for_rotations(SpeedPercent(50), SpeedPercent(75), 10) # drive in a different turn for 3 seconds #tank_drive.on_for_seconds(SpeedPercent(60), SpeedPercent(30), 3) def start(): pass
from ev3sim.code_helpers import wait_for_tick from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B from ev3dev2.sensor import Sensor, INPUT_1 m_l = LargeMotor(OUTPUT_A) m_r = LargeMotor(OUTPUT_B) compass = Sensor(INPUT_1, driver_name="ht-nxt-compass") robot_speed = 26.316 compass.command = "BEGIN-CAL" compass.command = "END-CAL" n = int(input()) commands = [] x = 0 while x < n: msg = input().split() name = msg[0] distance = int(msg[2]) degrees = int(msg[5]) commands.append((name, distance, degrees)) x = x + 1 goto = input() wanted = goto.split()[2] print(goto) # Now, find the command which has name == wanted.
def setSensor(GyroSensor, Hitechnic): global Gyro, ColorSight Gyro = GyroSensor(GyroSensor) ColorSight = Sensor(driver='ht-nxt-color-v2', address=Hitechnic)
us_units = us.units ir_value = ir.value(0) ir_proximity = ir.proximity # an estimate of the distance between the sensor and the objects in front of it ir_beacon= ir.distance(channel=1) # returns distance (0,100) to the beacon on the given channel ir_heading = ir.heading(channel=1) # returns heading (-25,25) to the beacon on the given channel ir_address = ir.address gyro_value = gyro.value(0) gyro_angle = gyro.angle gyro_address = gyro.address gyro_units = gyro.units # Sensors connected to NXT-Sensor-Split MUX - Barometer--------------------------------------------------------- temp1 = Sensor(INPUT_1) temp1.mode = 'TEMP' Temp_NXT = (temp1.value(0)/1000) pressure = Sensor(INPUT_1) pressure.mode = 'PRESS' Pressure_NXT = ((pressure.value(0)/10)*25.4) # Sensors connected to either INPUT3 or INPUT 4 ------------------------------------------------------------------- a = LegoPort(INPUT_4) a.set_device = 'lego-nxt-sound' sleep(1) ss = SoundSensor() spl = ss.sound_pressure_low sound_value = ss.value(0)
from ev3sim.code_helpers import wait_for_tick from ev3dev2.motor import LargeMotor from ev3dev2.sensor import Sensor motor = LargeMotor("outA") # Put in the right values here. ir = Sensor("in1", driver_name=???) ir.mode = ??? # Write some code here!
def getIrValue(): ir = Sensor(address="i2c-legoev33:i2c8") return ir.value()