|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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}};
. . .
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;
}
}
|
|
#2
|
||||
|
||||
|
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}
};
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
};
Last edited by Sentient : 21-03-2009 at 11:29. |
|
#3
|
||||
|
||||
|
Re: HELP: Robot Crash in Autonomous
Thanks! I'll try that during the next build window!
|
|
#4
|
|||
|
|||
|
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... |
|
#5
|
||||
|
||||
|
Re: HELP: Robot Crash in Autonomous
Quote:
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. |
|
#6
|
||||
|
||||
|
Re: HELP: Robot Crash in Autonomous
also if you do this
Code:
type *varname; ... varname=value; you need to do this Code:
type *varname; ... *varname=value; OR type *varname; ... varname=&value; Last edited by byteit101 : 22-03-2009 at 21:09. |
|
#7
|
||||
|
||||
|
Re: HELP: Robot Crash in Autonomous
Quote:
Code:
struct Command *currentCommand; // ... currentCommand = command_list_X; |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |