Go to Post I've got a couple team members who have an incredible knack for destroying robot parts... - Tom Line [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Events   CD-Media   CD-Spy   FRC-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 02-10-2005, 10:01 PM
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
None #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 660
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Send a message via AIM to Chris_Elston Send a message via Yahoo to Chris_Elston
If statement in .h file or Indirect addressing? Kevin NAV code...

Ok...so I am not sure how to explain what I need but I know what I want to do.

I am helping our team program this year and really this is the first time I've dabble in C code. I normally program PLCs (programmable logic controllers)...anyway...

I noticed that Kevins nav code uses a commands.h file which is a structured something to send commands in a list format to the robot.c file. Very slick and easy. We've made our own functions and commands for our automous mode.

What I'd like to do is alter this with some inputs to create multi automous modes. We have all the inputs selections working but not sure what would be the best approach in C code to do what we need.

I want to do something like this:


Code:
if (input = 1)
    {
    struct commands command_list[] = {

    /*   Command              parm 1     parm 2   parm 3   */

    {CMD_WAIT,                 1000,        0,      0},
    {CMD_DRIVE,                1500,        0,      0},
    {CMD_WAIT,                 4000,        0,      0},
    }
};

Of course that's not exactly legal in C code....

So if I was doing this in a PLC, I might use what is called indirect addressing, or assigning a varaible to a pointer.

Like:

struct commands command_list*myvarible*[] =

so if myvarible is equal to 1 then run the commands in list #1. If myvarible is equal to 2 then run the commands in list #2.

Is there such a thing like this in C Code?

I hope that makes sense...
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #2   Spotlight this post!  
Unread 02-10-2005, 10:12 PM
Workaphobia Workaphobia is offline
Registered User
AKA: Jon
FRC #1546 (Chaos Inc.)
Team Role: Alumni
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Long Island
Posts: 26
Workaphobia will become famous soon enough
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

You want the struct you use to be one of a predefined set, that is selected at the beginning of runtime? You could try a big switch() statement that would contain assignment statements.
Code:
struct commands mycommand;
switch(input)
{
  case 1:
    mycommand = {...};
    break;
  case 2:
    ..... repeat
}
Not sure if I have the syntax right - it's late, and I haven't used structs in a while.

I wouldn't use a pointer to an array element because you'd be allocating space for structs that would not be used - you wouldn't switch modes after starting the process.
  #3   Spotlight this post!  
Unread 02-10-2005, 10:25 PM
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,351
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by chakorules
Is there such a thing like this in C Code?
I think what you're looking for is to assign a pointer to the correct array of autonomous commands based on some input at runtime.

Example:

.h file:
Code:
struct commands command_list_drive_straight[] = {

    /*   Command              parm 1     parm 2   parm 3   */

    {CMD_WAIT,                 1000,        0,      0},
    {CMD_DRIVE,                1500,        0,      0},
    {CMD_WAIT,                 4000,        0,      0}
};

struct commands command_list_grab_tetra[] = {

    /*   Command              parm 1     parm 2   parm 3   */

    {CMD_WAIT,                 1000,        0,      0},
    {CMD_DRIVE,                1500,        0,      0},
    {CMD_WAIT,                 4000,        0,      0}
};
In .c:
Code:
struct commands* cmd_list = NULL;

if(input == 1)
{
    cmd_list = command_list_drive_straight;
}
else if(input == 2)
{
    cmd_list = command_list_grab_tetra;
}
else
{
    /* Don't forget the else, else you'll be accessing a null pointer... :( */
}
etc...

Quote:
Originally Posted by Workaphobia
I wouldn't use a pointer to an array element because you'd be allocating space for structs that would not be used - you wouldn't switch modes after starting the process.
This actually doesn't matter - the compiler has to store the values that your code will use to initialize the array somewhere inside the program anyway, so it's essentially the same. Actually, using globals with assigned default values (especially if you make them const) is more efficient because then they can be stored directly in Flash and don't need to consume any RAM.
  #4   Spotlight this post!  
Unread 02-10-2005, 11:20 PM
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
None #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 660
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Send a message via AIM to Chris_Elston Send a message via Yahoo to Chris_Elston
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by Dave Flowerday
I think what you're looking for is to assign a pointer to the correct array of autonomous commands based on some input at runtime.

Yeah, yeah...great fixer-her-upper. What we got Dave is some thumbwheels from digikey connected to the four inputs on a joyport. Using BCD (0-9), we can run up to 99 different automodes with eight inputs on two ports. I'll give your example a shot and see what we come up with...PS thanks alot for sharing your thoughts.

Next on the list is shaving more program FAT. Every time we turn around we are at our 32K limit...yuck...We aren't ready to get rid of the printf statements, we need those to debug but if it keeps it up, we are going to have to dump some printf to get more code packed in...
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #5   Spotlight this post!  
Unread 02-11-2005, 09:24 AM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 7,189
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by chakorules
Next on the list is shaving more program FAT. Every time we turn around we are at our 32K limit...yuck...We aren't ready to get rid of the printf statements, we need those to debug but if it keeps it up, we are going to have to dump some printf to get more code packed in...
I found it much easier to use primitives rather than printf. printf takes 4K while a simple set of primitives takes 40 bytes. The ones we use aren't even the real way I'd normally implement such a thing, but it made a good "hello world" type intro to programming project for the freshmen.
__________________
"A child of five could understand this. Fetch me a child of five." - Groucho Marx
  #6   Spotlight this post!  
Unread 02-11-2005, 11:37 PM
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
None #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 660
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Send a message via AIM to Chris_Elston Send a message via Yahoo to Chris_Elston
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Hey DAVE...

We just wanted to tell you this worked like a champ...BUT..holey-moley...does it eat up some program space... With the default struct command, we had 23K of program space left to mess with. Then we did your suggestions, and we duplicated the struct with three sets to represent three different automous modes, after we done that we now have 28K of complied program...whew... That was a big hit in our software... Anyway though, it worked like a champ, exactly what we need in conjuction with our thumbwheel 0-9 BCD (binary coded decemical) input wheels.
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #7   Spotlight this post!  
Unread 02-14-2005, 09:34 AM
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,351
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by chakorules
BUT..holey-moley...does it eat up some program space...
How many commands do you have in each of your autonomous programs? Each "command" inside the commands array looks like it takes up 10 bytes... You'd have to have some pretty big autonomous programs to add 5K. You are just using a pointer to the correct struct, right (versus creating a new struct and assigning the values from the struct in commands.h to it)?
  #8   Spotlight this post!  
Unread 02-14-2005, 10:27 AM
Jon236's Avatar
Jon236 Jon236 is offline
Registered User
AKA: Jon Mittelman
FRC #2648 (Infinite Loop)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2000
Location: Windsor, Maine
Posts: 733
Jon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond reputeJon236 has a reputation beyond repute
Send a message via Skype™ to Jon236
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by chakorules
Ok...so I am not sure how to explain what I need but I know what I want to do.

I am helping our team program this year and really this is the first time I've dabble in C code. I normally program PLCs (programmable logic controllers)...anyway...

I noticed that Kevins nav code uses a commands.h file which is a structured something to send commands in a list format to the robot.c file. Very slick and easy. We've made our own functions and commands for our automous mode.

What I'd like to do is alter this with some inputs to create multi automous modes. We have all the inputs selections working but not sure what would be the best approach in C code to do what we need.

I want to do something like this:


Code:
if (input = 1)
    {
    struct commands command_list[] = {

    /*   Command              parm 1     parm 2   parm 3   */

    {CMD_WAIT,                 1000,        0,      0},
    {CMD_DRIVE,                1500,        0,      0},
    {CMD_WAIT,                 4000,        0,      0},
    }
};

Of course that's not exactly legal in C code....

So if I was doing this in a PLC, I might use what is called indirect addressing, or assigning a varaible to a pointer.

Like:

struct commands command_list*myvarible*[] =

so if myvarible is equal to 1 then run the commands in list #1. If myvarible is equal to 2 then run the commands in list #2.

Is there such a thing like this in C Code?

I hope that makes sense...

It does....another approach (which might save some memory space), is based on your thumbwheel positions, call a function which puts the dsitred command list into the structure. That way you'll just have one structure, not multiple copies.

Jon Mittelman
Mentor
  #9   Spotlight this post!  
Unread 02-14-2005, 02:14 PM
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

I was pretty excited about the scripting idea. I modified the system to allow multiple "scripts" in the one array. I even allowed scripts to call other scripts, subroutine-like. This took a lot of time to get working, but I figured I we would write a short 4-6 command script for each of a dozen different autonomous programmes, and life would be grand.

But by the time I got up to about 7 scripts, the linker said it couldn't fit the array. I even tried breaking the array up into multiple arrays, but the linker was still upset. I posted a question about modifying the linker script, but, so far, no responses.

Realizing that there is far less RAM for data like script arrays than there is memory for program storage, I threw out my wonderful scripting system and I plan to implement a nested-state machine system. There's still, what? a week left?

-Norm
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #10   Spotlight this post!  
Unread 02-14-2005, 02:27 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 7,734
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Send a message via AIM to Alan Anderson
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by gnormhurst
Realizing that there is far less RAM for data like script arrays than there is memory for program storage, I threw out my wonderful scripting system and I plan to implement a nested-state machine system.
You can put data like script arrays in the program storage area. Just declare it as a rom const.
  #11   Spotlight this post!  
Unread 02-14-2005, 02:46 PM
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,351
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

Quote:
Originally Posted by Jon236
It does....another approach (which might save some memory space), is based on your thumbwheel positions, call a function which puts the dsitred command list into the structure. That way you'll just have one structure, not multiple copies.
You'd be better off declaring all the arrays as rom const as Alan described, because if you have a function copy the data into the struct then all that data still has to reside in Flash AND then you end up using a bunch of RAM for the RAM copy. You may as well just use it out of Flash directly. Plus you'll waste a bunch of CPU cycles doing the copying . We're not using the script language but from what I understand the array does not get changed at run-time so it's a good candidate for making rom const. Unfortunately that doesn't help with Chris' other problem of filling up the 32K of Flash.

Something that might be worth considering is using the EEPROM to store your autonomous programs to get them out of Flash - you'll have to use the EEPROM code that someone posted on here last year though and it would generally be painful, but it would free up some space.
  #12   Spotlight this post!  
Unread 02-14-2005, 10:39 PM
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
None #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 660
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Send a message via AIM to Chris_Elston Send a message via Yahoo to Chris_Elston
Re: If statement in .h file or Indirect addressing? Kevin NAV code...

This is some really good stuff guys. Thanks for posting information and sharing your ideas. I'll have to try this when I get back, I am out of town on business away from the team right, bad timing to have to leave on business for the "real" job on the last week of the build season...

Anyway, we have done pretty much what Dave has suggested:

Code:
if(thumbwheel == 0)
	{
    cmd_list = command_list_00;
	}
else if(thumbwheel == 11)
	{
    cmd_list = command_list_11;
	}
else if(thumbwheel == 21)
	{
    cmd_list = command_list_21;
	}
else if(thumbwheel == 31)
	{
    cmd_list = command_list_11;
	}
else
	{
    cmd_list = NULL;

Then our command list looks something like this...I won't post them all, but you can see how the flow goes.


Code:
//do nothing but sit here and look pretty
struct commands command_list_00[] = {
{CMD_WAIT,                  1000,        0,      0},
{NULL,                         0,        0,      0}

};

//ok robot starting on the left or right side MODE 11
struct commands command_list_11[] = {
{CMD_DRIVE_UNTIL,         	  1400,        180,      0}, //drive time with PWM value
{CMD_SELECT_GREEN,       	     0,        0,      0}, //change color to green, no values needed.
{CMD_WAIT_UNTIL_VISLOCK_GREEN,	 0,        0,      0}, //lock on green, not values needed.
{CMD_WAIT,                 		50,        0,      0}, //YOU MUST DELAY HERE! (wait for in milli-seconds)
{CMD_DRIVE_ALIGN_COLOR,      	149,        5,      20}, //line up with tetra, PWM value and SCALER number here
{CMD_WAIT,               		50,        0,      0}, //wait in milli-seconds.
{CMD_DRIVE_UNTIL_CLOSE,         160,       200,      0}, //drive straight PWM value, then how close in second value is size bigger means closer.
{CMD_DRIVE_UNTIL,         	   600,        165,      0}, //drive time with PWM value
{CMD_SELECT_YELLOW,       	     0,        0,      0}, //change color to yellow, no values needed.
{CMD_WAIT_UNTIL_VISLOCK_YELLOW,	 0,        0,      0}, //lock on yellow, not values needed.
{CMD_WAIT,                 		50,        0,      0}, //YOU MUST DELAY HERE! (wait for in milli-seconds)
{CMD_DRIVE_ALIGN_COLOR,      	149,        5,      20}, //line up with tetra, PWM value and SCALER number here
{CMD_NO_VISION,           	    0,        0,      0}, //disable vision system no values needed
{NULL,                    	    0,        0,      0} //turn off auto mode no values needed

};
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
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
Example gyro code released. Kevin Watson Programming 60 03-17-2005 05:32 PM
Updated: Serial Port Driver Code Kevin Watson Programming 4 02-05-2005 05:39 PM
Kevin Watson's Kick-off Demo Code! Mr. Lim Programming 27 01-22-2005 02:38 AM
heres the code. y this not working omega Programming 16 03-31-2004 02:18 PM
File Sharing and Revision Tracking Madison FIRST-related Organizations 14 06-04-2003 05:01 PM


All times are GMT -5. The time now is 10:48 AM.

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


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