stefanbesler / struckig

Full port of pantor ruckig library (Community version) to IEC 61131-3 Structured Text. Online Trajectory Generation. Real-time. Time-optimal. Jerk-constrained.
https://stefanbesler.github.io/struckig/
GNU General Public License v3.0
75 stars 14 forks source link

Division by zero problem #22

Open yakunsjtu opened 7 months ago

yakunsjtu commented 7 months ago

I tried the code on TwinCAT3 and found the PLC will run into fault because the denominator is zero (3 places as shown in the following figure). I am wondering how to fix it?

image

image

image

Also the sqrt of a negative number will occasionally happen: image

stefanbesler commented 7 months ago

Thanks for finding those, one big part of porting from C++ to ST involves to handle those exceptions as C++ doesn’t throw on those and the calculation is thrown away later if this happens. Could you post the trajectories you are trying to compute please (start,end,limits,…), then I can add unittests for them.

The fix is to check if those arguments are 0 and skip the calculation if they are. Struckig is always calculation all potential solutions and the using the fastest one. Since you seem to use the main branch of Struckig and not a release, you could fix them simply by adding checks like IF ABS(C) < Constants.DoubleEpsilon and skipping to calculating the next possible solution. I’d appreciate if your do a PR after fixing them, but if not I can do it in the coming days.

stefanbesler commented 7 months ago

Seeing the in the last commit the unittests failed, this could also be a regression, you could try with an actual release by downloading it from

https://github.com/stefanbesler/struckig/releases

or checking out the tag of the release (git checkout v0.9.3)

but anyway, this needs a fix :-)

stefanbesler commented 7 months ago

Issue should be fixed with the latest commit https://github.com/stefanbesler/struckig/commit/b9ca5aa700e8a879824e18173b1e809e9e4dd1a9

Unittests don't cover this path at the moment, would be great if you could send me the trajectories where you got these exceptions.

yakunsjtu commented 7 months ago

@stefanbesler Thanks for your fix. I did not record the original input. Will verify this fix after your merge.