Error with custom action in ros2 on R2025a Macbook 15.5

5 views (last 30 days)
% Script to create ApplyTorque.action, TestMessage.msg, and run ros2genmsg with enhanced debugging
% Step 1: Define paths
customFolder = '/Users/xxxx/MATLAB/Projects/utilities/custom';
actionFolder = fullfile(customFolder, 'action');
msgFolder = fullfile(customFolder, 'msg');
% Create directories
if ~exist(customFolder, 'dir')
mkdir(customFolder);
end
if ~exist(actionFolder, 'dir')
mkdir(actionFolder);
end
if ~exist(msgFolder, 'dir')
mkdir(msgFolder);
end
% Step 2: Create ApplyTorque.action
actionFilePath = fullfile(actionFolder, 'ApplyTorque.action');
actionContent = [
'# Goal\n' ...
'float64[6] torque\n' ...
'\n' ...
'---\n' ...
'# Result\n' ...
'bool success\n' ...
'\n' ...
'---\n' ...
'# Feedback\n' ...
'float64[6] positions\n'
];
fid = fopen(actionFilePath, 'w', 'n', 'UTF-8');
if fid == -1
error('Cannot create file at %s', actionFilePath);
end
fprintf(fid, '%s', actionContent);
fclose(fid);
% Step 3: Create a test .msg file
msgFilePath = fullfile(msgFolder, 'TestMessage.msg');
msgContent = 'string name\nint32 age\n';
fid = fopen(msgFilePath, 'w', 'n', 'UTF-8');
if fid == -1
error('Cannot create file at %s', msgFilePath);
end
fprintf(fid, '%s', msgContent);
fclose(fid);
% Step 4: Verify files and permissions
fprintf('Verifying folder structure:\n');
actionFiles = dir(fullfile(actionFolder, '*.action'));
msgFiles = dir(fullfile(msgFolder, '*.msg'));
if isempty(actionFiles)
warning('No .action files found in %s', actionFolder);
else
fprintf('Found .action file: %s\n', actionFiles(1).name);
end
if isempty(msgFiles)
warning('No .msg files found in %s', msgFolder);
else
fprintf('Found .msg file: %s\n', msgFiles(1).name);
end
% Check permissions
[status, attr] = fileattrib(customFolder);
if status && attr.UserRead && attr.UserWrite
fprintf('Permissions OK for %s\n', customFolder);
else
fprintf('Fixing permissions for %s\n', customFolder);
system(['chmod -R u+rw ' customFolder]);
end
% Step 5: Display paths
fprintf('Action file created at: %s\n', actionFilePath);
fprintf('Message file created at: %s\n', msgFilePath);
fprintf('Folder path for ros2genmsg: %s\n', customFolder);
% Step 6: Validate ROS2 environment
fprintf('Checking ROS2 environment...\n');
try
ros2('node', 'list'); % Test ROS2 connectivity
fprintf('ROS2 environment appears accessible.\n');
catch
warning('ROS2 environment not accessible. Please configure in Preferences > ROS Toolbox.\n');
end
% Step 7: Run ros2genmsg
fprintf('Attempting ros2genmsg with both .action and .msg files...\n');
try
ros2genmsg(customFolder);
fprintf('Successfully generated ROS2 message interfaces.\n');
catch e
fprintf('Error running ros2genmsg: %s\n', e.message);
fprintf('Debug: Trying with only .msg file...\n');
% Temporarily move action folder
tempActionFolder = fullfile(customFolder, 'action_temp');
movefile(actionFolder, tempActionFolder);
try
ros2genmsg(customFolder);
fprintf('Successfully generated .msg interfaces. Issue may be specific to .action files.\n');
catch e2
fprintf('Error with .msg only: %s\n', e2.message);
fprintf('Debug: Check ROS2 environment in Preferences > ROS Toolbox and MATLAB/ROS2 compatibility.\n');
end
% Restore action folder
movefile(tempActionFolder, actionFolder);
end
This is the output
Verifying folder structure:
Found .action file: ApplyTorque.action
Found .msg file: TestMessage.msg
Permissions OK for /Users/xxxx/MATLAB/Projects/utilities/custom
Action file created at: /Users/xxxx/MATLAB/Projects/utilities/custom/action/ApplyTorque.action
Message file created at: /Users/xxxx/MATLAB/Projects/utilities/custom/msg/TestMessage.msg
Folder path for ros2genmsg: /Users/xxxx/MATLAB/Projects/utilities/custom
Checking ROS2 environment...
ROS2 environment appears accessible.
Attempting ros2genmsg with both .action and .msg files...
Identifying message files in folder '/Users/xxxx/MATLAB/Projects/utilities/custom'..Done.
Error running ros2genmsg: Unable to find packages with '.msg' files, or '.srv' files, or '.action' files in /Users/xxxx/MATLAB/Projects/utilities/custom. Each message package must contain '.msg' files in a directory named 'msg', or '.srv' files in a directory named 'srv', or '.action' files in a directory named 'action', or a combination of any of these directories.
Debug: Trying with only .msg file...
Identifying message files in folder '/Users/xxxx/MATLAB/Projects/utilities/custom'..Done.
Error with .msg only: Unable to find packages with '.msg' files, or '.srv' files, or '.action' files in /Users/xxxx/MATLAB/Projects/utilities/custom. Each message package must contain '.msg' files in a directory named 'msg', or '.srv' files in a directory named 'srv', or '.action' files in a directory named 'action', or a combination of any of these directories.
Debug: Check ROS2 environment in Preferences > ROS Toolbox and MATLAB/ROS2 compatibility.
  4 Comments
Walter Roberson
Walter Roberson on 21 Jun 2025
I agree.
I wonder, though, whether it just might help to end customFolder with a / -- so asking it to look in /Users/xxxx/MATLAB/Projects/utilities/custom/ instead of in /Users/xxxx/MATLAB/Projects/utilities/custom . Just in case it is failing to put in the / itself when it is looking for the msg or action folders.
Carlos Javier
Carlos Javier on 21 Jun 2025
I did it both ways. I have also changed it in different locations. But it is still generating the same errors.

Sign in to comment.

Answers (1)

Carlos Javier
Carlos Javier on 21 Jun 2025
Moved: Walter Roberson on 22 Jun 2025
with the help of cursor and testing a lot i could solve it:
% Simple script to create ROS2 message and action files
% Create package directory structure
customFolder = fullfile(pwd, 'custom');
packageFolder = fullfile(customFolder, 'ultimate_msgs'); % Create ultimate_msgs package
actionFolder = fullfile(packageFolder, 'action');
msgFolder = fullfile(packageFolder, 'msg');
% Create directories
if ~exist(customFolder, 'dir')
mkdir(customFolder);
end
if ~exist(packageFolder, 'dir')
mkdir(packageFolder);
end
if ~exist(actionFolder, 'dir')
mkdir(actionFolder);
end
if ~exist(msgFolder, 'dir')
mkdir(msgFolder);
end
% Create package.xml (required for ROS2 package recognition)
packageXmlContent = sprintf(['<?xml version="1.0"?>\n' ...
'<package format="3">\n' ...
' <name>ultimate_msgs</name>\n' ...
' <version>0.0.1</version>\n' ...
' <description>Custom messages and actions for ultimate robot arm</description>\n' ...
' <maintainer email="your_email@example.com">Your Name</maintainer>\n' ...
' <license>BSD</license>\n\n' ...
' <buildtool_depend>ament_cmake</buildtool_depend>\n' ...
' <build_depend>rosidl_default_generators</build_depend>\n' ...
' <exec_depend>rosidl_default_runtime</exec_depend>\n' ...
' <member_of_group>rosidl_interface_packages</member_of_group>\n\n' ...
' <export>\n' ...
' <build_type>ament_cmake</build_type>\n' ...
' </export>\n' ...
'</package>']);
fid = fopen(fullfile(packageFolder, 'package.xml'), 'w');
fprintf(fid, '%s', packageXmlContent);
fclose(fid);
% Create ApplyTorque.action
actionContent = sprintf('# Goal\nfloat64[6] torque\n---\n# Result\nbool success\n---\n# Feedback\nfloat64[6] positions');
fid = fopen(fullfile(actionFolder, 'ApplyTorque.action'), 'w');
fprintf(fid, '%s', actionContent);
fclose(fid);
% Create TestMessage.msg
msgContent = sprintf('string name\nint32 age');
fid = fopen(fullfile(msgFolder, 'TestMessage.msg'), 'w');
fprintf(fid, '%s', msgContent);
fclose(fid);
% Run ros2genmsg
try
ros2genmsg(customFolder); % Point to the parent folder containing ultimate_msgs
disp('Successfully generated ROS2 message interfaces.');
catch ME
fprintf('Error: %s\n', ME.message);
end
  1 Comment
Walter Roberson
Walter Roberson on 22 Jun 2025
Moved: Walter Roberson on 22 Jun 2025
It is not clear to me what the key change was?
I see that you moved the key files one more layer down in the hierarchy.
I see that you added a package.xml file.
I see that you corrected a mistake where before you wrote 'string name\nint32 age' to a file and now you sprintf() that first so that the \n are converted to newlines properly.
But none of these changes look significant? Was the major change adding the package.xml file ?

Sign in to comment.

Categories

Find more on Custom Message Support in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!