Encoders incrementing regardless of motor direction

Hi All -

I’ve been experimenting with the encoders on my motors. What seems strange to me is that the value always increments, no matter which direction the motor is turning. Is this correct? It doesn’t seem like this would work correctly in a PID controller. I’d appreciate some guidance on this topic.

Thanks
John

#include “hFramework.h”
#include “hCloudClient.h”

void cfgHandler()
{
platform.ui.loadHtml({Resource::URL, “/ui.html”});
}

void encCnt()
{
for( ; ; )
{
platform.ui.label(“lb_enc”).setText(“%d”, hMotB.getEncoderCnt());
platform.ui.label(“l1”).setText(“uptime %u”, sys.getRefTime());
sys.delay(100);
}
}

void hMain()
{

sys.delay(5000);
platform.begin(&RPi);
sys.taskCreate(encCnt);
platform.ui.setProjectId("0c8c31995442bca0");
platform.ui.configHandler = cfgHandler;

while (1)
{
    hMotB.setPower(300);
    sys.delay(1000);
    LED1.toggle();
    hMotB.setPower(-300);
    sys.delay(1000);
    LED1.toggle();
}

}

Hi John.

Probably it’s not a problem in code, I think that your 2 channels of encoder output are shorted. In this case value of getEncoderCnt() will be increment to +inf or decrement to -inf. Please try with another encoder, it should work perfectly fine.

Regard,
Hubert.

Hi Hubert -

Thanks very much for the quick answer.

The encoder count is not going to -inf or +inf. It is incrementing by about 8,500,000 in a one second of motion. I get a similar result on another channel (channel A) with another motor. Any ideas?

Thanks
John

Hi John.

It may be caused by disturbances produced by the motor. Try to rotate your encoder by hand without motor control, or without connecting out A and out B pins, and observe the changes of value returned by getEncoderCny().

Regard,
Hubert.

Hi Hubert -

I finally figured this out. The pinout for the encoder on the motor’s info page is wrong. The pins are reversed. Now I am getting some results that make sense from the encoders.

However, I am having some other difficulties with the hPIDRegulator which I will post on a new thread.

Thanks very much for your help.