Go to Post FIRST tricks you into using your brain on a regular basis. - Gabe [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 20-02-2005, 00:55
Vincent Chan's Avatar
Happy Birthday! Vincent Chan Vincent Chan is offline
the Friendly Team Asian
#1127 (Lotus Robotics)
Team Role: Student
 
Join Date: Feb 2003
Location: Alpharetta, GA
Posts: 125
Vincent Chan is on a distinguished road
Send a message via AIM to Vincent Chan
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[];
and so on and so forth, with a method calling it like this:

Code:
int move_forward(void)
{
     end_time = command_list[current_command].parm_1;
     if(curr_time > end_time)
            rc = 1;
     else
            rc = 0;
}
(So that you can understand what's going on, end_time would be the time at which you stop moving forward, and curr_time would count millisecs since the match began. You would grab the parameter that end_time is set to from a command type structure out of an array of those structs. command_list[] is that array and current_command is a variable that increments as commands are completed.)

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?
__________________
Lotus Robotics, Team #1127

"So everybody's got ideas. Ideas are cheap. What's unique is the conviction to follow through: to work at it until it pays off. That's what separates the person who thinks I wonder why they can't just make shampoo and conditioner in one? from the one who thinks Now, should I get the Mercedes, or another BMW?"
--Scat, in Syrup by Maxx Barry
  #2   Spotlight this post!  
Unread 20-02-2005, 03:55
ace123's Avatar
ace123 ace123 is offline
Registered User
AKA: Patrick Horn
FRC #0008 (Paly Robotics - http://robotics.paly.net/)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Palo Alto, CA
Posts: 50
ace123 has a spectacular aura aboutace123 has a spectacular aura about
Send a message via AIM to ace123
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...
  #3   Spotlight this post!  
Unread 20-02-2005, 10:07
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
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[];
commands.c
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}
}
user_routines_fast.c (at top of file)
Code:
#include "commands.h"
user_routines_fast.c (in autonomous function, before the while loop begins)
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;
Then, add the newly created commands.c to the project.

So yeah, your idea works.
__________________

  #4   Spotlight this post!  
Unread 20-02-2005, 11:55
Hieb Hieb is offline
Registered User
no team
Team Role: Teacher
 
Join Date: Mar 2004
Rookie Year: 2004
Location: St. Louis, MO
Posts: 125
Hieb is a splendid one to beholdHieb is a splendid one to beholdHieb is a splendid one to beholdHieb is a splendid one to beholdHieb is a splendid one to beholdHieb is a splendid one to beholdHieb is a splendid one to behold
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
switching modes Allison Programming 8 06-04-2004 13:37
Switching allys? archiver 1999 1 23-06-2002 22:34
Team 45 Dual Motor Gear Switching Assembly WorkThoseBuns Motors 1 20-03-2002 19:27
switching joystick axis????? team222badbrad Technical Discussion 9 16-01-2002 23:07
[white paper] Dual Motor Gear Switching Assy Brandon Martus Motors 0 29-06-2001 12:56


All times are GMT -5. The time now is 22:27.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi