Libraries.Robots.Lego.Motor Documentation

This class is an object representation of a LEGO Mindstorms EV3 motor. It is used to control movement of the EV3 robot.

Example Code

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility

class Main
action Main
    Motor motor
    Utility utility
    
    motor:SetSpeed(motor:MOTOR_A, 720)  //two motors for moving the robot, connected to ports A and B
    motor:SetSpeed(motor:MOTOR_B, 720)  //these motors will fully rotate twice per second
    motor:RotateForward(motor:MOTOR_A)
    motor:RotateForward(motor:MOTOR_B)
    utility:DelayMilliseconds(3000)     //lets the motors rotate for 3 seconds
    motor:Stop(motor:MOTOR_A)
    motor:Stop(motor:MOTOR_B)
end
end

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
text MOTOR_A
text MOTOR_DThis action sets the speed of a motor. Alone, this action does not cause a motor to move, but rather, it lets the motor know how fast to go when it moves. If this method is not called, motors will default to 50% of the max speed.
text MOTOR_B
text MOTOR_C

Actions Documentation

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Example

Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

Example

use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)

GetHashCode()

This action gets the hash code for an object.

Return

integer: The integer hash code of the object.

Example

Object o
integer hash = o:GetHashCode()

GetRotation(text motorID)

This action will give the position of a motor based on how many degrees it has rotated.

Parameters

  • text motorID: specifies the motor being checked for its position. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Return

integer: the position of the motor in degrees, based on how much it has rotated.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Utility

Motor motor
Sound sound
Utility utility

motor:SetSpeed(motor:MOTOR_D, 600)  //moves at 600 degrees per second
motor:RotateForward(motor:MOTOR_D)
repeat while motor:GetRotation(motor:MOTOR_D) < 3600
    sound:Beep()
    utility:DelayMilliseconds(1000) //allows the motor to keep moving for a second before checking its rotation again
end
sound:Buzz()

GetRotationTarget(text motorID)

This action reports the target degree of a rotating motor.

Parameters

  • text motorID: specifies which motor is being checked for its rotation target. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Return

integer: the degree that the motor is rotating to.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Screen
use Libraries.Robots.Lego.Button

Motor motor
Sound sound
Screen screen
Button button

motor:SetSpeed(motor:MOTOR_C, 500)
repeat while motor:GetRotationTarget(motor:MOTOR_C) < 1000  //repeat until motor C is attempting to go past 1000 degrees of rotation
    sound:Beep()
    motor:RotateByDegrees(motor:MOTOR_C, 100)
    screen:ScrollUp("Target: " + motor:GetRotationTarget(motor:MOTOR_C))
end
motor:Stop(motor:MOTOR_C)   //stop it from going past 1000
screen:ScrollUp("Press any button")
button:WaitForButtonPress()

GetSpeed(text motorID)

This action will give the speed associated with the motor requested.

Parameters

  • text motorID: specifies the motor being checked for its speed. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Return

integer: the speed associated with the motor requested in a percentage of the motor's max speed, between 0 and 100.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility

Motor motor
Utility utility

motor:SetSpeed(motor:MOTOR_B, 50)
motor:SetSpeed(motor:MOTOR_C, 50)
repeat 5 times  
    motor:SetSpeed(motor:MOTOR_C, motor:GetSpeed(motor:MOTOR_C) + 10)   //speeds up the motor rotation by 10% each iteration
    motor:SetSpeed(motor:MOTOR_B, motor:GetSpeed(motor:MOTOR_B) + 10)
    motor:RotateForward(motor:MOTOR_C)
    motor:RotateForward(motor:MOTOR_B)
    utility:DelayMilliseconds(1000) //lets the motors go forward for 1 second
end

IsMoving(text motorID)

This action is used to find out if a specific motor is currently moving or not. It can be used to detect if a motor is stalled. It takes about one second of a motor failing to move before it is determined to be stalled.

Parameters

  • text motorID: specifies the motor being checked for movement. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Return

boolean: a boolean value where the value 'true' means the motor is moving, and the value 'false' means the motor is not moving (stalled).

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Utility

Motor motor
Button button
Utility utility

motor:RotateByDegrees(motor:MOTOR_A, 1080)
button:SetLightPattern(button:SOLID_GREEN_LIGHT) //will stay on while the motor is moving
repeat while motor:IsMoving(motor:MOTOR_A)
    utility:DelayMilliseconds(100)  //check if the motor is moving every 100 milliseconds
end
button:SetLightPattern(button:SOLID_RED_LIGHT)

ResetRotation(text motorID)

This action resets the motor's memory of its current rotation. This will not cause the motor to rotate.

Parameters

  • text motorID: specifies which motor to reset. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Sound

Motor motor
Sound sound

motor:SetSpeed(motor:MOTOR_A, 300)
motor:RotateToDegree(motor:MOTOR_A, 360) //after this, the motor's rotation is at 180 degrees
sound:Beep()
motor:ResetRotation(motor:MOTOR_A)       //this will change the motor's rotation memory to 0, without moving the motor
motor:RotateToDegree(motor:MOTOR_A, 360) //since the motor's current degree was reset to 0, this will move it 180 degrees forward
sound:BeepTwice()

RotateBackward(text motorID)

This action will cause the specified motor to rotate backward until stopped by the Stop action, or if the program ends.

Parameters

  • text motorID: represents which motor will be changed by this action. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.InfraredSensor

Motor motor
InfraredSensor infraredSensor

infraredSensor:SetPort(infraredSensor:PORT_1)   //infrared sensor connected to port 1 of the robot
motor:RotateBackward(motor:MOTOR_A)               //two motors for moving the robot, connected to ports A and D
motor:RotateBackward(motor:MOTOR_D)
repeat until infraredSensor:GetDistance() > 40  //repeat until the sensor gets far enough away from an object
end
motor:Stop(motor:MOTOR_A)
motor:Stop(motor:MOTOR_D)

RotateByDegrees(text motorID, number degrees)

This action rotates a motor by the amount of degrees specified

Parameters

  • text motorID: specifies the motor that will rotate. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.
  • number degrees: tells the motor how many degrees to rotate by. Negative values will cause the motor to roatate backwards.

Example

use Libraries.Robots.Lego.Motor

Motor motor

motor:SetSpeed(motor:MOTOR_A, 75)
motor:SetSpeed(motor:MOTOR_B, 75)           //two motors used to move the robot, connected to ports A and B
motor:RotateByDegrees(motor:MOTOR_A, 3600)  //motor A rotates 10 full revolutions forward
motor:Wait(motor:MOTOR_A)
motor:RotateByDegrees(motor:MOTOR_B, -3600) //motor B rotates 10 full revolutions backward
motor:Wait(motor:MOTOR_B)

RotateForward(text motorID)

This action will cause the specified motor to rotate forward until stopped by the Stop action, or if the program ends.

Parameters

  • text motorID: represents which motor will be changed by this action. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.InfraredSensor

Motor motor
InfraredSensor infraredSensor

infraredSensor:SetPort(infraredSensor:PORT_1)   //infrared sensor connected to port 1 of the robot
motor:SetSpeed(motor:MOTOR_B, 720)
motor:SetSpeed(motor:MOTOR_C, 720)
motor:RotateForward(motor:MOTOR_B)
motor:RotateForward(motor:MOTOR_C)
repeat until infraredSensor:GetDistance() < 30   //repeat until the sensor is close to an object
end
motor:Stop(motor:MOTOR_B)
motor:Stop(motor:MOTOR_C)

RotateToDegree(text motorID, number degreeTarget)

This action causes a motor to move to a certain degree, based on its current rotation memory.

Parameters

  • text motorID: specifies the motor that will rotate. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.
  • number degreeTarget: indicates what degree the motor should rotate to. Negative values can be used here.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility

Motor motor
Utility utility

motor:SetSpeed(motor:MOTOR_C, 720)
motor:RotateByDegrees(motor:MOTOR_C, 360) //rotates one revolution
motor:Wait(motor:MOTOR_C)
utility:DelayMilliseconds(500)
motor:RotateToDegree(motor:MOTOR_C, 360)  //will do nothing since the motor is already at that degree
motor:Wait(motor:MOTOR_C)
utility:DelayMilliseconds(500)
motor:RotateToDegree(motor:MOTOR_C, 720)  //rotates one more revolution from its starting position of 360 degrees
motor:Wait(motor:MOTOR_C)
utility:DelayMilliseconds(500)
motor:RotateToDegree(motor:MOTOR_C, 0)    //rotates backwards two revolutions from its current position of 720 degrees
motor:Wait(motor:MOTOR_C)

SetSpeed(text motorID, integer speed)

This action sets the speed of a motor. Alone, this action does not cause a motor to move, but rather, it lets the motor know how fast to go when it moves. If this method is not called, motors will default to 50% of the max speed.

Parameters

  • text motorID: represents which motor will be changed by this action. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.
  • integer speed: represents how fast the motor will turn as a percentage of the max speed. The max speed will cause the motor to complete two revolutions per second. Valid speeds are integers between 0 and 100. The maximum speed is limited by battery voltage and how much the robot is already doing, so a speed of 100 may exceed the motor's capabilities under some conditions. In this case, the motor will just move as fast as it can.

Example

use Libraries.Robots.Lego.Motor

Motor motor

motor:SetSpeed(motor:MOTOR_C, 100)          //two revolutions per second
motor:RotateByDegrees(motor:MOTOR_C, 720)   //reaches rotation target in one second
motor:SetSpeed(motor:MOTOR_C, 50)           //one revolution per second
motor:RotateByDegrees(motor:MOTOR_C, -720)  //reaches rotation target in two seconds

Stop(text motorID)

This action causes a motor to stop moving.

Parameters

  • text motorID: specifies which motor should be stopped.

Example

use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.InfraredSensor

Motor motor
InfraredSensor infraredSensor

infraredSensor:SetPort(4)
repeat while true
    if infraredSensor:GetRemoteCommand(infraredSensor:CHANNEL_1) = infraredSensor:BUTTON_TOP_LEFT
        motor:RotateForward(motor:MOTOR_C)
    else
        motor:Stop(motor:MOTOR_C)
    end
end

Wait(text motorID)

This action will cause the robot's program to wait until the specified motor stops moving.

Parameters

  • text motorID: indicates which motor the robot's program should wait for before moving on. There are class constants that can be used here. Valid motor IDs are the capital letters A B C or D and correspond to the labelled ports on the EV3 brick.

Example

use Libraries.Robots.Lego.Motor

Motor motor

motor:SetSpeed(motor:MOTOR_A, 480)      //two motors for moving the robot, connected to ports A and B
motor:SetSpeed(motor:MOTOR_B, 480)
motor:RotateForward(motor:MOTOR_A)
motor:RotateByDegrees(motor:MOTOR_B, 1440)
motor:Wait(motor:MOTOR_B)   //motor A will continue moving during this time, as well
motor:Stop(motor:MOTOR_A)