Pages: [1] 2   Go Down
Author Topic: Interrupts and PWM/DigitalWrite  (Read 3179 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I have a problem. I am trying to use my rotary encoders and interrupts. Whenever I try to use interrupts to count the number of rotation hits, the value is totally wrong.  If I rotate the wheel by hand the value is correct but when I use the analogWrite function for PWM, the value is way higher then it should be.  I've even tried using the digitalWrite function for 100% duty cycle PWM but I get the same result, an incorrect count value for the encoders.  I am using DFRobot encoders: http://www.dfrobot.com/index.php?route=product/product&product_id=98
The resolution is: 20 PPR.

here is my code:

#define encoder1PinB  3
#define rightMotorIn 5
#define rightMotorOut 6
volatile unsigned int encoder1Pos = 0;
int dutyCycleRight = 200;

void setup() {
  pinMode(encoder1PinB, INPUT);
  digitalWrite(rightMotorOut, LOW);
  attachInterrupt(1, doEncoder1, RISING);
  Serial.begin (9600);
  Serial.println("start");                // a personal quirk

}

void loop(){
  analogWrite(rightMotorIn, dutyCycleRight);
}

void doEncoder1() {
    encoder1Pos++;
    Serial.print(encoder1Pos);
    Serial.print('\n');
}


Any help would be awesome!

Thanks!
Logged

Australia
Offline Offline
Full Member
***
Karma: 8
Posts: 161
You dont have to touch powerlines for them to kill you, even looking at them for too long will make them angry.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you tell me exactly what you have connected the encoder to? Also, how fast are you spinning the encoder?
Logged

"Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent di

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 129
Posts: 8528
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd start by moving

Serial.print(encoder1Pos);
Serial.print('\n');

out of the ISR or up the buad rate to 115200.

You shouldn't be printing inside an ISR but for a quick test it should be OK as long as the print is fast enough. At 9600bps I think you may be blocking for too long when using the PWM.

Also

analogWrite(rightMotorIn

is that the correct pin? And I don't see a pinMode (rightMotorOut, OUTPUT);


______
Rob
« Last Edit: February 19, 2011, 09:37:07 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks guys,

i'm using DFRobot 6V motors http://www.robotshop.ca/dfrobot-6v-180-rpm-micro-dc-geared-motor-with-back-shaft.html
I don't think they are going faster than the Arduino can handle as they have a top RPM of 180.

I'll try upping the baud rate.

The 3 wires of the encoders are Vcc, Grd, and Signal wired up to Vcc, Grd and pin 3 respectively.
Logged

Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The interrupt itself can handle the speed of 120*180 pps. I built a freq counter using interrupts and it bottomed at 30+ kHz (which leads me to suspect I used an INT for counting, but anyways you should have a margin)

Why not put that *sdfs* out of the interrupt sub and into the main loop instead and do something like this instead.

void doEncoder1() {
    encoder1Pos++;
pulse_read =true ;
                         }

And in the main loop something like this:
if (pulse_read =true) (
Serial.print(encoder1Pos);
    Serial.print('\n');
pulse_read =false;
                             (

Then you should be confident that the interrupt routine is not to slow and does not miss a pulse




Logged

Australia
Offline Offline
Full Member
***
Karma: 8
Posts: 161
You dont have to touch powerlines for them to kill you, even looking at them for too long will make them angry.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What method is the encoder? is it optical?
Logged

"Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent di

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup optical/rotary encoder
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have the very same Problem. Did some of the above solve the problem? For me it does not.

I am actually using 2 encoders for left and right wheel of my 2WD plattform. Using DFRobots Romeo Board, the one (connected to ISR0) is giving me the actual result, the one connected to ISR1 is giving like double the updates. And as mentioned the posts earlier all of this is only happening when the motors are driven through PWM. Turning the wheels by hand both encoders give me the right result.

So i thought that this might has to do something with the digital pin 3 being PWM enabled.
I tried to disable PWM on digital Pin 3 ( TCCR2A &= ~(_BV(COM2B1)); ), but that does not seem to be the problem.

Any suggestions?

Alex
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same here!

Using exactly the same encoders and the same motors. I've already created a new topic (http://arduino.cc/forum/index.php/topic,68166.0.html) but then i realized i had exactly the same problem as you guys.

I already tried to put the serial prints out of the interruption and all that stuff but still my the readings are completely wrong.

Have you found any solution to this problem?

Cheers,

Víctor.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same problem here!  smiley-roll-sweat

I am also using DFR Robot Wheel Encoders. http://www.dfrobot.com/index.php?route=product/product&product_id=98

And, of course, when I slowly turn wheels by hand everything is ok, but when I try to turn it more rapidly readings go crazy. I get too big numbers, like 200 for one rotation of the wheel and normal resolution is 20 PPR. I tried all interrupt modes and there is only counter incrementation in interrupt functions but still no success.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear all,

I'm experiencing same problem explained here several months ago regarding DFRobot rotary encoders not properly working when powering PWM motors. By any chance, did you find any solution to this issue?

I'll appreciate very much any hint on this topic!

Many thanks in advance,
roverico
Logged

Shanhghai
Offline Offline
Newbie
*
Karma: 0
Posts: 26
DFRobot Tech Support
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear all:

Sorry to spot this thread so late. We will look into this issue and should be able to provide a solution to this.
Give us few days to get it solved.

Logged

DFRobot-Drive The Future
Official Technical Support for DFRobot Product
www.DFRobot.com

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Has there been a resolution of this issue yet? I am in the midst of teaching a HS robotics course and just ran headlong into this problem, also. I wish I had seen this post last summer, but alas... So, now I have an interesting curricular problem. Sigh.

Any work-arounds that have proven useful?

Thanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I posted a support ticket with DFRobot... here is the response:

Your ticket, "[0138] Interrupts and PWM/DigitalWrite", has been replied to, by Ricky.

====Begin Comment====
Hi:

Sorry for not following this issue.  We are in holiday until 8th Oct. I will make sure that this issue will be followed and at least a test report will be provided.
====End Comment====

You can read the reply here:
http://www.dfrobot.com/forum/index.php?action=helpdesk;sa=ticket;ticket=138.msg614#msg614

Regards,
The DFRobot Forum  Team.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I have a problem. I am trying to use my rotary encoders and interrupts. Whenever I try to use interrupts to count the number of rotation hits, the value is totally wrong.  If I rotate the wheel by hand the value is correct but when I use the analogWrite function for PWM, the value is way higher then it should be.  I've even tried using the digitalWrite function for 100% duty cycle PWM but I get the same result, an incorrect count value for the encoders.  I am using DFRobot encoders: http://www.dfrobot.com/index.php?route=product/product&product_id=98
The resolution is: 20 PPR.

here is my code:

#define encoder1PinB  3
#define rightMotorIn 5
#define rightMotorOut 6
volatile unsigned int encoder1Pos = 0;
int dutyCycleRight = 200;

void setup() {
  pinMode(encoder1PinB, INPUT);
  digitalWrite(rightMotorOut, LOW);
  attachInterrupt(1, doEncoder1, RISING);
  Serial.begin (9600);
  Serial.println("start");                // a personal quirk

}

void loop(){
  analogWrite(rightMotorIn, dutyCycleRight);
}

void doEncoder1() {
    encoder1Pos++;
    Serial.print(encoder1Pos);
    Serial.print('\n');
}


Any help would be awesome!

Thanks!


Hi,

I have used the sample code to test my encoder with my DFRobot Romeo and it works fine.
Here's a list of what I'm using.
1. Arduino IDE 1.01
2. DFRobot Romeo controller
3. Same encoders from DFRobot

Generally speaking, if you could get the right feedback from the encoders, when turning the wheels by hand. But it's not working properly when running the motors. It means that your power supply is not strong enough. So the working voltage of your system is not stable @5v(maybe lower than 4v). Then the sensor can't work properly. Then you get the wrong interrupts.

1.So I suggest you test your 4wd/2wd with the external power supply such as 7.4v battery or 7v power adapter to supply power for your Arduino board. The max current of usb is just 800mA. So the single power supply from USB is not suitable for testing the sensors when running the motors.
2.Test the working voltage of the encoders to avoid the power supply problems.




 smiley
This's my connection pictures.


* 2012-10-09 15-19-50.145.jpg (519.37 KB, 1280x720 - viewed 29 times.)
Logged

Pages: [1] 2   Go Up
Jump to: