Libraries.Robots.Lego.InfraredSensor Documentation

The InfraredSensor class is an object representation of the LEGO Mindstorms EV3 Infrared Sensor. It is used to measure distance between the robot and other physical objects. The range at which this sensor works is from around 5 to 40 centimeters. Additionally, the EV3 remote, also called a beacon, interacts with the robot through this sensor.

Example Code

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

class Main
    action Main
        InfraredSensor infraredSensor
        Motor motor
        Sound sound
        Screen screen
        
        integer command = 0
        infraredSensor:SetPort(infraredSensor:PORT_4)   //tells the program that there is an infrared sensor connected to port 4 of the robot
        repeat while true
            if infraredSensor:GetDistance() < 25
                screen:Output("Object nearby.", 0)
                sound:Buzz()
            else
                screen:Clear()
                command = infraredSensor:GetRemoteCommand(infraredSensor:CHANNEL_1)  //determines what button was pressed on the EV3 remote over channel 1
                if command = 1                    //forward
                    motor:SetSpeed("B", 480)
                    motor:SetSpeed("C", 480)
                    motor:RotateForward("B")
                    motor:RotateForward("C")
                elseif command = 2                //backward
                    motor:SetSpeed("B", 480)
                    motor:SetSpeed("C", 480)
                    motor:RotateBackward("B")
                    motor:RotateBackward("C")
                elseif command = 3                //right
                    motor:SetSpeed("B", 480)
                    motor:SetSpeed("C", 480)
                    motor:Stop("B")
                    motor:RotateForward("C")
                elseif command = 4                //left
                    motor:SetSpeed("B", 480)
                    motor:SetSpeed("C", 480)
                    motor:Stop("C")
                    motor:RotateForward("B")
                else
                    motor:Stop("B")
                    motor:Stop("C")
                end
            end
        end
    end
end

Inherits from: Libraries.Language.Object

Summary

Variable Summary Table

VariablesDescription
integer PORT_3
integer BUTTON_TOP_RIGHT
integer BUTTONS_BOTTOM_LEFT_TOP_LEFT
integer PORT_4
integer BUTTON_BOTTOM_LEFT
integer CHANNEL_3
integer BUTTONS_BOTTOM_LEFT_BOTTOM_RIGHT
integer PORT_2
integer BUTTON_CENTER
integer PORT_1
integer BUTTON_BOTTOM_RIGHT
integer BUTTONS_TOP_RIGHT_BOTTOM_RIGHT
integer BUTTON_TOP_LEFT
integer CHANNEL_1
integer BUTTONS_TOP_LEFT_BOTTOM_RIGHT
integer BUTTONS_TOP_LEFT_TOP_RIGHT
integer CHANNEL_2
integer CHANNEL_4
integer BUTTONS_BOTTOM_LEFT_TOP_RIGHT

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.
GetDistance()This action uses the infrared sensor to measure the distance between it and a physical object directly ahead of it.
GetHashCode()This action gets the hash code for an object.
GetRemoteCommand(integer channel)This action retrieves a command, or button press, from an EV3 remote.
GetRemoteDirection(integer channel)This action is used to find the direction of an EV3 remote with respect to the infrared sensor.
GetRemoteDistance(integer channel)This action is used to find the distance of an EV3 remote with respect to the infrared sensor.
SetPort(integer portNumber)This action lets the program know which port on the robot that the infrared sensor being used is plugged into.

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.

GetDistance()

This action uses the infrared sensor to measure the distance between it and a physical object directly ahead of it. The reliable range for measuring distance is up to around 50 centimeters.

Example Code

use Libraries.Robots.Lego.InfraredSensor
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.Sound
    
    InfraredSensor infraredSensor
    Motor motor
    Sound sound
    
    infraredSensor:SetPort(infraredSensor:PORT_3)   //tells the program that the infrared sensor is plugged into port 3 of the robot
    motor:RotateForward("B")
    motor:RotateForward("C")
    repeat until infraredSensor:GetDistance() < 40  //the robot moves forward until getting close to an object
    end
    sound:BeepTwice()

Return

integer: an integer representation that corresponds to units of centimeters between the sensor and object. The reflectivity of the object may cause the number to be inaccurate. Possible return values range from 5 to around 50, depending on the object.

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.

GetRemoteCommand(integer channel)

This action retrieves a command, or button press, from an EV3 remote.

Example Code

use Libraries.Robots.Lego.InfraredSensor
    use Libraries.Robots.Lego.Sound

    InfraredSensor infraredSensor
    Sound sound

    infraredSensor:SetPort(infraredSensor:PORT_4)
    integer command = 0
    repeat while true
        command = infraredSensor:GetRemoteCommand(infraredSensor:CHANNEL_1)
        if command = 1              //top left remote button
            sound:PlayTone(200, 20)
        elseif command = 2          //bottom left remote button
            sound:PlayTone(400, 20)
        elseif command = 3          //top right remote button
            sound:PlayTone(800, 20)
        elseif command = 4          //bottom right remote button
            sound:PlayTone(1600, 20)
        end
    end

Parameters

Return

integer: an integer representing the key(s) pressed on the EV3 remote. There are class constants available to help determine the return value. Return values correspond to the following key(s) pressed on the EV3 remote: 1 TOP-LEFT 2 BOTTOM-LEFT 3 TOP-RIGHT 4 BOTTOM-RIGHT 5 TOP-LEFT + TOP-RIGHT 6 TOP-LEFT + BOTTOM-RIGHT 7 BOTTOM-LEFT + TOP-RIGHT 8 BOTTOM-LEFT + BOTTOM-RIGHT 9 CENTER 10 BOTTOM-LEFT + TOP-LEFT 11 TOP-RIGHT + BOTTOM-RIGHT

GetRemoteDirection(integer channel)

This action is used to find the direction of an EV3 remote with respect to the infrared sensor.

Example Code

use Libraries.Robots.Lego.InfraredSensor
    use Libraries.Robots.Lego.Motor
    
    InfraredSensor infraredSensor
    Motor motor

    infraredSensor:SetPort(infraredSensor:PORT_4)
    motor:SetSpeed("B", 720)    //this motor should move the right side of the robot
    motor:SetSpeed("C", 720)    //this motor should move the left side of the robot

    integer direction = 0
    repeat while true
        direction = infraredSensor:GetRemoteDirection(infraredSensor:CHANNEL_1) //tells the program to interact with a remote using channel 1
        if direction > 0            //remote is on the right side of the sensor
            motor:RotateForward("C")
            motor:Stop("B")
        elseif direction < 0        //remote is on the left side of the sensor
            motor:RotateForward("B")
            motor:Stop("C")
        elseif infraredSensor:GetRemoteDistance(1) < 100   //remote straight ahead and remote still in range of the sensor
            output infraredSensor:GetRemoteDistance(1)
            motor:RotateForward("B")
            motor:RotateForward("C")
        else                        //found the remote
            motor:Stop("B")
            motor:Stop("C")
        end
    end

Parameters

Return

integer: an integer representing the relative angle, in degrees, of the remote with respect to the robot. Values returned will be between -180 to 180, where: negative integers represent that the remote is to the left of the sensor, positive integers represent that the remote is to the right of the sensor, and a value of 0 indicates that the remote is directly ahead of the sensor.

GetRemoteDistance(integer channel)

This action is used to find the distance of an EV3 remote with respect to the infrared sensor.

Example Code

use Libraries.Robots.Lego.InfraredSensor
    use Libraries.Robots.Lego.Sound

    InfraredSensor infraredSensor
    Sound sound
    
    infraredSensor:SetPort(infraredSensor:PORT_4)
    repeat while true
        if infraredSensor:GetRemoteDistance(1) <= 20
            sound:PlayTone(1100, 20)
        elseif infraredSensor:GetRemoteDistance(1) <= 40
            sound:PlayTone(900, 20)
        elseif infraredSensor:GetRemoteDistance(1) <= 60
            sound:PlayTone(700, 20)
        elseif infraredSensor:GetRemoteDistance(1) <= 80
            sound:PlayTone(500, 20)
        elseif infraredSensor:GetRemoteDistance(1) <= 100
            sound:PlayTone(300, 20)
        else
            sound:PlayTone(20, 20)
        end
    end

Parameters

Return

integer: an integer representing the centimeters between the infrared sensor and the EV3 remote using the specified channel. Return values are 1 through 100 if the remote is found, otherwise the value will be the maximum integer value (2147483647).

SetPort(integer portNumber)

This action lets the program know which port on the robot that the infrared sensor being used is plugged into. As such, this action must be called before any other action in the InfraredSensor class will work.

Example Code

use Libraries.Robots.Lego.InfraredSensor

    InfraredSensor infraredSensor1
    InfraredSensor infraredSensor2
    InfraredSensor infraredSensor3
    
    infraredSensor1:SetPort(1)                      //we can have multiple infrared sensors plugged into the robot at once
    infraredSensor2:SetPort(2)                      //after setting the ports, we can now use any other action in the ColorSensor class
    infraredSensor3:SetPort(infraredSensor3:PORT_3) //we can use a class constant to designate a port, as well

Parameters