Hello,
I am extremely new to using LCM and have been working with a research partner (a bit of an LCM Guru around the lab) to develop LCM functions for Matlab by developing a mex file which can be run from a Matlab script or command line.
Using a simplified version of the provided C++ example we have been successful in creating a publisher function which can take any scalar variable form matlab and post it to the "Example" channel.
In matlab command line,
=======================================================
>> Variable = 10; %% set any scalar variable
>> LCMPublish(Variable); %% LCMPulish.mexw32 is called and the Variable is then published to the channel
=======================================================
...this runs the following cpp program in the background.
=======================================================
#include <lcm/lcm-cpp.hpp>
#include "test3/test3_t.hpp"
#include "mex.h"
/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double value;
value = mxGetScalar(prhs[0]); // gets the scalar value from the right hand side of the mex function called
// Create the LCM object handle
lcm::LCM lcm;
// define the structure
test3::test3_t my_data; // header file <test_t> struture <my_data>
// fill in the variable
my_data.heading = value;
// publish using object <lcm>
lcm.publish("EXAMPLE", &my_data);
}
================================================================
This function is confirmed to be publishing by using the listener example without any bugs yet...
We were also somewhat successful in developing a listener function to catch a variable that is being published to the channel and store it for processing in matlab.
In matlab command line,
=======================================================
>> Variable = LCMListen; %% stores the scalar value from the published channel
=======================================================
This works so far but with a strange catch...
After 55 consecutive calls of the listener function LCM basically crashes (from my understanding)...
We believe this is from not disconnects from the LCM network, and destroying the subscription objects.
As a very green newcomer to programming (outside of using matlab for developing simulations) I am at a loss on how to do this yet!
I learn from examples and would greatly appreciate it if anyone could point me the right direction, or show me what I am missing?
The cpp code that runs outside is as follows.
=============================================================
#include <Windows.h>
#include <stdio.h>
#include <lcm/lcm-cpp.hpp>
#include "test3/test3_t.hpp"
#include "mex.h"
double compass;
class Handler
{
public:
~Handler() {}
void handleMessage(const lcm::ReceiveBuffer* rbuf, const std::string& chan, const test3::test3_t* msg)
{
extern double compass;
compass = msg->heading;
mexPrintf("2");
}
};
/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
extern double compass;
lcm::LCM lcm;
Handler handlerObject;
// subscibe to the lcm channel on the network
lcm.subscribe("EXAMPLE", &Handler::handleMessage, &handlerObject);
mexPrintf("1");
// open the lcm channel handler and grab whats inside
lcm.handle();
mexPrintf("3");
// clears the data... if something there before...
mxFree(plhs[0]);
// store published variable to left hand side of mex function in matlab
plhs[0] = mxCreateDoubleScalar(compass);
mexPrintf("4");
// destroy lcm with fire!!!!
~lcm(); // this does not work...
//lcm.unsubscribe(lcm.subscribe("EXAMPLE", &Handler::handleMessage, &handlerObject));
mexPrintf("5");
//delete [] &handlerObject;
//mexPrintf("6");
mxDestroyArray(plhs[0]);
return;
}