|
Re: C: Cascading for Aliases?
You probably want to use either an array or arrays, an array and an index array or else pointers:
array or array method:
Code:
struct comands cmd_list[][20] = {
{
{CMD_DRIVE,1,2,3,4},
{CMD_DRIVE,1,2,3,4},
{CMD_DRIVE,1,2,3,4},
{NULL,0,0,0,0}
},
{
{CMD_DRIVE,1,2,3,4},
{CMD_DRIVE,1,2,3,4},
{CMD_DRIVE,1,2,3,4},
{NULL,0,0,0,0}
}
};
This might use up a lot of memory, and probably will run out of space if you haven't already.
Another method:
Code:
// It may be easier to understand if you know assembly and you have an array of function pointers.
struct comands cmd_list[] = {
{CMD_DRIVE,1,2,3,4}, // 0x0000 DRIVE 1,2,3,4
{CMD_DRIVE,1,2,3,4}, // 0x0001 DRIVE 1,2,3,4
{CMD_DRIVE,1,2,3,4}, // 0x0002 DRIVE 1,2,3,4
{NULL,0,0,0,0}, // 0x0003 RET 0,0,0,0
{CMD_DRIVE,1,2,3,4}, // 0x0004 DRIVE 1,2,3,4
{CMD_DRIVE,1,2,3,4}, // 0x0005 DRIVE 1,2,3,4
{CMD_DRIVE,1,2,3,4}, // 0x0006 DRIVE 1,2,3,4
{CMD_DRIVE,1,2,3,4}, // 0x0007 DRIVE 1,2,3,4
{CMD_DRIVE,1,2,3,4}, // 0x0008 DRIVE 1,2,3,4
{NULL,0,0,0,0} // 0x0009 RET 0,0,0,0
{CMD_TURN,1,2,3,4}, // 0x000a TURN 1,2,3,4
};
int cmd_list_index[]={
0, // CALL 0x0000
4, // CALL 0x0004
10 // CALL 0x0010
}
That one might take up less memory.
The advantage of this is that it's one long list of stuff.
And it's already compatible with kevin's code!
just add anywhere you want in the pre-autonomous routine (disabled mode), some code that sets:
Code:
extern int current_command; // To interface with robot.c from another file.
void Default_Routine(void) {
int switch;
switch = rxdata.oi_swB_byte.allbits; // Will give you a 16-bit # based on the port2 digin.
switch = p2_sw_aux1*8 + p2_sw_trig*4 + p_sw_top*2 + p2_sw_aux2; // Longer but more correct way of doing that....
switch = rc_dig_in03*8+rc_dig_in04*4+rc_dig_in05*2+rc_dig_in07; // Will give you a 16-bit int based on the digital input on the rc.
current_command = cmd_list_index[switch]; // basically a goto statement.
// goto cmd_list_index[switch]
}
Basically you can view the NULL items as return statements and if you set the statement "pointer" to one past the return statement you can go instead with the next function.
Kevin's function will just think that it somehow ended up past the NULL statement and will keep going until the next NULL.
__________________
-Patrick Horn, Paly Robotics
Check out the space simulator called Vega Strike, modelled after the space simulator games Elite and Wing Commander. It's Open Source too!
If you have ever played Wing Commander, or especially Privateer, and had a feeling of nostalga derived from the you will enjoy these two Vega Strike mods: Privateer Gemini Gold and Privateer Remake!
I'm working on adding multiplayer support this year...
|