Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpLibrary writers › can't use multiple encoders properly

Page Index Toggle Pages: 1
can't use multiple encoders properly (Read 7705 times)
07/09/09 at 05:08:23

fizzer   Offline
YaBB Newbies

Posts: 4
*
 
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  Smiley, I'm trying to use a mouse to track movement of a small robot for a robot competition in a project class.
 
IP Logged
 
Reply #1 - 07/10/09 at 04:19:05

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
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.
 
IP Logged
 
Reply #2 - 07/10/09 at 23:22:38

fizzer   Offline
YaBB Newbies

Posts: 4
*
 
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);
 }
}
[/code]

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);
[/code]

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.
 
IP Logged
 
Reply #3 - 07/11/09 at 21:39:08

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
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.
 
IP Logged
 
Reply #4 - 07/14/09 at 21:26:43

fizzer   Offline
YaBB Newbies

Posts: 4
*
 
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.
 
IP Logged
 
Reply #5 - 07/16/09 at 03:01:00

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
great, could you please submit your patch, thanbks for looking into it.
 
IP Logged
 
Reply #6 - 07/20/09 at 02:10:02

fizzer   Offline
YaBB Newbies

Posts: 4
*
 
sure, would u like me to jus post the new files here? or is there another method?
 
IP Logged
 
Reply #7 - 07/20/09 at 02:50:26

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
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.
 
IP Logged
 
Page Index Toggle Pages: 1