Shared Memory Plugin For VREP /Matlab Communication

Typically: "How do I... ", "How can I... " questions
Post Reply
dds
Posts: 72
Joined: 20 Mar 2014, 14:35

Shared Memory Plugin For VREP /Matlab Communication

Post by dds » 25 Aug 2015, 14:51

Hi! Here I left the last version of the v_repExtShareMemory plugin for Vrep. (UPDATED 13/09/2017)
This Plugin allows fast inter process communication of V-REP. It's particularly useful to communicate V-REP with Matlab or Simulink.
Here some information:

¿What is Share Memory?
“In computer programming, shared memory is a method by which program processes can exchange data more quickly than by reading and writing using the regular operating system services”

¿Why use Share Memory instead V-REP communication Plugin?
Share Memory plugin is the fastest and simplest interprocess communication method. It doesn’t make any synchronization; it has not transfer delay, death times or transfers buffers/queues.

¿It is secure?
In order to ensure top speed communication this plugin don’t use any inter process synchronization method, so NO, IT’s NOT SECURE. Lossless communication is not ensured.
If a single program writes the memory, in practice it will not crash. For for bilateral communication of two programs create two separate share memory’s!

¿For what it would be used?
It allows communicating V-REP with external process (running in the same PC), so you could communicate with almost any program developed in any language that allows Share Memory communication.
For example you could communicate V-REP with C/C++ custom program ,Matlab m-function , Simulink block, LabView scheme, and any other program/language that can access share memory.
You could, for example , make bridge between your robots hardware and Vrep.
Event functionality could be use for fast synchronization of V-REP with external program too.

Main Functions Prototype:

Code: Select all

number result,number ID=simExtShareMemoryOpen(string Name,int size)
Creates and open a new shared memory
ID it’s the number assigned to the share memory, it is his main identification but not the only one.
Name is the Share memory name, it must be unique in all system. All process that access to a share memory with this name could read and write it!
Size is the size in bytes of the share memory, take care of the dimension assigned, if you try to read or write out of this area you will get an error.

Code: Select all

number result=simExtShareMemoryClose(number ID)
Close specific share memory by its ID. If you let this empty, it will try to close any opened memory.
Be aware: If you don’t close the memory at the end of the simulation, it will remain opened. If try to open it again, you will get an error. When the program is closed all instances of share memories are going to get closed.
In fact all share memory’s remains opened until all programs close it. It’s a good practice to close the share memory in the same script that you create it.

Code: Select all

number result,charbuff data=simExtShareMemoryRead(int ID)
Reads a share memory by its ID.
charbuff data shoud has full size of the memory , so be aware to create a sufficient big variable to contain the hole memory. After you may format data with simUnpackFloats() /simUnpackInts() functions.

Code: Select all

number result=simExtShareMemoryWrite(intId,charbuff data)
It writes the Idmemory data should not exceed the size of the memory .Before you may format your data package with simPackFloats ()/simPackInts().

Auxiliary functions

Code: Select all

number result, string name=simExtShareMemoryGetName(int ID)
Given a ID It returns the memory name.

Code: Select all

numberresult,number ID=simExtShareMemoryGetId(string Name)
Given a Name it returns the memory ID, you may use it to read/write a memory created in other script.

Code: Select all

tableID,tablenames,table sizes=simExtShareMemoryList()
It returns all memories ID, name and size. It is a way to find an specific memory created in any Script.

Files
https://drive.google.com/drive/folders/ ... sp=sharing
Last edited by dds on 13 Sep 2017, 13:53, edited 6 times in total.

coppelia
Site Admin
Posts: 6224
Joined: 14 Dec 2012, 00:25

Re: Share Memory Plugin For VREP /Matlab Communication

Post by coppelia » 27 Aug 2015, 08:42

Hello Diego Daniel,

thanks a lot for sharing this with us!

Cheers

Richardlee
Posts: 1
Joined: 22 Dec 2015, 15:00

Re: Shared Memory Plugin For VREP /Matlab Communication

Post by Richardlee » 01 Mar 2016, 15:32

Thanks for sharing!

But maybe there is a mistake in your description.

For Create and open a new shared memory the code should be:

number result,number ID=simExtShareMemoryOpen(string Name,int size)

otherwise it will have warning.

but rest of the functions work perfectly!Great Job!

Richard

dds
Posts: 72
Joined: 20 Mar 2014, 14:35

Re: Shared Memory Plugin For VREP /Matlab Communication

Post by dds » 11 May 2016, 14:01

Richardlee wrote:Thanks for sharing!

But maybe there is a mistake in your description.

For Create and open a new shared memory the code should be:

number result,number ID=simExtShareMemoryOpen(string Name,int size)

otherwise it will have warning.

but rest of the functions work perfectly!Great Job!

Richard
Thanks for the observation. The error was corrected and the links updated . Cheers!

avico23
Posts: 5
Joined: 06 Sep 2017, 12:41

Re: Shared Memory Plugin For VREP /Matlab Communication

Post by avico23 » 10 Sep 2017, 19:12

Hello, I'm trying to connect vrep to Simulink via Shared memory communication, by courtesy of Diego Daniel Santiago.
I have copied the dll file (v_repExtShareMemory.dll) to V-REP main folder (C:\Program Files\V-REP3\V-REP_PRO_EDU)
When I run the example codes (see camerasExample,…) I receive the following error inside the vrep terminal :
"
Lua runtime error: [string "SCRIPT visionSensor#1"]:45: attempt to call global 'simExtShareMemoryWrite' (a nil value)
stack traceback:
[string "SCRIPT visionSensor#1"]:45: in main chunk
"
Can you please help me understand why.

coppelia
Site Admin
Posts: 6224
Joined: 14 Dec 2012, 00:25

Re: Shared Memory Plugin For VREP /Matlab Communication

Post by coppelia » 12 Sep 2017, 14:39

Hello,

we can't really help you here since we didn't develop that plugin and also didn't keep track of it.
But have a look at this topic.

Cheers

Post Reply