Comparators are used to compare values and create conditional logic; aka as Logical Operators or Relational Operators.
For example, the below examples cause Sphero to accelerate until its speed is greater than 150:
JavaScript:
async function startProgram() {
setSpeed(5);
while (!(getSpeed() >= 150)) {
setSpeed(getSpeed() + 5);
await delay(0.2);
}
}
Python:
async def start_program():
set_speed(5)
while not (get_speed() >= 150):
set_speed(get_speed() + 5)
await delay(0.2)
Many comparators are the same between JavaScript and Python. Here are common comparators to familiarize yourself with:
== Evaluates if the left value is equal the right value, compensating for different data types (example: 3 == "3" is true)
!= Evaluates if the left value differs from the right value, compensating for different data types (example: "3.01" != 3.01 is false)
< Evaluates if the left value is less than the right value
<= Evaluates if the left value is less than or equal to the right value
> Evaluates if the left value is greater than the right value
>= Evaluates if the left value is greater than or equal to the right value
JavaScript Only:
=== Evaluates if the left value is equal to the right value, does not compensate for different data types (example: "3" === 3 is false)
!== Evaluates if the left value differs from the right value, does not compensate for different data types (example: "3.01" !== 3.01 is true)
NOTE: It is generally encouraged to use === and !== instead of == and != when programming with JavaScript.
Links multiple conditions, requiring that all conditions must be true for the set to evaluate as true:
JavaScript: &&
Python: and
Links multiple conditions, allowing the set to evaluate as true if any condition is true:
JavaScript: ||
Python: or
Checks which type of Sphero robot is currently connected and allows execution of logic based on the robot type, where the robot names are BOLT+, BOLT, RVR/RVR+, Mini, Ollie, (SPRK+/SPRK/2.0), BB8, BB9E, R2D2, and R2Q5. Useful for writing generic programs that adjust dynamically depending on connected hardware.
Javascript: getConnectedRobotType()
Python: get_connected_robot_type()
Returns:
{RobotType} - The name of the connected robot
For example, to set the speed based on the connected robot, use:
JavaScript:
var botSpeed = 0;
async function startProgram() {
if (getConnectedRobotType() === RobotType.BOLT) {
botSpeed = 100;
}
if (getConnectedRobotType() === RobotType.Mini) {
botSpeed = 200;
}
setSpeed(botSpeed);
}
Python:
botSpeed = 0
async def start_program():
global botSpeed
if get_connected_robot_type() == RobotType.BOLT:
botSpeed = 100
if get_connected_robot_type() == RobotType.MINI:
botSpeed = 200
set_speed(botSpeed)
This program uses a > operator combined with an if then, else statement to modulate color values based on the Gyroscopic Rotation. Spin your spherical robot like a top clockwise (negative yaw degrees) to see the red LED channel, or spin it counterclockwise (positive yaw degrees) to see the green LED channel.
JavaScript:
async function startProgram() {
setStabilization(false);
while (true) {
if ((getGyroscope().yaw > 1.0)) {
setMainLed({ r: 0, g: getGyroscope().yaw / 7.84, b: 0 });
} else {
setMainLed({ r: Math.abs(getGyroscope().yaw / 7.84), g: 0, b: 0 });
}
await delay(0.25);
}
}
Python:
async def start_program():
set_stabilization(False)
while True:
if (get_gyroscope()['yaw'] > 1.0):
set_main_led({'r': 0, 'g': get_gyroscope()['yaw'] / 7.84, 'b': 0})
else:
set_main_led({'r': abs(get_gyroscope()['yaw'] / 7.84), 'g': 0, 'b': 0})
await delay(0.25)
The Spinning Top shows that the combining several programming fundamentals like Control Flow, Operators, Comparators and Sensors unleashes the creative power of programming. You also used a new tool called "normalization" in this program. Normalizing modifies a value so it fits within a different range, just like how percentages normalize any 2 numbers from 0 - 100%. Dividing the gyroscope sensor data by 7.84 ensures that gyro values will generate valid LED values. This is needed because the gyro range is -2,000° - 2,000°, whereas the LED range is 0 - 255. Hence, our normalization rate can be calculated as 2,000 / 255 = 7.84.