Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   !HELP! IM STUCK IN A SUBROUTINE!!! !HELP! (http://www.chiefdelphi.com/forums/showthread.php?t=49759)

cdennisxlx2 02-11-2006 03:33

!HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Im developing a set of subroutines to make it easyer for my teams programmers. But when i tried to show them how to call them it calls the first one and stays.

example: From user_routines_fast.c

This is the autonomous code i made to test calling subroutines [line: 141]
Code:

/*******Autonomy Notes*********************
* "time" was initalized on line 24
* relay1_fwd/rev = Bottom Cylinder
* relay3_fwd/rev = Top Cylinder
*            pwm01 = Left Drive motor
*          pwm02 = Right Drive motor
*          pwm03 = Shooter
*          pwm04 = Tilt
*          26 loops = One Second (roughly)
****************************************/               
               
if (time >= 0 && time < 50)
{       
        Left();
}
else if (time > 50 && time < 100)
{
        Stop();
}
else if (time > 100 && time < 150)
{
        Right();
}
else if (time > 150)
{
        pwm01 = pwm02 = 127;
}
time++;  //Incriment time


These are the subroutines [line: 290 in user_routines_fast.c]
Code:

unsigned char Left()
{
        pwm01 = 0;
        pwm02 = 254;
        return 0;       
}

unsigned char Right()
{
        pwm01 = 254;
        pwm02 = 0;
        return 0;       
}

unsigned char Stop()
{
        pwm01 = pwm02 = 127;
        return 0;
}


The FUNCTION PROTOTYPES for the subroutines in user_routines.h
Code:

unsigned char Left(void);
unsigned char Right(void);
unsigned char Stop(void);


Can anyone tell me why when i run this it stays in Left()??????????


thank you in advanced

Mike Betts 02-11-2006 04:23

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Cameron,

I can not identify exactly where your problem is. Here are some ideas.

I would add a check after incrementing time and set time = 0 if time < 0.

I would look at where time is declared and insure that it is static. Beware of dynamic initializations...

As a matter of style, I would use the form ((A > B) && (A < C)) rather than (A > B && A < C) when working with students. It adds nothing to the speed of the code and eliminates the memorization of operator precedence.

Lastly, I would add a printf to determine what is going on. I strongly suspect the initialization of time is either implicit or the variable is getting stepped on between loops.

Regards,

Mike

Stuart 02-11-2006 07:30

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
well first you have 3 holes
50 100 and 150.

dont think that will fix the problem. but at 6am thats all that I can see is wrong.

Mark McLeod 02-11-2006 07:59

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
After you get past the "Left();" problem, "time" will eventually wrap around to 0 and start all over again depending on how you've declared it.

I vote with Mike. It seems like "time" could easily be a local variable and thus always be 0. If you remove the "=" from "if (time >= 0 && time < 50)" it would probably not even do "Left()".

P.S.
It's 38 loops per second (26.2 milliseconds per loop) on the FRC.
Why didn't you choose to call Stop() at the end?

KenWittlief 02-11-2006 09:00

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
printf is your best debug friend

computers have a nasty habit of doing exactly what you tell them to do

instead of what you want them to do

whenever a program is not doing what you expected, printf the variables to see whats going on inside the SW

in fact, you should get in the habit of checking SW modules this way, to make sure it is doing exactly what you thought it would do (module level testing). Then if its working correctly take the debug (printf...) statements out, or have a debug variable that you set to 0 to disable them.

JamesBrown 02-11-2006 09:42

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Assuming he didn't change the comment at the beginning of the code (which I am guessing he didn't because other wise the line numbers don't do much good) then he initialized time in the right place.

The only problem I could see there is if you just declared time and didn't initialize it. Assuming you initialized it as 0 I don't see that as being your problem. I am not sure if it is enough to cause the problem you have but at the very least it is sloppy to return a value through the function if you aren't assigning it to anything.

If you still can't figure out the problem, post all of your user_routine_fast.c and we can probably help more.

James

Daisy 02-11-2006 10:27

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
After running it in my head (which may not be accurate), I think time is the problem. You're incrementing time, so that after 49, it reaches 50, but at it, you do not define what it does with it (ie Left() or Stop(), etc), so it will keep repeating left.

Daisy

Dave Scheck 02-11-2006 10:34

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Quote:

Originally Posted by Daisy
After running it in my head (which may not be accurate), I think time is the problem. You're incrementing time, so that after 49, it reaches 50, but at it, you do not define what it does with it (ie Left() or Stop(), etc), so it will keep repeating left.Daisy

Yeah, but time gets incremented outside of the if statements. Unless he is clearing the PWM values somewhere, whatever was set in Left will still be sent to the motors for an additional cycle. If he is clearing the values, there would be a 1 cycle pause in the PWM output.

I agree with what was said above. Most likely the time variable isn't global or statically defined. Debug prints should make this apparent rather quickly if this is the case.

cdennisxlx2 02-11-2006 15:11

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Time is not the problem, that part works great. All i need to know is did i code the subroutines wrong or am i calling them wrong? If im doing those right then why is the code stuck in Left() when i run the robot in autonomous mode?

Mark McLeod 02-11-2006 15:18

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Post a zip of your user_routines_fast.c here and we'll tell you.

Right now it looks like you're taking something for granted when you post the problem and not bothering to tell us about some seemingly unimportant detail. In programming all details are important. If you weren't overlooking something your code would be working. :) As with most bugs you don't know what the important detail is yet.

Alan Anderson 02-11-2006 15:55

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Quote:

Originally Posted by ghhs_1527
Time is not the problem, that part works great.

How do you know that? My first guess is that you're "initializing" the time variable every time through the loop, so it is always equal to zero when you hit the if statement.

Speaking of "the loop", what is causing it to run 26 times per second? We definitely need to see more of the code in order to help you.

Stuart 02-11-2006 17:50

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
ok its now 5pm and Im awake

ok 50% chance your resetting time to be = to 0 every time you loop.

30% chance that that loop isnt happening every 26 secs(I know it says "im fast I run every 26.1 secs" but its a liar liar pants on fire)

20% chance its something else

cdennisxlx2 02-11-2006 19:08

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
2 Attachment(s)
I know it works because it works in all the other modes, just not the one with the subroutines, we have a dip switch to choose autonomous modes. but here are the user_routines_fast.c and user_routines.h files (attached)

Alan Anderson 02-11-2006 21:27

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Code:

        if (rc_dig_in11 = 1) /* 1st Autonomy code */
There's your problem right there. You just tried to assign the value 1 to the variable rc_dig_in11. To test for equality, you must use the == symbol, with two equal signs.

The code you posted will always run the BASH routine. You need to change all those single = to double == in your if and else if statements.

JamesBrown 02-11-2006 22:43

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Quote:

Originally Posted by Alan Anderson
Code:

        if (rc_dig_in11 = 1) /* 1st Autonomy code */
There's your problem right there. You just tried to assign the value 1 to the variable rc_dig_in11. To test for equality, you must use the == symbol, with two equal signs.

The code you posted will always run the BASH routine. You need to change all those single = to double == in your if and else if statements.

Yep, that would be it.

As a note to any one else who ever needs help the more code you post the better we can help, don't worry about people stealing it, it wont work on our robots anyway.

James

Dave Scheck 02-11-2006 23:06

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
I agree with Alan and James that a problem exists in the use of the assignment operator, but the initial problem stated that the code was stuck in Left. I don't see how this could be reached. How was it determined that it was getting stuck in Left?

cdennisxlx2 03-11-2006 02:25

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
i had a student telling me what directions the wheels were turning and i took his word for it instead of double checking, but thank you all for the help

Matt Krass 03-11-2006 09:25

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
I still think we need to see the rest of the code, or at least how the time variable is defined. If it isn't defined static, the variable will be re-initialized everytime its host entity is restarted. So if it's at the top of your function, and this function runs over and over every loop, it will be reinitialized every loop to 0, no matter how much you increment it. Or, time can be a global and it will retain its value, but if you're assigning 0 to it every loop that will also ruin your day. I think you have a pretty simple (and somewhat hard to notice) problem that I'm unfortunately very familiar with, as Mark McLeod can tell you, how many times did I hit Chris with the robot again? ;)

cdennisxlx2 03-11-2006 15:28

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
the .c file is attached above, but i know for a fact thats not the problem because ive defined "time" and incrimented it the same way for 3 years now and i have had no problem with that at all.

Dave Flowerday 03-11-2006 15:50

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Quote:

Originally Posted by Matt Krass
I still think we need to see the rest of the code, or at least how the time variable is defined. If it isn't defined static, the variable will be re-initialized everytime its host entity is restarted.

It's there, line 25 of user_routines_fast.c, as a global, so it should be fine. I'm not exactly sure what you mean by "If it isn't defined static, the variable will be re-initialized everytime its host entity is restarted."

Alan Anderson 03-11-2006 15:57

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Quote:

Originally Posted by ghhs_1527
...ive defined "time" and incrimented it the same way for 3 years now and i have had no problem with that at all.

You probably have had problems, but you didn't recognize them as being caused by the way you're initializing your time variable. The way you're doing it, the initialization happens once when the robot powers up. You won't be able to rerun the autonomous mode correctly unless you reset the robot first. Better would be set time to zero explicitly when you enter the User_Autonomous_Code() function, just before the while (autonomous_mode) loop.

Matt Krass 03-11-2006 17:15

Re: !HELP! IM STUCK IN A SUBROUTINE!!! !HELP!
 
Quote:

Originally Posted by Dave Flowerday
It's there, line 25 of user_routines_fast.c, as a global, so it should be fine. I'm not exactly sure what you mean by "If it isn't defined static, the variable will be re-initialized everytime its host entity is restarted."

I missed the attached posting, my fault for trying to do too much at once, sorry about that.

As far as what I meant, I meant that a local variable inside of a function would be reset everytime the function ended and started again. Not sure why I worded it that way, let me just chalk it up to a goofy morning eh? :)


All times are GMT -5. The time now is 23:32.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi