I implemented a globals.h file to handle all variables that need to be accessed across multiple .c files, and a handy macro that can make all these declarations in a single statement, which makes things easy if you need to add more variables, or change the name of an existing one.
Code:
/********************************************************************************
* FILE NAME: globals.h
*
* DESCRIPTION:
* This file contains the definitions of global variables, used by multiple
* modules in the robot program. A macro is used to resolve the separate
* definition of each variable, in each module. Simply include this file in
* all of your C Source files, but also make sure to insert the line
* #define MAIN_C
* in your main.c file.
*
********************************************************************************/
/* GLOBAL_VAR(Type, Name, Initial Value); */
#ifdef MAIN_C
#define GLOBAL_VAR(t, n, v) t n = v
#else
#define GLOBAL_VAR(t, n, v) extern t n
#endif
/* example */
GLOBAL_VAR(unsigned char, test, 0);
In addition, I came up with a similar macro to deal with the relays.
Code:
#define drt_hand_piston_fwd relay2_fwd
#define drt_hand_piston_rev relay2_rev
...
#define RELAY_FWD 1
#define RELAY_OFF 0
#define RELAY_REV -1
#define Generate_Relay(r)\
{\
r##_fwd = 0;\
r##_rev = 0;\
if(r == 1)\
r##_fwd = 1;\
else if(r == -1)\
r##_rev = 1;\
}
...
GLOBAL_VAR(char, drt_hand_piston, 0);
...
Generate_Relay(drt_hand_piston);
I also tried to use the EEPROM to make the arm_position self-calibrating, but seeing how I only got the robot for programming late on Monday, I didn't have enough time to make it work.