How to change the color of a rigidbody visual.

조회 수: 32 (최근 30일)
Control Systems Guy
Control Systems Guy 2023년 12월 22일
댓글: Control Systems Guy 2023년 12월 28일
Hello,
I am creating a rigid body (robot link) and would like to change the color of the visual. For example:
body1 = rigidBody('body1');
jnt1 = rigidBodyJoint('jnt1','revolute');
tform = trvec2tform([0 1 0]);
setFixedTransform(jnt1,tform);
body1.Joint = jnt1;
robot = rigidBodyTree('DataFormat', 'column');
addVisual(body1, "sphere", 1.2)
addBody(robot,body1,'base')
show(robot,"Visuals", "on", 'Frames', 'off')
It always generates the figure in gray scale. I would like to assign a different color to different bodies. How can I do that? I know that I can also add collision boxes or spheres, but they are similarly of the same color by default (green).

채택된 답변

Chunru
Chunru 2023년 12월 23일
body1 = rigidBody('body1');
jnt1 = rigidBodyJoint('jnt1','revolute');
tform = trvec2tform([0 1 0]);
setFixedTransform(jnt1,tform);
body1.Joint = jnt1;
robot = rigidBodyTree('DataFormat', 'column');
addVisual(body1, "sphere", 1.2)
addBody(robot,body1,'base')
g = show(robot,"Visuals", "on", 'Frames', 'off');
% You can change color of individual object
g.Children(1).FaceColor = 'r';
  댓글 수: 1
Control Systems Guy
Control Systems Guy 2023년 12월 23일
편집: Control Systems Guy 2023년 12월 23일
This is great! Thank you very much! I still have 2 questions/problems I would like an answer.
1) Is there a way to change the color of all bodies at once? Something like: g.Children(1:end).FaceColor
For example, if I have a robot (rigidbody) with two links:
body1 = rigidBody('body1');
jnt1 = rigidBodyJoint('jnt1','revolute');
tform = trvec2tform([0 1 0]);
setFixedTransform(jnt1,tform);
body1.Joint = jnt1;
robot = rigidBodyTree('DataFormat', 'column');
addVisual(body1, "sphere", 1.2)
addBody(robot,body1,'base')
body2 = rigidBody('body2');
jnt2 = rigidBodyJoint('jnt2','revolute');
tform2 = trvec2tform([1, 0, 0]);
setFixedTransform(jnt2,tform2);
body2.Joint = jnt2;
addVisual(body2, "capsule", [.5, 3],[0 0 1 0; 0 1 0 0; -1 0 0 0; 0 0 0 1])
addBody(robot,body2,'body1'); % Add body2 to body1
figure(1)
g = show(robot,"Visuals", "on", 'Frames', 'off');
g.Children(1).FaceColor = 'r';
2) Can I make the color change permanent? I am working with a robot of multiple links that will rotate and move, and I would like to avoid having to change the color at each step of the animation.
Once again, thank you!

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

추가 답변 (1개)

Chunru
Chunru 2023년 12월 24일
See below for the question (1). I am not sure the part (2) and you may want to show your current code.
body1 = rigidBody('body1');
jnt1 = rigidBodyJoint('jnt1','revolute');
tform = trvec2tform([0 1 0]);
setFixedTransform(jnt1,tform);
body1.Joint = jnt1;
robot = rigidBodyTree('DataFormat', 'column');
addVisual(body1, "sphere", 1.2)
addBody(robot,body1,'base')
body2 = rigidBody('body2');
jnt2 = rigidBodyJoint('jnt2','revolute');
tform2 = trvec2tform([1, 0, 0]);
setFixedTransform(jnt2,tform2);
body2.Joint = jnt2;
addVisual(body2, "capsule", [.5, 3],[0 0 1 0; 0 1 0 0; -1 0 0 0; 0 0 0 1])
addBody(robot,body2,'body1'); % Add body2 to body1
figure(1)
g = show(robot,"Visuals", "on", 'Frames', 'off');
% Change color for all applicable children
for i=1:length(g.Children)
try
g.Children(i).FaceColor = 'r';
end
end
  댓글 수: 1
Control Systems Guy
Control Systems Guy 2023년 12월 28일
Chunru, thanks again for helping me out. What I meant by the 2nd question is that I would like to avoid having to specificy the FaceColor every single time I call the robot again. For example:
body1 = rigidBody('body1');
jnt1 = rigidBodyJoint('jnt1','revolute');
tform = trvec2tform([0 1 0]);
setFixedTransform(jnt1,tform);
body1.Joint = jnt1;
robot = rigidBodyTree('DataFormat', 'column');
addVisual(body1, "sphere", 1.2)
addBody(robot,body1,'base')
body2 = rigidBody('body2');
jnt2 = rigidBodyJoint('jnt2','revolute');
tform2 = trvec2tform([1, 0, 0]);
setFixedTransform(jnt2,tform2);
body2.Joint = jnt2;
addVisual(body2, "capsule", [.5, 3],[0 0 1 0; 0 1 0 0; -1 0 0 0; 0 0 0 1])
addBody(robot,body2,'body1'); % Add body2 to body1
figure(1)
g = show(robot,"Visuals", "on", 'Frames', 'off');
% Change color for all applicable children
for i=1:length(g.Children)
try
g.Children(i).FaceColor = 'r';
end
end
for k = 1:5
q = [k/5; 0];
g = show(robot,q,"Visuals", "on", 'Frames', 'off');
drawnow
end
If I used one of the predefined robot models, I see that some models have a different color (other than gray) specified by default. In the following example, one doesnt need to speficy the color every single time I want to update the robot configuration. Maybe rather than using "show" I can use "set(robot,...)?
figure(2)
robot2 = importrobot('iiwa14.urdf');
robot2.DataFormat = "column";
for k = 1:5
q = [0 0 0 k*pi/10 0 0 pi/2]';
g = show(robot2,q,"Visuals", "on", "Frames","off");
drawnow
end

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

카테고리

Help CenterFile Exchange에서 Robotics에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by