Cannot get encoders to work

Hi All -

I am just getting started and have connected two motors with encoders to the core2. You can see the specs on the motors I am using: here. I have connected the encoders as follows:

signalMotor pincore2 pin
V+64
Gnd53
EncB45
EncA36

Here is the code I am using (which is very much based on example code):

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

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

hPIDRegulator myReg;

void setupMotorPID()
{
float kp = 5;
float ki = 0.02;
float kd = 100.0;

// hPID
myReg.setScale(1);
myReg.setKP(kp);
myReg.setKI(ki);
myReg.setKD(kd);
// hRegulator
myReg.dtMs = 5;
myReg.stableRange = 30;
myReg.stableTimes = 3;

hMotB.attachPositionRegulator(myReg);
hMotB.resetEncoderCnt();
}

void cloudTask() {
// sys.delay(5000); //necessary to boot CORE2-ROS (with ASUS Tinker Board), for CORE2 can be commented
platform.begin(&RPi);
//platform.ui.setProjectId(“@@@PROJECT_ID@@@”);
platform.ui.setProjectId(“0c8c31995442bca0”);
platform.ui.configHandler = cfgHandler;

platform.ui.onKeyEvent = (KeyEventType type, KeyCode code) {
if (type == KeyEventType::Pressed) {
if (code == KeyCode::Key_A) {
platform.ui.label(“lb_enc”).setText(“%d”, hMotB.getEncoderCnt());
hMotB.rotRel(90, 100); // rotate DC motor with encoder
platform.ui.label(“lb_enc”).setText(“%d”, hMotB.getEncoderCnt());
}
if (code == KeyCode::Key_Z) {
hMotB.rotRel(0, 0); // rotate DC motor with encoder
LED2.toggle();
}
}
};

while (1) {
sys.delay(500);
LED3.toggle();
auto lb1 = platform.ui.label(“l1”);
lb1.setText(“uptime %u”, sys.getRefTime());
auto lb_enc = platform.ui.label(“lb_enc”);
platform.ui.label(“lb_enc”).setText(“%d”, hMotB.getEncoderCnt());
}
}

void hMain()
{
sys.taskCreate(cloudTask);

setupMotorPID();

while (1) {
  sys.delay(250);
  LED1.toggle();

}
}

Pressing the ‘A’ and ‘Z’ keys will make the motor stop and start, as expected, but it doesn’t seem like the encoders are being read by the software. I would expect the motor to just turn a short distance and then stop, as I am using hMotB.rotrel(). Occasionally, the encoder count on the screen just goes to some large value, like 655360 or 131072, and stays there, so there is something being sensed from the encoder.

Am I missing something conceptually? Or do I have some parameters set wrong for the PID?

Any help getting me started would be appreciated.

Thanks
John

Hi John.

There are few explanation for such a situation:

You can have damaged encoder or you just have another encoder polarity.

Try to use code as simple as it posible without PID regulator first. Try someting like this:

#include "hFramework.h"
#include "hCloudClient.h"

void encCnt()
{
	for (;;)
	{
		printf("%d\n\t", hMotB.getEncoderCnt());
		sys.delay(100);
	}
}

void hMain()
{
	sys.delay(5000);
	platform.begin(&RPi);
	//hMotB.setEncoderPolarity(Polarity::Reversed); // uncommend if polarity is reversed
	sys.setLogDev(&Serial); //defining USB Serial as default one
	sys.taskCreate(encCnt);

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

After that you will know if your encoder work correctly. Let us know about the results.

Regard,
Hubert.