Motors do not reverse/stop on core2mini, works on core2

Hi All -

I hope you can help me with this. I have some code that works fine on the core2, but does not on the core2mini.

Specifically, the motors do not reverse on the core2mini, while they do on the core2. For example, to test this, I set up some code that turns the motor one direction when the ‘A’ key is pressed, it turns the other way when the ‘S’ key is pressed, and it stops when the ‘Z’ key is pressed. (The other motor reacts to the ‘J’, ‘K’ and ‘M’ keys respectively.)

Also, on the core2, the motor stops when it has moved the desired amount, while on the core2mini, the motor doesn’t stop until I press the ‘Z’ key.

On the core2mini, however, pressing the ‘A’ and ‘S’ keys, the motor turns the same direction. Can someone tell me why this might be?

The hardware setup is the core2mini on a RPi3 with two motors w/ encoder attached.

Here is the code:

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

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

hPIDRegulator myRegA;
hPIDRegulator myRegB;

void setupMotorPID()
{
    float kp = 40.0;
    float ki = 0.05;
    float kd = 1000.0;
    
    // hPID
    myRegA.setScale(1);
    myRegA.setKP(kp);
    myRegA.setKI(ki);
    myRegA.setKD(kd);
    myRegB.setScale(1);
    myRegB.setKP(kp);
    myRegB.setKI(ki);
    myRegB.setKD(kd);

    // hRegulator
    myRegA.dtMs = 5;
    myRegA.stableRange = 10;
    myRegA.stableTimes = 3;
    myRegB.dtMs = 5;
    myRegB.stableRange = 10;
    myRegB.stableTimes = 3;
    
    hMotA.attachPositionRegulator(myRegA);
    hMotA.resetEncoderCnt();
    hMotB.attachPositionRegulator(myRegB);
    hMotB.resetEncoderCnt();
}

void encCnt()
{
	for(;;)
	{
	    platform.ui.label("lb_enc_a").setText("%d", (int)hMotA.getEncoderCnt());
	    platform.ui.label("lb_enc_b").setText("%d", (int)hMotB.getEncoderCnt());
 	    platform.ui.label("l1").setText("uptime %u", (unsigned int)sys.getRefTime());
	    uint16_t v_int = hSens1.pin1.analogReadRaw();
	    platform.ui.label("lb_sensor").setText("%d", v_int);
		sys.delay(100);
	}
} 

void cloudTask() {
    platform.begin(&RPi);
	hMotA.setEncoderPolarity(Polarity::Reversed);
	hMotB.setEncoderPolarity(Polarity::Reversed);
    platform.ui.setProjectId("@@@PROJECT_ID@@@");
    platform.ui.configHandler = cfgHandler;
    
    platform.ui.onKeyEvent = [](KeyEventType type, KeyCode code) {
        if (type == KeyEventType::Pressed) {
            if (code == KeyCode::Key_A) {
                hMotA.rotRel(3840, 200);
            }
            if (code == KeyCode::Key_S) {
                hMotA.rotRel(-3840, 200);
            }
            if (code == KeyCode::Key_Z) {
                hMotA.rotRel(0, 0);
            }
            if (code == KeyCode::Key_J) {
                hMotB.rotRel(3840, 200);
            }
            if (code == KeyCode::Key_K) {
                hMotB.rotRel(-3840, 200);
            }
            if (code == KeyCode::Key_M) {
                hMotB.rotRel(0, 0);
            }
        }
    };
    
    while (1) {
        sys.delay(500);
        LED3.toggle();
    }
}

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

    setupMotorPID();
    
    while (1) {
        LED1.toggle();
    }
}

Thanks in advance for your help.

Hi jsmarion,

We will investigate the issue and let you know about fix as soon as it possible :wink:

Regards,
Hubert

Many thanks for your help!

Hi Hubert -

I did an apt-get update and apt-get upgrade to the RPi and restarted it (because it would no longer respond to the web interface), and now I get very odd behavior.

After doing this, both motors only move (and only in one direction) when I press one set of the ‘go’ keys, either A/S or J/K. It seems random which set works. I can press either Z or M to stop them. If I press Z to stop them, only the A and S keys work to make them move again. If I press the M key to stop them, then only the J and K keys will make them move again. In other words, the ‘stop’ key used to stop the motors determines which ‘go’ keys make them move again.

Both the A and B encoders are registering the movement of their respective motors.

After a few minutes, the web interface stops working again.

I would sure appreciate a fix for this.

Thanks,
John

Hi John,

We still investigate the issue. It turned out that the problem is on really low programming level, so we need some more time to fix it. I am very sorry and apologize for inconvenience.

Regards,
Hubert

Thanks for your help. Please let me know if you need anything from me.

Hi Hubert,
Cloud’s Motor Basic template used to test CORE2mini
Typing A, motor runs, but after half a minute , motor stops and schocking.

Afterthat CORE2mini gets offline and remains.
Powering down and up, and the above process can be repeated.

I have used the stable setting.
Let me know if I can be of any help.
Regards,
Fred

Hi Fred,

As I wrote in the previous post, we have not yet fixed the problem. The amendments will appear at the beginning of next week. Once again I apologize for the inconvenience.

Regards,
Hubert

Hi - has there been any progress on this?

Hi Husarion-Team,
Core2mini motors functions are very unstable.and weird.
I can not get my robotcar properly working…help
I have tried both Development and the stable version, no luck.
Here is my code to test both motors:
Regards,
Fred

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

void cfgHandler()
{
//console
auto cl1 = platform.ui.console(“cl1”);
cl1.setLocation(800, 160); //horizontal, vertical

platform.ui.loadHtml({Resource::URL, "/ui.html"});
platform.ui.video.enable();

}

//=============================================================================
void status_task()
{
while (1) {
platform.ui.console(“cl1”).printf("\r\nL_Mtr enc = %d", (int)hMotA.getEncoderCnt());
platform.ui.console(“cl1”).printf("\r\nR_Mtr enc = %d", (int)hMotB.getEncoderCnt());
sys.delay(2000);
}
} // end of status_task()

void ralayTask()
{
auto& relayPin = hSens1.pin2;
while (1) {
relayPin.toggle();
sys.delay(1000);
}
}

void servoTask()
{
hServoModule.enablePower();

//move servo right and left

while(1) {
    hServoModule.servo1.setWidth(1700); //set pulse width 1700 us
    sys.delay(1000);
    hServoModule.servo2.setWidth(2200);
    sys.delay(1000);
}

}

hPIDRegulator myRegA;
hPIDRegulator myRegB;

void setupMotorPID()
{
float kp = 5;
float ki = 0.02;
float kd = 100.0;
//=== motor 1 (left)
// hPID
myRegA.setScale(1);
myRegA.setKP(kp);
myRegA.setKI(ki);
myRegA.setKD(kd);
// hRegulator
myRegA.dtMs = 5;
myRegA.stableRange = 30;
myRegA.stableTimes = 3;

hMotA.setEncoderPolarity(Polarity::Reversed);
//hMotA.setMotorPolarity(Polarity::Reversed)

hMotA.attachPositionRegulator(myRegA);
hMotA.resetEncoderCnt();

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

hMotB.setEncoderPolarity(Polarity::Reversed);
//hMotB.setMotorPolarity(Polarity::Reversed);

hMotB.attachPositionRegulator(myRegB);
hMotB.resetEncoderCnt();

hMotA.setSingleMode();
hMotB.setSingleMode();
hMotA.setSlewRate(0.025);
hMotB.setSlewRate(0.025);

hMotA.setActiveBraking();
hMotB.setActiveBraking();

//hMotor::setPWMFreq(PWM_freq_hMotor_ID_A PWM_freq_21_kHz);
//hMotor::setPWMFreq(PWM_freq_hMotor_ID_B, PWM_freq_21_kHz);

}

void hMain()
{
platform.begin(&RPi);
platform.ui.setProjectId(“c167238835e5d5db”);
platform.ui.configHandler = cfgHandler;

setupMotorPID();

platform.ui.onKeyEvent = [](KeyEventType type, KeyCode code) {
	if (type == KeyEventType::Pressed) {
		if (code == KeyCode::Key_W) {
			hMotA.setPower(100);
            hMotB.setPower(100);
		}
		if (code == KeyCode::Key_S) {
			hMotA.setPower(-100);
            hMotB.setPower(-100);
		}
		if (code == KeyCode::Key_E) {
			hMotA.setPower(0);
            hMotB.setPower(0);
		}
		if (code == KeyCode::Key_1) {
			hMotA.setPower(100);
            hMotB.setPower(0);
		}
        if (code == KeyCode::Key_4) {
			hMotA.setPower(500);
            hMotB.setPower(0);
		}			
		if (code == KeyCode::Key_2) {
			hMotA.setPower(0);
            hMotB.setPower(100);
		}        
		if (code == KeyCode::Key_3) {
			hMotA.setPower(0);
            hMotB.setPower(500);                
		}
		if (code == KeyCode::Key_A) {
			hMotA.setPower(-100);
            hMotB.setPower(100);
		}
		if (code == KeyCode::Key_D) {
			hMotA.setPower(100);
            hMotB.setPower(-100);
		}
	}
};

sys.taskCreate(ralayTask);
sys.taskCreate(servoTask);
sys.taskCreate(status_task);

while (1) {
	sys.delay(500);
	LED1.toggle();
	auto lb1 = platform.ui.label("l1");
	lb1.setText("uptime %u", sys.getRefTime());
}

}

Hi all,

I apologize for your long waiting but the issue should be solved now. That was a bug in hFramework code.

Please use “Development” and “1.0.0” version for CORE2mini on WebIDE. I checked with the code from the 1st post in this topic and it works well.

Regards,
Radek.

Hi Radek,
I have compiled and uploaded the code I had sent you before.
I still get problem with the 2 motors turning.
I have keyed in a 1 or a 2 then a single motor should run.
It starts off and stops immediately.

Also I have tried to change the motorB polarity, but nothing happens. Keeps rotating in the same direction
Regards
Fred

Hi Radek,
Ignore my previous message.
Program is running fine.
My mistake was using the stable version, sorry.
Regards,
Fred

1 Like

Many thanks! It seems to be working well now.