Arduino is a great fit for the tracker controller subsystem. One of the nice things about Arduino is that, it takes very little effort to bring great ideas to life. Among other benefits, the Arduino is relatively inexpensive, small, robust. It has a fast micro-processor, memory and digital and analog interfaces on board. The direct USB connection for data and power makes development a breeze. Additionally, the structure of the Arduino software program makes it one of the easiest micro controllers to program. Obviously, much more information about this is available at their website arduino.cc.
There are 3 parts to understanding the Arduino and making it work: the electronics, the programming and the connections. Thanks to the Arduino inventors, we can safely ignore the electronics part almost completely.
Connections
Lets' talk connections quickly. The Arduino connects to a peronal computer (PC) via its USB port. It is very simple and convenient. To get started, visit arduino.cc for step by step instructions.
The Arduino connects to gadgets, gizmos, LEDs, motors, speakers, and you-name-it, via its digital and analog pins (aka connections). In this project, the connections from the Arduino to the head unit are presented on this page. It explains how the servo motors are connected and how power is supplied.
Program Philosophy
If you are good at programming, you may skip this paragraph. In any case, before we get deep into the program specifics, let's spend a few minutes to look at how a small restaurant coordinates its work. The waiter/waitress takes orders and passes them to the cook. The cook looks at the orders and starts processing them and everyone is happy as long as their order comes within a reasonable time. Now, imagine if the waitress were also to get involved in the kitchen, there would be no one to take new orders, and you would have one happy customer and a bunch of unhappy ones.
In much the same way, our controller program divides receiving orders and executing orders into two different functions. One function receives commands from the tracking program running on the PC and parses them for execution. A second function continually checks for completion of all remaining commands and tries to complete them as soon as possible. This may sound complicated to do it in software but that is where Arduino scores again. Its unique programming structure works to our advantage, read on...!
Now lets talk about the program that makes the Arduino a cool tracking controller. BTW, Arduino experts can just skip the following sections and simply download the files from the bottom of the page and be on their merry ways.
Arduino Program Structure
A simplified view of a typical Arduino program is illustrated below. A standard Arduino program structure requires two functions setup() and loop(), and the inclusion of necessary libraries as necessary.
// include all libraries here at the top of the program
// all globally shared variables go here
void setup() {
// everything that needs to be done once at the beginning goes here
// functions and global variables can be used and accessed from here
}
void loop() {
// everything that needs to done every few microseconds goes here
// functions and global variables can be used and accessed from here
}
// All functions, subroutines go here
As soon as power for the Arduino board is turned ON, the setup() function is executed once, and immediately followed by the execution of the loop() function every few microseconds. The same sequence is followed when the RESET button on the board is pressed and released. The loop() is repetitively invoked as long as the board is powered or the program has a bug and it stops.
So, you will want to put things to be done once inside the setup() function and things you want to be done repeatedly inside the loop() function.
For first time programmers, here are a few things that will help you get going :
- a variable is like the memory button on your calculator, anything you put in it will remain until you put something else in it
- a statement is like a command, "save it a variable" or "ring the bell"
- a function is like a pack of statements that you can use anywhere, like a wish
- a library is like a book of spells, you can invoke them without taking any responsibility :)
Next let's look at some sample commands, at this link, because we will now make the Arduino accept commands from the PC. Two computers taking to each other ! How cool would that be...
Part 1 - Command Handler
Commands are simply, a series of text characters, like "SPEED 50" or "ALL STOP". End of the command is usually terminated with a ENTER character. The commands will travel down the USB connection from the PC to the Arduino, very fast but one character at a time. So, at the Arduino we simply save all the characters coming down the pipe until we encounter an ENTER character. Following is the pseudo-code to better understand this:
void setup() {
// initialize serial comm
}
void loop() {
// call a function to check if we received any new characters
checkSerialPort();
}
void checkSerialPort() {
// check serial port for new characters
// read the character
// add the read character to a buffer
// when ENTER (NL/CR) is received, call another function to process the command
parseCommands();
}
Since we know the loop() function is executed every few microseconds, the checkSerialPort() function is called very often. It checks the serial port and saves all characters it has received, until the CR or LF character (i.e., ENTER) is received.
The parseCommands() function quickly checks the received command and calls other functions to complete the requested action. The file SatTracker.ino below, may look a bit intimidating at first, but it is simply a combination of the command processing logic translated into Arduino specific language, and many other statements that will be explained in the following paragraphs.
The file mod_easycomm2.ino contains the actual logic for parsing the commands defined by the EasyComm II protocol, that we are planning to implement. Again see this page for those specific commands. As we had mentioned in the beginning, it is possible to implement a different tracker protocol or functions by modifying just this file alone.
Part 2 - Servo Control Module
This module groups all servo motor control functions in one file, for the same modular reason. So, if you have to use a different servo, all you need to change this file.
Understanding the "restaurant" concept described above is key to understanding the servo functions. The command parser decodes the command and writes the final position in a variable. The loop program knows the current position of the servo, computes the direction the servo needs to move and start moving the servo one degree in a time frame defined by the speed rpm. RPM translates to degrees per second or better, degrees per microsecond. The loop program simply waits for that many microseconds before moving the servo another degree.
The second thing to remember is that servos are smart motors. You can easily find a lot of information on the web, about servos and how they are work. Just keep in mind that most servos are controlled by a digital signal that consists as short pulses. The width of each pulse defines the position of the servo in relation to its central reference position. One could say that the servo rotates +/- NN degrees from its middle position. Typical pulse widths range from 800 microseconds to 1500 microseconds.
Most common servos rotate +/- 45, 60, or 90 degrees based on their specs. For this project we choose the ones that rotate +/- 90 degrees. The Futaba ____ provides an excellent compromise between cost, torque and ruggedness. Hitec ____ is a similar choice.
Part 3 - Response Module
The file mod_response.ino is a set of commonly used functions to send textual response back to the tracking program. The functions simply response with the value of a specific system parameter like the current Azimuth/Elevation angles, and system parameters. These functions are usually used by the command processor function. Just wanted to add that having these functions in a separate file facilitates making changes in the command processor without affecting the rest.
Part 4 - EEPROM Module
This is a fun module that is good to have. As you have seen before a bunch of commands have been implemented to set system values. All this module does is provide functions to store the values of system parameters into a permanent storage like the onboard EEPROM, so that when the system looses power, these values are not lost.
The onboard EEPROM is a sizeable chunk of memory, arranged as 8 bit bytes having sequential addresses starting from zero. One 8 bit byte can store values from zero to 256, and since some of the system parameters are greater than 128, we decided to store all values a 16 bit word, that is 2 bytes. In this case our addresses will be 0, 2, 4, 6, 8, 10 and so on.
We will have to store the MIN and MAX values for the two servos, SPEED in rpm and TIMEOUT in milliseconds. So we just lay out the sequence for storing these values by defining these as program constants.
// EEPROM Word Addresses
#define EEPROM_AZMIN 0
#define EEPROM_AZMAX 2
#define EEPROM_ELMIN 4
#define EEPROM_ELMAX 6
#define EEPROM_SPEED 8
#define EEPROM_TIMEOUT 10
These are the first 6 lines of code in the file mod_eeprom.ino. The functions defined in the rest of the module help read and write the values to the EEPROM at the correct appropriate addresses.
Part 5 - Help Information Module
This module provides a bunch of functions to respond to a human operator. It will help display all the commands supported as well as current values of all important parameters system.
That's it folks, just download the code and off you go !
(link to download is at the end of this page)
The following instructions will show how to download the files, compile the code and and upload the program to your Arduino board. I have used this code on the Uno, Duemilanove and the Mini, no problems. Technically, it should work without any modification on other Arduino boards as well.
- Download program code files from the link to the ZIP file SatTrackerII.zip below.
- Unzipping the ZIP file will create a new folder called SatTrackerII with all the source code files in it.
- Optionally, move the folder SatTrackerII under the Sketches folder created by Arduino.
- Open the folder SatTrackerII and double-click the SatTrackerII.ino file, this should launch the Arduino programming application.
- Click the Check icon to compile the program files, and make sure it finishes compiling successfully
- Click the Upload icon to upload the compiled image to the Arduino board
See the Connections page for details on how to connect the power and control signals to the servos.
(Note: To use different pins, just change the physical connections and change the pin definitions in SatTrackerII.ino)
Internet References:
Arduino Serial Library Example http://arduino.cc/en/Tutorial/SwitchCase2
Arduino Servo Library Example, Sweep http://arduino.cc/en/Tutorial/Sweep
How servos work http://pcbheaven.com/wikipages/How_RC_Servos_Works/
Here is alternate link to download the ZIP file mentioned below Download SatTrackerII.ZIP