Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   encoder vs. motor (http://www.chiefdelphi.com/forums/showthread.php?t=31681)

stephenthe1 09-12-2004 15:38

encoder vs. motor
 
I have a robot with a mechanical encoder attatched to an arm with a motor to lift the arm.

the encoder is at 50 resolution
the arm motor is attatched to pwm_02
the encoder is attatched to rc_dig_in06
I calculated that the encoder will "click" or turn on and off 10.7 times for the distance I need.(the encoder is 50 resolution).

basically I need the source so the motor attatched to pwm_02 will stop once the encoder "ticks" 10.7 (or eleven) times. Please keep it simple. I don't have time to go through a tutorial right now. thank you so much for the help.

oh yeah, it would be nice If I could have the arm lift at the touch of a trigger on a joystick attatched to p1_sw_trig. and have the arm go back down at the touch of the trigger again. does the operator interface controller provide a way to simply shut everything down immediately in the event of a disaster?
please help.
thanks,
Stephen

Mike Betts 09-12-2004 15:52

Re: encoder vs. motor
 
Stephen,

I think it would be best for you to attempt to write this code yourself. It is very simple (count to 11)...

If you post your code, I'm sure that one of us will critique it for you before you load it into a robot.

Regards,

Mike

stephenthe1 09-12-2004 15:54

Re: encoder vs. motor
 
can you look at this and tell me if you see any errors.

Code:

        //the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        unsigned int encoder;
        if (p2_sw_trig == 1)
        pwm_02 = 220;
        if (rc_dig_in06 == 1)
        encoder = encoder + 1;
        if (encoder == 11)
        pwm_02 = 128;


Mark McLeod 09-12-2004 16:04

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
can you look at this and tell me if you see any errors.

Code:

        //the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        unsigned int encoder;
        if (p2_sw_trig == 1)
        pwm_02 = 220;
        if (rc_dig_in06 == 1)
        encoder = encoder + 1;
        if (encoder == 11)
        pwm_02 = 128;


Couple of problems, such as,
"encoder" needs to be static or a global, so it retains it's value from call to call.
The check on rc_dig_in06 will increment "encoder" more often than every "tick"
"encoder" might shoot right past 11 and keep going.

But first what model encoder are you using?

seanwitte 09-12-2004 16:05

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
can you look at this and tell me if you see any errors.

Code:

        //the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        unsigned int encoder;
        if (p2_sw_trig == 1)
        pwm_02 = 220;
        if (rc_dig_in06 == 1)
        encoder = encoder + 1;
        if (encoder == 11)
        pwm_02 = 128;


You only want to increment the encoder variable when rc_dig_in06 goes from 0-1 or from 1-0 (pick one). So you need to keep track of the previous value and the current value, then increment only when when it changes.

Mike Betts 09-12-2004 16:05

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
can you look at this and tell me if you see any errors.

Code:

        //the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        unsigned int encoder;
        if (p2_sw_trig == 1)
        pwm_02 = 220;
        if (rc_dig_in06 == 1)
        encoder = encoder + 1;
        if (encoder == 11)
        pwm_02 = 128;


Stephen,

A good start.

First of all, you have not initialized your counter (maybe this is done in your actual code... I don't know).

Second, what happens the next time through the loop? I'm assuming that you will want to perform this maneuver again and again (once again, I may be mistaken). For now, I will assume that this is a one shot deal...

Here is what I think will happen: When the encoder hits 11, the arm is turned off but the encoder micro-switch is still "closed". The count will increase to 12 and the arm will continue to travel. You might want the last test to be "encoder > 11"...

Does this make sense to you?

stephenthe1 09-12-2004 16:23

how about this code.
 
we tried to keep the motor speed down so the encoder will read each tick before the program refreshes. so the motor doesn't "beat" the program before it reads the encoder.

Code:

//the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        static unsigned int encoder;
        static unsigned int pressedornot;
        if (p1_sw_trig == 1)
{
        if (encoder <= 10)
{
    pwm_02 = 150;
        if (rc_dig_in06  == 1)
{
        encoder = encoder + 1;
        if (encoder > 10)
{
        pwm_02 = 127;
}
}
}
}


Mark McLeod 09-12-2004 16:37

Re: how about this code.
 
Could work.

You still need to initialize "static unsigned int encoder= 0;" as Mike mentioned.

I still think you're going to count each encoder tick more than once and your arm will end up short of the expected position.

( I have to pickup my daughter and will check back later)

Mike Betts 09-12-2004 16:51

Re: how about this code.
 
Quote:

Originally Posted by stephenthe1
we tried to keep the motor speed down so the encoder will read each tick before the program refreshes. so the motor doesn't "beat" the program before it reads the encoder.

Code:

//the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        static unsigned int encoder;
        static unsigned int pressedornot;
        if (p1_sw_trig == 1)
              {
          if (encoder <= 10)
                  {
                  pwm_02 = 150;
                  if (rc_dig_in06  == 1)
                    {
                encoder = encoder + 1;
                if (encoder > 10)
                        {
                    pwm_02 = 127;
                        }
                    }
                  }
              }


Stephen,

You can see I attempted to reformat your code to make it more readable. Now you can see how your code nests...

I agree with Mark. I'm not worried about you missing an encoder ping but rather that you will get multiple hits at one ping.

You should consider implementing a state machine. Do you have a mentor on your team (what team are you with anyway?) who can teach you what a state machine is?

stephenthe1 09-12-2004 18:51

Re: encoder vs. motor
 
thanks for the help and tips. I'll have to look into state machines. Today I was in a kind of hurry (which was probably obvious). but now I've got more time to think things through. Our team doesn't really have any mentors for programming. We never have, last year we had a wiz kid, and this year its kinda up to me and another guy. neither of us have much experience. I programmed with c#, but that was with Windows, which tends to behave much differently than a robot :yikes: . our team number is, I'm not sure. we're team "lugnut" though, from Ohio.

stephenthe1 09-12-2004 18:54

Re: how about this code.
 
Quote:

Originally Posted by Mark McLeod
Could work.

You still need to initialize "static unsigned int encoder= 0;" as Mike mentioned.

I still think you're going to count each encoder tick more than once and your arm will end up short of the expected position.

( I have to pickup my daughter and will check back later)

if I initialize it as zero, won't the program simply reset encoder to zero every time it rereads the code?

Mark McLeod 09-12-2004 18:55

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
Our team doesn't really have any mentors for programming. We never have, last year we had a wiz kid, and this year its kinda up to me and another guy. neither of us have much experience.

We'll be your mentors.;) There are quite a few of us to help you out.

Quote:

Originally Posted by stephenthe1
if I initialize it as zero, won't the program simply reset encoder to zero every time it rereads the code?

That declaration only takes effect when the program starts. The use of "static" fixes it permanently in memory and that line will not get executed each time the routine is called. Without the "static" the variable would get initialized each time the routine is called.

stephenthe1 09-12-2004 18:56

Re: encoder vs. motor
 
lol. great! yeah, this chiefdelphi site has been an awesome help.
well, the encoder is an EC202AXXX (with 50 resolution)

stephenthe1 09-12-2004 19:33

Re: encoder vs. motor
 
so like with this code

Code:

//the encoder is at 50 resolution
        //the arm motor is attatched to pwm_02
        //the encoder is attatched to rc_dig_in06
        static unsigned int encoder = 0;
        if (p1_sw_trig == 1)
{
        if (encoder <= 10)
{
    pwm_02 = 254;
        if (rc_dig_in06  == 1)
{
        encoder = encoder + 1;
        if (encoder > 10)
{
        pwm_02 = 127;
}
}
}
}

I assigned a value of zero to encoder, however, every time the code is reread (every 30 milliseconds or something?), the value for encoder will be set back to zero, making its function useless. wish there was a way to make it so the thing didn't reread the code every amount of time, but rather go over it once and remember everything, and do everything from there. that's the way it is in most programming languages isn't it. anyway, do you see what I mean? or will it only assign the value of zero to it once and then when it reanalyzes the code, it will use the last assigned value. I don't think it works that way though.

Mike Betts 09-12-2004 19:35

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
lol. great! yeah, this chiefdelphi site has been an awesome help.
well, the encoder is an EC202AXXX (with 50 resolution)

Steve,

What you have is not a mechanical encoder. It is an optical encoder made by CUI.

Here is a spec sheet: EC202A050A2(S or T)D. As you can see, it comes in two flavors a horizontal or vertical mount.

More importantly, it has a quadrature output so that you can determine direction in your code.

Team 177 used a very similar device last year. I very, very strongly suggest that you download Kevin Watson's example code for optical encoders at http://kevin.org/frc/ and download the file edu_encoder.zip open it up and examine his code very carefully.

What Kevin did is for the edubot. You will have to do the same stuff to the RC code. However, over 50% of the work has been done for you.

It works like a charm. Trust me...


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

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