Can't get auto mode to run with OI switches

Hey all…

I have a potentiometer with detents that is mounted on the oi controller.

I have no idea whats going on… I set up a static int newautoselect inside user_control.h,

int currentauto;

unsigned int autopos;
unsigned int temp_autopos;
int autocount;
autopos = p2_wheel;

autocount ++;

if(autocount >= 26)
{
	autopos = currentauto;
}

if(currentauto > 20 | currentauto < 40)
{
	newautoselect = 1;
}
else if(currentauto > 50 | currentauto < 60)
{
	newautoselect = 2;
}
else if(currentauto > 100 | currentauto < 120)
{
	newautoselect = 3;
}
else if(currentauto > 130 | currentauto < 150)
{
	newautoselect = 4;
}
else if(currentauto > 160 | currentauto < 190)
{
	newautoselect = 5;
}
else if(currentauto > 200 | currentauto < 250)
{
	newautoselect = 6;
}

why doesnt auto mode pick this up?

When the OI is switched into autonomous mode it no longer reads input from the joystick ports. All input values get zeroed (127 for analogs) until it is placed back into standard operating mode. What you’ll want to do is read the input value only when the robot is not in auto mode, then evaluate it when the robot is. Otherwise it will always read 127 as the selector input, no matter what position it’s in. Remember that the OI can receive input from the ports when the robot is disabled, which means that you will be able to set the autonomous mode selector at any point before the match begins.

Also, use “extern int newautoselect;” in the header,
and use “int newautoselect = 0;”, outside of any routine,
in one of the files that the header is included in. If you
use “static int newautoselect;” in the header a different
instance of newautoselect is what you will get in each file the
header is included in and desired communication will not occur.

You need to save the value of the OI reading the last cycle before you enter autonomous.

I recommend creating a char to hold the state of the OI input and updating it in Process_Data_From_Master_uP() as long as the autonomous_mode flag is not set.


unsigned char currentauto = 0; //Set outside function to make it global

void Process_Data_From_Master_uP(void)
{
	Getdata(&rxdata);

	if(!autonomous_mode)
		   currentauto = p2_wheel;

	// Remainder of function
}

Then in user_routines_fast you’ll need:


extern unsigned char currentauto;

to access it.

The only thing, I’m not sure if the name of the flag (autonomous_mode) is right, I don’t have code to reference at the moment.

:frowning: Okay,

I just put in

Getdata(&rxdata);

if (!autonomous_mode)
{

blha blah blah;
the other stuff up there goes here!

}

See, i moved the int declarations and that other stuff… and moved it up to the top of the user_routines.c

int currentauto;
int newautoselect;
unsigned int autopos;
unsigned int temp_autopos;
int autocount;

Now in user_routines_fast.c,

extern unsigned char currentauto;
extern unsigned char newautoselect;

WHAT IS WRONG!!?!?! :confused:

First of all, comments by others above me certainly apply. However, there is a more fundamental problem:


	autopos = p2_wheel;

	autocount ++;

	if(autocount >= 26)
	{
		autopos = currentauto;
	}

autopos is set to p2_wheel and then set to currentauto which has never been set…

I believe that that (last) line should be “currentauto = autopos;”…

Regards,

Mike

Okay,

Based on a if and elseif statement, the bot would read the potentiometer on the OI which is p2_wheel, called autopos.

Autopos is going to equal to currentauto within 26 ms. once that happens, current auto is compared using a little deadband then setting newautoselect.

newautoselect is now supposed to be pulled from the user_routines.c

now newautoselect is put into a little if and then and then the new auto mode is supposed to be selected.

Shouldnt that code already work?

Wouldn’t you want to use AND not OR because it would always evaluate as true with OR since it is either greater than 20 OR less than 40.

Indirectly related, but you should also code for the in-betweens: What If currentautro were to evaluate at, say 45? At least trap and manage the error.

Don

I second that. Also, if you are meaning to do a logical OR, you need to be using a double pipe (i.e ||) not a single (i.e. |). The single pipe represents a bitwise OR, which can give you drastically different results. If you switch over to the AND, make sure that you use a double ampersand (i.e. &&) for the same reason.

You can find more information about bitwise operations here

Yeah… I forgot about using &, the code has been replaced. I’m going to write the newautomode into the eraseable memory because im having SO much trouble with this… Hopefully that would work. :frowning:

You have a type miss-match between your extern declarations
and the definitions in user_routines.c You need them to be
consistent. If you put the extern declarations in a common header
file that is also included in user_routines.c the compiler will catch
this sort of error for you.