Querying sensor data allows you to react to real-time values coming from the robots’ physical sensors. For example, "if accelerometer z-axis > 3G's, then set LED's to green."
Retrieves motion acceleration data along a given axis measured by the Accelerometer, in g's, where 1 g is 9.80665 m/s².
X acceleration is the left-to-right acceleration, from -8 to 8 g's:
JavaScript: getAcceleration().x
Python: get_acceleration()['x']
Y acceleration is the forward-to-back acceleration, from of -8 to 8 g's:
JavaScript: getAcceleration().y
Python: get_acceleration()['y']
Z acceleration is the upward-to-downward acceleration, from -8 to 8 g's:
JavaScript: getAcceleration().z
Python: get_acceleration()['z']
Total acceleration is the combined vector acceleration of all 3 axes, from 0 to 14 g's:
JavaScript: Math.sqrt((getAcceleration().x**2)+(getAcceleration().y**2)+(getAcceleration().z**2))
Python: Math.sqrt((get_acceleration()['x'] ** 2) + (get_acceleration()['y'] ** 2) + (get_acceleration()['z'] ** 2))
Vertical acceleration is the upward or downward acceleration regardless of the robot's orientation, from -8 to 8 g's:
JavaScript: getVerticalAcceleration()
Python: get_vertical_acceleration()
Returns:
{{ x: number; y: number; z: number }} - An object containing x, y, and z acceleration values, where x is left-to-right acceleration, y is forward-to-back acceleration, and z is upward-to-downward acceleration (-8g to 8g)
Retrieves the tilt angles of the robot measured by the Gyroscope, in degrees.
Pitch is the forward or backward tilt angle, from -180° to 180°:
JavaScript: getOrientation().pitch
Python: get_orientation()['pitch']
Roll is left or right tilt angle, from -90° to 90°:
JavaScript: getOrientation().roll
Python: get_orientation()['roll']
Yaw is the spin (twist) angle, from -180° to 180°:
JavaScript: getOrientation().yaw
Python: get_orientation()['yaw']
Returns:
{{ pitch: number; roll: number; yaw: number }} - An object containing pitch, roll, and yaw values, where pitch is the forward or backward tilt angle (-180° to 180°), roll is the left or right tilt angle (-90° to 90°), and yaw is the spin (twist) angle (-180° to 180°)
Retrieves the rate of rotation around a given axis measured by the Gyroscope, in degrees per second.
Pitch is the rate of forward or backward spin, from -2,000° to 2,000° per second:
JavaScript: getGyroscope().pitch
Python: get_gyroscope()['pitch']
Roll is the rate of left or right spin, from -2,000° to 2,000° per second:
JavaScript: getGyroscope().roll
Python: get_gyroscope()['roll']
Yaw is the rate of sideways spin, from -2,000° to 2,000° per second:
JavaScript: getGyroscope().yaw
Python: get_gyroscope()['yaw']
Returns:
{{ pitch: number; roll: number; yaw: number }} - An object containing pitch, roll, and yaw rotation rates, where pitch is the forward or backward spin rate, roll is the left or right spin rate, and yaw is the sideways spin rate (-2,000° to 2,000° per second)
Retrieves the velocity along a given axis measured by the motor encoders, in centimeters per second.
X velocity is the right (+) or left (-) velocity, in centimeters per second:
JavaScript: getVelocity().x
Python: get_gyroscope()['x']
Y velocity is the forward (+) or back (-) velocity, in centimeters per second:
JavaScript: getVelocity().y
Python: get_gyroscope()['y']
Total velocity is the combined vector velocity of both axes which will always be a positive value, in centimeters per second:
JavaScript: Math.sqrt((getVelocity().x ** 2) + (getVelocity().y ** 2))
Python: Math.sqrt((get_velocity()['x'] ** 2) + (get_velocity()['y'] ** 2))
Returns:
{{ x: number; y: number }} - An object containing x and y velocity values, where x is the right (+) or left (-) velocity in cm/s, and y is the forward (+) or backward (-) velocity in cm/s
Retrieves the robot's current location (x, y) relative to the program start origin, in centimeters. This is the offset from the origin, not the total distance traveled.
X location is the right (+) or left (-) distance from the origin of the program start, in centimeters:
JavaScript: getLocation().x
Python: get_location()['x']
Y location is the forward (+) or backward (-) distance from the origin of the program start, in centimeters:
JavaScript: getLocation().y
Python: get_location()['y']
Total location is the total distance from the origin of the program start, which will always be a positive value, in centimeters:
JavaScript: Math.sqrt((getLocation().x ** 2) + (getLocation().y ** 2))
Python: Math.sqrt((get_location()['x'] ** 2) + (get_location()['y'] ** 2))
Returns:
{{ x: number; y: number }} - An object containing x and y position values, where x is the right (+) or left (-) distance from the origin in cm, and y is the forward (+) or backward (-) distance from the origin in cm
Retrieves the total distance traveled during the program, in centimeters.
JavaScript: getDistance()
Python: get_distance()
Returns:
{number} - The total distance the robot has moved since the program started, in centimeters
Retrieves the current target speed of the robot, from -255 to 255. Positive values indicate forward movement, negative values indicate backward movement, and zero (0) means the robot is stopped.
JavaScript: getSpeed()
Python: get_speed()
returns:
{number} - The current target speed of the robot (-255 to 255)
Retrieves the target directional heading of the robot, in degrees. Assuming the aim of the robot, then 0° heading is forward, 90° is right, 180° is backward, and 270° is left.
JavaScript: getHeading()
Python: get_heading()
Returns:
{number} - The robot's current heading angle, in degrees
Retrieves the current RGB color of the main LEDs. Each color channel ranges from 0 to 255.
The full RGB color of the main LEDs, from 0 - 255 for each color channel:
JavaScript: getMainLed()
Python: get_main_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getMainLed().r
getMainLed().g
getMainLed().b
Python:
get_main_led()['r']
get_main_led()['g']
get_main_led()['b']
Retrieves the current RGB color of the front LEDs. Each color channel ranges from 0 to 255.
JavaScript: getFrontLed()
Python: get_front_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getFrontLed().r
getFrontLed().g
getFrontLed().b
Python:
get_front_led()['r']
get_front_led()['g']
get_front_led()['b']
Compatible Robots:
BOLT, BOLT+, RVR, RVR+, R2-D2, R2-Q5
Retrieves the current RGB color of the left headlight LED. Each color channel ranges from 0 to 255.
JavaScript: getLeftHeadlightLed()
Python: get_left_headlight_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getLeftHeadlightLed().r
getLeftHeadlightLed().g
getLeftHeadlightLed().b
Python:
get_left_headlight_led()['r']
get_left_headlight_led()['g']
get_left_headlight_led()['b']
Compatible Robots:
RVR, RVR+
Retrieves the current RGB color of the right headlight LED. Each color channel ranges from 0 to 255.
JavaScript: getRightHeadlightLed()
Python: get_right_headlight_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getRightHeadlightLed().r
getRightHeadlightLed().g
getRightHeadlightLed().b
Python:
get_right_headlight_led()['r']
get_right_headlight_led()['g']
get_right_headlight_led()['b']
Compatible Robots:
RVR, RVR+
Retrieves the current RGB color of the left LED. Each color channel ranges from 0 to 255.
JavaScript: getLeftLed()
Python: get_left_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getLeftLed().r
getLeftLed().g
getLeftLed().b
Python:
get_left_led()['r']
get_left_led()['g']
get_left_led()['b']
Compatible Robots:
BOLT+, RVR, RVR+
Retrieves the current RGB color of the right LED. Each color channel ranges from 0 to 255.
JavaScript: getRightLed()
Python: get_right_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getRightLed().r
getRightLed().g
getRightLed().b
Python:
get_right_led()['r']
get_right_led()['g']
get_right_led()['b']
Compatible Robots:
BOLT+, RVR, RVR+
Retrieves the current RGB color of the back LED. Each color channel ranges from 0 to 255.
JavaScript: getBackLed()
Python: get_back_led()
Returns:
{{ r: number; g: number; b: number }} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getBackLed().r
getBackLed().g
getBackLed().b
Python:
get_back_led()['r']
get_back_led()['g']
get_back_led()['b']
Retrieves the current blue channel value of the back LED, from 0 to 255. Some robots, like Mini and SPRK+, only have a blue back LED, which is primarily used for aiming. For these robots, this function effectively retrieves the brightness value of that LED. This will return the blue color channel value for robots that have a full RGB back LED (for example, BOLT, BOLT+, and RVR+).
JavaScript: getBackLed().b
Python: get_back_led()['b']
Retrieves the amount of time that the program has been running, in seconds.
JavaScript: getElapsedTime()
Python: get_elapsed_time()
Returns:
{number} - The elapsed time since the program started, in seconds
Retrieves the current state of stabilization for the robot. Stabilization is "on" if set with the stabilization function.
JavaScript: getStabilization()
Python: get_stabilization()
Returns:
{boolean} - A boolean for the state of stabilization (on = true, off = false)
This function does not have a respective block in the block canvas. As a result, it is only useable in the text canvas.
Retrieves the ambient light intensity in lux from the ambient light sensor. Light levels range from 0 (complete darkness) to 30,000+ (direct sunlight).
JavaScript: getAmbientLight()
Python: get_ambient_light()
You may need to adjust the condition based on ambient light in different environments as light intensity can vary greatly between rooms. Here are some examples of common lux values for different environments:
Direct sunlight: 30,000-100,000
Daylight: 10,000–25,000
Phone flashlight: 5,000-20,000
Overcast daylight: 1,000-5,000
Average office/school ambient light: 300-500
Average home ambient light: 50-300
Moonlight: <0.5
Returns:
{number} - The ambient light intensity in lux
Compatible Robots:
BOLT, BOLT+, RVR, RVR+
Retrieves the channel number of the last received infrared message. Requires a declared IR message event before retrieving this value.
JavaScript: getLastIRMessage()
Python: get_last_ir_message()
Returns:
{number} - The IR channel of the last received message
Compatible Robots:
BOLT, BOLT+, RVR, RVR+
Retrieves the real-world offset between the aim heading and the last compass North reading. 0° is due North, 90° is due East, 180° is due South, and 270° is due West. Requires the Calibrate Compass command before retrieving this value. Metallic and magnetic objects affect the accuracy of the compass reading.
JavaScript: getCompassDirection()
Python: get_compass_direction()
Returns:
{number} - The compass direction, in degrees
Compatible Robots:
BOLT
Retrieves the current RGB color for the color sensor, from 0 - 255 for each color channel.
JavaScript: getColor()
Python: get_color()
Parameters:
[channel] {ColorChannel} - whether to return a specific Red/Green/Blue channel or the full color if unspecified
Returns:
{{r: number, g: number, b: number}} - An object containing the RGB color values, where r is the red channel, g is the green channel, and b is the blue channel (0 to 255)
Use the following to get individual red, green, or blue color channels:
JavaScript:
getColor("red")
getColor("green")
getColor("blue")
Python:
get_color("red")
get_color("green")
get_color("blue")
The color sensor function can be used for all sorts of interesting purposes. One fun example is changing the color of the main LEDs to match what RVR+'s color sensor is returning:
When the program starts, the main LEDs on RVR+ will update to reflect the color underneath RVR+'s color sensor. Here is the JavaScript and Python code for the block example above:
JavaScript:
async function startProgram() {
setMainLed(getColor());
}
Python:
async def start_program():
set_main_led(get_color())
There are a lot of ways to take this basic concept and make it more advanced. In the example below, RVR+ will drive continuously while the main LEDs are updated to display the returned color from the color sensor. This results in a "chameleon" mode where the RVR+ LEDs will reflect the color of the surface underneath RVR+.
JavaScript:
async function startProgram() {
setSpeed(100);
while (true) {
setMainLed(getColor());
await delay(0.025);
}
}
Python:
async def start_program():
set_speed(100)
while True:
set_main_led(get_color())
await delay(0.025)
Compatible Robots:
RVR, RVR+
Retrieves the numerical value for a specified color channel from the color sensor, from 0 - 255.
Use the following to get individual red, green, or blue color channels:
JavaScript:
getColorChannel("red")
getColorChannel("green")
getColorChannel("blue")
Python:
get_color_channel("red")
get_color_channel("green")
get_color_channel("blue")
Parameters:
channel {ColorChannel} - the Red/Green/Blue component of the color to query
Returns:
{number} - The numerical value for the Red, Green, or Blue color channel (0 - 255)
Compatible Robots:
RVR, RVR+
Retrieves the brightness of the two single-color LEDs (red and blue) in the BB-9E dome. Values range from 0 (off) to 15 (full brightness).
JavaScript: getDomeLeds()
Python: get_dome_leds()
Returns:
{number} - The brightness value, from 0 - 15
Compatible Robots:
BB-9E
Retrieves the brightness of the Holographic Projector white LED, from 0 to 255.
JavaScript: getHoloProjectorLed()
Python: get_holo_projector_led()
Returns:
{number} - The brightness level, from 0 to 255
Compatible Robots:
R2-D2, R2-Q5
Retrieves the brightness of the Logic Display LEDs, from 0 to 255.
JavaScript: getLogicDisplayLeds()
Python: get_logic_display_leds()
Returns:
{number} - The brightness level, from 0 to 255
Compatible Robots:
R2-D2, R2-Q5