MATLAB coder error while using 'rospublisher'

I'm facing the error while using MATLAB coder. The error shows 'Value cannot be displayed because the output of 'coder.ceval' does not have a known type. To display the output, first assign the result to a variable of known type.' , and the error was due to
pub = rospublisher("/path", "geometry_msgs/Point", "DataFormat", "struct");
And below is part of my code:
r = rosrate(10);
pub = rospublisher("/path", "geometry_msgs/Point", "DataFormat", "struct");
% ...
while true
% Create a new ROS message
msg = rosmessage(pub);
msg.Data = struct_target_path;
% Publish the message
send(pub, msg);
waitfor(r);
end
What I want to do is to publish a structure message(struct_target_path) with the topic named "/path".
I have already tried various methods, but all of them didn't work.
Thank you for anyone who can help me to fix it.

Risposte (1)

Hi JhenMin,
The message struct you are creating in while loop using rosmessage API, is of message type "geometry_msgs/Point". This type has no such field 'Data'. Instead it has fields X, Y and Z. So, it is not allowed to do the following:
msg.Data = struct_target_path;
Instead the below syntax is allowed
msg.X = 10;
Thanks,
Karthik Reddy

5 Commenti

Thanks for your replying!
While,after modifying the code as you mentioned, the error is still apear.
How can i fix it?
Hi JhenMin,
Could you share the complete script for which you are generating the code, to see if there is any other line of code that could be causing this error.
Thanks,
Karthik Reddy
Thank you for your prompt reply.
The following is my complete code.
%#codegen
function pathplanning0727
% subscribe: segnet_img=>uint8(480,640,3)
% publish:target_path
%==================== subscribe segnet_img ====================
segnet_img_sub = rossubscriber("/segnet/color_mask","sensor_msgs/Image","DataFormat","struct"); %subscribe the segnet img
ros_msg = rosmessage(segnet_img_sub);
segnet_img = rosReadImage(ros_msg);
segnet_img = im2uint8(segnet_img);
%==================== crop image ====================
segnet_img = im2uint8(segnet_img);
cropped_img = zeros([200 440 1],'uint8');
cropped_img(1:200,1:440,1) = segnet_img(251:450,101:540,3);
%==================== threshold setting and polybuffer ====================
cropped_img_3 = cropped_img(:,:,1); % 'blue' channel of RGB-images
cropped_img_3(cropped_img_3>=50) = uint8(255); % threshold setting
cropped_img_3(cropped_img_3<49) = uint8(0);
uint8 L
[B,L] = bwboundaries(cropped_img_3); % get max perimeter
perim = zeros([1 0],'uint8');
for k = 1:1:uint8(size(B,1))
perim = [perim size(B{k,:},1)];
end
Max_perim = uint8(find(perim == max(perim)));
B1_cell = B{Max_perim,:};
B1 = B1_cell;
Obj_perim_length = size(B1,1); % perimeter length in pixels
xp = uint8(B1(:,2)); % x in pixel coordinates
yp = uint8(B1(:,1)); % y in pixel coordinates
D = zeros(size(segnet_img),'uint8');
for k = 1:1:numel(xp) % numel(xp):number of units in xp
D(yp(k),xp(k)) = 1;
end
n_polybuffer1 = uint8(15);
n_polybuffer2 = uint8(-35);
B1_polyshape = polyshape(B1(:,2)+100,B1(:,1)+250);
pb1 = polybuffer(B1_polyshape,n_polybuffer1);
pb2 = polybuffer(pb1,n_polybuffer2);
pb2_vertices = double(pb2.Vertices);
[isnan_row, isnan_col] = find(isnan(pb2_vertices));
[n1 n2] = size(isnan_row);
size_pb2_vertices = size(pb2_vertices);
break_point = zeros((n1/2)+2,1,'uint8');
break_point(1) = uint8(1);
break_point(2:(n1/2)+1) = uint8(isnan_row(1:n1/2));
break_point((n1/2)+2) = uint8(size_pb2_vertices(1,1));
area = zeros([1 2],'uint8');
area_size = zeros(0,'uint8');
for i_1 = 1:(n1/2)+1
pb2_vertices_a = uint8(break_point(i_1));
pb2_vertices_b = uint8(break_point(i_1+1)-1);
area_a = uint8(2*i_1-1);
area_b = uint8(2*i_1);
area(pb2_vertices_a:pb2_vertices_b,area_a:area_b) = pb2_vertices(pb2_vertices_a:pb2_vertices_b,:);
area_size(i_1) = pb2_vertices_b;
end
area_size_top = uint8(maxk(area_size,1));
selected_area = area_size_top;
path_area = zeros([1 2]);
for k = 1:(n1/2)+1
if (break_point(k+1)-break_point(k)) == selected_area
path_area = pb2_vertices(break_point(k):break_point(k+1),:); %path_area(m,n)=(y,x)
end
end
%==================== boundary setting ====================
path_area_sorted = sortrows(path_area);
[path_area_sorted_m path_area_sorted_n] = size(path_area_sorted);
boundary_1 = zeros(1,2);
boundary_2 = zeros(1,2);
boundary_c1 = 1;
boundary_c2 = 1;
divided_point = uint8(1);
for l = 2:path_area_sorted_m
if path_area_sorted(l,1)-path_area_sorted(l-1,1) > 60
divided_point = uint8(l-1);
end
end
boundary_1 = path_area_sorted(1:divided_point,:);
boundary_2 = path_area_sorted(divided_point+1:end-1,:);
% setting the p_boundary as a straight line if there is only a point in boundary
if size(boundary_1) == [1 2]
p_boundary1 = [0 boundary_1(1,1)]
else
p_boundary1 = polyfit(boundary_1(:,2),boundary_1(:,1),1);
end
if size(boundary_2) == [1 2]
p_boundary2 = [0 boundary_2(1,1)];
else
p_boundary2 = polyfit(boundary_2(:,2),boundary_2(:,1),1);
end
x_boundary = double(linspace(251,450));
f_boundary1 = double(polyval(p_boundary1,x_boundary));
f_boundary2 = double(polyval(p_boundary2,x_boundary));
target_path_boundary = (f_boundary1+ f_boundary2)/2;
target_path = zeros([100 2],'double');
target_path(:,1) = target_path_boundary+251;
target_path(:,2) = x_boundary;
p_path = polyfit(target_path(:,2),target_path(:,1),1);
p_path_1 = p_path(1);
p_path_2 = p_path(2);
% % ==================== publish the designed path ====================
r = rosrate(10);
pub = rospublisher('path','geometry_msgs/Point',"DataFormat","struct");
msg = rosmessage('geometry_msgs/Point',"DataFormat","struct");
msg.X = target_path(:,2);
msg.Y = target_path(:,1);
send(pub,msg);
end
Hi JhenMin,
Are you able to directly run the above script without any issues in MATLAB, but facing issues only using MATLAB Coder when generating the code?
Thanks,
Karthik Reddy
Yes, it runs well in MATLAB, but only facing the issue while generating the code. And it also faced the same error when i used the function
[ros_msg,status,statustext] = receive(segnet_img_sub);

Accedi per commentare.

Prodotti

Release

R2023a

Tag

Richiesto:

il 26 Lug 2023

Commentato:

il 1 Ago 2023

Community Treasure Hunt

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

Start Hunting!

Translated by