# Sensors

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

### Accelerometer

Provides motion acceleration data along a given axis measured by the Accelerometer, in g's, where g = 9.80665 m/s^2.

getAcceleration().x is the left-to-right acceleration, from -8 to 8 g's.

getAcceleration().y is the forward-to-back acceleration, from of -8 to 8 g's.

getAcceleration().z is the upward-to-downward acceleration, from -8 to 8 g's.

Math.sqrt((getAcceleration().x ** 2) + (getAcceleration().y ** 2) + (getAcceleration().z ** 2)) is the combined vector acceleration of all 3 axes, from 0 to 14 g's.

getVerticalAcceleration() is the upward or downward acceleration regardless of the robot's orientation, from -8 to 8 g's.

### Orientation

Provides the tilt angle along a given axis measured by the Gyroscope, in degrees.

getOrientation().pitch is the forward or backward tilt angle, from -180° to 180°.

getOrientation().roll is left or right tilt angle, from -90° to 90°.

getOrientation().yaw is the spin (twist) angle, from -180° to 180°.

### Gyroscope

Provides the rate of rotation around a given axis measured by the gyroscope, from -2,000° to 2,000° per second.

getGyroscope().pitch is the rate of forward or backward spin, from -2,000° to 2,000° per second.

getGyroscope().roll is the rate of left or right spin, from -2,000° to 2,000° per second.

getGyroscope().yaw is the rate of sideways spin, from -2,000° to 2,000° per second.

### Velocity

Provides the velocity along a given axis measured by the motor encoders, in centimeters per second.

getVelocity().x is the right (+) or left (-) velocity, in centimeters per second.

getVelocity().y is the forward (+) or back (-) velocity, in centimeters per second.

Math.sqrt((getVelocity().x ** 2) + (getVelocity().y ** 2)) is the combined vector velocity of both axes which will always be a positive value, in centimeters per second.

### Location

Provides the location where the robot is in space (x,y) relative to the origin, in centimeters. This is not the distance traveled during the program, it is the offset from the origin (program start).

getLocation().x is the right (+) or left (-) distance from the origin of the program start, in centimeters.

getLocation().y is the forward (+) or backward (-) distance from the origin of the program start, in centimeters.

Math.sqrt((getLocation().x ** 2) + (getLocation().y ** 2)) is the total distance from the origin of the program start, which will always be a positive value, in centimeters.

### Distance

getDistance() is the total distance traveled in the program, in centimeters.

### Speed

getSpeed() is the current target speed of the robot, from -255 to 255, where positive is forward, negative is backward, and 0 is stopped.

getHeading() is the target directional angle, in degrees. Assuming you aim the robot with the blue tail light facing you, then 0° heading is forward, 90° is right, 180° is backward, and 270° is left.

### Main LED

getMainLed() is the RGB color of the main LEDs, from 0 to 255 for each color channel.

getMainLed().r is the red channel, from 0 - 255.

getMainLed().g is the green channel, from 0 - 255.

getMainLed().b is the blue channel, from 0 - 255.

### Back LED - Blue Only

getBackLed().b is the brightness of the back LED, from 0 to 255. For Sphero BOLT, use getBackLed() to get the RGB value.

### Time Elapsed

getElapsedTime() is the amount of time that the program has run for, in seconds. For example, you could use getElapsedTime() to swap between forward and backward movements every 4s in an ocean buoy animation using:

var lastTime = 0;
var loopCount = 0;

async function startProgram() {
setBackLed(255);
for (var _i1 = 0; _i1 < 4; _i1++) {
await Sound.Water.Waves.play(false);
if ((loopCount % 2) === 1) {
while (!(((getElapsedTime() - lastTime) >= 4))) {
setMainLed({ r: 11, g: 255, b: 0 });
await rawMotor(1000, 1000, 0.4);
setMainLed({ r: 0, g: 0, b: 0 });
await delay(0.5);
}
lastTime = getElapsedTime();
} else {
while (!(((getElapsedTime() - lastTime) >= 4))) {
setMainLed({ r: 255, g: 41, b: 16 });
await rawMotor(-1000, -1000, 0.4);
setMainLed({ r: 0, g: 0, b: 0 });
await delay(0.5);
}
lastTime = getElapsedTime();
}
loopCount = (loopCount + 1);
await delay(0.025);
}
}

## Sphero BOLT Sensors

### Compass Direction

getCompassDirection() is 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 to be run before you can get this value. Metallic and magnetic objects affect the accuracy of the compass reading.

### Luminosity

getLuminosity() is the light intensity from 0 - 100,000 lux, where 0 lux is full darkness and 30,000-100,000 lux is direct sunlight. You may need to adjust a condition based on luminosity in different environments as light intensity can vary greatly between rooms.

### Last Message Received

getLastIRMessage() returns which channel the last infrared message was received on. You need to declare the onIRMessage() event for each IR message you plan to see returned.

### Back LED

getBackLed() is the RGB color of the back LED, from 0 to 255 for each color channel.

### Front LED

getFrontLed() is the RGB color of the front LED, from 0 to 255 for each color channel.

## Sphero RVR Sensors

### Color Sensor

getColor() is the RGB color, from 0 to 255 for each color channel, that is returned from RVR's color sensor.

getColor("red") is the red channel, from 0 - 255, that is returned from RVR's color sensor.

getColor("green") is the green channel, from 0 - 255, that is returned from RVR's color sensor.

getColor("blue") is the blue channel, from 0 - 255, that is returned from RVR's color sensor.

The color sensor block 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 code for the block example above:

async function startProgram() {
setMainLed(getColor());
}

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 floor underneath RVR.

async function startProgram() {
setSpeed(100);
while (true) {
setMainLed(getColor());
await delay(0.025);
}
}

### Color Channel

getColorChannel("red") is the numeric value for the red channel, from 0 - 255, that is returned from RVR's color sensor.

getColorChannel("green") is the numeric value for the channel, from 0 - 255, that is returned from RVR's color sensor.

getColorChannel("blue") is the numeric value for the blue channel, from 0 - 255, that is returned from RVR's color sensor.

## R2-D2 & R2-Q5 Sensors

### Back LED

getBackLed() is the RGB color of the back LED, from 0 to 255 for each color channel.

### Front LED

getFrontLed() is the RGB color of the front LED, from 0 to 255 for each color channel.

### Dome LEDs

getDomeLeds() is the brightness of the Dome LEDs, from 0 to 15.

### Holo Projector LED

getHoloProjectorLed() is the brightness of the Holographic Projector LED, from 0 to 255.

### Logic Display LEDs

getLogicDisplayLeds() is the brightness of the white Logic Display LEDs, from 0 to 255.