WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

My first program

Now that our environment has been set up correctly we can concentrate on writing some code.
Every program you write that uses my library will be of the same format:-
// Place any #define statements here before you include ANY other files
 
// You must ALWAYS specify the board you are using
// These are all in the 'sys' folder e.g.
#include "sys/axon.h" // I am using an Axon
 
// Now include any other files that are needed here
#include "uart.h"
#include "rprintf.h"
 
// Now create any global variables such as motors, servos, sensors etc
 
// This routine is called once only and allows you to do set up the hardware
// Dont use any 'clock' functions here - use 'delay' functions instead
// Use UART1 as that is the USB port on the Axon so can be connected to the PC
void appInitHardware(void){
    // Set UART1 to 19200 baud
    uartInit(UART1, 19200);
    // Tell rprintf to output to UART1
    rprintfInit(&uart1SendByte);
}
// This routine is called once to allow you to set up any other variables in your program
// You can use 'clock' function here.
// The loopStart parameter has the current clock value in µS
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
    return 0; // dont pause after
}
 
// This routine is called repeatedly - its your main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
    rprintf("Hello world\n");
    return 1000000; // wait for 1 second before calling me again. 1000000us = 1 second
}
Now obviously that program doesn't actually do anything except send 'Hello World' every 1 second over UART0 but it should compile. If not then you have probably failed to set up your environment correctly.
The first change you will notice is that your program doesn't have a 'main' function. This function actually exists in the library and is responsible for calling 'appInitHardware', 'appInitSoftware' and 'appControl'.
You will also notice that 'appControl', unlike the usual 'main', actually exits - ie it is not surrounded by the traditional:-
while(1){
... Keep doing stuff for ever ...
}
The reason being that the routine is passed in several useful variables:-
1. 'loopCount' is an incrementing number which will eventually wrap around back to zero.
2. 'loopStart' is the current clock in µS.
You will also see that 'appControl' can return a value. This represents the frequency at which you want your appControl to be called - in microseconds. So returning a value of 20000 will mean that your appControl will be called every 20ms. Obviously if your code takes longer than that to run, say 25ms, then it will be called again almost immediately.
So WebbotLib spends some of its time running your program and another lump of time just 'hanging around' before calling your code again. Although not yet implemented by the library this means that we can calculate the amount of time each iteration through 'appControl' actually takes and so you could implement a 'CPU utilisation' graph similar to that in the Windows Task Manager.

Valid XHTML 1.0 Transitional