Movements control the robot's motors and control system. You can use sequential movement commands by separating them with line breaks, like the Hello World! program. Sphero robots move with three basic instructions: heading, speed, and duration. For example, if you set heading = 0°, speed = 60, duration = 3s, the robot would roll forward for 3s at a moderate speed.
await roll()
combines heading(0-360°), speed(-255-255), and duration(time (s)) to make the robot roll with one line of code. For example, to have the robot roll at 90°, at speed 200 for 2s, use await roll(90, 200, 2)
setSpeed()
sets the speed of the robot from -255 to 255, where positive speed is forward, negative speed is backward, and 0 is stopped. Each robot type translates this value differently into a real world speed; Ollie is almost three times faster than Sphero. For example, use setSpeed(188)
to set the speed to 188 which persists until you set a different speed. You can also read the real-time velocity value in centimeters per second reported by the motor encoders.
What is the 255 scale?
The scale relates to computer memory and it's used instead of a percentage to allow young users to truly understand how computer memory works. Sphero robots only have 128,000 - 256,000 bytes of memory, which is tiny compared to your computer which has 1,000,000,000's of bytes. Memory at its most basic form consists of 1's and 0's. A simple example is how a computer counts to 5: 0, 1, 10, 11, 100, 101. Each byte can contain eight 1's or 0's, and with eight digits the biggest number you can make is 255, using "11111111". Therefore, using the smallest amount of memory for a value (one byte) offers a range of 0-255.
stopRoll()
sets the speed to zero to stop the robot, effectively the same as the setSpeed(0)
command.
setHeading()
sets the direction the robot rolls. Assuming you aim the robot with the blue tail light facing you, then 0° is forward, 90° is right, 270° is left, and 180° is backward. For example, use setHeading(90)
to face right.
await spin()
spins the robot for a given number of degrees over time, with 360° being a single revolution. For example, to spin the robot 360° over 1s, use: await spin(360, 1)
. Use setSpeed()
prior to await spin()
to have the robot move in circle or an arc or circle.
setStabilization(true)
turns the stabilization system on and setStabilization(false)
turns it off. Stabilization is normally on to keep the robot upright using the Inertial Measurement Unit (IMU), a combination of readings from the Accelerometer (directional acceleration), Gyroscope (rotation speed), and Encoders (location and distance). When setStabilization(false)
and you power the motors, the robot will not balance, resulting in possible unstable behaviors like wobbly driving, or even jumping if you set the power very high. Some use cases to turn it off are:
When stabilization is off you can't use setSpeed
to set a speed because it requires the control system to be on to function. However, you can control the motors using Motor Power with rightMotorPwm
and leftMotorPwm
when the control system is off.
await rawMotor()
controls the electrical power sent to the left and right motors independently, on a scale from -255 to 255 where positive is forward, negative is backward, and 0 is stopped. If you set both motors to full power the robot will jump because stabilization (use of the IMU to keep the robot upright) is disabled when using this command. This is different from setSpeed
because Raw Motor sends an "Electromotive force" to the motors, whereas Set Speed is a target speed measured by the encoders. For example, to set the raw motor to full power for 4s, making the robot jump off the ground, use await rawMotor(255, 255, 4)
.
rightMotorPwm()
and leftMotorPwm()
control the electrical power sent to the left and right motors independently, on a scale from -255 to 255. If you set both motors to full power the robot will jump because setStabilization(false)
when using this command. Motor Power is different from setSpeed
because Raw Motor sends an "Electromotive force" to the motors, whereas Set Speed is a target speed measured by the encoders.
resetAim()
resets the heading calibration (aim) angle to use the current direction of the robot as 0°, within a range of 0-360°. For example, use resetAim(90)
to use the current right facing direction of the robot as 0°. When used in the block canvas, resetAim()
is limited to set the current front facing direction of the robot as 0°.
Sphero BOLT has a compass (magnetometer) sensor that has unique functionality. Nearby metallic and magnetic objects can affect the accuracy of the compass, so try to use this feature in an area without that interference, or hold it up in the air if you can't get away from interference.
await calibrateCompass()
calibrates the magnetometer by spinning the robot in place. You need to run this command before setting or getting the compass direction. Nearby metallic and magnetic objects can affect the accuracy of the compass.
setCompassDirection()
sets the real-world direction based on the last compass calibration. 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 set this value. For example, use the below to point Sphero BOLT to the north:
async function startProgram() { await calibrateCompass(); setCompassDirection(0); }Copy
1
async function startProgram() {
2
await calibrateCompass();
3
setCompassDirection(0);
4
}