Sphero robots are fun, durable and programmable robots! The Sphero Edu Programming Wiki is a guide to learn how to program Sphero robots with JavaScript and Python. You will need a Sphero robot, the Sphero Edu app on a compatible device, and a hunger to learn.
JavaScript and Python are two of the most popular programming languages in the world today. Both JavaScript and Python are object oriented, dynamically typed, high-level languages with many applications. JavaScript is mostly used for front-end and back-end web development. Python is popular for use in data science, automation, machine learning, and back-end web development.
If you are considering a career in software development, technology, or robotics, both languages are useful to learn! Even if you aren't pursuing a career in these fields, you'll get to be creative and solve some intriguing problems through programming.
Using your device with the Sphero Edu app, create a new text program, select your programming language (Python or JavaScript), and add these code samples into the text canvas. Don't forget to aim your robot, and then tap the Start button to see what happens!
JavaScript:
async function startProgram() {
await speak("Hello World", true);
setMainLed({ r: 0, g: 0, b: 255});
setSpeed(60);
await delay (2);
setSpeed(0);
}
Python:
async def startProgram():
await speak("Hello World", True)
set_main_led({'r': 0, 'g': 0, 'b': 255})
set_speed(60)
await delay(2)
set_speed(0)
Now transform your first program into a Square with more logic.
JavaScript:
async function startProgram() {
setMainLed({ r: 0, g: 0, b: 255 });
await speak("Hello Square", true);
await delay(1);
for (var _i1 = 0; _i1 < 4; _i1++) {
setMainLed(getRandomColor());
await Sound.Game.Coin.play(true);
await roll((getHeading() + 90), 60, 1);
await delay(1);
}
}
Python:
async def startProgram():
set_main_led({'r': 0, 'g': 0, 'b': 255})
await speak("Hello Square", True)
await delay(1)
for _i1 in range(4):
set_main_led(get_random_color())
await Sound.Game.Coin.play(True)
await roll((get_heading() + 90), 60, 1)
await delay(1)
Your code streams in real-time at 20 hertz (times per second) between your device and the robot, instead of running locally on the robot. This is pretty cool for a few reasons. The memory on Sphero robots is very small, so streaming allows you to use your device memory as the only constraint, which on today's mobile devices and computers is basically limitless. Also, it enables you to interact with the program in real-time through sensor data and other inputs. The streaming nature of your program requires async in certain cases such as before the Start Program function, indicating that data is transmitted to/from the robot whenever it's needed. One downside of streaming is that some commands are slower due to the latency between the device and robot. For example, running a raw motor command for 0.05 seconds is the fastest (shortest) amount of time you can run raw motor (1s / 20hz = 0.05s). If you used a shorter delay like 0.04s, it would still perform at 0.05s.
Syntax is the rule set for how programs are constructed.
Most identifiers are tough to describe with one word, so most are 2+ words known as "compound identifiers." Programming languages generally don't allow spaces in identifiers, so character case is the method by which you join compound identifiers to deliminate words. There are hundreds of programming languages in the world, but nearly all of them use one of these four joining methods: thisIsCamelCase, ThisIsPascalCase, this-is-spinal-case, and this_is_snake_case. JavaScript in Sphero Edu is case sensitive and uses camelCase, such as setSpeed to set the speed. Python in Sphero Edu uses snake_case, such as set_speed to set the speed.
Code needs to be structured so it can be interpreted by the robot, just like the way humans have agreed on punctuation standards in writing. If authors did not write with punctuation rules, readers would have a tough time learning new rules in every book they read. Pay very close attention to the use of these characters or your robot will not be able to "read" your program.
JavaScript:
async function startProgram() starts a program
{ and } braces contain all program code, except for global functions and variables
// indicates a comment, and does not affect the program logic. If you have a long comment that spans >1 code line, you need to use this notation on each line
; ends a statement
( ) contains a value
, separates values
____ a tab space starts the first statement in a program, and subsequent conditions must indent further
Python:
async def start_program(): starts a program
# indicates a comment, and does not affect the program logic. If you have a long comment that spans >1 code line, you need to use this notation on each line
( ) contains a value
, separates values
____ indentation (tab space) is used to define code blocks instead of { and } braces
Errors tell you when the robot can't interpret your syntax. In the Sphero Edu text editor, there is both a linter and runtime error reporting to help track down programming errors. A linter is a tool that analyzes your code and checks for syntax and formatting errors. As you type, the linter will continuously analyze your code and underline any issues it finds. All linter errors will also be displayed in the "problems" panel, which is accessible at the bottom of the editor when at least one error is present in your program.
Even if the linter displays errors, you can still attempt to run your program. In some cases, the program may run despite the reported errors. However, many times you will get a run time error when the code is executed. These errors will stop program execution and a popup will be displayed with instructions on how to resolve the error.
In the below example, notice that the set speed function is spelled incorrectly. In this case the robot can't read the command and the error will be underlined in the editor and a warning will be displayed at runtime. Copy this program and fix it so it runs without an error:
JavaScript:
async function startProgram() {
setSpee(188);
setMainLed({ r: 0, g: 255, b: 0 });
delay(2);
}
Python:
async def start_program():
set_spee(188)
set_main_led({'r': 0, 'g': 255, 'b': 0})
delay(2)
There are different types of data in programming that describe how much space a number occupies in storage and how it's interpreted.
Some languages define more data types than others. There are differences between JavaScript and Python, which are outlined below:
Basic Data Types for JavaScript:
number: values like -7 and 3.14159265359
string: combinations of letters, numbers, and punctuation, like "Don't be L8 to class."
boolean: one of the logical values true or false
null and undefined: value meaning "defined as nothing" and "this variable has never been defined", respectively
Basic Data Types for Python:
int: integer number values like -7 or 3
float: float number values like 3.14159265359
str: combinations of letters, numbers, and punctuation, like "Don't be L8 to class."
bool: one of the logical values true or false
None: value meaning "defined as nothing"