The following tasks assumes you have already installed Python on the EV3 brick and on your computer and are able to communicate with the Robot from your computer. See : https://sites.google.com/site/ev3devpython/upgrading-to-python-library-v2 for more details EV3 Brick pre-requisites:
This chart may help you understand the relationship between MoveTank and MoveSteering. It is intended to show what values of left_speed and right_speed (the MoveTank parameters) correspond to all possible values of steering and a speed value of 40 (the MoveSteering parameters).
Before you can use these MoveTank and MoveSteering functions you must import them and you must define the motor pair.
simpleMotorTest.py #includes 2 x Large Motors (B&C) #!/usr/bin/env python3 from random import randint
from ev3dev.ev3 import * from ev3dev2.motor import LargeMotor, MoveSteering
from ev3dev2.motor import OUTPUT_B, OUTPUT_C #Declare 2 Large Motors on Ports B&C motorLeft = LargeMotor(OUTPUT_B); motorLeft.stop_action = 'coast' #tells the motor to coast to a stop, not stop abruptly
motorRight = LargeMotor(OUTPUT_C); motorRight.stop_action = 'coast'
#Default Speed and duration of motor speed=70 # Speed between -100 (Backwards) to 100 (Forwards) duration = 1 # seconds #Pair 2 motors to allow MoveSteering to work steering_pair = MoveSteering(OUTPUT_B,OUTPUT_C) #Steering Directions constants left = -100 straight = 0 right = 100 stg=straight while True: # Choose a random direction between Far Left and Far Right stg=randint(left,right) # Drive in that direction for duration steering_pair.on_for_seconds(steering=stg, speed=speed, seconds=duration) motorTest1.py #includes 2 x Large Motors (B&C) and Ultrasonic Sensor (1) #!/usr/bin/env python3 from random import randint
from ev3dev.ev3 import * from ev3dev2.motor import LargeMotor, MoveSteering
from ev3dev2.motor import OUTPUT_B, OUTPUT_C from ev3dev2.sensor.lego import UltrasonicSensor motorLeft = LargeMotor(OUTPUT_B); motorLeft.stop_action = 'coast' #tells the motor to coast to a stop, not stop abruptly
motorRight = LargeMotor(OUTPUT_C); motorRight.stop_action = 'coast'
speed=70 duration = 1 #seconds steering_pair = MoveSteering(OUTPUT_B,OUTPUT_C) us=UltrasonicSensor() left = -100 straight = 0 right = 100 stg=straight while True: if us.distance_centimeters < 40: stg=randint(left,right) steering_pair.on_for_seconds(steering=stg, speed=-speed, seconds=duration) else: steering_pair.on_for_seconds(steering=stg, speed=speed, seconds=duration) motorTest2.py # TWO Ultrasonic sensors #!/usr/bin/env python3 from random import randint
from ev3dev.ev3 import * from ev3dev2.motor import LargeMotor, MoveTank, MoveSteering
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, INPUT_1, INPUT_2 from ev3dev2.sensor.lego import TouchSensor, UltrasonicSensor motorLeft = LargeMotor(OUTPUT_B); motorLeft.stop_action = 'coast' #tells the motor to coast to a stop, not stop abruptly
motorRight = LargeMotor(OUTPUT_C); motorRight.stop_action = 'coast'
speed=70 duration = 1 #seconds steering_pair = MoveSteering(OUTPUT_B,OUTPUT_C) usL=UltrasonicSensor(INPUT_1) usR=UltrasonicSensor(INPUT_4) left = -100 straight = 0 right = 100 stg=straight while True: dL=usL.distance_centimeters dR=usR.distance_centimeters if dL < 40 or dR < 40: if dL<dR: stg=right steering_pair.on_for_seconds(steering=stg, speed=speed, seconds=duration) elif dL>dR: stg=left steering_pair.on_for_seconds(steering=stg, speed=speed, seconds=duration) else: stg=straight steering_pair.on_for_seconds(steering=stg, speed=-speed, seconds=duration) stg=right steering_pair.on_for_seconds(steering=stg, speed=speed, seconds=duration) |
