# passing variable to a callback function in matlab

Hey Guys, i wrote a simple code in matalb which connect my matlab with my roscore. Thee code looks as follow:

Main

  rosinit('192.168.179.98')
odomSub= rossubscriber('/odom');
odomMsg = odomSub.LatestMessage;
odomSubCallback = rossubscriber('/odom',@odomCallback,odomMsg, fig,1});
pose = odomMsg.Pose.Pose;


odomCallback

     function [] = odomCallback(odomMsg, fig, varargin)
% Callback function for plotting robot path on topic odom
% fig : figure in which to plot the path
% varargin: use for poseHandle in task 20).
% Use the posehandle if the varargin argument is provided, otherwise use
% the global variables
figure(fig); % switch current figure
title('callbackfigure');
hold on;
% convert odom message to pose vector
[ x1, y1, ~ ] = OdometryMsg2Pose( odomMsg );
xlabel("X-Pose");
ylabel("y-Pose");
plot(h_ax1,x1,y1,'--rx');

end


OdometryMsg2Pose

function [ x, y, theta ] = OdometryMsg2Pose( odomMsg )
% convert odometry message to vector
% use quat2eul to get theta
poseMsg = odomMsg.Pose.Pose;
w = poseMsg.Orientation.W;
x = poseMsg.Position.X;
y = poseMsg.Position.Y;
z = poseMsg.Orientation.Z;
eul=quat2eul([w x y z]);
theta=eul(1);


end

suddenly I do not get the figure 3 drawn and I really do not know why. I also think that Matlab does not go through this line.

odomSubCallback = rossubscriber('/odom',@odomCallback,odomMsg, fig,1});


because as soon as I run this code, I don't get an error message, but I also don't get the figure. and when I make a breakpoint on this line, I get recursion errors. Sometimes I have the problem that my callback function gets empty parameters.

i'll be really greatful if someone could help. I can also send you a file with the code if this could help. Thanks in advance.

edit retag close merge delete