Forum >Problem with Wheel Encoder SEN0038 [Resolved]
ArduinoGeneral

Problem with Wheel Encoder SEN0038 [Resolved]

userHead NooTe94 2012-01-16 09:57:24 9294 Views5 Replies
Hi,
I add these wheel encoders [url=http://www.dfrobot.com/index.php?route=product/product&product_id=98]http://www.dfrobot.com/index.php?route=product/product&product_id=98[/url] in my DC motor on DFR Pirate-4WD mobile plateform [url=http://www.dfrobot.com/index.php?route=product/product&filter_name=4wd&product_id=97]http://www.dfrobot.com/index.php?route=product/product&filter_name=4wd&product_id=97[/url].
I get correct values from encoders at low speed, but not a high speed.

Have you an idea ?
I've try to use attachInterrupt() function too... but with no success :(

This is my current code (I've remove motor and others stuffs coding ^^).
[code]
const float _mywheel_step = (3.1428f*6.5f)/20.0f; // get centimeter value
int _encoder_step_left=0;
int _encoder_step_right=0;
int _old_state_left=0;
int _old_state_right=0;
void reinit_encoders()
{
_encoder_step_left = _encoder_step_right = 0;
_old_state_right=digitalRead(encoder_motor_right);
_old_state_left=digitalRead(encoder_motor_left);
}
void setup_encoders(void)
{
pinMode(encoder_motor_right, INPUT);
pinMode(encoder_motor_left, INPUT);
reinit_encoders();
}
void read_encoders(void)
{
if(get_encoder_step(encoder_motor_right, _old_state_right))
{
_encoder_step_right++;
}

if(get_encoder_step(encoder_motor_left, _old_state_left))
{
_encoder_step_left++;
}
}
boolean get_encoder_step(const int iEnc, int &old_dr)
{
int dr=digitalRead(iEnc);
if(dr!=old_dr)
{
old_dr = dr;
return true;
}

return false;
}
void setup()
{
setup_encoders();
}
void loop()
{
read_encoders();
}
[/code]
2012-01-20 18:42:59 Great!


Please come back and share your project with us when its finished, or if you need more help!


Happy Hacking!
userHeadPic Hector
2012-01-20 04:36:52 Very nice, exactly what i needed !

I've just read attachInterrupt() function documentation on Arduino site. I've see my error then a try to attachInterrupt (need to pin encoder on specific input on card).

Thanks a lot :)
userHeadPic NooTe94
2012-01-20 02:26:00 Hi Noote94,


We have created a sample code for these encoders. I will update the product page and wiki soon.


here is the code for you


[code]

// #
// # Editor    : Lauren from DFRobot
// # Date      : 17.01.2012




// # Product name: Wheel Encoders for DFRobot 3PA and 4WD Rovers
// # Product SKU : SEN0038




// # Description:
// # The sketch for using the encoder on the DFRobot Mobile platform




// # Connection:
// #        left wheel encoder  -> Digital pin 2
// #        right wheel encoder -> Digital pin 3
// #








#define LEFT 0
#define RIGHT 1




long coder[2] = {
  0,0};
int lastSpeed[2] = {
  0,0}; 








void setup(){
 
  Serial.begin(9600);                            //init the Serial port to print the data
  attachInterrupt(LEFT, LwheelSpeed, CHANGE);    //init the interrupt mode for the digital pin 2
  attachInterrupt(RIGHT, RwheelSpeed, CHANGE);  //init the interrupt mode for the digital pin 3
 
}




void loop(){
 
  static unsigned long timer = 0;                //print manager timer
 
  if(millis() - timer > 100){                 
    Serial.print("Coder value: ");
    Serial.print(coder[LEFT]);
    Serial.print("[Left Wheel] ");
    Serial.print(coder[RIGHT]);
    Serial.println("[Right Wheel]");
   
    lastSpeed[LEFT] = coder[LEFT];  //record the latest speed value
    lastSpeed[RIGHT] = coder[RIGHT];
    coder[LEFT] = 0;                //clear the data buffer
    coder[RIGHT] = 0;
    timer = millis();
  }
 
}








void LwheelSpeed()
{
  coder[LEFT] ++;  //count the left wheel encoder interrupts
}








void RwheelSpeed()
{
  coder[RIGHT] ++; //count the right wheel encoder interrupts
}
[/code]
userHeadPic Hector
2012-01-20 00:24:21 Hi Hector,

thanks for doc (appreciate the buzzer sample code very fun I will add it in my robot ;))... but this is not resolving to may problem :p

All samples I see for encoders use attachInterrupt() arduino function. This is working for quadrature encoders (one analogic and two numeric pins for one encoder). But DFRobot encoders have only numeric pin ! I havent found a code sample which use theses particular encoder style.

In that sample, it use ISR for a timer fonctionality. It's not what I seek for : I try to attach an interupt to a raison signal (analogic or numeric) for counting encoder step.
I dont need quadrature (because I already know what direction take of my robot).
I need something like a tachymeter functionnality :)

This is strange than DFRobot dont give code sample for theses encoders :(
userHeadPic NooTe94
2012-01-16 19:28:44 Hi NooTe94,


You might want to take a look at this wiki:[url=https://www.dfrobot.com/wiki/index.php?title=4WD_MiniQ_Complete_Kit_(SKU:ROB0050)]https://www.dfrobot.com/wiki/index.php?title=4WD_MiniQ_Complete_Kit_(SKU:ROB0050)[/url]


at the bottom there is a link to additional sample codes. There is a sample code for some motor encoders using the Arduino's interrupts, while it may not be exactly what you need, it should point you in the right direction.


I think digital pins are not a good option for motor encoders since they will not be fast enough to react to the motor's spin...That's probably why you are not getting good readings at higher speeds...The digital pins can only be read during their allotted function time, while the interrupts can keep count throughout the whole program's process.
userHeadPic Hector