How to implement concurrent programming between simulink and matlab - concurrency

I need help for the following situation.
My project is to use simulink to simulate a robot.The output of the simulink model are robbot's position and torques at each timestep. My problem is on the data collection part. I plan to use a buffer to store the simulink output and use antoher matalb function to access the same buffer to get the data out of the buffer for online data analysis. The requirement is the simulink model and matlab data analysis function need to run simultaneously. And the matalb data analysis function decide when to get the data out of the buffer. This is like a producer-consumer problem, where the simulink is the producer and the data analysis matlab function acts as a consumer.
My question is how to protect the buffer for mutual exclusion. I do not want to use To workspace block, because it only updates data when simulink is paused or stopped. I do not find any smeaphore or mutex like structure provided by matalb or simulink. I have tried the following ways to solve the problem, but non of them works:
I have tried to use the queue and buffer block in DSP toolbox, this two blocks provides mutual exclusion, but the size of the output data is changed during the simulation. Basically when the matlab function collecting data, it takes all the data stored in the buffer at the moment. Then buffer block seems to output one by one at each simulink timestep.
I have tried to implement a queue by a persist variable in a embedded function. When the matalb function want to collect data, it flip a signal flag to tell the simulink to output the data into workspace. But in this method, the matalb function have to get the data by two calls. The 1st call to flip the flag and then return. In the next, the 2nd call is used to search the workspace to find the data outputted by simulink. This method is denied by my advisor, because it is not elegant.
I think RTW may solve this problem, but the simulink model and matalb analysis function code are often changed, so for debugging purpose, I plan to not change the simulink in to C/C++. But I wonder whether I can use C to implement a mutex and call by simulink and Matlab. If the answer is yes, then how to do this?
I really hope someone can help me out. Any suggestion is appreciated. By the way, I am using Linux system.

Have a look at Access Block Data During Simulation in the Simulink documentation and also at Simulink Signal Viewing using Event Listeners and a MATLAB UI on the File Exchange. I think this will do what you want.

Related

Using gazebo with drake for quadrotor

My objective is to use finite horizon linear quadratic regulator(FHLQR) to follow a trajectory generated by a mathematical program and simulate it in Gazebo. As FHLQR needs a system as input, I'm using the QuadrotorPlant, but I'm not sure if this is ideal. My problems arise when I try to connect the state from Gazebo into Drake. In short, what would be the proper way of coupling state from Gazebo with a controller such as FHLQR?
I've thought about editing the context of QuadrotorPlant to mirror the state in Gazebo, but after this update I'm having trouble getting control output from the controller. I've also thought about coupling the Simulator between the output of the controller and input of the QuadrotorPlant, but haven't figured out how to modify the Simulator to mirror Gazebo.
And for your gazebo interface, and assuming you're all in c++, then I'd imagine it will look something like this:
// setup
auto regulator = MakeFiniteHorizonLinearQuadraticRegulator(...);
auto context = regulator.CreateDefaultContext();
// during execution
context->SetTime(time_from_gazebo);
context->FixInputPort(0, Eigen::VectorXd([state obtained from gazebo]));
Eigen::VectorXd u = regulator->get_output_port(0)->Eval(context);
// then apply u to your gazebo interface
I think we should be able to help, but probably need a few more details.
My mental model of your setup is that you use QuadrotorPlant to design the trajectory and the MakeFiniteHorizonLinearQuadraticRegulator methods in drake... the result of that is a drake System that has an input port expecting a vector of doubles representing the state of the quadrotor, and it outputs a vector of doubles that represent the thrust commands for the quadrotor.
Separately, you have a Gazebo simulator with a quadrotor, that is accepts commands and simulates the dynamics.
Are you trying to connect them via message passing (as opposed to a single executable)? Presumably with ROS1 messages? (If yes, then we have little systems in drake that can send/receive ROS messages, which we can point you to)
FWIW, I would definitely recommend trying the workflow with just running an LQR controller in drake before trying the trajectory version.

How to use device data from a C++ library in a follow-up analysis in another language

I have an API in C++ which connects via bluetooth to a device and measures data.
Now I want to use this captured data LIVE and evaluate it in another language like R or Python, how is this done?
So I get live data from my c++ API from console applicaiton within visual studio, now I want to pipe this data stream to another "instance" like Python or R (maybe from within another IDE) and run my script on the data. Afterwards the data does not need to be piped back.
How is a good or correct way to achieve this? In the beginning I thought I would have to add native support for Python within my C++ project, now however I think it would be enough to just takes this little bit of data and pipe it to a local server instance where e.g. my R/Shiny application runs and read it in as a dataframe?
Has anyone worked with a C++ library for a device and piped that data live down another analysis setup in a different language? How have you done it?
I think the best way would be to use TCP/ IPC communication over socket.
In C++, implement a server which read data and publish it into a socket.
In Python implement a client which simply listen to a socket and process the data every time it's published by the C++ server.
If you want an easy C++ library for socket communication I suggest looking into either ZMQ or nanomsg but if your use case is simple enough, using native socket can do the job simply and efficiently.
Edit : If you wish to go the ZMQ way you can start with the ZGuide. You also have this tutorial about sending data between C++ and Python using zmq.
Nanomsg is a fork of ZMQ, so most of the concept of ZMQ will apply to it.
If you want to use native socket, there are already plenty of tutorial in both C++ and Python, just search on google.
If the both programs are independent you can just use standard system pipe.
You just run the both programs from a system terminal, piping the output of the first one as the input of the second one.
The syntax is usually:
cpp_program.exe | python_program.py
Then you just use standard output in the C++ program (functions like printf or std::cout which just write data to terminal). In the other program you use standard function for reading data from terminal.
This solution has a few disadvantages:
Input/output streams are usually treated as text. If you want to pipe binary data there may be some problems. For example on some systems bytes/characters "\n" may be replaced with "\r\n".
You cannot take user input in the second program. (At least not without using some tricks to access real terminal input.)
Pipes have finite size. If the second program is to slow to process data as fast as the first program produces them, the first program may be slowed down by print operations which waits for pipe to empty. (Or maybe it throws exception. I'm not sure.) In this case it may be a better idea to use a file as a buffer.

Interprocess Communication CUDA

I am trying to understand interprocess communication in CUDA. I would like some help with being able to understand this concept and trying to apply this to a project I am doing.
I have a image acquisition system that provides N number of input images. Each raw input image is first processed and then, stored in a single variable called 'Result'. There are four functions which do the processing of the image, Aprocess, Bprocess, Cprocess and Dprocess. Each time a new image is acquired by the system, the four functions mentioned above are called to do the processing. The final image 'Result' is stored in Dprocess.
What I would like to do is:
Create a new process, 'process2', where I can hand off one (final) image stored in 'Result', each time that image is obtained, and put it in a buffer called 'Images'. I would like to do this for 10 images. 'process2' should wait for a new image to be passed to it and not terminate because the first process has to keep calling the four functions and get a final processed image.
What I have come across so far: cudaIpcGetMemHandle, cudaIpcOpenMemHandle and cudaIpcCloseMemHandle
Question: How do I use the above function names to achieve IPC?
Question: How do I use the above function names to achieve IPC?
The CUDA simpleIPC sample code demonstrates that.
There is also a brief mention of how to use CUDA IPC API in the programming guide.
Finally, the API itself is documented in the runtime API reference manual
Note that this functionality requires cc 2.0 or higher, and a 64-bit Linux OS.

How to communicate between two processes

Hi I'm working on a c++ project that I'm trying to keep OS independent and I have two processes which need to communicate. I was thinking about setting up a 3rd process (possibly as a service?) to coordinate the other two, asynchronously.
Client 1 will tell the intermediate process when data is ready, and send the data to it. The intermediate process will then hold this data until client 2 tells it that it is ready for the data. If the intermediate process has not received new data from client 1, it will tell client 2 to wait.
Since I am trying to keep this OS independent I don't really know what to use. I have looked into using MPI but it doesn't really seem to fit this purpose. I have also looked into Boost.ASIO, Named Pipes, RPC's and RCF. Im currently programming in Windows but I'd like to avoid using the WIN_API so that the code could potentially be compiled in Linux.
Here's a little more detail on the two processes.
We have a back end process/model (client 1) that will receive initial inputs from a GUI (client 2, written in Qt) via the intermediate process. The model will then proceed to work until the end condition is met, sending data to the server as it becomes ready. The GUI will ask the intermediate process for data on regular intervals and will be told to wait if the model has not updated the data. As the data becomes available from the model we also want to be able to keep any previous data from the current session for exporting to a file if the user chooses to do so (i.e., we'll want the GUI to issue a command to the interface to export (or load) the data).
My modification privleges of the the back end/model are minimal, other than to adhere to the design outlined above. I have a decent amount of c++ experience but not much parallel/asynchronous application experience. Any help or direction is greatly appreciated.
Standard BSD TCP/IP socket are mostly platform independent. They work with some minor differences on both windows and Unices (like linux).
PS windows does not support AF_UNIX sockets.
I'd checkout the boost.interprocess library. If the two processes are on the same machine it has a number of different ways to communicate between processes, and do so in an platform independent manner.
I am not sure if you have considered the messaging system but if you are sending structured data between processes you should consider looking at google protocol buffers.
These related to the content of the messaging (what is passed) rather than how they are passed.
boost::asio is platform independent although it doesn't imply C++ at both ends. Of course, when you are using C++ you can use boost::asio as your form of transport.

How to enable Matlab to listen real time data through C++ application

I need to create a C++ add-in to Matlab where add-in will listen to packet coming from network and notify Matlab to draw an packet analysis graph. I understood that using a MEX file I can easily call c functions inside Matlab, but I could not find a way to notify Matlab when data is available at C++ end. Is there any way we can pass a user-defined Matlab function pointer into my C++ add-in?
BTW, I found this thread: real-time-data-in-matlab
Unfortunately suggestion is to use ActiveX control, but in my case, I need to create add-in in pure C++.
Have a look at Gurobi. It 'just' prints status information to the command window. Using a mex command like mexCallMATLAB you may access 'any' matlab function.
Would it make your life easier if you could listen for the network data directly from Matlab? I've never tried it, but there are a few submissions on MathWorks' File Exchange site that allow you to create sockets within Matlab. Here's a TCP/IP example that creates both a client and a server, and here's a similar UDP example.