WebbotLib AVR library
WebbotLib It just does it

Photovore

This is an implementation of the Society of Robots 'Photovore' program but using this library instead.
We will use PWM to control the modified servos and so, for the Axon, we will use ports E3 and E4 that are connected to the PWM outputs of Timer 3. Just make sure that your servos can cope with the voltage on those pins.
#include "sys/axon.h"
#include "servos.h"
#include "a2d.h"
 
// Define two light sensors connected to ADC channels 0 and 1
#define sensorLeft ADC_NUMBER_TO_CHANNEL(0)
#define sensorRight ADC_NUMBER_TO_CHANNEL(1)
 
// Define two servos
SERVO left = MAKE_SERVO(FALSE, E3,1500, 500);
SERVO right = MAKE_SERVO(TRUE , E4,1500, 500);
 
// Create the list - remember to place an & at the
// start of each servo name and to use PROGMEM
SERVO_LIST PROGMEM servos[] = {&left,&right};
 
// Create a driver for the list of servos
SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);
 
//the larger this number, the more likely your robot will drive straight 
#define threshold 8
 
// In my initialising code - pass the list of servos to control
void appInitHardware(void){
    // Initialise the servo controller
    servoPWMInit(&bank1);
    // Give each servo a start value of 'stop'
    act_setSpeed(&left, 0);
    act_setSpeed(&right,0);
}
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
    return 0;
}
 
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
    uint8_t lightLeft = a2dConvert8bit(sensorLeft);
    uint8_t lightRight = a2dConvert8bit(sensorRight);
    if(lightLeft > lightRight && (lightLeft - lightRight) > threshold){
        // go left
        act_setSpeed(&left,DRIVE_SPEED_MIN);
        act_setSpeed(&right,DRIVE_SPEED_MAX);
    }else if(lightRight > lightLeft && (lightRight - lightLeft) > threshold){
        // go right
        act_setSpeed(&left,DRIVE_SPEED_MAX);
        act_setSpeed(&right,DRIVE_SPEED_MIN);
    }else{
        // Go forwards
        act_setSpeed(&left,DRIVE_SPEED_MAX);
        act_setSpeed(&right,DRIVE_SPEED_MAX);
    }
    return 20000; // wait for 20ms to stop crazy oscillations
}
Servos all re-act somewhat differently, compounded with how accurately you have centred them, so I suggest you read the comments in servos.h for MAKE_SERVO about changing the parameters to the MAKE_SERVO calls above to adjust its behaviour to match your servos.
Now let's make a big change. Instead of servos we decide to use DC motors attached to a Sabertooth motor controller from Dimension Engineering which is accessed via a UART using 'simplified serial mode 3' mode at 19200 baud; and the board is set up as address 128 - although this is not used in this mode.
#include "sys/axon.h"
#include "Motors/DimensionEngineering/Sabertooth.h"
#include "a2d.h"
 
// Define two light sensors connected to ADC channels 0 and 1
#define sensorLeft ADC_NUMBER_TO_CHANNEL(0)
#define sensorRight ADC_NUMBER_TO_CHANNEL(1)
 
// Define two motors
SABERTOOTH_MOTOR left = MAKE_SABERTOOTH_MOTOR(FALSE, 128,1);
SABERTOOTH_MOTOR right = MAKE_SABERTOOTH_MOTOR(TRUE , 128,2);
 
// Create the list - remember to place an & at the
// start of each servo name, and to use PROGMEM
SABERTOOTH_MOTOR_LIST PROGMEM motors[] = {&left,&right};
 
// Create a driver for the list of motors
SABERTOOTH_DRIVER driver = MAKE_SABERTOOTH_DRIVER(motors, UART0, 19200, SIMPLE);
 
//the larger this number, the more likely your robot will drive straight 
#define threshold 8
 
// In my initialising code - pass the list of motors to control
void appInitHardware(void){
    // Initialise the motor controller
    sabertoothInit(&driver);
 
    // Give each servo a start value of 'stop'
    act_setSpeed(&left, 0);
    act_setSpeed(&right,0);
}
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
    return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
    uint8_t lightLeft = a2dConvert8bit(sensorLeft);
    uint8_t lightRight = a2dConvert8bit(sensorRight);
    if(lightLeft > lightRight && (lightLeft - lightRight) > threshold){
        // go left
        act_setSpeed(&left,DRIVE_SPEED_MIN);
        act_setSpeed(&right,DRIVE_SPEED_MAX);
    }else if(lightRight > lightLeft && (lightRight - lightLeft) > threshold){
        // go right
        act_setSpeed(&left,DRIVE_SPEED_MAX);
        act_setSpeed(&right,DRIVE_SPEED_MIN);
    }else{
        // Go forwards
        act_setSpeed(&left,DRIVE_SPEED_MAX);
        act_setSpeed(&right,DRIVE_SPEED_MAX);
    }
    return 20000; // wait for 20ms to stop crazy oscillations
}
You will see that my 'appControl' function has not changed at all!

Valid XHTML 1.0 Transitional