CORE2 becomes unresponsive when using hPIDRegulator

Hi All -

I am using the following code on a motor/encoder:

#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 encCnt()
{
	for(;;)
	{
        platform.ui.label("lb_enc").setText("%d", (int)hMotB.getEncoderCnt());
 	    platform.ui.label("l1").setText("uptime %u", (unsigned int)sys.getRefTime());
	    sys.delay(100);
	}
} 

void cloudTask() {
    platform.begin(&RPi);
	hMotB.setEncoderPolarity(Polarity::Reversed);       // uncomment if polarity is reversed
    platform.ui.setProjectId("0c8c31995442bca0");
    platform.ui.configHandler = cfgHandler;
    
    platform.ui.onKeyEvent = [](KeyEventType type, KeyCode code) {
        if (type == KeyEventType::Pressed) {
            if (code == KeyCode::Key_A) {
                hMotB.rotRel(3840, 500);
            }
        }
    };
}

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

    setupMotorPID();
}

Usually, when I press the ‘A’ button, the motor rotates about 1 turn, which is what I expected. It’s not exactly one turn, but for the moment I am not worried about that.

The problem is that occasionally, and it’s always after a random number of presses, LED1-LED3 start flashing (LED1 & LED2 alternating at about 4Hz, LED3 at 2 Hz) and the motor keeps doing what it was doing. In other words, if it was turning, it keeps turning indefinitely. If it was not turning, pressing ‘A’ again does not make it turn. Also, the encoder count on the UI no longer updates. It’s like the system gets caught in an infinite loop inside the PID code. The only way to get it to respond again is to re-upload the software from the website.

Can someone give me a clue as to what is happening and suggest how I can debug this?

Thanks.

I’m not seeing anything that should force it into a infinite loop in weird conditions. Best I can think of is that maybe it has to do with using the web IED or its connection to the cloud controls? Have you tried loading the software on using VS code, and tried running it without trying to control it from the internet?

What I am thinking is that something with the web controls gets ‘stuck’, and keeps sending a to your CORE2. Maybe try adding a way to break any loops? Some kind of counter that rejects a commands after so many occur within a period of time (or in total). At the very least, would be helpful for troubleshooting.

Thanks very much for your response. I have never tried using the VS Code approach, but I will.

However, I doubt that the web UI getting stuck is the issue, because the LEDs start flashing. My code doesn’t use the LEDs at all. Does this give you any kind of hint where to look?

I have seen this same “Flashing Green LED Hang” when running some of the simple sample sypbot code. AN it will happen after a few minutes. Just happened now - I tried to upload video - but .mov is not allowed. I can clear the error by reflashing the firmware, video stream is also fine, just motion controls hung…

Thanks for confirming my experience, Matt. I am waiting on the device to connect to the debug port on the CORE2, so I will be able to pursue the VS Code approach. But I’m fairly certain that the behavior I’m seeing is not related to the web UI. I’m pretty sure that there is something funny going on in the PID/hRegulator code.

Any suggestions would be appreciated.

1 Like

We’re still investigating the issue, but if you suspect that something is wrong with hPIDRegulator, please remember that hFramework source code is open and available on our github hFramework/hPIDRegulator.h at master · husarion/hFramework · GitHub . We will solve the problem as soon as possible.

Thanks very much for investigating. As mentioned, I am waiting for the ST-LINK device to come in so I can debug.

This may help: Every once in a while, maybe 10% of the time, when the LEDs start flashing, the motor begins to turn in the opposite direction as it has been turning and keeps turning until I reset the board.Otherwise, it just stops turning and won’t turn again until I have reset the board.

In the meantime, I wonder if my choices of parameters for the motors might be causing the problem to appear. Is there any guidance on how to properly choose these? I believe the values of P, I and D are particular to the PID algorithm and probably are likely not the problem, but perhaps the hRegulator parameters are the issue, i.e. dtMs, stableRange and stableTimes?

Hi jsmarion.

Your code is perfectly fine. We already resolved this issue, it was related to hCloudClient library. Changes were implemented and are ready to use in our online IDE. New Husarion extension 1.5.7 for VSC will be available soon.

Regard,
Hubert.

1 Like

Hi Hubert -

That seems to have fixed my issue. Thanks!

I’m looking forward to doing more work with the CORE2.

Regards
John

2 Likes

Fixed my issue as well. Simple re flashed code and was good to go. Thanks for the quick fix!

2 Likes