how to draw a circle with Rviz?

asked 2021-10-31 23:28:05 -0500

Redhwan gravatar image

updated 2022-01-31 09:43:03 -0500

lucasw gravatar image

I would like to draw a circle with Rviz based on the persons' location using python to represent the person.

I have (x,y) for every person on the map after converting 3D to 2D on the map, as you see in Figure.

    # encoding: utf-8
#https://www.elephantrobotics.com/docs/myCobot-en/6-kit/2-preparation/3-build_cube.html
import rospy
import time
# from visualization_msgs.msg import
from visualization_msgs.msg import Marker
from visualization_msgs.msg import MarkerArray

class Send_marker(object):
    def __init__(self):
        # Inherit object class objects
        super(Send_marker, self).__init__()
        # Initialize a node. If the node is not created, the information cannot be published
        rospy.init_node("send_marker", anonymous=True)
        # Create a publisher to publish marker
        self.pub = rospy.Publisher("/test_marker", Marker, queue_size=1)
        # Create a block model that marker uses to create
        self.marker = Marker()
        # Configure its ownership relationship, and its coordinates are relative to / joint1.
        # /Joint1 represents the bottom of the manipulator in the model.
        self.marker.header.frame_id = "/odom"
        # Set marker's name
        self.marker.ns = "test_marker"
        # Set the type of marker to be square
        self.marker.type = self.marker.CYLINDER
        # Set marker's action to add (marker without this name will add one)
        self.marker.action = self.marker.ADD
        # Set the actual size of marker, in m
        self.marker.scale.x = 0.04
        self.marker.scale.y = 0.04
        self.marker.scale.z = 0.04
        # Set marker's color, 1.0 for 255 (this represents a ratio conversion)
        self.marker.color.a = 10.0
        self.marker.color.g = 10.0
        self.marker.color.r = 10.0
        # Initialize marker's position and its four-dimensional attitude
        self.marker.pose.position.x = 0
        self.marker.pose.position.y = 0
        self.marker.pose.position.z = 0.03
        self.marker.pose.orientation.x = 0
        self.marker.pose.orientation.y = 0
        self.marker.pose.orientation.z = 0
        self.marker.pose.orientation.w = 1.0

    # Modify coordinates and publish marker
    def pub_marker(self, x, y, z=0.03):
        # Set marker's timestamp
        self.marker.header.stamp = rospy.Time.now()
        # Set marker's spatial coordinates
        self.marker.pose.position.x = x
        self.marker.pose.position.y = y
        self.marker.pose.position.z = z
        # Release marker
        self.pub.publish(self.marker)

    # Let marker happen y
    def run(self):
        time.sleep(1) 
        self.pub_marker(0.1, -0.1)
        time.sleep(1)
        self.pub_marker(0.15, 0)
        time.sleep(1)
        self.pub_marker(0.15, -0.1)
        time.sleep(1)
        self.pub_marker(0.2, -0.1)
        time.sleep(1)
        self.pub_marker(0.15, -0.05)
        time.sleep(1)


if __name__ == '__main__':
    marker = Send_marker()
    marker.run()

image description

edit retag flag offensive close merge delete

Comments

Quick comment: look at markers. See Markers: Sending Basic Shapes (C++) for an example (in C++, but it's just messages, so translates to Python easily).

gvdhoorn gravatar image gvdhoorn  ( 2021-11-01 02:53:19 -0500 )edit

Thanks, I can't find a method for a circle. I will put the full code as an update to my question. I found some explain here, but I can't solve it as an open circle.

Redhwan gravatar image Redhwan  ( 2021-11-01 03:55:05 -0500 )edit
1

It's not a method to "draw a circle". It's a way to place arbitrary 3D / 2D shapes in the 3D rendering offered by RViz.

A circle can be approximated by a flattened cylinder, as you already found. Alternatively, a line-strip (but that would show discretisation probably).


Edit: it seems there are quite a few older Q&As which discuss similar topics. See #q326136 and #q64801 for instance, which seem to suggest the same approaches (line-strip or flat cylinder).

gvdhoorn gravatar image gvdhoorn  ( 2021-11-01 04:12:30 -0500 )edit

in c

p.x = 500 * mm2m;
p.y = 200 * mm2m;
p.z = 290 * mm2m; // YUKARI ASSA

float r = 0.05;
for ( int angle = 0; angle < 360; angle += 1 )
{
    p.x = r * cos( angle * deg2rad );
    p.y = 0.5 + r * sin( angle * deg2rad );

    line.points.push_back( p );   //DRAW WITH LINE
    point.points.push_back( p );  //DRAW WITH POINT
    trajectoryPath.push_back( p );  //SAVE FOR LATER
}
omeranar1 gravatar image omeranar1  ( 2021-11-01 05:47:34 -0500 )edit