Author Topic: Need an introduction to interrupts on Axon  (Read 3094 times)

0 Members and 1 Guest are viewing this topic.

Offline TomasTopic starter

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 0
Need an introduction to interrupts on Axon
« on: January 23, 2009, 10:00:38 AM »
Anyone care to point me in the right direction?

I don't have much experience using this, only from classes at my university, and there we used pre written interrupt logic which I really don't remember.

My main issue is where I would write this interrupt code. I understand it's probably not going in the control.c-file, but elsewhere. And how does the logic work? Anyone care to explain, or maybe give an example?

Offline yerbie

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 3
Re: Need an introduction to interrupts on Axon
« Reply #1 on: January 23, 2009, 10:33:20 AM »

Offline TomasTopic starter

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 0
Re: Need an introduction to interrupts on Axon
« Reply #2 on: January 23, 2009, 11:02:47 AM »
Wow, thats great. Thanks!

edit: Could someone give an example of a simple program using interrupt on axon? That would be very appreciated.
« Last Edit: January 23, 2009, 12:01:37 PM by Tomas »

Offline Dscrimager

  • Full Member
  • ***
  • Posts: 57
  • Helpful? 0
Re: Need an introduction to interrupts on Axon
« Reply #3 on: January 23, 2009, 08:27:07 PM »
Well I have some snippets that might help, it's part of a larger more complex program but I have an interrupt routine that checks a bunch of my sensors:
In an init routine I set up the timer:
   
Code: [Select]
timerAttach(TIMER2OVERFLOW_INT, &sample_sensors );
Then in the routine I update my real time clock and check my sensors:
Code: [Select]
void sample_sensors(void) // this exists to call in the timers, no input allowed
{
// track time
time = (time + 1) % TICSPERSECOND; // get next tic and scale to 1 second increments
if (time == 0)
seconds_elapsed = seconds_elapsed + 1;
    if (seconds_elapsed == 60)
{
minutes_elapsed = minutes_elapsed + 1;
seconds_elapsed = 0;
}
    if (minutes_elapsed == 60)
{
hours_elapsed = hours_elapsed + 1;
minutes_elapsed = 0;
}
sample_sensors_report(time, FALSE);
}

Does this give you something to start with? What do you want to do in your interrupt?

Doug



Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Need an introduction to interrupts on Axon
« Reply #4 on: January 23, 2009, 10:38:49 PM »
I have plans at some point to write easy to use interrupt functions (both hardware and timer interrupts) for the Axon. But probably won't have that done till summer begins.

There has been much higher demand for other software so interrupts on on the back burner.

I encourage to post your code when you get something working!

Offline TomasTopic starter

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 0
Re: Need an introduction to interrupts on Axon
« Reply #5 on: January 24, 2009, 05:37:46 AM »
Admin, I definitly will. I will most probably make a project of the robot me and some other guys are working at when we get where we want, and I'll make sure I'll include the source code.

Well I have some snippets that might help, it's part of a larger more complex program but I have an interrupt routine that checks a bunch of my sensors:
In an init routine I set up the timer:
   
Code: [Select]
timerAttach(TIMER2OVERFLOW_INT, &sample_sensors );
Then in the routine I update my real time clock and check my sensors:
Code: [Select]
void sample_sensors(void) // this exists to call in the timers, no input allowed
{
// track time
time = (time + 1) % TICSPERSECOND; // get next tic and scale to 1 second increments
if (time == 0)
seconds_elapsed = seconds_elapsed + 1;
    if (seconds_elapsed == 60)
{
minutes_elapsed = minutes_elapsed + 1;
seconds_elapsed = 0;
}
    if (minutes_elapsed == 60)
{
hours_elapsed = hours_elapsed + 1;
minutes_elapsed = 0;
}
sample_sensors_report(time, FALSE);
}

Does this give you something to start with? What do you want to do in your interrupt?

Doug




Im not sure what I want to do with it yet, as I don't really know what I could do with it (yet, I am still waiting for sensors to my robot). So I just want to test it, see what it can do and where I might find it useful. Just for personal learning right now, I guess.
« Last Edit: January 24, 2009, 07:59:05 AM by Tomas »

Offline Dscrimager

  • Full Member
  • ***
  • Posts: 57
  • Helpful? 0
Re: Need an introduction to interrupts on Axon
« Reply #6 on: January 24, 2009, 08:40:43 AM »
Well you can use it to maintain a RTC for now (Although you need to pick the correct high precision timer I believe).
The basics that I followed are this:
- pick a timer interrupt (look at the data sheet) appropriate to my use
- write the function to be called in the interrupt
but I'm not doing any hardware interrupts.

As I understand they can be used for a number of things:
- generate a signal (like PWM or bit-banging serial output) on an output pin
- handle input on a pin (a hardware interrupt like when a collision switch is detected)
- handle internal housekeeping ( like my sensor sweep and RTC code)

My setup ensures that my other routines have a good time base to track time and that the internal variables that track world state ( such as distance to anything in front of the robot) are updated very often without explicit control.

Check out the code I supplied and look at the basic support in the SofR_Utils code. I was able to get mine (albeit simple) going pretty much my first try.

It's essentially a subroutine (with no input parameters) that either get's called under certain hardware conditions or that get's called on a periodic basic from a timer. The nice thing is that when you need something to happen ( like my sensor sweeps) in the background and automatically it is done without you explicitly having to send control to that routine. Or in the case of hardware interrupts where you don't want to sit and wait for the next event to occur you can use the interrupt.


Doug

Offline Dscrimager

  • Full Member
  • ***
  • Posts: 57
  • Helpful? 0
Re: Need an introduction to interrupts on Axon
« Reply #7 on: January 24, 2009, 10:07:45 AM »
And while this is not AXON it is AVR:

http://www.windmeadow.com/node/19

Offline TomasTopic starter

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 0
Re: Need an introduction to interrupts on Axon
« Reply #8 on: January 24, 2009, 01:36:32 PM »
Allright, lets say I want to do a function 1 time every second. How would I do this? Where would I place the code?

Im sorry that I need this in with a teaspoon, but I only have a few days of experience programming AVR.

Btw, I tried your code Dscrimager, but I couldnt make any sense out of what I got (I placed a rprintf function inside the sample_sensors to print out the seconds_elapsed function). I also removed TICSPERSECOND, as I dont know what this is and it was an undeclared function\variable.

What I got on the terminal was the number "-30944" repeated several times.

Offline Dscrimager

  • Full Member
  • ***
  • Posts: 57
  • Helpful? 0
This should be pretty close
« Reply #9 on: January 25, 2009, 12:58:04 AM »
- don't put a print in the interrupt routine, anything slow like serial output to a screen will not really keep up with the interrupt rate. You want your interrupt routine to be fast.
- this assumes the standard output setup for the AXON (USB already setup)
- this should result in time being printed to the serial out about 1/second

Code: [Select]
//declare these global in control.c
#define TICSPERSECOND 1000
unsigned int time;
unsigned int seconds_elapsed;
unsigned int minutes_elapsed;
unsigned int hours_elapsed;
void int_routine(void);

// interrupt routine
void int_routine(void)
{
time = (time + 1) % TICSPERSECOND; // get next tic and scale to 1 second increments
if (time == 0)
seconds_elapsed = seconds_elapsed + 1;
    if (seconds_elapsed == 60)
{
minutes_elapsed = minutes_elapsed + 1;
seconds_elapsed = 0;
}
    if (minutes_elapsed == 60)
{
hours_elapsed = hours_elapsed + 1;
minutes_elapsed = 0;
}
}


void control(void)// replace your control function with this
{
        int seconds;

time = hours_elapsed = minutes_elapsed = seconds_elapsed = 0;
seconds = 0;

// not completely sure if both of these are necessary but it's working for me
timer2Init();
reset_timer_2();
        delay_cycles(6000); // let system stabilize

// once you do this the interrupt will be called
// this timer is already scaled to 1000/second (TICSPERSECOND) above
timerAttach(TIMER2OVERFLOW_INT, &int_routine );

while(1)
{
if(seconds_elapsed != seconds) // if timer ticks over then another second has gone by print time
{
                        //this will only be reached if interrupt routine is updating the global variables, i.e. seconds_elapsed, outside of this while loop
seconds = seconds_elapsed;
rprintf("H:%d M:%d S:%d  ",hours_elapsed, minutes_elapsed, seconds_elapsed);
}
}
}

 


Get Your Ad Here

data_list