필터 지우기
필터 지우기

How to store ROS callback data in class properties?

조회 수: 3 (최근 30일)
Alexandre Teixeira
Alexandre Teixeira 2015년 8월 3일
댓글: AYKUT SIRMA 2022년 8월 26일
Hello folks,
I am trying to store a callback data from ROS's topic called /scan in a properties (laser) of class that I created.
I got an error like this:
"No public field laser exists for class robotics.ros.Subscriber.
Error in MobileRobot/Callback_Laser (line 14) obj.laser = [message.Ranges];
Error in @(varargin)robot.Callback_Laser(varargin{:})
Error in robotics.ros.internal.onNewMessageCallback (line 44) feval(callbackFcn, source, message, userData{:});
Warning: Error occurred while evaluating listener callback."
Where is my mistake e how can I store a the callback data in a properties class?
Next the code class.
classdef MobileRobot < handle
properties(Access = public)
odom=[];
laser = [];
end
methods(Access = public)
function obj = MobileRobot() % Constructor
end
function Callback_Laser(~, obj, message)
% global laser
% laser = [message.Ranges];
obj.laser = [message.Ranges];
end
end
end
and the main code
clear all;close all;clc;
rosinit
global rosMasterIp;
rosMasterIp = 'http://192.168.0.113:11311';
global localhostIp;
localhostIp = '192.168.0.113';
global robot;
robot = MobileRobot();
set_pose = robotics.ros.Node('getLaser',rosMasterIp,'NodeHost' , localhostIp);
subs = rossubscriber('/scan',@robot.Callback_Laser);
Thank you!

채택된 답변

Sebastian Castro
Sebastian Castro 2015년 8월 3일
The issue here seems to be that the callback tied to rossubscriber is required to have 2 inputs "src" and "msg", where "src" is the subscriber itself and "msg" is the message received.
Notice that the way you have defined your Callback_laser method, "obj" isn't the instance of the class, but rather a reference to the subscriber... which, as the error message suggests, does not have a "laser" property.
Does it work if you change your method to be the following?
function Callback_Laser(obj, src, message)
obj.laser = [message.Ranges];
end
- Sebastian
  댓글 수: 2
Alexandre Teixeira
Alexandre Teixeira 2015년 8월 4일
It worked perfectly. I didn't see rossubscriber definition. Thank you.
AYKUT SIRMA
AYKUT SIRMA 2022년 8월 26일
To receive only the Axes and Buttons data readings from 'sensor_msgs/Joy' message, what do I need to do? I try to write a callback function to get these 'sensor_msgs/Joy' to access Axes and Buttons information and put them into an array to use. I will be very glad if you would guide me, thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Services and Actions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by