Using Motors


Note that EV3 Basic is compatible with the large and medium EV3 motors and also with NXT motors. EV3 Basic makes no distinction between these different types of motor.

EV3 Basic has nine commands that can be used to make motors move, but only four are really needed at beginner level: Motor.Move, Motor.MoveSync, Motor.Start and Motor.StartSync. You will also need Motor.Stop, of course. You can read about the other Motor commands and motor-related functions in the EV3 Basic manual

Parameters

Motor commands use the following parameters: 
  • port(s) =  Motor port name(s). E.g. "BC". Always put the letters in alphabetical order.
  • degrees =  Number of degrees to turn the motor. Motor commands will always use the absolute value of the degree parameter. In other words, any negative sign for the angle will be ignored. Sif you want the motor to turn backwards then give it a negative speed rather than a negative angle. Convert rotations to degrees by multiplying by 360, if necessary.
  • brake  = "True", if the motor(s) should switch on the brake after the movement, as is almost always the case.
  • speed = speed from -100 (full reverse) to 100 (full forward)
Specific Angle
  • To make a single motor turn through a specific angle, you should use Motor.Move(ports, speed, degrees, brake). Example: turn motor A at speed 40 through 80°: Motor.Move("A", 40, 80, "True")

  • To make two (or more) motors turn with the same speed through a specific angle, as when moving in a straight line, you should also use Motor.Move(ports, speed, degrees, brake). Example: turn motors B and C at speed -30 for two wheel rotations or 720°: Motor.Move("BC", -30, 720, "True")

  • To make the robot follow a curved path or turn on the spot for a given angle of wheel rotation (two motors moving with different speeds), use Motor.MoveSync(ports, speed1, speed2, degrees, brake). Speed1 and Speed2 will be the speeds of the motor with the lower port letter and the higher port letter, respectively. Degrees will be the angle turned by the faster motor. This command is similar to the 'Move Tank' block in the standard Lego EV3 software.
    Example: turn motor B at speed 30 and motor C at speed 40 for a gentle left turn, with the faster motor turning 720°: Motor.MoveSync("BC", 30, 40, 720, "True")
Specific Time
  • In general, to make motors turn for a given length of time, turn them on, then use Program.Delay to make the program wait the desired time, then stop the motors with Motor.Stop.

  • To make one or more motors turn with the same speed for a specific time you should use Motor.Start(ports, speed). Example: Turn on motors B and C at speed 80 for 3 seconds:
        
    Motor.Start("BC",80)
        Program.Delay(3000)  
    ' 3000 milliseconds
        Motor.Stop("BC","True")    ' "True" means 'apply the brake'

  • To make the robot follow a curved path or turn on the spot for a specific time (two motors moving with different speeds), use Motor.StartSync(ports, speed1, speed2). Example: Make the robot turn on the spot for 5 seconds with motor B turning at speed 50 and motor C turning at speed -50:
        Motor.StartSync("BC", 50, -50)
        Program.Delay(5000)

        Motor.Stop("BC","True")

Speed vs. Power

Some other motor commands have a 'power' parameter, which in EV3 Basic is not the same as 'speed'. The 'power' parameter can be considered to be for experts only. To learn the difference between 'speed' and 'power', check the EV3 Basic Manual. 

Modifying speed

When you have to change the speed of an already-running motor, just re-issue a start command with the appropriate speed value. The motor will then seamlessly switch over to the new speed.

Sample Programs

Here are some sample programs that shows different ways of controlling the motors with EV3 Basic. It is assumed that you are using a robot similar to the standard Lego 'driving base' or 'educator robot' with motors attached to ports B and C. Since your robot will be running around, it will be helpful to establish a wireless (Bluetooth or WiFi) connection to the EV3, rather than using a USB cable connection. Clear HERE for help setting up the Bluetooth connection (you may need to attach a Bluetooth dongle to your PC) or HERE to set up a WiFi connection (you need to purchase a compatible WiFi dongle for the brick). You can copy and paste each program directly into Small Basic and it should work, but it will only work in 'PC mode' (running the program in Small Basic). It cannot be compiled and downloaded to the brick to be run from the brick's menus (in 'brick mode') because it makes use of Small Basic's text window. Some programs are in many parts, in which you can also just copy and paste suitable fragments of the program. Everything after a single quote is a comment that is only there to help you - Small Basic simply ignores comments when the program is run. Small Basic colours comments green and uses other colour coding to help you.

'****************************************************************************
'Simple motor control. Attach motors to ports B and C.
'You may have to reverse the connections to ports B and C
'or make some values negative if the robot turns or
'moves the wrong way.

'Make the robot turn right by running motor B
'at 50% speed through 360°, then apply the brake
Motor.Move ("B", 50, 360, "True")

Program.Delay(2000)  ' Pause for 2 seconds (2000 milliseconds)

'Make the robot move forward at 30% speed for two wheel rotations
'(720°) then apply the brake.
Motor.Move ("BC", 30, 720, "True")

Program.Delay(2000)

'To make the motion start and finish more softly you can use the 'Schedule' or
'SchedulePower command. These commands have an acceleration step, a
'constant speed step, and a deceleration step. For example:
'Motor.Schedule (ports, speed, step1, step2, step3, brake)
'Step1 and step3 are the angles through which to accelerate and decelerate

'To make the robot accelerate forward for one wheel rotation,
'then continue forward at 60% speed for two wheel rotations (720°),
'then decelerate for three wheel rotations (1080°) while still moving forward:
Motor.Schedule("BC", 60, 360, 720, 1080, "True")
Motor.Wait ("BC")
'Note that the Motor.Schedule() command does NOT pause the program until
'the motion has completed so if you want the program to pause you 
'should use a Motor.Wait() command as above.

Program.Delay(2000)

'If you use the Motor.Move() command to control multiple motors the 
'motors will always move with the SAME SPEED. So how can you make
'motors move simultaneously with DIFFERENT SPEEDS?

'The standard Lego graphical interface has a 'move tank' block that lets you
'set two motors to move with different speeds for a given number of
'degrees, rotations or seconds.

'In EV3 Basic the equivalent to the 'move tank' block, which you should
'use when you want to make TWO motors move with
'two different speeds (such as when you want the robot to turn
'on the spot or follow a curved path) is
'Motor.MoveSync(ports,speed1,speed2,degrees,brake)
'Speed1 and Speed2 are the speeds of the two motors IN ALPHABETICAL ORDER
'and 'degrees' refers to the motor with the higher speed.
Motor.MoveSync("BC", 50, -60, 215, "True")
'In this example motor B turns at 50% speed and motor C turns at -60% speed 
'i.e. backwards. Motor C (the faster motor) will turn through 215°. 
'To make the robot turn on the spot, just set speed1 and speed2 to be equal
'and opposite. Motor.MoveSync makes the program wait until the movement
'completes before continuing.

Program.Delay(2000)

'Here is how to make your robot draw a square (you might 
'have to adjust the value of 430° motor rotation which is 
'supposed to make the robot turn 90°, especially if you are
'not using the standard Lego driving base model). We are 
'using a value of 430° instead of the 215° used previously 
'because only one wheel is turning here

For i=1 to 4
  Motor.Move ("BC", 40, 720, "True")
  Motor.Move ("B", 40, 430, "True")
EndFor

Program.Delay(2000)

'Both the above examples will give squares with rounded 
'corners because the robot will not turn on the spot. 
'To make the robot do sharp turns on the spot 
'(around the mid-point between the wheels) we must 
'make the wheels turn in opposite directions as the robot turns:

For i=1 to 4
  Motor.Move ("BC", 40, 720, "True")   'Move forwards
  Motor.MoveSync("BC", 40, -40, 215, "True")
EndFor

'***********************************************************

This next example makes the robot trace out a regular polygon. The user must enter the desired number of sides into the text window:

'***********************************************************

Textwindow.Write ("Trace a polygon with how many sides (3-9)?")
sides=textwindow.ReadNumber()
degrees=2.39*360/sides 'Each wheel must turn this angle.
'Since each wheel turned 215° to make the robot turn 90°, we need to turn 
'each wheel 215/90=2.39°  to make the robot turn 1°.

For i=1 to sides
  Motor.Move("BC", 50, 720, "True")
  Program.Delay(500)
  Motor.MoveSync("BC", 20, -20, degrees, "True")
  Program.Delay(500)
EndFor

'*******************************************************

This next program lets you use the EV3's buttons to control two motors attached to ports B and C. Instructions are displayed on the EV3's LCD screen.

'***********************************************************

LCD.Clear()
LCD.Text(1, 0,30, 1, "Control motors with")
LCD.Text(1, 0,45, 1, "directional buttons")
LCD.Text(1, 0,70, 1, "Left/Right: Motor B")
LCD.Text(1, 0,85, 1, "Up/Down: Motor C")

while "True"  '"True" is always true, of course, so the loop repeats forever.
  K = Buttons.Current  ' request button state

  SB = 0   '(pre)set speed of motor B to zero
  SC = 0   '(pre)set speed of motor C to zero
  If Text.IsSubText(K, "L") then 
'checks whether the text string in K contains "L" (because left button was pressed)
    SB = -40    
  elseif Text.IsSubText(K, "R") then
    SB = 40
  endif
  If Text.IsSubText(K, "U") then
    SC = 40    
  elseif Text.IsSubText(K, "D") then
    SC = -40
  endif
  
  ' Adjust motor speeds
  Motor.Start("B", SB)
  Motor.Start("C", SC)
  
  ' wait a little (100 milliseconds or 0.1 second) before polling again
  Program.Delay(100)
Endwhile
Comments