Computer Inputs

Introduction

In this lesson we will learn more about how to enable Sparki to communicate with a computer, both from the computer to the robot (to send commands), and from the robot to the computer (to receive data from sensors and robot status). Before starting with this lesson, it’s strongly recommended that you read and understand the concepts in the Using USB Serial and Bluetooth Communications lesson.  

What You’ll Need

 

How It Works

Sparki is an autonomous robot. This means that we can store a program on it’s non-volatile memory and he will act according to that program’s instructions, without any help from the outside world. This is what we have been doing in all the previous lessons. That way of using Sparki has a some advantages:
  • It’s simple: there is just one program, and it runs inside the robot once uploaded.
  • We don’t rely on anything else than a Sparki once it has been programmed.
  • Batteries last long if there is no wireless communication system active in the robot (like the Bluetooth module, for example).
  But in this lesson we are going to learn how to program Sparki to receive and send data from and to an external computer. This may be useful in a lot of situations. For example:
  • When our application needs more processing power.
  • When we want Sparki to interact with a graphical user interface.
  • When we need to exchange data between the robot and the outside world (for example from the Internet).
  • When we want the robot to interact with external sensors or with the environment itself.
  • When we need to coordinate the work of more than one robot.
 

First Example

Let’s start by a simple serial remote control example, where we just send some simple commands to the robot using its virtual serial port through the USB cable . Then we can always modify the example to work with the Bluetooth Module. Warning_Triangle Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on.  Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki. Here is the initial code:

As you can see, the core of this program is a switch case statement. Be sure to not forget the break statement at the end of each condition instructions. This program is very simple, since it only allows for communications in one direction (from the computer to the robot) and with very simple one-character commands. The upcoming character is received in the inByte variable, using the Serial.read instruction. But please note that the read is made only when the Serial.available function returns true. This way we ensure that the program is not polling the serial port on every iteration of the loop cycle. Now, let’s add some code to read some of the Sparki’s sensors (the ultrasonic distance ranger and the line and edge sensors): Adding this was also easy: we just decode some new one-character commands that triggers the reading of the specific sensors that we want to read. After the reading, the program sends the sensor value to through the serial port using the Serial.print and Serial.println instruccions.  

Asynchronous Events

And what about sending information from the robot in an asynchronous way? For example, why not trigger an event when a sensor reading reaches a threshold? That’s easy to do: just add the code to test that sensor, and when the comparison with the threshold becomes true, tell it to the remote computer: Here is a small complete program to do that:

In the future (as an extra activity), you can make the thresholds programmable through the communication protocol, so they are not hardcoded.  

Using Bluetooth Instead of the USB Serial

One of the main uses of these kind of programs, is to control it remotely through a wireless connection. As you may already know, one of the best ways to connect to Sparki wirelessly is by using the The Bluetooth module. But from the Using USB Serial and Bluetooth Communications lesson you will remember that any program that uses the Serial port nees some minor modifications to work with the Serial1 port instead (Serial1 is where the Bluetooth module is connected, in Sparki’s Expansion Port). Here we suggest a way of defining an alias for the serial port, called just “serial“, so you can then select Serial or Serial1 with just one line change in the #define at the beginning of your program: Also, please remember that the Serial1 port needs to be initialized in your setup function (we are going to work at 9600 bauds here): So, here is how your program will look like regarding the calls to Serial.available, Serial.read, etc.:  

Creating a Parsable Output for Multiple Sensor Readings

In the previous examples, the phrases returned by the robot were easy to read by a human, but not that easy to be parsed by a computer. So, if we want something more advanced, like reading the sensors values to be used by the high level program in our remote computer, we may prefer to format a bit the returned data from the robot. In the following example we are doing exactly that. Another change that you will see in he following code is that now, a single command (with the character “r”, from “read”) is used to read all the robot sensors (well, not all, but all the sensors that matters to us in this example):

This way, each sensor reading is enclosed with some symbols, which clearly delimit the start and the end of the sensor value, while also giving information about which sensor has been read. For example, the reading from the ultrasonic ranger will look a bit like this:

<p=20=p>

Where 20 is the reading (20 centimeters in this case) and the p identifies the ping sensor. Of course that all this stuff can be done in a lot of different ways, but this one is very simple, and this particular program will be the one that we are going to use for the initial basic integration between Sparki and Android™ devices when working with multiple sensors. Finally, please note that if you want to read other sensors (like the accelerometer, or the magnetomer, for example), you just need to  add a small and simple block of code (try it!).  

Related Lessons

A very interesting usage of the programs created here is the integration between Android™ devices, like cellphones and tablets with Sparki. The basics of this integration are explained in the Sparki + Android™ lesson.  

Extra Activities

Now that we can control Sparki remotely and use an external computer (or a mobile device!) as its brain, the possibilities are limitless. So, if you can write desktop applications in any programming language that can read an write a serial port, why not try to use a remote computer as the real controller of your Sparki? For example: try to write a small line following program, but with all the logic on the remote computer side instead inside Sparki’s program memory. If you don’t have a favorite programming language, you can take a look at Processing, which you will find really similar to the Arduino programming environment (in fact, Arduino uses a lot of Processing elements, such as the IDE). Here is the link to the Serial library from Processing.