Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   encoders (http://www.chiefdelphi.com/forums/showthread.php?t=32611)

stephenthe1 12-01-2005 15:07

encoders
 
I'm posting this in hope that someone who has actually registered on this site will post something. there are so many people viewing this, but not posting anything. with that said, I thought this would be a nice place for people to discuss encoders. of course, my main motivation is that these things have been my killing me over the past month. I have tried numerous things, and I can never seem to get it to work. Coding, for the most part, I don't think is a problem. I'm using 2004's code written by Mr. Watson that supports encoders and interrupts. I have everything configured, the only thing that could possible be keeping the encoder from working is my code in User_Routines_Fast.c
what the code does is this, when the third joystick is pushed forward, the arm on last year's robot goes up untill the encoder has "clicked" 12 times. then the motor on the arm is set to neutral. it's as simple as that. (for testing purposes).

Code:

        static long lec = 0;            //lec = encoder count
        lec = lec + Get_Left_Encoder_Count();    //add previous count
      //plus the new count
//the static long is supposed to hold the value every loop                                                             
        if (lec >= 12)
        {
        pwm02 = 127;                //set it to neutral
        }
        else
        {
        pwm02 = p3_y;
        }

can you please tell me why this won't work, or if you think it should. please tell me yourself and don't refer me to somewhere. I've spent so much time, and a simple few lines of code would help me to understand this almost completely. (don't forget, Mr. Watson's 2004 encoder code is what I'm using.)

HELP!!!
thanks,
Stephen :yikes:

seanwitte 12-01-2005 15:19

Re: encoders
 
What does the robot do when you move the joystick? Have you tried turning the encoder by hand and printing the value of lec to make sure its being incremented correctly?

Since the call to Get_Left_Encoder_Count() does not reset the internal value, your local variable (lec) will saturate very quickly. Once the encoder moves one tick it will increment lec over 12 almost instantly. You either need to reset the value to 0 or use Get_Left_Encoder_Count() on its own as your counter.
Code:

    if (Get_Left_Encoder_Count() >= 12)   
    {
          pwm02 = 127;
    }
    {
          pwm02 = p3_y;
    }


stephenthe1 12-01-2005 15:29

Re: encoders
 
Quote:

Originally Posted by seanwitte
What does the robot do when you move the joystick? Have you tried turning the encoder by hand and printing the value of lec to make sure its being incremented correctly?

Since the call to Get_Left_Encoder_Count() does not reset the internal value, your local variable (lec) will saturate very quickly. Once the encoder moves one tick it will increment lec over 12 almost instantly. You either need to reset the value to 0 or use Get_Left_Encoder_Count() on its own as your counter.


do you mean set the "Get_Left_Encoder_Count()" function variable to 0? so your saying that Get_Left_Encoder_Count()" function variable holds its value every time it interrupts, even if I call it once and then call it again. that is definitely what Mr. Watson seems to have stated in his code. that makes sense, though I don't see how it holds the total value every time it interrupts without being declared as static.

seanwitte 12-01-2005 15:33

Re: encoders
 
Quote:

Originally Posted by stephenthe1
do you mean set the "Get_Left_Encoder_Count()" function variable to 0? so your saying that Get_Left_Encoder_Count()" function variable holds its value every time it interrupts, even if I call it once and then call it again. that is definitely what Mr. Watson seems to have stated in his code. that makes sense, though I don't see how it holds the total value every time it interrupts without being declared as static.

Get_Left_Encoder_Count() is a function that interacts with a static variable in encoder.c named Left_Encoder_Count. Since Left_Encoder_Count is not exposed by encoder.h you can only get the value using the supplied function, Get_Left_Encoder_Count(). To change its value use the function Set_Left_Encoder_Count().

Tom Bottiglieri 12-01-2005 15:36

Re: encoders
 
Since the encoder may have ticked before you hit this block of code, you may want to do something like this.

Code:

//define a variable outside the loop called 'temp1'

if(temp1==0)
{
    temp1=Get_Left_Encoder_Count();
}

static long lec = Get_Left_Encoder_Count() - temp1;

if (lec >= 12)
        {
        pwm02 = 127;                //set it to neutral
        }
        else
        {
        pwm02 = p3_y;
        }


stephenthe1 12-01-2005 16:09

Re: encoders
 
how do I turn on the compressor in the 2004 default code? thanks. (though I'll be using it in Mr. Watson's 2004 encoder code).

Tom Bottiglieri 12-01-2005 16:23

Re: encoders
 
Hook up your compressor to a spike, so that the red lead is attached to the M+ output and so that the black lead is connected to your ground junction. In your code, turn that relay on and off.

So, if you used relay1, the code would look something like this..

Code:

void TurnOnComp(void)
{
    relay1_fwd=1;
}

void TurnOffComp(void)
{
    relay1_fwd=0;
}


stephenthe1 12-01-2005 16:29

Re: encoders
 
um, where do I put those functions at? (which file)

stephenthe1 12-01-2005 16:37

Re: encoders
 
ok, our compressor is hooked up to digital 17. from last year's white sheet with the different wires labeled, it says that the sensor for the relay is hooked up to analog 1. I'm new to the compressor stuff. Is this all of what you need to write the code for it? It is hooked up to a spike. unless this is hard, could you show me the code for this, and tell me where to put it. That would allow me to learn from it as I get confused easily when I have to write things on my own without experience. as soon as you can is best, as I'm at a robotics meeting right now.
thank you for your help,
Stephen

cbolin 12-01-2005 18:19

Re: encoders
 
Stephen,
Regarding your first code posting
Code:

static long lec = 0;            //lec = encoder count
Did someone explain that if you initialize "lec" every time this function is called, it will always be zerod? You could get rid of the 'lec = 0'...that way it would always remember the last value (which is what you were trying to do).

If you already figured this out or I missed this in the above response...just ignore what I said. :-)

Regards,
ChuckB

Greg Ross 12-01-2005 19:52

Re: encoders
 
Quote:

Originally Posted by cbolin
Stephen,
Regarding your first code posting
Code:

static long lec = 0;            //lec = encoder count
Did someone explain that if you initialize "lec" every time this function is called, it will always be zerod? You could get rid of the 'lec = 0'...that way it would always remember the last value (which is what you were trying to do).

Actually, Chuck, because lec is declared static, it will only be initialized once, not every time the function is called.

CJO 12-01-2005 20:34

Re: encoders
 
On the compressor, I would do something like this:

"if (p1_wheel => 127)
{
relay1_fwd = 1
)
else
{
relay1_fwd = 0
)
"

The setup should be as follows:

You should have a joystick connected to port 1. You should have a spike sonnected to relay port 1.

When you rotate the throttle (the big wheel) on the joystick away from you all the way the compressor will turn on, otherwise, the compressor will not turn on.

Greg Ross 12-01-2005 20:45

Re: encoders
 
Quote:

Originally Posted by seanwitte
What does the robot do when you move the joystick?

Stephen,

We're still looking for a description of the symptoms. (I asked too in the "What do you think about how easy theyre making programming?" thread.) All you've told us so far is that "the encoder doesn't work."

Have you tried what Sean suggested?:
Quote:

Originally Posted by seanwitte
Have you tried turning the encoder by hand and printing the value of lec to make sure its being incremented correctly?

You mention you're using Kevin's 2004 encoder code. The version I have has a fairly complete read-me file included in the .zip file. Have you read it? If you don't have the read-me, maybe you should get Kevin's latest version, and use it instead of the old one.

stephenthe1 12-01-2005 21:34

Re: encoders
 
thanks guys. actually, I found out in this thread that the Get_Left_Encoder_Count() function sends its returned value to the "Left_Encoder_Count" variable, which IS static. therefore I don't need the (poorly named) "lec" (left encoder count, but left count is being used for the arm this time, with the tick delta set to +1).

however, all I need for the compressor is to turn it on, but thanks for the wheel idea, as I'll try that out. It'll make testing more practical. thanks again!

seanwitte 12-01-2005 22:36

Re: encoders
 
Quote:

Originally Posted by stephenthe1
thanks guys. actually, I found out in this thread that the Get_Left_Encoder_Count() function sends its returned value to the "Left_Encoder_Count" variable, which IS static. therefore I don't need the (poorly named) "lec" (left encoder count, but left count is being used for the arm this time, with the tick delta set to +1).
however, all I need for the compressor is to turn it on, but thanks for the wheel idea, as I'll try that out. It'll make testing more practical. thanks again!

Don't use the wheel input to control the compressor! The pressure switch output is open (0) when the pressure drops below a threshold, then closes (1)when it hits 120 psi. It stays closed (1) until the pressure drops below the threshold again. That means there is no logic involved in turning the compressor on and off, just grab the value from the switch. Its right there in Default_Routine(). The compressor is connected to relay 8 and the pressure switch to digital input 18:

Code:

relay8_fwd = !rc_dig_in18;  /* Power pump only if pressure switch is off. */
relay8_rev = 0;



All times are GMT -5. The time now is 03:05.

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