Pre_Auton in Robotc

these days i have been trying to program gyro since provincial is coming, i am planning to init the gyro in preauton but i have a few questions about the pre_auton fuction
1.If no competition switch is connected, when you turn robot, joystick on, will it directly go into driver control or will it run pre_auton first and then go into driver control? Because i dont see a point where you need to use gyro in driver control and waste 2 secs every time you turn the robot on to reset it.
2.If the joystick and robot is connected, then you plug in to competition switch(disabled), will you then go into and run pre_auton?
Thx in advance

Pre_auton runs whenever you turn on the robot and it syncs with a joystick or whenever the robot restarts the code after the joystick syncs. In my case, when my robot completely shuts down for a second I may have to wait for some tasks to initialize before it can drive again (I have a good amount of tasks with timers in them starting). However, if the robot only desyncs, it does not run preauton again. You can even see this if you have an led or LCD display that you can turn on or write something to in preauton.

It will run preauton then disable the robot. It will not run preauton afterwards.

You should only have to initialize the gyro once, and for a match it should be good, but every time you restart the code (turn off the robot and on again, wirelessly download code, etc.) it will run preauton as well as reset the variables to their first state.
Hope this helps! Your Welcome!

You could always check for the presence of the competition switch and choose to initialize the gyro.

Detecting the presence of the competition switch
https://vexforum.com/index.php/conversation/post/85246

In first u said that preauton will run once the robot connects to the joystick, but in the second one u said that it will run once the competition switch is connected? Does that mean it will run once when the robot synced, and another time when you plug the competition switch in?

pre_auton will run exactly once when the program starts, this is usually when the robot is turned on but could also be when it is started using the ROBOTC debugger.

If a competition switch is subsequently connected it is not run again. pre_auton is a function, just like any other function, that the main task in the competition template calls when it is first started.

So it will run ONLY once, when the joystick and cortex is synced, NO MATTER if the competition switch is plugged in or not? Sry for the caps just want to make it clear :slight_smile:

It will run only once when the ROBOTC program runs. The competition switch being plugged in or not is irrelevant. When the cortex connects to a joystick using VEXnet, sometimes the cortex will force the ROBOTC program to run more than once (we have no control over this), it’s just the same as turning the cortex off and on again. So in that unusual situation pre_auton will run every time the ROBOTC program starts.

Here is the code at the beginning of the competition template (you can easily open this file yourself). The main task (every program, competition or not, has to have a main task) starts, the LCD is cleared and then the pre_auton function called.

task main()
{
	// Master CPU will not let competition start until powered on for at least 2-seconds
	clearLCDLine(0);
	clearLCDLine(1);
	displayLCDPos(0, 0);
	displayNextLCDString("Startup");
	wait1Msec(2000);

	pre_auton(); // <<< the call to pre_auton <<<<<

	while (true)
	{
// and so on...........

@jpearman I have often wondered why the wait 2 seconds is in the competition template. Is there a logical reason for it? Does this give time for other items to run in the firmware? I do not see any explicit code that is executing so what is under the covers?

It would seem to add to refresh time to a static discharge induced Cortex reset.

Lost in the annals of history.

I have no idea why we always have the 2 second delay. I have never found a good reason for it, there’s nothing magic going on in the ROBOTC VM during that time. When I wrote ConVEX I also included a 2 second delay, I did it because ROBOTC did it. It may have been needed when using the old PIC processor, there may have been some issues with VexNET connection, who knows. At this stage I think we are all afraid to remove it just in case it screws something up.

There will be an updated competition template with the next update to ROBOTC, I thought I had pushed out a preview of that a few weeks ago but can’t find it right now, perhaps I didn’t. Anyway I almost removed that 2 second delay but decided to leave it in.

Well anyone can change the competition template so add it to the list of off season testing…