Subscripted Assignment to Create Array Element
15 次查看(过去 30 天)
显示 更早的评论
Hi,
I am using MATLAB with Robotics System Toolbox. I am running a Gazebo simulation in ROS that I want MATLAB to talk to. In particular, I want to make a node for a given topic related to that simulation. The way to do this is following (as I understood from the tutorials and examples):
pub_cmd=rospublisher('<any topic that receives commands>');
pub_cmd_msg=rosmessage(pub_cmd)
Now, pub_cmd_msg returns the following:
pub_cmd_msg =
ROS JointTrajectory message with properties:
MessageType: 'trajectory_msgs/JointTrajectory'
Header: [1x1 Header]
JointNames: {0x1 cell}
Points: [0x1 JointTrajectoryPoint]
Use showdetails to show the contents of the message.
Now as I try to assign some values to different fields in this pub_cmd_msg, I get the following error:
pub_cmd_msg.Points
ans =
0x1 ROS JointTrajectoryPoint message array with properties:
MessageType
TimeFromStart
Positions
Velocities
Accelerations
Effort
>> pub_cmd_msg.Points.Positions=ones(1,7)
Property assignment is not allowed when the object is empty. Use subscripted assignment to create an array element.
I have no idea what the error means and how can subscripted assignment help? Kindly guide how can I solve this problem.
0 个评论
采纳的回答
Benjamin Brandwin
2017-4-26
编辑:Benjamin Brandwin
2017-4-26
This was one tough cookie but I figured it out. The key was actually in the PR2 example code. If you read it carefully and compare it to the data types in the jointMsg below you can figure this out.
[jointClient,jointMsg] = ...
rosactionclient('/robot/limb/right/follow_joint_trajectory');
Take a look at jointMsg
jointMsg =
ROS FollowJointTrajectoryGoal message with properties:
MessageType: 'control_msgs/FollowJointTrajectoryGoal'
Trajectory: [1×1 JointTrajectory]
GoalTimeTolerance: [1×1 Duration]
PathTolerance: [0×1 JointTolerance]
GoalTolerance: [0×1 JointTolerance]
We look closer to find an object called jointMsg.Trajectory.Points.Position but when we do this :
>> jointMsg.Trajectory.Points.Positions = [0;0;0;0;0;0;0]
Property assignment is not allowed when the object is empty.
Use subscripted assignment to create an array element.
We dig a little deeper an find
>> jointMsg.Trajectory.Points
ans =
0×1 ROS JointTrajectoryPoint message array with properties:
MessageType
TimeFromStart
Positions
Velocities
Accelerations
Effort
Ok, so we have a JointTrajectoryPoint object. So, we will make a JointTractoryPoint message:
joint_send = rosmessage('trajectory_msgs/JointTrajectoryPoint')
joint_send =
ROS JointTrajectoryPoint message with properties:
MessageType: 'trajectory_msgs/JointTrajectoryPoint'
TimeFromStart: [1×1 Duration]
Positions: [0×1 double]
Velocities: [0×1 double]
Accelerations: [0×1 double]
Effort: [0×1 double]
Use showdetails to show the contents of the message
Now we plug in Positions and velocities
>> joint_send.Positions = zeros(7,1);
>> joint_send.Velocities = zeros(7,1);
Now we set our original message
jointMsg.Trajectory.Points = joint_send;
Lets take a look
jointMsg.Trajectory.Points
ans =
ROS JointTrajectoryPoint message with properties:
MessageType: 'trajectory_msgs/JointTrajectoryPoint'
TimeFromStart: [1×1 Duration]
Positions: [7×1 double]
Velocities: [7×1 double]
Accelerations: [0×1 double]
Effort: [0×1 double]
Use showdetails to show the contents of the message
Voila
Now that your object is no longer empty you can work with it directly.
>> jointMsg.Trajectory.Points.Positions = [1;1;1;1;1;1;1]
>> jointMsg.Trajectory.Points.Positions
ans =
1
1
1
1
1
1
1
0 个评论
更多回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Publishers and Subscribers 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!