Libraries.Robots.Lego.GyroSensor Documentation

The GyroSensor class is an object representation of the LEGO Mindstorms EV3 Gyro Sensor. It is used to measure the rotation and rotation speed of the robot if the robot is turning. This is useful if we need to have information on the orientation of our robot, for example, to keep the robot moving in a straight line.

Example Code

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

class Main
action Main
    Motor motor
    GyroSensor gyro
    Utility utility

    gyro:SetPort(2)

    motor:RotateForward(motor:MOTOR_B)
    motor:RotateForward(motor:MOTOR_C)

    repeat while true
        // if robot is moving too far to the right, adjust speed of motor
        // C (the left motor) to keep the robot moving in a straight line
        if gyro:GetRotation() < -1.0
            motor:Stop(motor:MOTOR_C)
            motor:SetSpeed(motor:MOTOR_C, motor:GetSpeed(motor:MOTOR_C) + 5)
            motor:RotateForward(motor:MOTOR_C)
        // if robot is moving too far to the left, adjust speed of motor
        // B (the right motor) to keep the robot moving in a straight line
        elseif gyro:GetRotation() > 1.0
            motor:Stop(motor:MOTOR_B)
            motor:SetSpeed(motor:MOTOR_B, motor:GetSpeed(motor:MOTOR_B) + 5)
            motor:RotateForward(motor:MOTOR_B)
        end
        // wait a second before sampling the angle again
        utility:DelayMilliseconds(1000)
    end
end
end

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
integer PORT_4This action lets the program know which port on the robot that the gyro sensor being used is plugged into. This action must be called before any other action in the GyroSensor class will work.
integer PORT_3
integer PORT_2
integer PORT_1

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

This action measures the rotation angle in degrees of the robot with respect to its starting orientation. If the robot has turned clockwise with respect to its starting orientation, the gyro sensor will report a negative angle. If the robot has turned counter-clockwise with respect to its starting orientation, the gyro sensor will report a positive angle.

Return

number: The rotation in degrees of the robot with respect to its starting orientation.

Example

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

class Main
    action Main
        Motor motor
        GyroSensor gyro
        Utility utility
        Screen screen
        Button button

        integer lineNumber = 0

        gyro:SetPort(2)

        motor:SetSpeed(motor:MOTOR_B, 100)
        motor:SetSpeed(motor:MOTOR_C, 10)

        motor:RotateForward(motor:MOTOR_B)
        motor:RotateForward(motor:MOTOR_C)

        repeat while lineNumber <= 7
            text message = "Rotation: " + gyro:GetRotation()
            screen:Output(message, lineNumber)
            lineNumber = lineNumber + 1
            utility:DelayMilliseconds(1000)
        end

        motor:Stop(motor:MOTOR_B)
        motor:Stop(motor:MOTOR_C)

        repeat until button:IsButtonPressed(button:CENTER_BUTTON)

        end
    end
end

GetRotationSpeed()

This action reports the rotation speed of the robot in degrees per second. If the robot is turning clockwise with respect to its starting orientation, then the rotation speed is negative. If the robot is turning counter-clockwise with respect to its starting orientation, then the rotation speed is positive.

Return

number: The rotation speed of the robot

Example

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

class Main
    action Main
        Motor motor
        GyroSensor gyro
        Utility utility
        Screen screen
        Button button

        integer lineNumber = 0

        gyro:SetPort(2)

        motor:SetSpeed(motor:MOTOR_B, 100)
        motor:SetSpeed(motor:MOTOR_C, 10)

        motor:RotateForward(motor:MOTOR_B)
        motor:RotateForward(motor:MOTOR_C)

        repeat while lineNumber <= 7
            text message = "Speed: " + gyro:GetRotationSpeed()
            screen:Output(message, lineNumber)
            lineNumber = lineNumber + 1
            utility:DelayMilliseconds(1000)
        end

        motor:Stop(motor:MOTOR_B)
        motor:Stop(motor:MOTOR_C)

        repeat until button:IsButtonPressed(button:CENTER_BUTTON)

        end
    end
end

Reset()

This action resets the gyro sensor so that any measurements taken after the sensor was reset will be with respect to the orientation of the robot at that time. For best results, the sensor should not be moving while it is being reset.

Example

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

class Main
    action Main
        Motor motor
        GyroSensor gyro
        Utility utility
        Screen screen
        Button button

        integer lineNumber = 0

        gyro:SetPort(2)

        motor:SetSpeed(motor:MOTOR_B, 100)
        motor:SetSpeed(motor:MOTOR_C, 10)

        motor:RotateForward(motor:MOTOR_B)
        motor:RotateForward(motor:MOTOR_C)

        repeat while lineNumber <= 4
            text message = "Angle: " + gyro:GetRotation()
            screen:Output(message, lineNumber)
            lineNumber = lineNumber + 1
            utility:DelayMilliseconds(1000)
        end

        motor:Stop(motor:MOTOR_B)
        motor:Stop(motor:MOTOR_C)

        gyro:Reset()

        utility:DelayMilliseconds(3000)

        motor:SetSpeed(motor:MOTOR_B, 100)
        motor:SetSpeed(motor:MOTOR_C, 100)

        motor:RotateForward(motor:MOTOR_B)
        motor:RotateForward(motor:MOTOR_C)

        repeat while lineNumber <= 7
            text message = "Angle:" + gyro:GetRotation()
            screen:Output(message, lineNumber)
            lineNumber = lineNumber + 1
            utility:DelayMilliseconds(1000)
        end

        motor:Stop(motor:MOTOR_B)
        motor:Stop(motor:MOTOR_C)

        repeat until button:IsButtonPressed(button:CENTER_BUTTON)

        end
    end
end

SetPort(integer portNumber)

This action lets the program know which port on the robot that the gyro sensor being used is plugged into. This action must be called before any other action in the GyroSensor class will work.

Parameters

  • integer portNumber: specifies the port on the robot that the gyro sensor is plugged into. The port number corresponds to the actual number printed above the port on the EV3 brick. Valid port numbers are 1, 2, 3 or 4.

Example

use Libraries.Robots.Lego.GyroSensor

GyroSensor gyro1
GyroSensor gyro2            // we can have multiple gyro sensors attached to the robot

gory1:SetPort(1)            // this tells the robot that the gyro sensor is in port 1
gyro2:SetPort(gyro2:PORT_2) // we can also use the class constants to set the port