RealTimeBiomechanics / rtosim

The real-time OpenSim extension (RTOSIM) is a set of libraries in C++ that wrap OpenSim APIs to enable the real-time computation of inverse kinematics and inverse dynamics.
Apache License 2.0
33 stars 14 forks source link

Added compatibility with OpenSim 4.3 #19

Closed RobertoRoos closed 2 years ago

RobertoRoos commented 2 years ago

Modified RTOSIM to work with OpenSim 4.3.

Relates to #18 .

This is still a work in progress!

Major changes:

Note: I have to load cmake with cmake .. -DOPENSIM_INSTALL_DIR=/opensim_install -DCMAKE_CXX_STANDARD=17

Without the CXX17 flag std::optional wasn't available. Should be add this to the main CMakeLists.txt?

Testing build with the following Dockerfile:

FROM stanfordnmbl/opensim-cpp:4.3

COPY ./rtosim /rtosim

# Dependencies
RUN cd ~ && git clone https://github.com/RealTimeBiomechanics/Concurrency.git && mkdir -p Concurrency/build && \
    cd Concurrency/build && cmake .. && make -j4 && make install

RUN cd ~ && git clone https://github.com/RealTimeBiomechanics/Filter.git && mkdir -p Filter/build && \
    cd Filter/build && cmake .. && make -j4 && make install

RUN mkdir -p /rtosim/build && cd /rtosim/build && \
        cmake .. -DOPENSIM_INSTALL_DIR=/opensim_install -DCMAKE_CXX_STANDARD=17 && \
        make -j8
RobertoRoos commented 2 years ago

I forked the official OpenSim Docker image (which only has 4.3) into https://hub.docker.com/repository/docker/be1et/opensim-cpp to also create 4.0, 4.1 and 4.2.

I tested this branch on all those versions and only 4.0 fails. I think that's a reasonable score.

I had a quick look to demonstrate this with a Github action, but I couldn't get it to work (I have no experience with these actions yet).

cpizzolato commented 2 years ago

Thank you very much for the great work @RobertoRoos!

I have refactored Concurrency a few months ago. Other than a code clean, the main changes in Concurrency were an automatic management of poison tokens to close the consumers threads, which can now be achieved by calling the member function rtb::Concurrency::Queue;:close(), like this:

Queue<int> q;

void produce(int n) {
    for (int i{ 0 }; i < n; ++i) {
        q.push(i);
    }
    q.close();
}

From the consumer perspective, the data loop can now be simplified to something like the following:

void consume() {
    q.subscribe();
    while (auto val{q.pop()}) {
        cout << val.value() << endl;
    }
    q.unsubscribe();
}

Which uses some of the properties of std::optional. The last substantial change I made was simplifying the implementation of the thread pool pattern, which is now much more streamlined to use, see example .

All these changes are in the develop branch of Concurrency and the master still works as it used to be. However, I have just realised that I made the develop branch the default branch when clonig, which is why you might have had some problems with linking.

Please let me know if you would like to use and test the old version of Concurrency library (which just mean switching to the master branch of Concurrency in your testing environment and reverting the .value()) or use the new version and do a couple of extra checks that thread still close correctly.

Thank you very much for your help!

RobertoRoos commented 2 years ago

@cpizzolato , sorry for the late response. Yeah, I'll base this on the real master branch of Concurrency instead. So the current / old version.

We can then re-evaluate this when a real release of Concurrency is ready and update RTOSim accordingly.

RobertoRoos commented 2 years ago

Okay, I think I'm done. It's building now with OpenSim 4.3 and the current master branch of Concurrency.

Let me know if and how you would like it rebased.

cpizzolato commented 2 years ago

Thank you very much for your help! I have merged your changes