PDA

View Full Version : Rewriting main loop


Max Lobovsky
01-04-2005, 01:52 PM
I want to rewrite the main loop so that the program enters one of the two possible 26.2ms loop functions (User_Autonomous_Code() and Process_Data_From_Master_uP()) from the same place in the code. As it is, when the code enters autonomous mode, it stays in User_Autonomous_Code() until autonomous mode is unset. The main reason I'd like to change this is so that I can have a single counter counting 26.2ms cycles, rather than one in each function. Secondly, its much more logical to do it this way.

I tried to implement my method, but I get some inexplicable problem where User_Initialization() (which should be called prior to any of my changes) is not getting called, and consequently, the rest of the program cannot run. Does the master processor detect my changes and decide they are illegal or something? Why is the code written like this in the first place?

The changes I made:

in main.c replace


while (1) /* This loop will repeat indefinitely. */
{
#ifdef _SIMULATOR
statusflag.NEW_SPI_DATA = 1;
#endif

if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{ /* I'm slow! I only execute every 26.2ms because */
/* that's how fast the Master uP gives me data. */
Process_Data_From_Master_uP(); /* You edit this in user_routines.c */

if (autonomous_mode) /* DO NOT CHANGE! */
{
User_Autonomous_Code(); /* You edit this in user_routines_fast.c */
}
}
Process_Data_From_Local_IO(); /* You edit this in user_routines_fast.c */
/* I'm fast! I execute during every loop.*/
} /* while (1) */
} /* END of Main */


with


while (1) /* This loop will repeat indefinitely. */
{
#ifdef _SIMULATOR
statusflag.NEW_SPI_DATA = 1;
#endif

if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{ /* I'm slow! I only execute every 26.2ms because */
/* that's how fast the Master uP gives me data. */
/* You edit this in user_routines.c */

if (autonomous_mode) /* DO NOT CHANGE! */
{
User_Autonomous_Code(); /* You edit this in user_routines_fast.c */
}
else
{
Process_Data_From_Master_uP();
}
/*counter++; can go here*/
}
Process_Data_From_Local_IO(); /* You edit this in user_routines_fast.c */
/* I'm fast! I execute during every loop.*/
} /* while (1) */
} /* END of Main */


In user_routines_fast.c replace

while (autonomous_mode) /* DO NOT CHANGE! */
{
if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{
Getdata(&rxdata); /* DO NOT DELETE, or you will be stuck here forever! */

/* Add your own autonomous code here. */



Generate_Pwms(pwm13,pwm14,pwm15,pwm16);

Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */
}
}


with


Getdata(&rxdata); /* DO NOT DELETE, or you will be stuck here forever! */
Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */

Mark McLeod
01-04-2005, 04:00 PM
Your code looks fine. There is no magic check to see if you've changed anything in main.c. Lots of teams have rewritten main.c without issues. All the teams I mentor modify main.c for this and a variety of other reasons as well.
I'll see if I can find some past threads on this topic. [edit]http://www.chiefdelphi.com/forums/showthread.php?t=24590

Since the call to User_Initialization comes before the loop you posted I suspect other problems.

Here's one stripped down version. For clarity I removed some lines that delayed all action until the sensors stabilize after power-up, resets autonomous upon disable, and a couple of other specialized routine calls like conversion from 0->254 to -127->127. The Put/Getdata calls are also brought into main.c instead of being repeated elsewhere, so User_Autonomous() is a normal routine.

void main (void)
{
#ifdef UNCHANGEABLE_DEFINITION_AREA
IFI_Initialization (); /* DO NOT CHANGE! */
#endif

User_Initialization(); /* You edit this in user_routines.c */

statusflag.NEW_SPI_DATA = 0; /* DO NOT CHANGE! */

while (1) /* This loop will repeat indefinitely. */
{
#ifdef _SIMULATOR
statusflag.NEW_SPI_DATA = 1;
#endif

if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{
Getdata(&rxdata); /* Get fresh data from the master microprocessor. */

Process_Data_From_Master_uP(); // Modified to only handle slow loop sensor stuff

if (competition_mode) // Robot is disabled
{
Gyro(); // Initialize gyro assumes FRC is powered up only when robot is in position
}
else if (autonomous_mode) // Robot is in autonomous
{
User_Autonomous_Code();
}
else // Robot is in normal human driver mode
{
User_Driver(); // Joystick driving
}

Putdata(&txdata); /* DO NOT CHANGE! */
}


Process_Data_From_Local_IO();

} /* while (1) */
} /* END of Main */

Max Lobovsky
01-04-2005, 05:47 PM
I guess I'll look for some typo or something somewhere. I think I will also move put and getdata() there, too.

Ryan M.
01-04-2005, 06:05 PM
What exactly are the errors you get? It looks like this code is fine to me. If you do get it working, post it back up here (or on the code repository! :)). I'd like to use it. (And I don't want to have to figure this out as well...)

Max Lobovsky
01-04-2005, 06:35 PM
That wasn't an actual copy of the code I tried to compile, so I imagine there was some small error somewhere in my code.. I just quickly rewrote the changes for the post (it's really just deleting some stuff and moving a single line).