- NewMessageFcn callback
- LatestMessage property
- receive method
handle class obj is getting only zero
1 次查看(过去 30 天)
显示 更早的评论
Hey Guys,
Could someone help to find the problem here.
So I started to define a class called PoseHandle. This class has only three parameters that will store the detected positions.
classdef PoseHandle < handle
%POSEHANDLE Summary of this class goes here
% Detailed explanation goes here
properties
x = 0;
y = 0;
theta = 0;
end
methods
end
end
Then i started writing my main which looks like this:
rosinit('192.168.179.98');%build connection with turtle 3
% Instiantiate PoseHandle
% your code (task 21) goes here
pose = PoseHandle;
goIntoLoop = true;
if goIntoLoop
maxTime = 20; % Maximum loop time (loop terminate after maxTime sec)
% initialize callback subscriber
% your code (task 21+22) goes here
% initialize data arrays (x, y, theta, t) for plotting
xdata =size(1);
ydata =size(1);
thetadata = size(1);
t = size(1);
% initialize figure
fig = figure(5);
hold on;
title('Figure using class');%set figure titel
odomSubCallback = rossubscriber('/odom',{@odomCallback,pose,fig,3});
i=1;% initialize array counter
rateObj.reset; % reset time at the beginning of the loop
while rateObj.TotalElapsedTime < maxTime
% append values from pose handles to data arrays
% your code (task 21+22) goes here
%odomCallback(odomSub,pose,fig)% normal function
xdata(i) = pose.x; %load x-position
ydata(i) = pose.y;%load y-position
thetadata(i) = pose.theta;%load thetha
t(i) = rateObj.TotalElapsedTime; %get time
i = i+1;%increase counter
waitfor(rateObj);%wait
end
pause(1);
clear odomSubCallback;
% Plot robot pose components versus time
% your code goes here
plot(t,xdata,'.-m');
plot(t,ydata,'--bx');
plot(t,thetadata,'--gx');
disp('Loop ended');
end
what the main do is: it builds a connection to a roscore then try to get using a callback function the coordinates of a robot.
the callback function looks like this:
function [] = odomCallback(~,odomMsg ,pose, 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
% your code (task 16) goes here
%global xr yr thetar
figure(fig);% Use global or handle objects.
odomMsg =receive(odomMsg);
%[ xr, yr, ~ ] = OdometryMsg2Pose( odomMsg );% convert odom to pose vector
[ pose.x, pose.y, pose.theta ] = OdometryMsg2Pose( odomMsg );% convert class obj to pose vector
ylabel("X-Pose/Y-Pose/tetha-Pose" );
xlabel("t-Pose");
%plot(xr,yr,'--rx');% plot pose vector with quiver
end
What weird is when i call the callback function as a normal function a get a figure as follow:
when i use the callback function all my variables are set to zero which i really don't understand and i get such a figure out:
thanks in advance
0 个评论
采纳的回答
Cam Salzberger
2022-4-25
Hello Zakaria,
I'm not certain, but I suspect the issue is that you have this line:
odomMsg =receive(odomMsg);
The "odomMsg" that is passed into your callback is already coming out of the subscriber. If you call "receive" on it within the callback, it blocks until the next message is received (which calls the callback again). Because you don't have anything indicating the callback should "give up the thread" at any point, I suspect MATLAB never actually finishes executing the callback - it just keeps working down an infinite recursion of waiting for messages and calling the callback.
Remember that there are three ways to access a message from the subscriber. Typically, you only want to use one of them with any one subscriber:
And between the three, "receive" has the fewest use-cases where it is the "correct" or "best" way of doing things. I put together an answer that goes into design patterns a bit more here, if you want some more information.
-Cam
更多回答(1 个)
Richard
2022-4-24
I think that you might have incorrect syntax for the function definition of the rossubscriber callback. The documentation for the NewMessageFcn property of rossubscriber specifies that the callback must have at least 2 inputs - src and msg, followed by any additional inputs you provide. In this case I would expect the function to be defined as:
function odomCallback(src, odomMsg, pose, fig)
3 个评论
Richard
2022-4-24
Hi Zakaria, I'm afraid I don't have any concrete suggestions. However you should be able to set debug breakpoints in your loop and/or in the callback function and step through the code. This will allow you to determine whether the callback function is being called and if se what is happening to the values from the messages.
If the callbacks seem to work while debugging then one possibility is that the callbacks are unable to interrupt the tight loop that you have which is pulling values out of the pose object. If that is the problem then inserting a call to drawnow or pause at the start of the loop should allow the pending callbacks to execute when not debugging.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specialized Messages 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!