Thread: encoders
View Single Post
  #56   Spotlight this post!  
Unread 08-02-2005, 18:57
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: encoders

Quote:
Originally Posted by stephenthe1
if the pin state hasn't changed, how does it know to not make another tick count? is there a flag, kind of like interrupts that must be cleared before you can register another tick? if so, that's pretty neat.
This is what state variables are for. As an example:

Code:
// assumptions:
//   left encoder phase-A signal goes to rc_dig_in01
//   left encoder phase-B signal goes to rc_dig_in06
//   right encoder phase-A signal goes to rc_dig_in02
//   right encoder phase-B signal goes to rc_dig_in08
 
static long left_encoder_count = 0;
static long right_encoder_count = 0;
static unsigned char prior_left_phase_a_state = 0;
static unsigned char prior_right_phase_a_state = 0;
 
// look for a rising edge on left phase A
if(rc_dig_in01 != prior_left_phase_a_state && rc_dig_in01 != 0)
{
   // get direction information from left phase B
   if(rc_dig_in06 != 0)
   {
	  left_encoder_count++;
   }
   else
   {
	  left_encoder_count--;
   }
}
 
// look for a rising edge on right phase A
if(rc_dig_in02 != prior_right_phase_a_state && rc_dig_in02 != 0)
{
   // get direction information from right phase B
   if(rc_dig_in08 != 0)
   {
	  right_encoder_count++;
   }
   else
   {
	  right_encoder_count--;
   }
}
 
// update state variables
prior_left_phase_a_state = rc_dig_in01;
prior_right_phase_a_state = rc_dig_in02;
Placed in the fast loop, this code will emulate the interrupt-driven code.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org