def read_value(self) -> int: """ Reads a single value, converts it and returns the binary expression :return: int """ # implementation ls = ev3.LightSensor('in3') ls.mode = 'REFLECT' light_value = ls.value() if (light_value > 540): light_value = 0 else: light_value = 1 return light_value
REVERSE_DIST = 100 #---------------------------------------------------------------------------------------------------------------------- #Attach motors mA is left, mb right mA = ev3.LargeMotor('outA') mB = ev3.LargeMotor('outB') mB.run_direct() mA.run_direct() #Set motor direction mA.polarity = "normal" mB.polarity = "normal" #Attach sensors colorSensorLeft = ev3.ColorSensor('in1') colorSensorRight = ev3.ColorSensor('in2') lightSensorFront = ev3.LightSensor('in3') #Check sensors if they are connected, will throw an error message if not assert colorSensorLeft.connected, "ColorSensorLeft (CS) is not connected to in1" assert colorSensorRight.connected, "ColorSensorLeft (CS) is not connected to in2" assert lightSensorFront.connected, "LightSensoFront (LS) is not connected to in3" # Put the color sensor into COL-REFLECT mode # to measure reflected light intensity. colorSensorLeft.mode = 'COL-REFLECT' colorSensorRight.mode = 'COL-REFLECT' #For shutdown, stop motors def signal_handler(sig, frame): print('Shutting down gracefully')
# state constants ON = True OFF = False RUNNING = 1 STOP = 0 SPD_LIMIT = 100 lightChecker = None # hardware setting _sensor_1 = ev3.UltrasonicSensor('in1') #_sensor_1 = ev3.UltrasonicSensor('in1') _sensor_3 = ev3.UltrasonicSensor('in3') # set-up the light sensor light_sensor = ev3.LightSensor('in2') _left_motor = ev3.LargeMotor('outB') _right_motor = ev3.LargeMotor('outC') def main(): find_light_target() ''' def main(): # This is the main method timer1 = time.time() bot_state = RUNNING
#!/usr/bin/python3 #https://sites.google.com/site/ev3python/learn_ev3_python/using-sensors/sensor-modes #http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html import ev3dev.ev3 as ev3 sensorRight = ev3.ColorSensor('in1') sensorLeft = ev3.ColorSensor('in4') sensorGripper = ev3.LightSensor('in3') assert sensorRight.connected, "sensorRight(ColorSensor) is not connected" assert sensorLeft.connected, "sensorLeft(ColorSensor) is not conected" assert sensorGripper.connected, "sensorGripper(LightSensor) is not conected" AMBIENT = 0 REFLECT = 1 COLOR = 2 arrayofcolors = ('unknown', 'black', 'blue', 'green', 'yellow', 'red', 'white', 'brown') filterValue = [0, 0, 0, 0] def setmodeSensorsLR(mode): if mode == 0: sensorLeft.mode = 'COL-AMBIENT' # measures lux sensorRight.mode = 'COL-AMBIENT' # measures lux sensorGripper.mode = 'AMBIENT' print("Sensor mode set to ambient") elif mode == 1: sensorLeft.mode = 'COL-REFLECT' # measures light intensity
import ev3dev.ev3 as ev3 from time import sleep import signal mA = ev3.LargeMotor('outA') mB = ev3.LargeMotor('outB') THRESHOLD_LEFT = 30 THRESHOLD_RIGHT = 350 BASE_SPEED = 30 TURN_SPEED = 80 lightSensorLeft = ev3.ColorSensor('in1') lightSensorRight = ev3.LightSensor('in2') assert lightSensorLeft.connected, "LightSensorLeft(ColorSensor) is not connected" assert lightSensorRight.connected, "LightSensorRight(LightSensor) is not conected" mB.run_direct() mA.run_direct() mA.polarity = "inversed" mB.polarity = "inversed" def signal_handler(sig, frame): print('Shutting down gracefully') mA.duty_cycle_sp = 0 mB.duty_cycle_sp = 0
def main(): # Setup input ports colorSensorStop = ev3.LightSensor('in1') colorSensorLeft = ev3.ColorSensor('in2') colorSensorRight = ev3.ColorSensor('in3') assert colorSensorStop.connected, "LightSensorStop(ColorSensor) is not connected" assert colorSensorLeft.connected, "LightSensorLeft(ColorSensor) is not conected" assert colorSensorRight.connected, "LightSensorRight(ColorSensor) is not conected" #colorSensorStop.MODE_REFLECT #colorSensorLeft.raw #colorSensorRight.raw # Setup output ports mDiff = MoveDifferential(OUTPUT_A, OUTPUT_D, EV3Tire, 15 * STUD_MM) mDiff.left_motor.polarity = "normal" mDiff.right_motor.polarity = "normal" # setup gracefully shutdown def signal_handler(sig, frame): print('Shutting down gracefully') mDiff.stop() exit(0) signal.signal(signal.SIGINT, signal_handler) print('Press Ctrl+C to exit') # setup decoder and define chain of actions #string_of_actions = "urrruulldddl" #string_of_actions = "fffblfrffrfrffb" #"ffffrfrfrfblfflffrflfb" string_of_actions = "llll.uddllu.r.r.r.r.rdr.u.uruulld.r.rlddllu.luulld.rur.d.dull.d.rd.r.r.rdr.u.uruurrd.lul.dulld.rddlllluur.dld.r.r.rdr.u.udlllldllu.r.r.r.r.rdr.u" #string_of_actions = "lffffrffbtffrffrfrffffffbrflflfffbrflfflfflflfffbtflffrffrflffbrfflfflflffblfrffffbtflfflffblffbrflffffbrflflfffbrflfflfflflffblfrfrffbtfrffrfrffbrflfflfffrffffrffrfrffbrflflffffbrflflfffbtfrfffflfrffrfrffffffbrflflff" #string_of_actions = "lfffrfflf" #string_of_actions = "l" decoder = Decoder(string_of_actions, DEFINED_ACTIONS) current_action = decoder.get_next_action() while current_action != NOT_DEFINED: # current action == -1 -> no more actions to execute if (current_action == FORWARD): move_forward(mDiff, colorSensorStop, colorSensorLeft, colorSensorRight) current_action = decoder.get_next_action() #if(current_action == FORWARD_GYRO): # move_forward_gyro(mDiff, colorSensorStop, colorSensorFollow, gyro) # current_action = decoder.get_next_action() # if(current_action == FORWARD): # move_forward_dual(mDiff, colorSensorStop, colorSensorLeft, colorSensorRight) # current_action = decoder.get_next_action() elif (current_action == CONTINUE): cont_forward(mDiff, colorSensorStop, colorSensorLeft, colorSensorRight) current_action = decoder.get_next_action() elif (current_action == BACKWARD): move_backward(mDiff, colorSensorStop) current_action = decoder.get_next_action() elif (current_action == ONE_EIGHTY): turn_one_eighty(mDiff) current_action = decoder.get_next_action() elif (current_action == LEFT): turn_left(mDiff) current_action = decoder.get_next_action() elif (current_action == RIGHT): turn_right(mDiff) current_action = decoder.get_next_action() else: raise TypeError( "No switch case implemented for action/behaviour: {}".format( current_action))