35
TRAKR Codebook: A Crash Course in C Programming for the Spy Video TRAKR CONTENTS I. So you wanna write an app for the TRAKR. ......................................... 0 JARGON ............................................................................................ 0 SYNTAX ............................................................................................ 0 FUNCTIONS ....................................................................................... 0 PARAMETERS ..................................................................................... 0 DATA TYPES ...................................................................................... 0 Reference Manuals ............................................................................. 0 Defining Functions: Creating a TRAKR program ...................................... 0 COMMENTS ....................................................................................... 0 COMPILER ......................................................................................... 0 STRATEGIES ...................................................................................... 0 II. Try These Example TRAKR Apps .......................................................... 0 Sleeping............................................................................................ 0 Moving under program control.............................................................. 0 Motor Speed ...................................................................................... 0 Display Voltage and Current with Text ................................................... 0 Polling the remote buttons ................................................................... 0 Night Vision ....................................................................................... 0 Audio Recording and playback .............................................................. 0 Video ................................................................................................ 0 Log files ............................................................................................ 0 III. TRAKR FUNCTION REFERENCE ............................................................ 0 ClearRectangle() ................................................................................ 0 ClearScreen() .................................................................................... 0 CloseFile() ......................................................................................... 0 CloseFileSystem()............................................................................... 0 CloseGraphics().................................................................................. 0 CloseImageRegister() ......................................................................... 0 CloseIR()........................................................................................... 0 CloseMotors() .................................................................................... 0 CreateFile() ....................................................................................... 0 DeleteFile() ....................................................................................... 0 DrawImage() ..................................................................................... 0 DrawRectangle() ................................................................................ 0 DrawText() ........................................................................................ 0 FlushFile() ......................................................................................... 0 GetBatteryVoltage() ............................................................................ 0 GetLeftMotorCurrent()......................................................................... 0 GetRemoteKeys() ............................................................................... 0 GetRemoteKeyStatus()........................................................................ 0 GetRightMotorCurrent()....................................................................... 0 IsAudioPlaying() ................................................................................. 0 OpenFile() ......................................................................................... 0 OpenFileSystem() ............................................................................... 0 OpenGraphics() .................................................................................. 0 OpenImageRegister().......................................................................... 0 OpenIR() ........................................................................................... 0 OpenMotors() .................................................................................... 0 ReadFile() ......................................................................................... 0

Trakr Programming Manual

Embed Size (px)

Citation preview

Page 1: Trakr Programming Manual

TRAKR Codebook:A Crash Course in C Programming for the Spy Video TRAKR

CONTENTSI. So you wanna write an app for the TRAKR. ......................................... 0

JARGON ............................................................................................ 0SYNTAX ............................................................................................ 0FUNCTIONS ....................................................................................... 0PARAMETERS..................................................................................... 0DATA TYPES ...................................................................................... 0Reference Manuals ............................................................................. 0Defining Functions: Creating a TRAKR program ...................................... 0COMMENTS ....................................................................................... 0COMPILER ......................................................................................... 0STRATEGIES...................................................................................... 0

II. Try These Example TRAKR Apps.......................................................... 0Sleeping............................................................................................ 0Moving under program control.............................................................. 0Motor Speed ...................................................................................... 0Display Voltage and Current with Text ................................................... 0Polling the remote buttons................................................................... 0Night Vision ....................................................................................... 0Audio Recording and playback.............................................................. 0Video................................................................................................ 0Log files ............................................................................................ 0

III. TRAKR FUNCTION REFERENCE ............................................................ 0ClearRectangle() ................................................................................ 0ClearScreen() .................................................................................... 0CloseFile() ......................................................................................... 0CloseFileSystem()............................................................................... 0CloseGraphics().................................................................................. 0CloseImageRegister() ......................................................................... 0CloseIR()........................................................................................... 0CloseMotors() .................................................................................... 0CreateFile() ....................................................................................... 0DeleteFile() ....................................................................................... 0DrawImage() ..................................................................................... 0DrawRectangle() ................................................................................ 0DrawText()........................................................................................ 0FlushFile() ......................................................................................... 0GetBatteryVoltage()............................................................................ 0GetLeftMotorCurrent()......................................................................... 0GetRemoteKeys() ............................................................................... 0GetRemoteKeyStatus()........................................................................ 0GetRightMotorCurrent()....................................................................... 0IsAudioPlaying()................................................................................. 0OpenFile() ......................................................................................... 0OpenFileSystem()............................................................................... 0OpenGraphics().................................................................................. 0OpenImageRegister().......................................................................... 0OpenIR()........................................................................................... 0OpenMotors() .................................................................................... 0ReadFile() ......................................................................................... 0

Page 2: Trakr Programming Manual

ReadTimer() ...................................................................................... 0RegisterImage() ................................................................................. 0ResetTimer() ..................................................................................... 0SeekFile().......................................................................................... 0SetIR() ............................................................................................. 0SetLeftMotor() ................................................................................... 0SetLineWidth()................................................................................... 0SetMotors() ....................................................................................... 0SetRectangle() ................................................................................... 0SetRightMotor() ................................................................................. 0SetScreen() ....................................................................................... 0SetTextColor() ................................................................................... 0Show() ............................................................................................. 0Sleep() ............................................................................................. 0StartAudioPlayback() .......................................................................... 0StartAudioRecording()......................................................................... 0StopAudioRecording() ......................................................................... 0WriteAudioData() ............................................................................... 0WriteFile() ......................................................................................... 0Data Types ........................................................................................ 0TOOLS .............................................................................................. 0

Page 3: Trakr Programming Manual

So you wanna write an app for the TRAKR.

Well, you've come to the right place. The TRAKR Codebook wont turn youinto a programmer overnight, but you will be able to build some basic appsfor your TRAKR. When you're ready to learn more about the world of Cprogramming, go online and take advantage of free resources like Wikipedia.Remember -- the capabilities of the TRAKR are as vast as your imagination!

Read on for a crash course in TRAKR coding basics: TRAKR-specific softwarefunctions, some code examples, and even a few implementation guidelines.Pretty soon that genius app idea will be out of your brain and on yourTRAKR.

if(obvious_meaning()==true){goToSection(examples||reference);}

Uh, WHAT?!?

No, we're not writing in some robot language from the future -- that's asnippet of C, the programming language in which the TRAKR's software iswritten. Looks cool? Then keep reading.

JARGON

Jargon is the vocabulary of a specialty. It can be made-up words or regular words whichhave a different meaning to experts than to the rest of us. Experts use jargon as shorthandfor complex ideas. Learning these special words is a big part of becoming an expert onanything.For example, if you didn't know anything about baseball and heard a radio announcertalking about "knuckle balls" and "bunts", you wouldn't have a clue as to what he meantunless you knew some baseball jargon. As you read through these pages and try some ofthe examples, you'll start using some jargon, too. You'll be amazed at how different asentence can look when you have some simple jargon in your vocabulary. Jargon savestyping and makes things clear by associating complex processes with short words.

SYNTAX

Syntax refers to the rules for making sentences in a language. Just like natural languages,programming languages use syntax to make sure terms are ordered in a way that can beunderstood.There are many special characters used to make syntactical distinctions in programming.For instance, a semicolon ";" is interpreted in C to be the end of a statement. You canseparate two lines by a return, but they may be read by the machine as being one

Page 4: Trakr Programming Manual

statement without a semicolon. If you omit a semicolon, you will be looking for a "syntaxerror" when you read through your code checking that your statements all have ends.Most errors in your programs will be syntax errors. They are the easiest to make and thehardest to find. As you master the syntax, you'll find that coding gets a lot more fun!

FUNCTIONS

In the C language, a function is a piece of code which is "called" upon to run when its nameis invoked from another piece of code. Often times, the code which calls a function isexpecting to get some data back from the function. The type of data expected shouldmatch the type of data the function will generate. For instance, if you had created afunction called "TheCurrentTime()", you would expect to get some data back whichrepresents the time of day.

Sometimes a function does not return anything to the code which called it. It just runs itsown code and then returns to the code which called it. You might expect a function named"Sleep()" to pause for a while and then return to the code without producing any data. Thefunction "Sleep()" is, in fact, a function which the TRAKR understands.

PARAMETERS

Many functions expect parameters to work with. A parameter is anything passed on to thefunction from the code which called it. The parenthesis after a functions name are where theparameters are specified and multiple parameters can be separated by a comma within theparenthesis.In the example of the Sleep() function, there is the essential parameter of "...for how long?"which needs to be passed when we call it. We could write "Sleep(for 3 seconds)" but that isto wordy and not what the function expects. All we need to send is a number which thefunction can use. We could write "Sleep(3)" but this function expects the parameter to be inmilliseconds. To get 3 seconds of sleeping, we should write "Sleep(3000)". This parameter isan integer (a whole number) which corresponds to the number of 1/1000ths of a second topause our program.

DATA TYPES

Data is sent to a function when it is called and it needs to meet the expectations of thefunction. The main characteristic to regard is the type of data."3 seconds", ,"3" "3000"ms, "3.0" or "00000011" could all be different ways of expressingthe same time to Sleep(), but only the "3000" will get the expected result. Checking thatyou are sending the right kind of data in your function calls will be your first tactic indebugging.

Reference Manuals

No one could make any progress if they tried to remember every detail of every functionthey had used and made, so we store the vital statistics in a list to refer back to as needed.

Page 5: Trakr Programming Manual

When we look up Sleep() in the reference, we might find something like the following:

Sleep()

Description:Pauses the program for the amount of time (in milliseconds) specified as parameter.

(There are 1000 milliseconds in a second.)Syntax:

Sleep(uint32 us)Parameters:

uint32 us: the number of milliseconds to pause(unsigned long, 0 through 4,294,967,295)

Returns:nothing

Example:#include "svt.h"

void Start(){

// nothing to do here}

bool Run(){

Sleep(1000);// do somethingSleep(1000);// do something elsereturn true;

}

void End(){

// do nothing}

We can see from this example reference page that Sleep() expects to get a PARAMETER,which in this case is the value it will interpret as being the number of milliseconds to waitbefore returning to the program. Many of the functions in the TRAKR reference as well asmost C reference sources will also include sample code which can be copied directly intoyour own code to make things easier.

Defining Functions: Creating a TRAKR program

The TRAKR only calls 3 functions when it powers on. These functions are; Start(),Run()and End().The TRAKR will call each function in this order. Each TRAKR program you

Page 6: Trakr Programming Manual

create must have these functions defined by your code. How you define them determineswhat your program will do. The simplest TRAKR program that can be written, which doesnothing but load the sct.h library and define the 3 functions, is:

#include "svt.h"void Start(){}bool Run(){return true;}void End(){}

The following code functions the same as the above but is much easier to read. Byconvention, coders add extra returns and spaces to reveal the logical structure as well asthe order in which various operations occur.It is helpful to be able to understand which sections are contained within each other just byhow indented the text is. It might not look much better now but, later on, your ability todebug code will be largely dependent on understanding the order of operations andsubroutines.

#include "svt.h"

void Start{}

bool Run(){

return true;}

void End(){}

COMMENTS

Another tool to give your code clarity is the comment. Almost all code contains commentsto help make the code legible or to keep sections of code not in use but available forreference. Your comments should be clear and true. They should be edited and revised asyou work so that they remain accurate. They are there for you and others to helpunderstand what your code is doing. They do not get used by the machine but you will findthat if you can comment your code, you will have a much better understanding of how toimprove and adjust it.Comments in C start with two forward slashes and end with a return. They can be on theirown line or after some functional code. Most of the following examples will use commentsto explain their workings. The same code as above with some comments:

// we must include the svt.h file so that the compiler knows// how to handle all the special functions for the// Spy Video TRAKR.

Page 7: Trakr Programming Manual

#include "svt.h"// define the Start function:void Start(){

// this is where things that need to happen at// start-up would go.

}// define Run function:bool Run(){

// This space would contain the main body of our code.return true;

}// lastly, the End function:void End(){

// and here is where we can do any final tasks before// closing? starting again? resetting?

}

COMPILER

rough:A compiler is a program which converts our code into an executable program. The compilerruns when we type "make" into a terminal program's command line. The compiler thenfollows any instructions in the "makefile" within that directory to convert the "app.c" file intoa TRAKR program. The compiler only looks for the "app.c" file in the current directory. Thisis the name we must always use for our programs. By separating our different programsinto unique directories, we can keep all the various "app.c" files organized.If during the compiling process, the compiler encounters something it can not convert intothe target format, it will stop trying and report the place at which it failed. This is often thefirst time you get a clue that there is a mistake in your code. By reading the compiler errorand trying to identify what caused it, you can edit your code until the compilers getsthrough it with no errors.If the TRAKR is connected by USB while you run the compiler, it will also load the files ontothe car and you will then be able to run it.

The compiler omits all our code comments and stream-lines our text into an application butit can add information as well.The "#include" statement within our code is an instruction to the compiler to compile the filereferred to. Every TRAKR application should have the line: #include "svt.h". svt standsfor Spy Video TRAKR and is the file which defines all the functions which are native to theTRAKR hardware. Without the "svt.h" file, the compiler would not know what to do withanything but regular C functions.

Page 8: Trakr Programming Manual

STRATEGIES

This may seem more complicated than it needs to be, but consider thatcomputers do not know anything which is not defined in advance for them.Even words which we might understand to have a single and clear meaningwill have to be defined for the machine before it can do anything with themand every time we use a word, it will have to look up the definition again.This means we must be VERY specific when we define something. Ourdefinition will have to be intelligible in all the foreseeable instances in whichit may be referred to. We must use only previously defined terms in ourdefinitions of new terms. Then we can use these new parts to create evenmore complex structures.

Before we jump into the examples, take some time to ponder a few thingswe have covered and how they can be used.

• Programming is Language◦ Come up with a great idea◦ Write it down in your own words◦ Translate (code) your idea into C

• Computers don't infer◦ Syntax is key◦ Capitalization and spelling are important

• Stay organized◦ Names must match◦ Directories must be located as expected

• Be Patient and Relaxed◦ Be prepared to try everything a few times before it works◦ Go slow and pay attention to each step

▪ Take notes on what steps you have already done▪ looking for bugs is Very much like looking for bugs

◦ Stretch your body as often as you can remember▪ That is where your brain lives▪ Look away from the screen to see the forest through the

trees

Try These Example TRAKR Apps

Ready to dig in to some code? The following examples are organized from the simple (sosimple it doesn't even try to DO anything) to the complex. Even if you're new toprogramming, try running some code on your TRAKR by modifying these examples.

Examples:

Page 9: Trakr Programming Manual

• Sleeping: Simple timing• Moving: Controlling the motors• Speed: control PWM• Text and Sensors: Printing to the screen and the ADCs• Remote: buttons states• IR LED: turning on and off the night vision• Audio Recording and playback• Video: recording a video• Logging to file: writing text to a file•

Page 10: Trakr Programming Manual

Sleeping...

The program on the TRAKR runs as fast as it can. As soon as it finishes one operation itbegins the next. It is often useful to slow things down. One way to do this is to simply stopthe program for some specific amount of time. The Sleep() function does this by takingwhatever integer value it gets and waiting that number of milliseconds (1/1000 of a second)before returning the processor to work on the next item.

// don't forget the svt.h file#include "svt.h"// define the Start function:void Start(){

// nothing to do here...}

bool Run(){

Sleep(1000);// do somethingSleep(1000);// do something elsereturn true;

}

void End(){}

Page 11: Trakr Programming Manual

Moving under program control

This example will show you how to program your TRAKR to move automatically. It will makethe TRAKR Car take control away from the remote, pause for 1 second, spin Counter-Clockwise for 1 second, Pause for 1 second, spin clockwise for 1 second and then allow theremote to control the TRAKR for 5 second before repeating.To do this, we need to take and let go of control of the motors. Many of the functions whichallow your code to control the TRAKR depend on the concept of opening and closing aconnection to a particular device. This is like getting someones attention before asking aquestion; "Excuse me Mrs. President. Could you kindly tell me ...". Sometime you can skipthis step but, if you really want to be understood and get an answer the first time, it is oftennecessary to explicitly "open" a dialog. Another important reason for take control of devicesis so that our processes will not be interrupted.

#include "svt.h"

void Start(){

// Take control of the motors:OpenMotors();

}bool Run(){

// Pause 1 secondSleep(1000);// Set the Right motor speed to go full forwardSetRightMotor(10000);// Set Left motor to go full backwardsSetLeftMotor(-10000);

// Pause 1 secondSleep(1000);// Stop both motorsSetMotors(0, 0);

// Pause 1 secondSleep(1000);// Set the Right motor speed to go backwardsSetRightMotor(-10000);// Set Left motor to go forwardsSetLeftMotor(10000);

// Pause 1 secondSleep(1000);// Stop both motors

SetMotors(0, 0);

Page 12: Trakr Programming Manual

// Release the motors so the remote can regain controlCloseMotors();// Do nothing for 5 seconds:Sleep(5000);// Take back control of the motors:OpenMotors();return true;

}void End(){

// remember to let go before you finish...CloseMotors();

}

Page 13: Trakr Programming Manual

Motor Speed

A variable is name given to a place in the computer's memory where we can record andrecall data. It is important to tell the machine what kind of data and how much of it weneed to store so that enough room is set aside for it in advance. If we try to put a valueinto a variable which is not of the correct type, we often get an error, but even worse, wemight be losing data and not even know it.

This example will also declare a variable to store an integer.An integer is a positive or negative whole number. It can have no decimal point.A 32bit integer is a whole number between −2,147,483,648 and 2,147,483,647

We will use the variable to keep track of the last speed value and add or subtract or invert itas appropriate.

As we have seen, by setting the motors to 0 we are telling them to stop, 10,000 is fullahead and -10,000 is full reverse. As you might guess, 5,000 is half speed forward. Infact, any integer between -10,000 and 10,000 corresponds to a speed from fast to slow tofast again in the opposite direction.This example uses the for() function to count with. This is a very useful C function which isa part of most any program. You can learn more about it from any C reference guide. It willserve you well.

Page 14: Trakr Programming Manual

#include "svt.h"

void Start(){

// Take control of the motors:OpenMotors();

}bool Run(){

// create and integer variable called "i"int i;// put a count from 0 to 10000 in "i"for (i=0; i==10000; i++){

// Pause 1 millisecondSleep(1);// Set the Right motor speed to the current countSetRightMotor(i);// Set Left motor to the negative of the current countSetLeftMotor(-i);

}// count from 0 to 10000for (i=10000; i==0; i--){

// Pause 1 millisecondSleep(1);// Set the Right motor speed to the current countSetRightMotor(i);// Set Left motor to the negative of the current countSetLeftMotor(-i);

}// Release the motors so the remote can regain controlCloseMotors();// Do nothing for 5 seconds:Sleep(5000);// Take back control of the motors:OpenMotors();return true;

}void End(){

// remember to let go before you finish...CloseMotors();

}

Page 15: Trakr Programming Manual

Display Voltage and Current with Text

functions:GetLeftMotorCurrent()GetRightMotorCurrent()GetBatteryVoltage()if()ClearScreen()DrawText()Show()

Concepts:

This example will take control of the remote display to show the data returned by theanalog to digital converters (ADC) on board the TRAKR. The screen on the remote has aresolution of 160 pixels across by 80 pixels high. Everything we put on the screen needs tobe placed by referring to the top left corner where we want to start drawing or writing as aset of coordinates. These coordinates are commonly referred to as "x" and "y"."x" is the number of pixels from a spot to the left side of the screen and "y" is the numberof pixels from that spot to the top of the screen.

By giving our function these parameters, we can tell each function call to place its data at aspecific place.

In the case of the DrawText() function, this data is text which is drawn in a font I do notknow the name of which seems to be of a variable width but a consistent height of ? pixels.The first letter's top left corner will be placed at the "x,y" coordinates we specify and therest of that text data will appear to the right until it reaches the edge of the screen at whichpoint it will wrap down and to the left and continue.This example also incorporates the idea of checking for change to avoid needlessly re-writing the same data over and over. To do this we use 2 variable for each kind of data wewant to evaluate. One variable to read the data into and another variable to store the lastvalue. This way, by comparing them, we know when something has changed and can takeaction.

Page 16: Trakr Programming Manual

// This example with use the graphics functions to write to// the values of the ADCs to the Remote screen#include "svt.h"

//declare some variables to use:int mVolts;int lastmVolts;int LMC;int lastLMC;int RMC;int lastRMC;

void Start(){

//take contro of the display:OpenGraphics();

}

bool Run(){

// read values of ADCs:LMC=GetLeftMotorCurrent();RMC=GetRightMotorCurrent();mVolts=GetBatteryVoltage();//if values have changed:Update screenif (LMC!=lastLMC || RMC!=lastRMC||mVolts!=lastmVolts){

//clear graphcs buffer:ClearScreen();//load the buffer with some new drawing instructions:DrawText(5,5,"Battery: %d mV", mVolts);DrawText(5,25,"Motor current");DrawText(5,45,"in miliampres");DrawText(5,65,"left: %d", LMC);DrawText(85,65,"right: %d", RMC);//Update buffer to screen:Show();//reset "last" values:lastLMC=LMC;lastRMC=RMC;lastmVolts=mVolts;

}//if no change, wait a little while before checking again:else{

Sleep(100);}return true;

Page 17: Trakr Programming Manual

}

void End(){CloseGraphics();}

Page 18: Trakr Programming Manual

Polling the remote buttons

This example checks all the buttons on the TRAKR remote and uses the display to showwhich are ones are pressed. The two functions which return button states are;GetRemoteKeys() or GetRemoteKeyStatus().

This example uses the GetRemoteKeys() function which sets an 8 bit variable to representthe button states at that moment. Remember; a bit is a single place to store a 1 or a 0 and8 bits are in a byte. Bytes are often represented as decimal integers where each bit is adigit in a binary number. TRAKR takes advantage of this easy conversion to be able to storeevery possible combination of button presses as a single number value between 0 and 255.We can then use a bit comparison (which is the ampersand logogram, "&") between thatnumber and a values of the "KEY_*" constants to check if that bit is high or low.

KEY_SELECT = goKEY_RUN = goKEY_HOME = picture of houseKEY_MENU = button cKEY_INPUT1 = button AKEY_INPUT2 = button bKEY_LEFT_BACK = steering control (lower left)KEY_LEFT_FORWARD= steering control (upperleft)KEY_RIGHT_BACK= steering control (lower right)KEY_RIGHT_FORWARD= steering control(upperright)

Page 19: Trakr Programming Manual

#include "svt.h"int LastKeyState;

void Start(){OpenGraphics();}

void End(){CloseGraphics();}

// consolidating code by declaring a custom function:void display(int keyState){ClearScreen();DrawText( 5, 0, "Key: %d", keyState ); //show the 8 bits of keyState as anintegerint y=30; // variable to increment text display downward

if(keyState&KEY_LEFT_BACK){ // if bit in "keyState" associated with"KEY_LEFT_BACK" is highDrawText( 5, y, "LEFT BACK" );// draw texty=y+15; //move text "pen" down}

if(keyState&KEY_LEFT_FORWARD){DrawText( 5, y, "LEFT FORWARD" );y=y+15;}

if(keyState&KEY_RIGHT_BACK){DrawText( 5, y, "RIGHT BACK" );y=y+15;}

if(keyState&KEY_RIGHT_FORWARD){DrawText( 5, y, "RIGHT FORWARD" );y=y+15;}

if(keyState&KEY_INPUT1){DrawText( 5, y, "LEFT INPUT" );y=y+15;}

if(keyState&KEY_INPUT2){DrawText( 5, y, "RIGHT INPUT" );

Page 20: Trakr Programming Manual

y=y+15;}

if(keyState&KEY_RUN){DrawText( 5, y, "GO" );y=y+15;}

if(keyState&KEY_MENU){DrawText( 5, y, "MENU" );y=y+15; //move draw "pen" down}

if(keyState==0){DrawText( 5, 30, "NONE PRESSED" );}Show();}

bool Run(){

int KeyState = GetRemoteKeys();// load button statesif(LastKeyState!=KeyState)// if buttons are in new state...

{display(KeyState);// show the names of pressed buttonsLastKeyState=KeyState;// update the old state to the new

}Sleep(100); no need to keep the radio working constantly.return true;}

Page 21: Trakr Programming Manual
Page 22: Trakr Programming Manual

Night Vision

The TRAKR camera can perceive light beyond the range which is visible to us into the InfraRed (IR) spectrum. The TRAKR has an IR light next to its camera. When it is active, therewill be a faint red glow visible to the naked eye, but to the camera it is a bright flood oflight. The camera automatically switches to night vision mode when the IR light isactivated. The image is rendered in gray tones with the brightness representing the amountof IR light being detected.

#include "svt.h"int BlinkSpeed=3000; // Variable to set frequency of blinknigbool state; // variable to toggle IR LED state

void Start(){

OpenIR(); // Take control of IR LED}

bool Run(){

//As long as the INPUT1 key is not pressed...while (GetRemoteKeyStatus(KEY_INPUT1)==0){

if (ReadTimer()>BlinkSpeed)// If enough time has passed{

state = !state;// toggle the value of "state"SetIR(state);// Set IR to new stateResetTimer();// reset timer to 0

}}// If the INPUT1 key is pressed:return false;

}

// When The Run() function returns false, the TRACKR calls the End() functionvoid End(){

CloseIR();}

Page 23: Trakr Programming Manual

Audio Recording and playback

#defineintwhile()

ClearScreen()DrawText()Show()Sleep()OpenGraphics()CloseGraphics()

GetRemoteKeyStatus()

StartAudioRecording()WriteAudioData()StopAudioRecording()StartAudioPlayback()IsAudioPlaying()

ResetTimer()ReadTimer()

To record audio, a file needs to be created which will be written to. Since audio can take upallot of space in memory very quickly, it works best with a flash memory card inserted inthe TRAKR car to write the file to. In this example, the audio will start recording 3 secondsof audio when the remote "GO" button is pressed. It will then play whatever was recordedwhen the "GO" button is pressed again.

#include "svt.h"#define AUDIO_FILENAME "A:\\Test\\Audio.wav"

int length = 3000;

void Start(){

OpenGraphics();}

bool Run(){

ClearScreen();DrawText( 35, 5, "Press Go" );DrawText( 35, 30, "to record" );DrawText( 25,55, "%d seconds", length/1000 );DrawText( 30, 80, " of Audio");Show();

Page 24: Trakr Programming Manual

while (!GetRemoteKeyStatus(KEY_RUN)){

Sleep(10);}

StartAudioRecording( AUDIO_FILENAME );ClearScreen();DrawText( 15, 30, "Recording" );Show();

ResetTimer();while( ReadTimer() < length ){

WriteAudioData();Sleep( 10 );

}

StopAudioRecording();

ClearScreen();DrawText( 15, 30, "DONE" );Show();Sleep(1500);

ClearScreen();DrawText( 35, 5, "Press Go" );DrawText( 25, 30, "to playback" );DrawText( 15,55, "Audio tecording" );Show();while (!GetRemoteKeyStatus(KEY_RUN)){

Sleep(10);}

ClearScreen();StartAudioPlayback( AUDIO_FILENAME );DrawText( 15, 30, "Playing" );Show();

ResetTimer();while( IsAudioPlaying() ){

Sleep( 10 );}Sleep( 1000 );ClearScreen();DrawText( 5, 80, "DONE" );Show();

Page 25: Trakr Programming Manual

Sleep(2000);return true;

}

void End(){

CloseGraphics();}

Page 26: Trakr Programming Manual

Video

Video is even more memory intensive than audio. Recording video is done one frame at atime...

#include "svt.h"

void Start(){}<br>bool Run(){

return true;}

void End(){}

Page 27: Trakr Programming Manual

Log files

A log is a list of entries made as events occur. Just as weather and the passage of time islogged in a ships logbook, a program can use a text file to record events by writing to it asthing happen. Sometimes it can be very useful just to log the number of times the Run()function is called. In this example, we will do that as well as log each press of the remotekeys. Files are limited in size by available memory and so caution must be exercised inwriting too much to a file causing it to get to big.

#include "svt.h"

void Start(){}<br>bool Run(){

return true;}

void End(){}

Page 28: Trakr Programming Manual

TRAKR FUNCTION REFERENCE

The body of this section is being organized in a spreadsheet here:https://spreadsheets.google.com/ccc?key=0AiaD69EFwpoTdFdlT2RQeEJHQVFBRXVyVUtGVVVoQ2c&hl=en

ClearRectangle()

ClearScreen()

CloseFile()

CloseFileSystem()

CloseGraphics()

CloseImageRegister()

CloseIR()

CloseMotors()

CreateFile()

Page 29: Trakr Programming Manual

DeleteFile()

DrawImage()

DrawRectangle()

DrawText()

FlushFile()

GetBatteryVoltage()

GetLeftMotorCurrent()

GetRemoteKeys()

GetRemoteKeyStatus()

GetRightMotorCurrent()

IsAudioPlaying()

Page 30: Trakr Programming Manual

OpenFile()

OpenFileSystem()

OpenGraphics()

OpenImageRegister()

OpenIR()

OpenMotors()

ReadFile()

ReadTimer()

RegisterImage()

ResetTimer()

SeekFile()

Page 31: Trakr Programming Manual

SetIR()

SetLeftMotor()

SetLineWidth()

SetMotors()

SetRectangle()

SetRightMotor()

SetScreen()

SetTextColor()

Show()

Sleep()

Description:Pauses the program for the amount of time (in milliseconds) specified as parameter.

(There are 1000 milliseconds in a second.)Syntax:

Sleep(uint32 us)

Page 32: Trakr Programming Manual

Parameters:uint32 us: the number of milliseconds to pause(unsigned long, 0 through 4,294,967,295)

Returns:nothing

Example:#include "svt.h"

void Start(){

// nothing to do here}

bool Run(){

Sleep(1000);// do somethingSleep(1000);// do something elsereturn true;

}

void End(){

// do nothing}

StartAudioPlayback()

Description:

Syntax:

Parameters:

Returns:

Example:#include "svt.h"

Page 33: Trakr Programming Manual

StartAudioRecording()

Description:

Syntax:

Parameters:

Returns:

Example:#include "svt.h"

StopAudioRecording()

Description:

Syntax:

Parameters:

Returns:

Example:#include "svt.h"

WriteAudioData()

WriteFile()

Data Types

Name Description Size Range

Page 34: Trakr Programming Manual

char Character or small integer. 1byte signed: -128 to 127unsigned: 0 to 255

shortint Short Integer. 2bytes signed: -32768 to 32767

unsigned: 0 to 65535

int Integer. 4bytessigned: -2147483648 to2147483647unsigned: 0 to 4294967295

long int Long integer. 4bytessigned: -2147483648 to2147483647unsigned: 0 to 4294967295

bool Boolean value. It can take one of twovalues: true or false. 1bit true or false

float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)

double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15digits)

longdouble Long double precision floating point number.8bytes +/- 1.7e +/- 308 (~15

digits)

wchar_t Wide character. 2 or 4bytes 1 wide character

borrowed from: http://arduino.cc/en/uploads/Reference/FuncAnatomy.png

TOOLS

• TRAKR car and remote• USB cable• Computer with Operating System: Windows, Mac, Linux• Software: text editor, command line terminal program

Page 35: Trakr Programming Manual

• Files: TRAKR directory

• The TRAKR's on-board computers are a pair of ARM 9 processors with8MB of RAM. These microprocessors are preloaded with the TRAKRfirmware which allows you to code in C and use the library of TRAKRfunctions.

• The computer you use to upload code to the TRAKR must supportUSB. You must have administrative privileges so that you can move,write and erase files in the root directory.

• You should use a text editor which is able to save files as plain text.there are many editors for coding which have extra features fororganizing your ideas and formatting your code but the free editorswhich come with most computers are fine as well.

• In order to get your code from a text editor on your computer to theTRAKR microprocessor, you will need to compile your text file into abinary file (.bin) and then upload it via a USB cable connection. Theprogram which does this is run from a command line prompt in aterminal program. In Windows, this is the "Command Prompt"program in the start menu and in Mac it is called "Terminal", locatedin ../applications/utilities.The compiler is run from the command prompt by typing "make"while in the directory were your app is located. The compiler thenreads the "makefile" in that directory for instructions on how to nameand where to place the new .bin file it will create.

• All the files which allow this functionality as well as templates,operating specific instruction as well as all the example programsused in this introduction are available as a single directory at " URL ".

• Follow all the instruction contained in the download for your operatingsystem. Pay special attention to the location of the directories and theorder of each operation.