|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
C: Cascading for Aliases?
I'm the interim programmer for our team, and I'm trying to write some dead-reckoning code for the robot. I've currently got all my dead-reckoning strategies written in a .h file as an array of command type structures, much like the demo gyro code, but I need to be able to switch between them.
We're using a DIP switch to choose the strategy we want, and I was wondering if I could use an array variable of command type structures in my functions that is aliased via a cascading-if in my code. (It would look something like this ![]() Code:
command struct command_list[];
if(dipswitch == 0)
command_list[] = strat1_list[]; //strat1_list[] is defined in an included h file
else if(dipswitch == 1)
command_list[] = strat2_list[];
Code:
int move_forward(void)
{
end_time = command_list[current_command].parm_1;
if(curr_time > end_time)
rc = 1;
else
rc = 0;
}
Our mentor wasn't sure whether the aliasing of arrays was legal in C, though it seems like it should be, and I need to change a pretty sizeable chunk of code to find out. Anyone have any ideas? |
|
#2
|
||||
|
||||
|
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}
}
};
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
}
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]
}
Kevin's function will just think that it somehow ended up past the NULL statement and will keep going until the next NULL. |
|
#3
|
||||
|
||||
|
Re: C: Cascading for Aliases?
Your idea should work. My team is doing it in basically the same way. Here's what I'd do.
commands.h Code:
extern struct commands *command_list; /* externs for the actual command lists */ extern struct command_list_1[]; extern struct command_list_2[]; Code:
#include "commands.h"
struct commands *command_list;
/* actual command lists */
struct commands command_list_1 = {
/* commands */
{NULL, 0, 0, 0}
}
struct commands command_list_2 = {
/* different command list */
{NULL, 0, 0, 0}
}
Code:
#include "commands.h" Code:
if(dipswitch == 0)
command_list = command_list_1; // This makes the command_list pointer look at command_list_1. Because arrays in C are pointers anyways, this _should_ work... Should. :)
else if(dipswitch == 1)
command_list = command_list_2;
So yeah, your idea works. |
|
#4
|
|||
|
|||
|
Re: C: Cascading for Aliases?
Just be aware that the multiple structs and multidimensional arrays are memory hogs. The most I was able to do with two different methods that I tried were 3 unique autonomous modes, while our strategy team has identified at least 5 that they would like to have available. As such, we are not going to use the scripting, although we are still going to use the same functions with some modifications.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| switching modes | Allison | Programming | 8 | 04-06-2004 01:37 PM |
| Switching allys? | archiver | 1999 | 1 | 06-23-2002 10:34 PM |
| Team 45 Dual Motor Gear Switching Assembly | WorkThoseBuns | Motors | 1 | 03-20-2002 07:27 PM |
| switching joystick axis????? | team222badbrad | Technical Discussion | 9 | 01-16-2002 11:07 PM |
| [white paper] Dual Motor Gear Switching Assy | Brandon Martus | Motors | 0 | 06-29-2001 12:56 PM |