Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Library writers >> can't use multiple encoders properly
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1247116103

Message started by fizzer on 07/09/09 at 05:08:23

Title: can't use multiple encoders properly
Post by fizzer on 07/09/09 at 05:08:23
Hey,

Has anyone been able to use the encoder library properly with multiple encoders? I've been trying and for some reason both encoder objects read the same value and respond to pulses from either encoder. It seems to me like both objects are using the same position variable. I'm using inputs 2 & 8 for encoder1 and  inputs 3&15 for encoder2. I've even tried writing different values to each encoder at the beginning of the program  but both end up showing the second written value. I've ruled out circuit problems by commenting out the second encoder but keeping it connected to see that it no longer affects encoder1. Any help or experience with this would be much appreciated  :), I'm trying to use a mouse to track movement of a small robot for a robot competition in a project class.

Title: Re: can't use multiple encoders properly
Post by barragan on 07/10/09 at 04:19:05
which Wiring version are you using and platform?
also please post some code to check the problem, perhaps an image with the connections you're using.
Let me know.

Title: Re: can't use multiple encoders properly
Post by fizzer on 07/10/09 at 23:22:38
I'm using wiring version 0019, on windows, with the wiring I/O board connected to a "shield" circuit board which just buffers all the inputs and outputs. I'm running the outputs from encoders on a mouse through comparators and then to the board. I've probed the outputs that go into the wiring board and verified that they are two sets square waves from 0 to 5V 90 degrees out of phase with their pair.
I'm quite sure that it isn't a hardware malfunction because if i run either of the encoders individually(the other encoder is commented out in code, but still connected) they work perfectly fine and are unaffected by the other encoder. However if i have both encoders defined in code, but only one connected, both values will be affected by the one connected encoder, and the values will stay identical.

here is the code that I'm using to test the encoders, u can see that i begin by setting the encoders to different values, but as soon as I start, they both read 6.

Code:
#include <phys253.h>        //   ***** from 253 template file
#include <LiquidCrystal.h>  //   ***** from 253 template file
#include <Encoder.h>

Encoder mousex;
int x;
Encoder mousey;
int y = 0;

void setup()
{
 portMode(0, INPUT) ;      //   ***** from 253 template file
 portMode(1, INPUT) ;      //   ***** from 253 template file
 mousex.attach(2,8);
 mousey.attach(3,15);
}


void loop()
{
 LCD.clear(); LCD.home();
 LCD.print("press start");
 while(!startbutton());
 mousex.write(0);
 mousey.write(6);
 while(!stopbutton())
 {
   x = mousex.read();
   y = mousey.read();
   LCD.clear(); LCD.home();
   LCD.print("x: "); LCD.print(x);
   LCD.setCursor(0,1);
   LCD.print("y: "); LCD.print(y);    
   delay(100);
 }
}


This is just a thought, but in encoder.h i can see theses declarations :

Code:
class Encoder
{
 private:
   uint8_t _index;
   static volatile uint8_t _pin_a;
   static volatile uint8_t _pin_b;
   static volatile int32_t _position;
   static uint8_t _count;
   static Encoder* _encoders[];
   static void service(void);


Correct me if I'm wrong but aren't static member variables shared by object of the same class in c++?   But i guess the code must be right since it is a library and it must have bin tested thoroughly before being released.  I'm lost with this one.. i might just end up managing the second encoder without library if i can't get it to work soon.

Title: Re: can't use multiple encoders properly
Post by barragan on 07/11/09 at 21:39:08
You're right, there is a bug there, you cna give it a try by changing the line yourself in the library .h code. Then remove the .o files inside the Encoder library and compile any sketch in the Wiring environment, the library will be automatically rebuilt. Please let me know.

Title: Re: can't use multiple encoders properly
Post by fizzer on 07/14/09 at 21:26:43
I've made some changes and got it to work by changing the variables to non-static and adding 4 static member functions for interrupt calls (one for each instance). Then changing the service function to non-static and having the interrupt responders call it.  It could probably be accomplished in a better way, but this works without changing too much and still maintains the same api.

Title: Re: can't use multiple encoders properly
Post by barragan on 07/16/09 at 03:01:00
great, could you please submit your patch, thanbks for looking into it.

Title: Re: can't use multiple encoders properly
Post by fizzer on 07/20/09 at 02:10:02
sure, would u like me to jus post the new files here? or is there another method?

Title: Re: can't use multiple encoders properly
Post by barragan on 07/20/09 at 02:50:26
You can send then over e-mail to hbarragan [at] uniandes.edu.co
thanks again. IŽll check them and add them to the repository as soon as possible. Fix will be included with release 0020.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.