View Single Post
  #9   Spotlight this post!  
Unread 01-02-2015, 11:41
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: How to implement an encoder with one speed clock, and one direction signal.

From what I'm reading, I'm guessing you're trying to implement an encoder that just counts the # of transitions from 1 to 0 or 0 to 1 off a single digital input, rather than an encoder and its two digital inputs.

In the past, my team has implemented such a thing by just having a fast-spinning loop in a separate thread (task) constantly checking the value of a DigitalInput.
Code:
class SimpleEncoder
{
public:
  SimpleEncoder(DigitalInput* _pDigIn) : pDigIn(_pDigIn),m_last(0) {};
  void ThdProc()
  {
     bool last = false;
     while(true)
     {
       bool thisSample = !!pDigIn->Get();
       if(thisSample != last)
       {
         m_count++;
         last = thisSample;
       }
     }
     Wait(0.001); // you MUST have a wait in here, or else this thread will never let other threads run
  }
  int GetCount() {return m_count;}
  int m_count;
  DigitalInput* pDigIn;
};
I'll leave "how to make new tasks" as an exercise for the reader, since I don't have WPILib in front of me. Also, this implementation won't work for things that are expected to go 01010101 extremely quickly. We usually use it for things that expected to max out at like 5 transitions per second.

Back in the day, you could implement something like this with an interrupt. You might still be able to (and it would be _far_ more efficient than the code above), but I'm not familiar with the (C|robo)Rio method of doing it.
Reply With Quote