Go to Post We are all insane aren't we? I just want to build a robot now. :P - Lowfategg [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 21-03-2009, 11:16
Shadow503's Avatar
Shadow503 Shadow503 is offline
printf("\r\n Moo!");
no team
Team Role: Human Player
 
Join Date: Mar 2007
Rookie Year: 1991
Location: na
Posts: 92
Shadow503 will become famous soon enoughShadow503 will become famous soon enough
Exclamation HELP: Robot Crash in Autonomous

I wrote some code for a simple table based autonomous mode, but whenever we ran it, the robot completely crashed (random motors will thrash around a split second before the thing finally dies).

I figure there must a seg fault caused by to the way I'm using a struct pointer, but I don't know enough C++ to get this coded the right way.

Anyone got a spare moment to look over the code? Thanks for your time!

auto_commands_2009.h:
Code:
//autonomous commands
#define AUTO_STOP			0
#define AUTO_DRIVE_4WHEEL	1
#define AUTO_DRIVE_PIVOT	2
#define AUTO_DRIVE_TANK		3
#define AUTO_DRIVE_CRAB		4


/*
struct commands
{
	int steer_mode;
	double time;
	double speed;
	double angle;
};
*/
struct Command
{
	int command;	//what command to run
	double param1;	//usually time
	double param2;	//usually speed
	double param3;	//usually angle
};

struct Command command_list_0[] = {	//DEFAULT - STAY STOPPED
		//   Command        parm 1     	parm 2   	parm 3  
		//	Steer mode		Time(Sec)	Speed		Angle
		{AUTO_STOP,       	0.0,        0.0,     	0.0}		
};

struct Command command_list_1[] = {

//   Command              parm 1     	parm 2   	parm 3  
//	Steer mode				Time(Sec)	Speed		Angle
{AUTO_DRIVE_4WHEEL,       	1.0,        10.0,     	0.0},
{AUTO_DRIVE_PIVOT,         	1.0,        10.0,     	10.0},
{AUTO_DRIVE_4WHEEL,         1.0,        10.0,     	10.0},
{AUTO_DRIVE_PIVOT,         1.0,        0.0,      	0.0},
{AUTO_STOP,          		1.0,       0.0,     	0.0}};
. . .
autonomous_2009.cpp:
Code:
/**
 * Call this method to drive
 * @param speed a double describing the speed		-15fps to +15fps
 */
void Autonomous::procTable(int AutoMode) {
	// TODO Finish autonomous code
	//		static INT32 autoSec = (INT32)GetClock() + 1;
	curTime+= TIME_PER_CYCLE;
	currentStateTime += TIME_PER_CYCLE;
	struct Command *currentCommand;
	
	//set the commandlist
	switch (AutoMode) {
	case AUTOMODE_0:		//Auton mode 0
		currentCommand = command_list_0;
		break;
	case AUTOMODE_1:		//Auton mode 0
		currentCommand = command_list_1;
		break;
	case AUTOMODE_2:		//Auton mode 0
		currentCommand = command_list_2;
		break;
	case AUTOMODE_3:		//Auton mode 0
		currentCommand = command_list_3;
		break;
	case AUTOMODE_4:		//Auton mode 0
		currentCommand = command_list_4;
		break;
	
	
	default:
		currentCommand = command_list_0;
		break;
	}
	
	//behave based on the current command
	switch (currentCommand[cur_command].command) {
	case AUTO_STOP:			//robot stopped
	default:		
		reqDriveSpeed = 0.0;				// Requested Drive speed
		reqAngle = 0.0;						// Requested steer angle
		reqDriveMode = STEER_MODE_4WHEEL;	// Requested Drive Mode
		if(currentCommand[cur_command].param1 != 0 && currentStateTime >= currentCommand[cur_command].param1 ) {	//time to move on to next state
			if(cur_command + 1 < sizeof(currentCommand)) {	//there is another command to run
				cur_command++;	//increment to next command
				currentStateTime = 0; //move on to next state
			}
		}		
		break;
	}
}
Reply With Quote
  #2   Spotlight this post!  
Unread 21-03-2009, 11:26
Sentient's Avatar
Sentient Sentient is offline
Registered User
FRC #0639 (Code Red)
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Ithaca
Posts: 21
Sentient is on a distinguished road
Re: HELP: Robot Crash in Autonomous

I'm not sure if this is the root of your problem, but I don't believe you are using structs properly, and I think it's strange the compiler didn't catch it. You only need the "struct" keyword before Command when you declare the struct. So, you don't need so say

Code:
struct Command command_list_0[] = {	//DEFAULT - STAY STOPPED
		//   Command        parm 1     	parm 2   	parm 3  
		//	Steer mode		Time(Sec)	Speed		Angle
		{AUTO_STOP,       	0.0,        0.0,     	0.0}		
};
but rather

Code:
Command command_list_0[] = {	//DEFAULT - STAY STOPPED
		//   Command        parm 1     	parm 2   	parm 3  
		//	Steer mode		Time(Sec)	Speed		Angle
		{AUTO_STOP,       	0.0,        0.0,     	0.0}		
};

The only place you need the struct keyword is here, when you declare the struct:

Code:
struct Command
{
	int command;	//what command to run
	double param1;	//usually time
	double param2;	//usually speed
	double param3;	//usually angle
};
You did this quite a bit. I hope this fixes the problem. C++ may be doing something weird and unpredictable because of all those extra struct keywords.

Last edited by Sentient : 21-03-2009 at 11:29.
Reply With Quote
  #3   Spotlight this post!  
Unread 21-03-2009, 12:44
Shadow503's Avatar
Shadow503 Shadow503 is offline
printf(&quot;\r\n Moo!&quot;);
no team
Team Role: Human Player
 
Join Date: Mar 2007
Rookie Year: 1991
Location: na
Posts: 92
Shadow503 will become famous soon enoughShadow503 will become famous soon enough
Re: HELP: Robot Crash in Autonomous

Thanks! I'll try that during the next build window!
Reply With Quote
  #4   Spotlight this post!  
Unread 22-03-2009, 00:43
Pancake Pancake is offline
Registered User
FRC #0991 (The Dukes)
Team Role: Photography
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Phoenix
Posts: 11
Pancake is an unknown quantity at this point
Re: HELP: Robot Crash in Autonomous

If you're looping the Autonomous::procTable(int AutoMode), make sure to delete the *currentCommand pointer.

And if you're using WPILib, I fail to see the point for using structs...
__________________
Team Webmaster
Reply With Quote
  #5   Spotlight this post!  
Unread 22-03-2009, 11:38
heydowns's Avatar
heydowns heydowns is offline
Registered User
AKA: Jeff Downs
FRC #1511 (Rolling Thunder)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Ra-Cha-Cha
Posts: 142
heydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond repute
Re: HELP: Robot Crash in Autonomous

Quote:
Originally Posted by Shadow503 View Post
Code:
/**
 * Call this method to drive
 * @param speed a double describing the speed		-15fps to +15fps
 */
void Autonomous::procTable(int AutoMode) {

// ........

			if(cur_command + 1 < sizeof(currentCommand)) {	//there is another command to run
				cur_command++;	//increment to next command
				currentStateTime = 0; //move on to next state
			}

// .........

}

This is at least part of the problem you are experiencing. "sizeof(currentCommand)" will not give the size of the commands array. sizeof evaluates at compile time to the size of a type, or, when used with a variable, the type of the variable. currentCommand's type is "struct Command *". So, the sizeof (regardless of which command table is chosen to have currentCommand point to) will always evaluate to the size of a struct Command pointer (probably 4). This could be greater than the actual size of your command table, causing your program to potentially walk off the end of an array.

The easiest way to change this would be to add another command type that signifies the end of the command table. When your autonomous code sees that command as it processes the table, it knows it has reached the last entry.
Reply With Quote
  #6   Spotlight this post!  
Unread 22-03-2009, 21:06
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: HELP: Robot Crash in Autonomous

also if you do this
Code:
 type *varname;
...
varname=value;
you assign varname's address at memory the value of value
you need to do this
Code:
type *varname;
...
*varname=value;
OR
type *varname;
...
varname=&value;
EDIT: if you do int* evilval; evilval=anynumber, especialy low ones like 0; and then later *evilval = another number; another number is going into the memory address anynumber crashing the computer, which is what i did when i was not thinking earlier in the season
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib

Last edited by byteit101 : 22-03-2009 at 21:09.
Reply With Quote
  #7   Spotlight this post!  
Unread 23-03-2009, 00:33
heydowns's Avatar
heydowns heydowns is offline
Registered User
AKA: Jeff Downs
FRC #1511 (Rolling Thunder)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Ra-Cha-Cha
Posts: 142
heydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond repute
Re: HELP: Robot Crash in Autonomous

Quote:
Originally Posted by byteit101 View Post
also if you do this
Code:
 type *varname;
...
varname=value;
you assign varname's address at memory the value of value
you need to do this
Code:
type *varname;
...
*varname=value;
OR
type *varname;
...
varname=&value;
Generally speaking, yes. However, OP's assignment of the only pointer he is using is correct:

Code:
struct Command *currentCommand;
// ...
currentCommand = command_list_X;
command_list_X is an array of "struct Command". Assigning to a pointer using an array (of the same base type) is perfectly legitimate and will simply result in the pointer pointing to the base of the array.
Reply With Quote
Reply


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
Very Strange cRIO Crash Hunter1100 C/C++ 0 19-01-2009 16:39
HDD crash, need help recovering data! David Kelly IT / Communications 16 28-05-2007 23:25
PlasmaCam Crash Course Andrew Blair Technical Discussion 0 30-12-2005 21:48
Prom Night Crash Nate Edwards General Forum 5 28-05-2005 15:15
3dsmax5 Crash? yangotang Inventor 5 09-03-2003 21:17


All times are GMT -5. The time now is 13:38.

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