handle class obj is getting only zero

4 ビュー (過去 30 日間)
Zakaria Boussaid
Zakaria Boussaid 2022 年 4 月 22 日
コメント済み: Zakaria Boussaid 2022 年 4 月 25 日
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

採用された回答

Cam Salzberger
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:
  1. NewMessageFcn callback
  2. LatestMessage property
  3. receive method
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 件のコメント
Zakaria Boussaid
Zakaria Boussaid 2022 年 4 月 25 日
I'm really grateful for your help it worked

サインインしてコメントする。

その他の回答 (1 件)

Richard
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
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.
Zakaria Boussaid
Zakaria Boussaid 2022 年 4 月 25 日
Thank you again but the Problem i'm using ros toolbox which means using breakpoints when interacting with ROS can be tricky because we pause only the MATLAB code execution, but not the ROS code exectionen.
When we set a breakpoint, MATLAB stops the code execution and won't handle any of the messages. Instead, these messages are queued for later use.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePublishers and Subscribers についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by