Society of Robots - Robot Forum

Software => Software => Topic started by: Tomas on February 11, 2009, 11:45:03 AM

Title: Need some help on interrupt code (axon)
Post by: Tomas on February 11, 2009, 11:45:03 AM
Hi. Im not sure how to use interrupts yet, I've read around on this site and others, but none has given me much information on how to do the thing.

Anyway, I want to use interrupt to update my IR sensors at a constant rate, 20 times a second (50ms delay).

This is three global variables which I want to update at the specified rate.
Code: [Select]
//global variables:
int Left;
int Front;
int Right;
//code
void UpdateSensors(void)
{
Left = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(0));
Front = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(1));
Right =sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(2));
}

Anyone care to explain to me how I can use interrupt to update these variables 20 times a second? Is it even recommended? :) Thanks!

Thanks for any help :)
Title: Re: Need some help on interrupt code (axon)
Post by: yerbie on February 11, 2009, 02:04:19 PM
you'll need to use timers from the axon.
1. initialize the specific timer for the correct resolution.  In other words, figure out the DIV with FCPU and figure out what the count should be to equal 50ms.
2. enable the timer
3. you can use the timerAttach function to attach your own method you want to run
4. change your variables to volatile int Left, etc...
5. your code might look like this:
Code: [Select]
volatile int Left;
volatile int Front;
volatile int Right;

void updateSensors(void) {
  if ((timer1overflow * 255 + timer1count) >= <some number calculated in  #1>) {
    Left = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(0));
    Front = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(1));
    Right =sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(2)); 
    reset_timer1_overflow & TCNT1 = 0
  }
}

timerAttach(updateSensors);

the code above is just pseudo code and I don't remember if timer1 is 8-bit or 16-bit, hence the 255 (for 8-bit).
Title: Re: Need some help on interrupt code (axon)
Post by: Admin on February 11, 2009, 11:10:10 PM
Quote
Anyone care to explain to me how I can use interrupt to update these variables 20 times a second? Is it even recommended?
The best time to read sensors is just before you are going to use sensor data. At 50ms delay, your sensor data will be outdated by up to 50ms. I wouldn't recommend using interrupts for this sensor unless you're doing something special I don't know about :P