Recalling values with Shaft encoders

I am trying to go a certain distance with the shaft encoders and then go half that distance I just traveled. I have been able to use the GetEncoder to cont the number of ticks but have not been able to recall that number that I just found.

Can you post your Code??

Usually you assign the Returned Value from GetEncoder() to a Variable, and then you can Print that Value from the Variable.

I can’t seem to post my easy c code but I’m trying to program my bot to find the center of a room with two shaft encoders. So far I can get the encoders to read the distance between the two walls and print that value to the screen, but I don’t know how I would take that value it just printed, take half of that and then make it move that distance. sorry if that’s confusing.

Place your EasyC Project File and Source Code File in a Zip File and Attach them to your Message…

ok here’s the code. The problem is the last while loop. It will print to screen the value of rightencoder and that encoder 1 has started but then it jumps to program finished print to screen block.
CAPS Project part 1.zip (21.2 KB)

The Zip File contains the HEX file of the Compiled and Linked Program and the Log File…

ok how’s this I think I got the right files this time
CAPS Project part 1.1.zip (2 KB)

You Zip File is named “CAPS Project part 1.zip”

Your EasyC Source Code should be “CAPS Project part 1.BDS”
Your EasyC Project File should be “CAPS Project part 1.ECP”

yeah those two are in the zip file I just posted.

Got the right files…

The first thing I see is that you don’t have an Endless While around all your Code. After all the While Conditions are met the program will end.

well the first two while loops work fine its just that it’s skipping that last one and not driving forward until it reaches half the distance it originally drove. My plan is to have it drive forward half the distance after finding the distance between the front and back walls, turn 90 degrees and then start that whole loop over again to find the center of the room the other way.

#include "Main.h"

void main ( void )
{
      int frontleftbumper; 
      int frontrightbumper; 
      int backleftbumper; 
      int backrightbumper; 
      unsigned long leftencoder; 
      unsigned long rightencoder; 
      int frontleftswitch; 
      int frontrightswitch; 

      frontleftbumper = GetDigitalInput ( 5 ) ;
      frontrightbumper = GetDigitalInput ( 6 ) ;
      frontleftswitch = GetDigitalInput ( 7 ) ;
      frontrightswitch = GetDigitalInput ( 8 ) ;
      backleftbumper = GetDigitalInput ( 9 ) ;
      backrightbumper = GetDigitalInput ( 10 ) ;
      while ( frontleftbumper ==1 || frontrightbumper == 1 )
      {
            frontleftbumper = GetDigitalInput ( 5 ) ;
            frontrightbumper = GetDigitalInput ( 6 ) ;
            frontleftswitch = GetDigitalInput ( 7 ) ;
            frontrightswitch = GetDigitalInput ( 8 ) ;
            SetMotor ( 2 , 100 ) ; // left motor forward
            SetMotor ( 1 , 154 ) ; // right motor forward
            if ( (frontleftbumper == 0 || frontleftswitch == 0) && frontrightbumper == 1 )
            {
                  SetMotor ( 2 , 127 ) ;
                  SetMotor ( 1 , 154 ) ;
                  Wait ( 1000 ) ;
            }
            if ( (frontrightbumper == 0 || frontrightswitch == 0) && frontleftbumper == 1 )
            {
                  SetMotor ( 1 , 127 ) ;
                  SetMotor ( 2 , 100 ) ;
                  Wait ( 1000 ) ;
            }
      }
      StartEncoder ( 1 ) ;
      while ( backleftbumper ||  backrightbumper )
      {
            frontleftbumper = GetDigitalInput ( 5 ) ;
            frontrightbumper = GetDigitalInput ( 6 ) ;
            SetMotor ( 1 , 100 ) ;
            SetMotor ( 2 , 154 ) ;
      }
      SetMotor ( 1 , 127 ) ;
      SetMotor ( 2 , 127 ) ;
      rightencoder = GetEncoder ( 1 ) ;
      PrintToScreen ( "The value:%d\n" , (int)rightencoder ) ;
      StopEncoder ( 1 ) ;
      StartEncoder ( 3 ) ;
      PrintToScreen ( "encoder 1 started\n" ) ;
      SetMotor ( 2 , 100 ) ;
      SetMotor ( 1 , 154 ) ;
      while ( leftencoder  < rightencoder/2 )
      {
            PrintToScreen ( "TICK\n" ) ;
            leftencoder = GetEncoder ( 3 ) ;
            rightencoder = GetEncoder ( 1 ) ;
            PrintToScreen ( "car finding half of distance\n" ) ;
      }
      SetMotor ( 1 , 127 ) ;
      SetMotor ( 2 , 127 ) ;
      PrintToScreen ( "program finished\n" ) ;
}


Are you seeing the the “program finished\n” message after your Code Runs??

OH… The Third While…
If the value of leftencoder IS NOT < rightencoder/2, the While will never execute…
You might set them to a known true value, like :


            leftencoder = 0 ;
            rightencoder = 2 ;


You could add a “PrintToScreen ( “left encoder --> %d\n”, leftencoder) ;” and “PrintToScreen ( “right encoder --> %d\n”, rightencoder) ;” near the program finish to see the value before you program quits.

well if I set the one encoder that just ran and has the length of the room = to a certain value like 2 than would i still be able to call back the value it just got.

ok i put those print to screens in and tested it twice and the rightencoder got a certain amount of ticks based on long I waited to push the back buttons but the leftencoder got exactly 4021 both times, so the rightencoder is working correctly but not sure what’s up with leftencoder.

And the Right Encoder is working fine?? That is strange… I also see in your Code, just before the Third While Loop, that you Stop the Encoder on Interrupt #1 (right encoder) and Start the Encoder on Interrupt #3 (left encoder) . And before you Start the Encoder on Interrupt #1, you start using [FONT=Courier New]GetEncoder ( 1 ). [/FONT] I would guess that means that the value of [FONT=Courier New]rightencoder[/FONT] never gets updated in the Third While Loop…



      ******************************************
      **************  Program Here *****************
      *************  << BIG SNIP >>  ****************
      ******************************************

      PrintToScreen ( "The value:%d\n" , (int)rightencoder ) ;
      StopEncoder ( 1 ) ;
      StartEncoder ( 3 ) ;
      PrintToScreen ( "encoder 1 started\n" ) ;
      SetMotor ( 2 , 100 ) ;
      SetMotor ( 1 , 154 ) ;
      while ( leftencoder  < rightencoder/2 )
      {
            PrintToScreen ( "TICK\n" ) ;
            leftencoder = GetEncoder ( 3 ) ;
            rightencoder = GetEncoder ( 1 ) ;
            PrintToScreen ( "car finding half of distance\n" ) ;
      }
      SetMotor ( 1 , 127 ) ;
      SetMotor ( 2 , 127 ) ;
      PrintToScreen ( "program finished\n" ) ;

Thank you for the help I somehow got it to work! I noticed when I changed the third while loop to left encoder > right encoder, the left encoder actually started to count so I put in a fourth while loop right after the third one and did left encoder < right encoder and it skipped the third loop but started counting and then it went right into the fourth loop and the motors stopped when the left encoder hit half the value of the previous right encoder value. but before the third while loop my left encoder was still reading 4102, but it works!

here’s the new code if you want to take a look.
CAPS Project part 1.zip (2.18 KB)

Thanks!!!

So the Fourth While Loop is running, but not the Third… That means that


      while ( leftencoder  > rightencoder/2 )

is FALSE

and


      while ( leftencoder  > rightencoder/2 )

is TRUE

(Third and Fourth while Loops )


      while ( leftencoder  > rightencoder/2 )
      {
            PrintToScreen ( "TICK\n" ) ;
            leftencoder = GetEncoder ( 2 ) ;
            rightencoder = GetEncoder ( 1 ) ;
      }
      while ( leftencoder < rightencoder / 2 )
      {
            PrintToScreen ( "2nd loop started\n" ) ;
            leftencoder = GetEncoder ( 2 ) ;
            rightencoder = GetEncoder ( 1 ) ;
            PrintToScreen ( "car finding half of distance\n" ) ;
      }


Did you expect the Condition of the Third Loop to be True, or the Condition of the Fourth Loop to be True???

You should have some expectation about what your program will do at each point. Otherwise you will test and record your data, to learn what is happening.

Full Program

#include "Main.h"

void main ( void )
{
      int frontleftbumper; 
      int frontrightbumper; 
      int backleftbumper; 
      int backrightbumper; 
      unsigned long leftencoder; 
      unsigned long rightencoder; 
      int frontleftswitch; 
      int frontrightswitch; 

      frontleftbumper = GetDigitalInput ( 5 ) ;
      frontrightbumper = GetDigitalInput ( 6 ) ;
      frontleftswitch = GetDigitalInput ( 7 ) ;
      frontrightswitch = GetDigitalInput ( 8 ) ;
      backleftbumper = GetDigitalInput ( 9 ) ;
      backrightbumper = GetDigitalInput ( 10 ) ;
      while ( frontleftbumper ==1 || frontrightbumper == 1 )
      {
            frontleftbumper = GetDigitalInput ( 5 ) ;
            frontrightbumper = GetDigitalInput ( 6 ) ;
            frontleftswitch = GetDigitalInput ( 7 ) ;
            frontrightswitch = GetDigitalInput ( 8 ) ;
            SetMotor ( 2 , 100 ) ; // left motor forward
            SetMotor ( 1 , 154 ) ; // right motor forward
            if ( (frontleftbumper == 0 || frontleftswitch == 0) && frontrightbumper == 1 )
            {
                  SetMotor ( 2 , 127 ) ;
                  SetMotor ( 1 , 154 ) ;
                  Wait ( 1000 ) ;
            }
            if ( (frontrightbumper == 0 || frontrightswitch == 0) && frontleftbumper == 1 )
            {
                  SetMotor ( 1 , 127 ) ;
                  SetMotor ( 2 , 100 ) ;
                  Wait ( 1000 ) ;
            }
      }
      StartEncoder ( 1 ) ;
      while ( backleftbumper ||  backrightbumper )
      {
            backleftbumper = GetDigitalInput ( 9 ) ;
            backrightbumper = GetDigitalInput ( 10 ) ;
            SetMotor ( 1 , 100 ) ;
            SetMotor ( 2 , 154 ) ;
      }
      SetMotor ( 1 , 127 ) ;
      SetMotor ( 2 , 127 ) ;
      rightencoder = GetEncoder ( 1 ) ;
      PrintToScreen ( "The value:%d\n" , (int)rightencoder ) ;
      StopEncoder ( 1 ) ;
      PrintToScreen ( "%d\n" , (int)leftencoder ) ;
      StartEncoder ( 2 ) ;
      PrintToScreen ( "encoder 1 started\n" ) ;
      SetMotor ( 2 , 100 ) ;
      SetMotor ( 1 , 154 ) ;
      while ( leftencoder  > rightencoder/2 )
      {
            PrintToScreen ( "TICK\n" ) ;
            leftencoder = GetEncoder ( 2 ) ;
            rightencoder = GetEncoder ( 1 ) ;
      }
      while ( leftencoder < rightencoder / 2 )
      {
            PrintToScreen ( "2nd loop started\n" ) ;
            leftencoder = GetEncoder ( 2 ) ;
            rightencoder = GetEncoder ( 1 ) ;
            PrintToScreen ( "car finding half of distance\n" ) ;
      }
      StopEncoder ( 2 ) ;
      PrintToScreen ( "leftencoder stopped%d\n" , (int)leftencoder ) ;
      SetMotor ( 1 , 127 ) ;
      SetMotor ( 2 , 127 ) ;
      PrintToScreen ( "program finished\n" ) ;
      PrintToScreen ( "leftencoder-->%d\n" , (int)leftencoder ) ;
      PrintToScreen ( "rightencoder - - >%d\n" , (int)rightencoder ) ;
}


I’m not even sure why it needs that third loop but for some reason it does I guess. Heres what I was getting from the terminal window:

value right encoder was x(certain number depending on how long wheels spun)
Value left encoder was 4012
Third loop executed ( leftencoder < rightencoder/2)
Value right was still x
Value left was 4012

value right encoder was x(certain number depending on how long wheels spun)
Value left encoder was 4012
Third loop executed ( leftencoder > rightencoder/2)
Value right was still x
Value left was 1

value right encoder was x(certain number depending on how long wheels spun)
Value left encoder was 4012
Third loop executed ( leftencoder > rightencoder/2)
fourth loop executed ( leftecoder < rightencoder/2)
Value right was still x
Value left was x/2

So for some reason it needed that third while loop to start counting from 1.

Can you post your New Code, the Code that generated the above Output??

well the three outputs i got are all different. I downloaded one code and got the first one, then downloaded a new code with the added loops and tested, etc.
I’m actually done with my project and my little robot can find the middle of a room fairly accurately only using optical shaft encoders as the measuring device. I have to fix some mechanical problems because it doesn’t drive as straight as I need it to, and other than the turning being slightly off, it works pretty well.
Oh and I think I finally figured out why I needed that third loop. I put print to screen blocks at the very top of my code and got the value of both the left and right encoders. Whenever I turn on my robot the left encoder reads 4102 and the right encoder reads 256 without me doing anything. So if I put a while loop that’s condition is ( leftencoder > rightencoder) it kicks the left encoder to 0 and then goes to the next line of code. That’s the best explanation I can come up with.