Error with custom action in ros2 on R2025a Macbook 15.5
5 views (last 30 days)
Show older comments
% 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
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.
Answers (1)
Carlos Javier
on 21 Jun 2025
Moved: Walter Roberson
on 22 Jun 2025
1 Comment
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 ?
See Also
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!