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

Summary

Variable Summary Table

VariablesDescription
text MOTOR_B
text MOTOR_D
text MOTOR_C
text MOTOR_A

Actions Summary Table

ActionsDescription
Compare(Libraries.Language.Object object)This action compares two object hash codes and returns a CompareResult.
Equals(Libraries.Language.Object object)This action determines if two objects are equal based on their hash code values.
GetHashCode()This action gets the hash code for an object.
GetRotation(text motorID)This action will give the position of a motor based on how many degrees it has rotated.
GetRotationTarget(text motorID)This action reports the target degree of a rotating motor.
GetSpeed(text motorID)This action will give the speed associated with the motor requested.
IsMoving(text motorID)This action is used to find out if a specific motor is currently moving or not.
ResetRotation(text motorID)This action resets the motor's memory of its current rotation.
RotateBackward(text motorID)This action will cause the specified motor to rotate backward until stopped by the Stop action, or if the program ends.
RotateByDegrees(text motorID, number degrees)This action rotates a motor by the amount of degrees specified
RotateForward(text motorID)This action will cause the specified motor to rotate forward until stopped by the Stop action, or if the program ends.
RotateToDegree(text motorID, number degreeTarget)This action causes a motor to move to a certain degree, based on its current rotation memory.
SetSpeed(text motorID, integer speed)This action sets the speed of a motor.
Stop(text motorID)This action causes a motor to stop moving.
Wait(text motorID)This action will cause the robot's program to wait until the specified motor stops moving.

Actions Documentation

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns a CompareResult. The compare result is either larger if this hash code is larger than the object passed as a parameter, smaller, or equal.

Example Code

use Libraries.Language.Support.CompareResult
        Object o
        Object t
        CompareResult result = o:Compare(t)

Parameters

Return

Libraries.Language.Support.CompareResult: The Compare result, Smaller, Equal, or Larger.

Equals(Libraries.Language.Object object)

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

Example Code

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

Parameters

Return

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

GetHashCode()

This action gets the hash code for an object.

Example Code

Object o
        integer hash = o:GetHashCode()

Return

integer: The integer hash code of the object.

GetRotation(text motorID)

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

Example Code

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()

Parameters

Return

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

GetRotationTarget(text motorID)

This action reports the target degree of a rotating motor.

Example Code

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()

Parameters

Return

integer: the degree that the motor is rotating to.

GetSpeed(text motorID)

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

Example Code

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

Parameters

Return

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

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.

Example Code

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)

Parameters

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).

ResetRotation(text motorID)

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

Example Code

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()

Parameters

RotateBackward(text motorID)

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

Example Code

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)

Parameters

RotateByDegrees(text motorID, number degrees)

This action rotates a motor by the amount of degrees specified

Example Code

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)

Parameters

RotateForward(text motorID)

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

Example Code

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)

Parameters

RotateToDegree(text motorID, number degreeTarget)

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

Example Code

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)

Parameters

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.

Example Code

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

Parameters

Stop(text motorID)

This action causes a motor to stop moving.

Example Code

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

Parameters

Wait(text motorID)

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

Example Code

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)

Parameters