Switching between autonomous and user controlled code

Hello,
Can I use an LCD to select between autonomous and user control codes? I am using the Vex competition template in ROBOTC and the LCD code is placed in the pre_auton() function.

Or the LCD buttons can be used only to switch between autonomous codes?

Thank you!

I may be wrong but I don’t think you are able to tell it what mode to be in using the lcd screen. Generally teams will use competition switches which mimics the competition software.

The code that controls the state of the robot by comp switch or otherwise is all downloaded and run with your code alongside it, so, in theory, you could manually call the


driver_control()

function, but I would recommend that you don’t

Thank you Tylennis and MayorMonty.

So how can I switch between autonomous and user control codes in the VEX competition template? Say that I turn off and turn on the cortex everytime before a particular task is run, so that the pre auton is read everytime. Also what is a comp switch ?

Thanks!

A competition switch lets you manually trip switches to enable/disable the bot and to put it in auton/user control.

You can also use a programming cable to download the program via vexnet. Then you can open a debugger window from the robot menu called competition control to chose what portion of the code to run.

If you have access to neither of those, you can simply put your autonomous code within your driver control task, and then the lcd buttons will be operable to run that portion of the code.

Oh, to be clear, you would need to have the LCD code inside the user control task in order to use it while the robot is in that mode.

Thanks Moyers.
So just to conform, you are asking me to put the LCD code to switch between the autonomous and user control tasks inside my user control task ? So like the whole code could be put inside just one task (task user_control) which can then call autonomous tasks? This means I wouldnt be using the VEX competition template?

I am really sorry if I am missing out something straightforward here!

Thanks!

The Lcd is only used for switching between autonomous programs.

If you want to switch between driver and autonomous modes without a compile, you must download your competition template and use the competition switch.

You can also use programming hardware kit to wirelessly download your driver and autonomous codes.

OK THIS CAN BE DONE.

Basically this is what you do, you write your autonomous code loop + setup IN A FUNCTION.

Wherever you normally actually write your auton, simply call the function. Yeah it’ll add one pitifully tiny function call to the stack, you don’t care nor should you.

Now as the the selector code. Since the robot normally jumps into driver code right away, let it do that. Basically your driver code starts running immediately as usual. But now, whenever you want you can call into your auton code. And via the magic of the break keyword you can bounce out of it whenever you want too.

So, in your driver control loop, setup either a joystick button combo or LCD menu to call into your autonomous function, and inside your autonomous function loop map again some LCD menu or button combo that specifically breaks out of the autonomous function call.

task actual_autonomous_thingy() {

	// Calls the autonomous function whenever the robot actually goes into autonomous
	auton();

	// Safeguard, most API's don't like the autonomous task to end prematurely, this ensures that doesn't happen even if auton() ends prematurely (which shouldn't happen if you do it correctly)
	while(true) {
		delay(1ms);
	}

}

task actual_driver_thingy() {

	while(true) {

		// Normal drive code

		// Check for magic combo to run auton
		if(whatever) {

			// Call auton in driver as a subroutine
			auton();

		}

	}

}

void auton() {

	// Setup

	// Loop
	while(whatever) {
	
		// Normal autonomous code

		// Check for magic combo to break out
		if(whatever) {

			// Will break out of the autonomous function call
			return whatever;

			// or break; if you have cleanup logic or something 

		}

	}

}

Obviously the normal button pressing rules apply. For example, you may want to wait for the button to be released first before doing either the switch or break in, or you risk doing that thing multiple times.

For example let’s say the same button on the joystick both goes into auton and break out of it.

Well pressing that button for what seems like an instant for us might be over 500 loop cycles for the Cortex. That’s going to bounce in and out of the autonomous mode half that many times and may leave you in or out depending on whether or not the number of times was even and odd.

So the simple/dumb way to fix that is to use different buttons. The smarter way involves a placeholder variable and making the action happen on the trailing edge. Which isn’t really complicated at all.

Skype me if you have issues,
Skype: codysmith105

-Cody

Thank you so much Cody!

I tried with the magic break command in the autonomous part and it worked! Thanks a lot again

One more quick question,
What do these lines of the Joystick code mean? What values do vexRT [Ch3] and vexRt [Ch2] take?

motor[leftMotor] = vexRT [Ch3] / 2;
motor[rightMotor] = vexRT [Ch2] /2;

I connected my right and left motors to ports 2 and 3 respectively but the wheels didnt move a bit when I executed the code.
Am I missing out something?

Thank you so much!

http://www.vexrobotics.com/media/catalog/product/cache/11/image/5e06319eda06f020e43594a9c230972d/f/i/file_51_3.jpg

If you look at the joystick (the controller that is) the buttons and joysticks are labeled with numbers. Ch2 and Ch3 correspond with the left and right Y axis. vexRT is the name of a global array which ROBOTC kind of dumps all the joystick values in for you approximately every 20ms.

So the code you have there apparently maps those sticks to whatever motor port is named leftMotor and rightMotor.

To find out which port those are mapped to see your motors and sensors setup dialog.

Break isn’t magic (even if half my CS dept doesn’t seem to know it exists outside of switch statements). Also, I should have noted that since this is a function a return call will also work.

Thank you Cody!
I agree with you on the return call statement too :slight_smile:

Thank you so much everyone for my Robot is now doing what it is supposed to! :slight_smile:

Your support is invaluable and I wholeheartedly appreciate it. This has been a great experience and I learned a lot during the course of programming the Robot.

Yes, that is basically what I meant. Sorry I didn’t reply sooner. I’m glad you now have it working. GJ Cody!