Python‎ > ‎EV3 MicroPython‎ > ‎EV3 Python‎ > ‎

1. Step By Step Tasks

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.


Task 1.  Get Motors Working

EV3 Brick pre-requisites:
  • 2 x Large Motors  connected to Ports B and D
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).


  • For all values of steering (-100 to +100) there is always at least one motor with a speed equal to the speed value, 40.
  • When steering=0 we note that left_speed and right_speed are both equal to speed, which is 40.
  • When steering=25 we note that left_speed=40 and right_speed=20.
  • When steering=50 we note that left_speed=40 and right_speed=0.
  • When steering=100 we note that left_speed=40 and right_speed=-40.
Before you can use these MoveTank and MoveSteering functions you must import them and you must define the motor pair. 
  • By default these functions will work with the Lego EV3 large motors and you don't need to import LargeMotor to use these functions with a pair of large motors. 
  • The following uses sensible names for the pairing of large motors:
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
tank_pair = MoveTank(OUTPUT_B, OUTPUT_C)

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)
 









Comments