Using generated C/C++ Code from Simulink in own C++ project - c++

I'm searching for a good tutorial or explanation for the following procedure.
I want to generate C/C++ Code from Simulink (Simulink Coder/Embedded Coder) and interface it with own C/C++ Code, such that I can use my existing algorithms from Simulink. I want to use sensor decoders explicitly in C/C++.
I know that I could use S-functions to integrate my decoders in Simulink, but I want to do the exact opposite because of several reasons.
Is there anyone who can provide me good tutorials/informations about that? Is there a standard C/C++ file structure to interface code-generated Simulink models?
Best regards
Max

There is no standard interfacing setup (just a default), that is why you can configure the interface of the generated code. These days the tool itself includes a helpful wizard to help you defining the interface of the generated code, see https://nl.mathworks.com/products/embedded-coder/features.html#configuring-for-code-generation
In case you are looking for C++ class generation (instead of a function-based interface), take a look at the following link:
https://nl.mathworks.com/help/ecoder/ug/customize-generated-cpp-class-interfaces.html

Related

confused how to use IDL to communicate with Ethovision

Ethovision is an old computer vision software writen in c++ used mainly for tracking mice. It has the possibility to Real Time Export this data and i have been given the task to establish a program that use this data. i am quite confused on how as i am a beginer programmer and have only done web design so far.
I have been provided with some IDL files but i have no clue what im supposed to do with them and im just really confused in general. Does any one have any general idea on what i should do/read to extract this data.
Sorry if this sounds dumb, im new at this.
Q. If its a typelib does it mean the functions are defined within that file? or are they somewhere else? If theyre defined in c++ how would i import them in another language?
No. The interface is only described in that file. But in the case of COM "type libraries", there will be GUID identifiers for
the type library
classes (co-classes)
interfaces (IUnknown/IDispatch derived)
search the registry (regedit.exe, regedt32.exe) for the typelibrary GUID to find what registered COM component implements it.
If it's a wellknown component, dropping the GUID in Google could reveal some useful documentation
You can import these in many languages that have COM interop, like VB6, C#, VB.Net, and yes even C++ provided that you either use MSVC's builtin extensions or external libraries to make it workable.
I'm going to give some pointers here assuming that you want the MSVC (Visual Studio's C++ compiler) route on windows:
Use oleview.exe to view typelibraries. You can also view dlls that have their typelibraries as embedded resources. You can then save the TLB file separately, or view the IDL
Use MIDL.EXE (The MIDL Compiler), specifically for COM https://learn.microsoft.com/en-us/windows/win32/midl/midl-and-com
I recommand #import directive which basically does the same as generating the header/implementation files and including them in one line
TL;DR Summary
If you can, don't do this from C++. The reason is that COM interfaces speak "high level" types that just require tedious, error prone handling in C or C++ (using wrappers like CComBSTR or bstr_t).
In a language like C# you get all the marshalling and threading ("apartments") guarantees for free.

ReactNative expose C++ native module

We have a lot of business logic written in cross-platform C++. We want to write cross-platform UI for our application and use this business logic to create whole cross-platfrom application.
Is it possible to expose native module purely written in C++ to react-native?
We don't want to create wrappers around C++ code in native language (Java or Objective-C). Such wrappers will add more complexity it will make debugging and investigation much harder.
I am also looking for a way to do this directly in C++ without writing JNI for Android and Obj-C for iOS. I have found the CxxNativeModule class in the react native source. See the sample implementation SampleCxxModule.
The last thing to figure out is how to register that module in C++. In the JNI for React Native Android, implementation of NativeModule says
NativeModules whose implementation is written in C++ must not provide
any Java code (so they can be reused on other platforms), and instead
should register themselves using CxxModuleWrapper.
Then implementation of CxxModuleWrapper says
This does nothing interesting, except avoid breaking existing code.
So, this may help you get started. The last thing to figure out is how to register a CxxNativeModule so that it can be used in JS.
It seems as though you would need to find the code which is os dependent and write different versions of this code for different operating systems.
Here is a link which suggests how this might be done:
How do I check OS with a preprocessor directive?

C/C++ Wrapper for SIMULINK models

I have a very big C/C++ software project in Visual Studio and Eclipse. There are some third party software in the form of SIMULINK models. Is it currently possible to:
1) Write C/C++ wrapper functions for SIMULINK models?
2) Invoke the functions from external environment such as VS or Eclipse C/C++ projects to perform integration and further tasks?
I know that you can use MEX (MATLAB Exec.) functions which are C/C++ or FORTRAN wrapped for MATLAB usage. But this is not ideal for me.
USE CASE
My use case is based on modelling and simulation, but my subsystems are quite diverse in terms of data format. 50% is in C/C++, 35% is in SIMULINK models, and the rest are in binary executables, object code, symbol data, etc. I am doing some complex system modelling; Sorry, but cannot tell you further due to protective reasons.
From the answers I am getting, it seems people are keen to just use what Mathworks offers. My intention was to get more like a yes no answer, but so far I got good explanations too.
UPDATE
I will have to evaluate SIMULINK's embedded coder and see how much extra effort is required on polishing the generated code. I read that there is production-level code generation facility in Embedded Coder; should be interesting.
There are two choices depending on exactly what your requirements are:
convert the Simulink models to C using Simulink Coder. This effectively gets you out of the Simulink environment, so integrating the model functionality into you existing environment is identical to incorporating any other C code. Of course you need access to Simulink Coder to do this.
Calling the MATLAB Engine. Note you cannot call just Simulink. You call MATLAB and issue various MATLAB commands to load, execute, and interrogate the Simulink model.

Haxe to C++ conversion: Generate C++ source?

I'm very new to Haxe, and specifically want to use it to produce C++ code from Haxe (actually the flow would be AS3->Haxe, then Haxe->C++). My understanding is that Haxe compiles Haxe directly to a (C++) executable. But does it explicitly output the generated source?
Can/does Haxe supply the C++ code that it produces in this process? -- As I could then take and use this source within another C++ cross-compiler such as Marmalade (with modifications, of course).
I'm wondering about the intensiveness of the conversions, also. If Haxe does produce/supply the C++ source, then what does this source look like? Is e.g. memory management all packaged up into native DLLs/SOs? In that case, it seems like Haxe wouldn't be an ideal option.
(Disclaimer: I'm just trying to get some preliminary information before I go down this road. In fact, more specifically, I want to port from AS3 to C++ for Marmalade. So I want to know if it is worth writing my own converter or if Haxe provides a viable alternative.)
If you're looking to go from AS3->C++ through Haxe, then you should check out NME. It allows you to use the Flash Player API to write applications to compile to native ones (through the C++ backend), swfs and html5 applications.
Also it offers a whole workflow for assets and such. And it has pretty good integration with FlashDevelop (windows only) and MonoDevelop, but you can of course use any IDE.
Yes, Haxe outputs the source for you. Haven't ever looked into it very deeply, but it's there. When you compile for a C++ target (e.g. Windows) the source can be found under bin\cpp\windows\obj.

C++ algorithm in iOS application

I've got a DSP audio aglo in C# that I'd like to incorporate into iOS. A friend suggested that I convert to C++ instead of Objective-C, as it would be platform agnostic. Is this a good practice, any pitfalls to be aware of? My instinct is to keep it native with Obj-C. How would I access C++ functions within iOS?
Thank you
You can freely mix C++ with Objective-C, it's known as "Objective-C++". To let the compiler know that is your intent, you need to rename you .m file as .mm.
Mixing code like that is not necessarily a good idea, and fairly pointless if you aim is portability. Better is to keep you C++ in separate .cpp files, and create a bare-bones wrapper class (obj-c++) to pass data between your objective-C and C++ objects (effectively the wrapper is mediating obj-C methods and C++ functions to send and return data).
I have recently posted a couple of simple image processing examples of this on github - openCVSquares and openCVStitch. It's worth you taking a look, just to see how the C++ code is separated out from obj-C.
The openCV libraries have a C++ interface, so using C++ in the project is unavoidable. It would be possible to mash all of the code into .mm files, but that's an easy way to make a mess.
As regards portability, you get this by separating your UI code from your data-manipulation code. You should be doing this anyway for MVC, so it will be little additional work to write your model in C++ as per these examples. The model is portable, the UI is - and should be - platform specific.
Whether you should do this will depend on other factors such as your fluency with each language, the choice of frameworks and libraries, and the likelihood of porting. Audio processing would be a natural candidate for such an approach though.
Some detail about the samples
Don't worry if you can't get the code to run (the openCV framework can be troublesome...) the main thing is to see how the code is pieced together. I've tried to keep the examples very simple.
The C++ code was ported from C++ samples included with the openCV distro: squares.cpp and stitching.cpp. I tried to alter these as little as possible in order to assess this idea of portability.
Roughly, I had to address two issues...
1/ removed any UI code (so we are just sending data back to the obj-C side).
2/ modify the main() function so it becomes a callable function from the obj-C side. In the .cpp header file I declare a simple class with a single static function to facilitate this. Obviously there is scope to get much more elaborate but this should be enough for you to get the idea.