PDA

View Full Version : Please help with our IR code, we are so confused


rjn
02-18-2008, 08:37 AM
This is our code for our hybrid, and we honestly have no idea what is wrong, it constantly blasts forward, even after a new button is hit


void Autonomous(void)
{
int move, othermove;

//printf("Time=%lu", Get_Time( ));

if ((rc_dig_in01 + rc_dig_in02 + rc_dig_in03 + rc_dig_in04) > 1)
{
rc_dig_in01 = rc_dig_in02 = rc_dig_in03 = rc_dig_in04 = 0;
}

othermove = move;
if (rc_dig_in01 == 1) {
move = 1;
}
else if (rc_dig_in02 == 1) {
move = 2;
}
else if (rc_dig_in03 == 1) {
move = 3;
}
else if (rc_dig_in04 == 1) {
move = 4;
}
else {
move = othermove;
}

printf("Move: %d In01: %d, In02: %d, In03: %d, In04: %d \r \n", move, rc_dig_in01, rc_dig_in02, rc_dig_in03, rc_dig_in04);
if (move == 0)
{
pwm01 = 127;
pwm02 = 127;
pwm03 = 127;
pwm04 = 127;
}
else if (move == 1) {
pwm01 = 187;
pwm02 = 67;
pwm03 = 187;
pwm04 = 67;
}
else if (move == 2) {
pwm01 = 187;
pwm02 = 187;
pwm03 = 67;
pwm04 = 67;
}
else if (move == 3) {
pwm01 = 67;
pwm02 = 187;
pwm03 = 67;
pwm04 = 187;
}
else if (move == 4) {
pwm01 = 67;
pwm02 = 67;
pwm03 = 187;
pwm04 = 187;
}
else {
pwm01 = 127;
pwm02 = 127;
pwm03 = 127;
pwm04 = 127;
}
}

Any help would be very appreciated.

gnormhurst
02-18-2008, 09:37 AM
What does the printf tell you? I only see one suspicious thing. In your printf you should probably cast the values to int:

printf("Move: %d In01: %d, In02: %d, In03: %d, In04: %d \r \n", move, (int)rc_dig_in01, (int)rc_dig_in02, (int)rc_dig_in03, (int)rc_dig_in04);


Printf's '%d' directive tells printf to look for an int, and although I didn't check, I suspect the dig_in values are not ints.

What are the values of the pwm's at the end of the function?

rjn
02-18-2008, 09:42 AM
Oh, sorry, it's kind've early, atleast for me. Our code is meant to fly in a direction based on the last button pressed on our remote, it compiles great, but when we flip our autonomous switch, even without any signals from the ir board, it goes still says move = 1(goes forward), and even after hitting a button, it simply goes back to move = 1, even though it should stay in a different move case(stored as an int). We honestly have no idea.

psy_wombats
02-18-2008, 09:59 AM
The variable 'move' should be static. Otherwise it will reset every time the function is called. Being a static variable causes it to remember its state from the previous call, and move as you would expect it. See if that works.

Jeff Waegelin
02-18-2008, 10:04 AM
Do the lights on your IR board light up properly when you press a button on your remote? (and do they turn off when you're not pressing a button?) It's possible the PIC on your IR board could have been corrupted- see Team Update 10 (http://www.usfirst.org/uploadedFiles/Community/FRC/FRC_Documents_and_Updates/2008_Assets/Team_Updates/Team%20Update%2010.pdf) for info on how to fix that. We had a similar problem on Saturday, and that was the cause.

Also, try checking the wiring of your IR board with an oscilloscope to make sure your RC is receiving the correct signal. You should get a 5V square wave on each output from the IR board if it's working right. When you think is right, but you still can't get it to work, it's always a good idea to make sure your sensors are fully functional in the first place.

The Lucas
02-18-2008, 10:05 AM
void Autonomous(void)
{
int move, othermove;

//printf("Time=%lu", Get_Time( ));

if ((rc_dig_in01 + rc_dig_in02 + rc_dig_in03 + rc_dig_in04) > 1)
{
rc_dig_in01 = rc_dig_in02 = rc_dig_in03 = rc_dig_in04 = 0;
}





I am pretty sure you cant assign digital outputs (rc_dig_in0x) to zero. Digital inputs are not variables, they are alias to input ports on the microcontroller and I am pretty sure if you set them as input, you cant write that register. So what is probably happening is all digital inputs are 1 (and not getting zerod) and move is being set to 1 (since that is first in else if block). Move if ((rc_dig_in01 + rc_dig_in02 + rc_dig_in03 + rc_dig_in04) > 1) {} to the first statement in the "move setting" else if block. Also you IR sensor is probably not hooked up correctly if you are getting all 1s.

Also it would be better if you declared move as static (static int move) and removed othermove. A static int never gets destroyed so it will keep its value from one loop to the other.

Tanis
02-18-2008, 10:17 AM
That version of our code is a tad outdated.

The digital ins are no longer being set to zero if their total is greater than one; when they are greater than one, move is now set to zero.

Changing it to static may fix it, so we'll try that.

If any of you are still confused- Its supposed to "remember" what button was last pushed (stored in move), but the variable move changes only when a button is pressed, then it immediately goes back to one.


The IR sensor IS hooked up right, and the printfs show that dig ins are zero until a button is pressed, when it gets a 1 every 100ms.

The Lucas
02-18-2008, 10:28 AM
That version of our code is a tad outdated.

The digital ins are no longer being set to zero if their total is greater than one; when they are greater than one, move is now set to zero.



Still need to move the greater than one test to the first statement in the else if block that sets move. Otherwise you are setting move to zero then setting to something else later.

rjn
02-18-2008, 10:40 AM
Making the int static did save it for the next loop, but a new problem has arrived, although it moves, everytime a new button is hit(or sometimes when you just hit a button) it seems to just reset, in that the terminal window says IFI>
IFI User Process Initiated...

And our timer even starts over.