for my master’s thesis i want to run my working acados MPC in TwinCat.
As described in their manual, one can just add the s-Function with the source code in the Simulink Model and the Simulink Coder compiles it in C++ for the use in TwinCat. Alternatively one can build the project manually in Visual Studio. Unfortunatelly i get lots of syntax errors like these:
So i wanted to ask the community if someone has experince with TwinCat + Acados?
I would also be very grateful for other suggestions regarding C++ and Acados.
So far i have built acados with the same compiler as is used for TwinCat (MSVC), targets for blasfeo and hpipm are generic and EXT_DEP=OFF for blasfeo. I have added the include folders and adapted the timing.h for the use with time.h from TwinCat.
I’m facing the exact same errors as you described in your post while integrating Acados with TwinCat. Have you managed to resolve them? Any guidance would be greatly appreciated!
actually i got it really running and i wanted to update this post this week.
But be aware, it is quite a time consuming way.
The way to go is:
S-Function Builder: You need to use a S-Function Builder block. There you have to activate “Generate wrapper TLC”. The goal is to successfully build the S-Function Builder. So you leave Start, Outputs, Derivatives, Update and Terminate empty. In Data Properties you define your In- and Outputs. Then in Libraries you write in “Includes:” all the headers which are included in the acados-generated S-Function. In the “Library/Object/Source” files you need to include ALL necessary source files (libraries do not work). So you try to build it, you get an error for a missing function, you search where the function is defined, you include the source file.
In my case it looked like this:
SRC_PATH - Folder with source files (which are in the list)
(there are no spaces between INC_PATH and the PATH because of a bug of 2018b.)
-D… - Directives for the compiler
my_exit.c is a self written file. Since the real-time pc does not have an exit function (what should he do after exit anyways), you have to define a exit or otherwise you get a linker error.
#include <stdlib.h>
#ifdef TARGET_TWINCAT
int __cdecl exit(int a)
{
return a;
}
#endif
If you can build your empty S-Function Builder, you can copy the code from the acados-generated S-Function. But you have to use a PWorks instead of S (Simu Struct). For example in the Start:
// Instead of
//ssSetUserData(S, (void*)capsule);
// you use
pW[0] = (void*)malloc( sizeof(capsule) );
pW[0] = capsule;
If you can build your S-Function Builder with the code, you need to build the whole Simulink model for TwinCat. Assuming you have installed TwinCat and the Simulink package, you need to configure under “Code Generation” the system target file as TwinCAT.tlc. The used directives in the S-Function Builder block have to be copied to “Code Generation/Custom Code/Additional build information/Defines”.
After a successfull build of the S-Function Builder it might be necessary to temporary change the system target file to anything else and then back to TwinCAT.tlc. (When you get the error because of the change of the make command)
Then you need to address the errors one after one, similar to the build of the S-Function Builder. For example i did rewrite the timing.c of acados, to always return 1. I guess it is only used for analyzing anyways.
If you can build your Simulink model, you get your Model for TwinCat, which you can add in the TwinCat project. If it does run in TwinCat but does not compute anything, you should deactivate the optimization flags for the c compiler. Either by adding -DNDEBUG in the Defines or by adding the *.vcproj file in VisualStudio under the C++ category and then you can build and publish your module with further settings.
It did run in my case, but in comparison to Simulink it did run much slower.
Great that you made it work and thanks for sharing the information on the process!
I don’t have experience with compilation for Beckhoff systems, but would like to comment.
It sounds a bit weird that libraries do not work.
Did you try to cross compile the acados core library?
Could it be that there was something wrong in that process?
Of course, it would be useful to implement this correctly.
A pull request on this would be very welcome.
Probably it is interesting for you to assess this further.
Things to look into:
The debug flags that you added, I am not sure what there were needed for or how you got the behavior.
If it does run in TwinCat but does not compute anything
What CPU is in your system? What would be the dedicated BLASFEO target, this could give a significant speedup compared to the GENERIC target.
Cross Compilation:
Unfortunatelly i don’t have much experince with this subject, but i did compile acados with the generic settings for blasfeo and hpipm as well as the EXT_DEP flag off (inspired by some other posts on this forum).
I just got the information that libraries do not work, due to some memory allocation things, from the colleague which gave me the hint with the S-Function Builder. He did use this approach and got a casadi controller running in TwinCat.
Timing:
To get the timing working on the Beckhoff PC, one would have to use the c++ functions provided by Beckhoff and do some time conversions i guess. But since these functions are not available in Simulink, i guess it would result in some other linker errors.
Or what was your thought about this?
Execution Speed
The use of the debug flag was not a precise description of the problem from me. The cause of the problem it the -Og optimization flag. When used, the resulting/compilated TwinCat Module does run, and is indeed alive (tested with a simple output=2*input code in the S-Function Builder) but it does not execute/call the acados solver. Some other colleague did hear about my problem and said he had a similar problem and that he had to use unique ponters instead of “regular” pointers. The debug capabilities are unfortunatelly limited in TwinCat and i could not track down what went wrong.
CPU
I will take a look later and check what CPU the PC has and if there is a target in BLASFEO.
Edit: It is a “Intel Atom® E3845, 1,91 GHz”.
Regarding the cross-compilation.
This should certainly be possible, but I can imagine that it might be easier to just add the needed sources from acados and BLASFEO in the Simulink builder.
However, compiling BLASFEO with a different target, probably does not work out of the box with your current approach, since it will then use different source files.
About execution speed and CPU.
I don’t have experience with the -Og flag, I either use an optimization or a debug flag, but this somehow does a mixture of both.
The dedicated target for your CPU should be X64_INTEL_CORE as the Silvermont architecture does not support AVX or AVX2 instructions.