""" from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port # Initialize devices. motor = Motor(Port.A) color_sensor = ColorSensor(Port.B) SPEED = 500 # Color angle targets angles = { "GREEN": 20, "BLUE": 110, "RED": 200, "YELLOW": 290, "BLACK": 250, "WHITE": 75, "NONE": 162, } previous_reflection = 0 # Verify that these colors have ascending reflection values. for name in ("NONE", "BLACK", "RED", "YELLOW"): motor.run_target(SPEED, angles[name]) reflection = color_sensor.reflection() assert reflection > previous_reflection, "Expected {0} > {1}".format( reflection, previous_reflection )
# Get motor position. position = (left.angle() + right.angle()) / 2 # Calculate motor speed. speed = (position - buf[idx]) / (window * DT) * 1000 buf[idx] = position idx = (idx + 1) % window # Calculate reference position, which just grows linearly with # time. reference = -max(watch.time() - PAUSE, 0) / 1000 * DRIVE_SPEED # Calculate duty cycle. diff = position - reference duty = 0.018 * rate + 19 * angle + 0.45 * diff + 0.16 * speed # Account for battery level and type. duty *= 7200 / hub.battery.voltage() # Calculate steering. reflection = sensor.reflection() steering = (reflection - 28) * 0.6 # Apply duty cycle for balancing and steering. left.dc(duty + steering) right.dc(duty - steering) # Wait some time. wait(DT)
Hardware Module: 1 Description: Verify that ambient() and reflection() refuse an argument. """ from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port # Initialize device. color_sensor = ColorSensor(Port.B) # Get the ambient light intensity to verify that the default works. ambient_light_default = color_sensor.ambient() # verify an argument passed to ambient is correctly refused. expected = "function doesn't take keyword arguments" try: ambient_light_surface_true = color_sensor.ambient(surface=True) except Exception as e: assert str(e) == expected, "Expected '{0}' == '{1}'".format(e, expected) # Get the reflected light intensity to verify that the default works. reflection_default = color_sensor.reflection() # verify an argument passed to reflection is correctly refused. expected = "function doesn't take keyword arguments" try: reflection_surface_true = color_sensor.reflection(surface=True) except Exception as e: assert str(e) == expected, "Expected '{0}' == '{1}'".format(e, expected)