struckig icon indicating copy to clipboard operation
struckig copied to clipboard

Division by zero problem

Open yakunsjtu opened this issue 10 months ago • 4 comments

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

yakunsjtu avatar Apr 20 '24 01:04 yakunsjtu

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 avatar Apr 20 '24 05:04 stefanbesler

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 avatar Apr 20 '24 06:04 stefanbesler

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.

stefanbesler avatar Apr 20 '24 15:04 stefanbesler

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

yakunsjtu avatar Apr 23 '24 07:04 yakunsjtu