View Single Post
  #1   Spotlight this post!  
Unread 17-02-2005, 17:09
Orborde Orborde is offline
Registered User
FRC #1747
Team Role: Mentor
 
Join Date: Apr 2004
Rookie Year: 2003
Location: Indianapolis, IN
Posts: 44
Orborde has a spectacular aura aboutOrborde has a spectacular aura about
Send a message via AIM to Orborde
Global Struct Arrays and Interrupts

I have an array of structs, seen below, that will be accessed by interrupts at some points; namely, the "ticks" variable of the structure will be changed by encoder-caused interrupts. "goal_velocity" is NOT read or modified by the interrupt routine, ever. My question, then, is whether I need to disable interrupts when I want to write to goal_velocity? Can I just declare the array as volatile, as shown below, or does that only act on the pointer? Could I throw in an extra "volatile" or two in the array definition to make goal_velocity safe to fiddle with without disabling interrupts? Or is it imperative that I disable interrupts before reading/writing goal_velocity?

Code:
typedef struct {
        unsigned char * output;          // motor output signal
        int goal_velocity;       // goal (ticks per 26.2msec)

        int ticks;               // ticks since last read
    
        int error_current;       // current error
        int error_last;          // error from last measure
        int error_acc;           // accumulated error for integral portion
        } drivemotor;
Code:
volatile drivemotor motors[NUM_MOTORS] = {
  {&drive1, 0, 0, 0, 0, 0},
  {&drive2, 0, 0, 0, 0, 0},
  {&drive3, 0, 0, 0, 0, 0},
  {&drive4, 0, 0, 0, 0, 0}
};
As if that weren't enough, assuming I can set goal_velocity without disabling interrupts first, I'd like to make them accessible by macro, as seen here:
Code:
#define speed1 motors[0].goal_velocity
#define speed2 motors[1].goal_velocity
etc...
I can't figure out how to declare the array so that it can be initialized easily per the definition above AND be accessible across multiple files so the macros WORK. Where do I declare it, and how?

Until some of you geniuses weigh in, I'll assume that interrupts do NOT need to be disabled, and I'll define an accessor function for the array.

Thanks for your time, folks. I'd really like a reply ASAP, as we want to have a testing version of the code ready tonight.

Last edited by Orborde : 17-02-2005 at 17:13. Reason: Corrected some of the quoted code; I can't tell the difference between } and ) in MPLAB on this silly high-res screen :)