Go to Post Mentor: You must first learn patience... Student (impatiently): How long is that going to take? - kaszeta [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 14-11-2008, 17:15
pogenwurst pogenwurst is offline
Ubuntu User
AKA: Brian
FRC #2007 (Robots of the Round Table)
Team Role: Leadership
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Duluth, GA
Posts: 78
pogenwurst is on a distinguished road
Send a message via AIM to pogenwurst
Troublesome encoder ISR

I'm having trouble with the encoder interupt routine I wrote yielding incorrect results. The tick count increases overall, but along the way the count "rolls back" despite the shaft turning in the same direction.

Would anyone care to make a quick check of it for me? It may just be a silly mistake, but I've gone over it again and again and still can't see where I've gone wrong.

A little background:
-> I'm only using one encoder, and resolution is very important for the task at hand, so I'm using the interrupt on digital inputs 3-6 (channel A is in 3, channel B is in 4) so that I can count the ticks for both channels and on both rising and falling transitions.
-> This is on a Vex controller, so I'm using the IFI code, not Kevin's.
-> The following code is contained within the appropriate conditional branch for interrupts 3-6.
-> "encoderCount" is a global volatile signed long, and oldPORTB is a global unsigned char (I use it nowhere but the ISR, so it needn't be indicated as volatile, correct?).

Code:
	const unsigned char aMask = 0x10;
	const unsigned char bMask = 0x20;

	unsigned char a = (PORTB & aMask) == aMask;
	unsigned char b = (PORTB & bMask) == bMask;

	if((PORTB ^ oldPORTB) == aMask)
	{
		if(a)						// a transitioned high
		{
			if(b)					// b is high
				++encoderCount;
			else					// b is low
				--encoderCount;
		}

		else						// a transitioned low
		{
			if(!b)					// b is low
				++encoderCount;
			else					// b is high
				--encoderCount;
		}
	}

	else if((PORTB ^ oldPORTB) == bMask)
	{
		if(b)						// b transitioned high
		{
			if(!a)					// a is low
				++encoderCount;
			else					// a is high
				--encoderCount;
		}

		else						// b transitioned low
		{
			if(a)					// a is high
				++encoderCount;
			else					// a is low
				--encoderCount;
		}
	}

	oldPORTB = PORTB;

	INTCONbits.RBIF = 0;     /*     and clear the interrupt flag         */
Thanks for any help you can offer
  #2   Spotlight this post!  
Unread 14-11-2008, 18:37
Andrew Schreiber Andrew Schreiber is offline
Joining the 900 Meme Team
FRC #0079
 
Join Date: Jan 2005
Rookie Year: 2000
Location: Misplaced Michigander
Posts: 4,069
Andrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond repute
Re: Troublesome encoder ISR

How fast are they spinning? What COULD be happening is you are spinning them fast enough that when you check for direction (I assume Quadrature Encoders based on a and b in your code) you have already transitioned too far. The only real solution would be slow it down. Turn the encoders by hand to see if this is happening then too. Hope this helps.
__________________




.
  #3   Spotlight this post!  
Unread 19-11-2008, 17:27
pogenwurst pogenwurst is offline
Ubuntu User
AKA: Brian
FRC #2007 (Robots of the Round Table)
Team Role: Leadership
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Duluth, GA
Posts: 78
pogenwurst is on a distinguished road
Send a message via AIM to pogenwurst
Re: Troublesome encoder ISR

They're spinning pretty slowly -- the 'bot takes several seconds to move a meter, and the shaft it's on has a sprocket quite a bit larger than that on the wheels' shaft (48 teeth encoder/motor shaft : 15 teeth wheel shaft, IIRC), which slows down the tick rate on the encoder even more if I'm not mistaken.

Also, when my ISR is simply "++encoderCount", it accurately counts the number of ticks (but without sense of direction, obviously). My direction-aware ISR isn't enormously complicated; it doesn't seem like enough to make it miss ticks, though I certainly could be mistaken.

Next time I have access to the machine I'll experiment with turning the shaft, though.

Thank you for your reply. I'll let you know if I figure it out.
  #4   Spotlight this post!  
Unread 19-11-2008, 22:05
Mr. Lim Mr. Lim is offline
Registered User
AKA: Mr. Lim
no team
Team Role: Leadership
 
Join Date: Jan 2004
Rookie Year: 1998
Location: Toronto, Ontario
Posts: 1,125
Mr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond repute
Re: Troublesome encoder ISR

I know this will only give you 1/4 the resolution you want, but does at least this work?

Code:
	const unsigned char aMask = 0x10;
        const unsigned char bMask = 0x20;

	unsigned char a = (PORTB & aMask) == aMask;
        unsigned char b = (PORTB & bMask) == bMask;


	if((PORTB ^ oldPORTB) & aMask == aMask)
	{
		if(a)						// a transitioned high
		{
			if(b)					// b is high
				++encoderCount;
			else					// b is low
				--encoderCount;
		}
	}

	oldPORTB = PORTB;

	INTCONbits.RBIF = 0;     /*     and clear the interrupt flag         */
If this does work, then chances are it's an ISR timing problem. If this doesn't work, then it's likely a logic issue that's not obvious to us. Either way, it'd help us narrow down the nature of the beast.

-Shawn T. Lim...
__________________
In life, what you give, you keep. What you fail to give, you lose forever...

Last edited by Mr. Lim : 19-11-2008 at 22:09.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Encoder help E_Unit Electrical 5 16-02-2007 11:02
Encoder Death rfolea Programming 8 14-02-2007 14:06
Encoder Problems bjimster1 Programming 2 10-02-2007 08:04
Encoder Hookup kutty18 Electrical 4 22-01-2005 12:17
encoder.c Gary Bonner Programming 2 14-01-2005 13:41


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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