pantor / ruckig

Motion Generation for Robots and Machines. Real-time. Jerk-constrained. Time-optimal.
https://ruckig.com
MIT License
721 stars 164 forks source link

Fixed Math Exceptions for TwinCat Runtime #120

Open bazoida opened 2 years ago

bazoida commented 2 years ago

Hi pantor. Kudos for ruckig.

At the moment i implement your source code in c++ runtime environment twincat (17c++) First test cases seems to run smooth and nice. I'm impressed on the performance.

Next to a judge namespace war i made in twincat i also needed to adjust the maths. https://github.com/bazoida/ruckig/tree/chore/fixMathForTwinCat/bazoida

Hint: Separated the includes working in twincat.

#pragma once

#ifdef windows
#include <cmath>
#include <numeric>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#endif// windows

#include <limits>
#include <optional>
#include <string>
#include <array>
#include <vector>
#include <algorithm>
#include <cstdlib>

namespace ruckig {
#ifdef windows
    typedef size_t SIZE_T;
#endif // windows

    const double Pi = 3.14159265358979323846264338327950;

    inline double Power2(double v) {
        return v * v;
    }

} // namespace ruckig

namespace std
{
#ifndef windows
    inline constexpr nullopt_t nullopt{ nullopt_t::_NulloptTag{} };

#endif // !windows
    template<class ForwardIt, class T>
    constexpr // since C++20
        void Iota(ForwardIt first, ForwardIt last, T value)
    {
        while (first != last) {
            *first++ = value;
            ++value;
        }
    }

    inline double Sqrt(double v) {
        if (isnan(v))
            return NAN;
        if (v < 0.0)
            return NAN;
        else
            return std::sqrt(v);
    }
}

TwinCat C++ seems to be more sensitive than the official legacy.

Perhaps you can have a look. I already tested the changes and also tracked your origin ramps and made a one to one comparison.

image

For TwinCat C++ in general we have to take care about 0 Divisions and negative Roots

pantor commented 2 years ago

Hey @bazoida, thanks for your changes - I'm very much interested in TwinCAT for Ruckig!

Do you know where I can find information about which subset of C++ is supported in TwinCat? I've found the Beckhoff Reference, and there is section 12.4 about its limitations, but I can't find anything special about zero devision or nan handling there.

Btw, there is also struckig, a port of Ruckig to Structured Text. Might be of interest to you.

bazoida commented 2 years ago

Hi @pantor

C++ for TwinCat -> Come in and find out. Hehe. Unfortunately Documentation is the weak point on the system.

Mostly you can reach same methods as in standard legacies. TwinCat is doing a great job on rebuilding the std classes for usage in deterministic realtime context. Exception handling by now is really nice. With last versions i wouldn't know any argument except additional license, not using c++ fully for plc programming. i.e. c++ methods can fully be used within codesys environment.

image

I will soon provide a fork shared library for usage standard and twincat within our organization https://github.com/ZoiglValley Need some more tests with extrema, jog mode, reference mode, automatic switch position <-> velocity drive.

I know struckig. kudos for rebuilding your code in codesys. I'm the lazy guy. To be honest, i even don't understand your magic a bit. I just built a wrapper.