Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New "LPF" filter for PID Conrolers in motor.cpp producing "NaN´s" after some (long) time..... #161

Open
MrTreeBark opened this issue Jul 21, 2024 · 5 comments

Comments

@MrTreeBark
Copy link

MrTreeBark commented Jul 21, 2024

What the description said, possibly due to an overflow. Observed a long time and validated with console, mower conrol loop runs with 50Hz. Mower will stop after ~ 1 - 2 hours and drives are dead, because pid is outputting 0, beside rpm set is given.
It just stops working and there are sudden NaN´s coming from function call motorRightOrLeftLpf(motorRightOrLeftRpmCurr); (logged)

motorRightPID.TaMax = 0.25; //motorRightPID.x = motorRightLpf(motorRightRpmCurr); //gives a NaN after some time.................. motorRightPID.x = motorRightRpmCurr; motorRightPID.w = motorRightRpmSet; motorRightPID.y_min = -pwmMax; motorRightPID.y_max = pwmMax; motorRightPID.max_output = pwmMax; motorRightPID.output_ramp = MOTOR_PID_RAMP; motorRightPID.compute(); motorRightPWMCurr = motorRightPWMCurr + motorRightPID.y;

@MrTreeBark
Copy link
Author

Update: it may give Nan´s after stopImmidialetly, i don´t know why...

@ShadedSelf
Copy link
Contributor

ShadedSelf commented Jul 22, 2024

I think its because stopImmediately resets the control loop time inside the pid controller, "Ta" then becomes 0 and you get division by zero.

This is a quick fix I did on PID.cpp:

Ta = max(Ta, 0.000001);

@MrTreeBark
Copy link
Author

MrTreeBark commented Jul 24, 2024

This is a good hint, but why? Ta is computed inside PID, its just the delta time, why should it stop working just like that and not recover, because cycletime would be possibly 0 for the pid on a reset, but just for one iteration? somehow it dies and keeps to be dead, even when mower is rebootet. Maybe: why is Ta computet as float? And why would the .h define of Ta would be a double?? its a time?! it should be millis or micros and that is ONLY an unsigned long?

float PID::compute() { unsigned long now = millis(); Ta = ((float)(now - lastControlTime)) / 1000.0; //printf("%.3f\n", Ta); lastControlTime = now;

This is flawed for sure.

But anyway, the PID seems flawed now i checked it out... I meant something else: the LPF filter giving Nan´s --> to the PID (initial post) : function for PID rpm input --> motorRightOrLeftLpf(motorRightOrLeftRpmCurr); giving Nan´s after some time

@MrTreeBark
Copy link
Author

MrTreeBark commented Jul 25, 2024

And something else, i read about those microcontrollers... this extensively using of floats for absolutely no reason but reading for human mind is not a good thing to do. cm, meters etc.. times ... everything should be integers and translated only in console or register for readability but not computing. all this floats are just eating away on computeprecision and hardware recources, nothing else.

@ShadedSelf
Copy link
Contributor

ShadedSelf commented Jul 25, 2024

Actually, i think this its just the LPF, not the PID, the PID controller might recover because it gets casted to int (?).

Might actually come from here:

float alpha = Tf/(Tf + dt);

Then y and y_prev become NaN.
Tf is 0 by default.

I dont have the LPF on my version so I cant say for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants