Ask Your Question
1

How to change color of turtlesim background using python code

asked 2018-02-21 16:48:09 -0500

bharadwaj26 gravatar image

updated 2018-02-21 17:11:14 -0500

Hi, guys, I'm trying to publish data to turtlesim color_sensor that is trying to display different colors my problem is it publishing data but it's not working. I did call $ rosservice call /clear after running program but it didn't work

 #!/usr/bin/env python

    import rospy
    from turtlesim.msg import Color

    rospy.init_node('color_sensor')
    pub = rospy.Publisher('/turtle1/color_sensor',Color,queue_size=4)
    rate = rospy.Rate(50)
    change=Color()
    change.r=255
    change.g=255
    change.b=255

    while not rospy.is_shutdown():
       pub.publish(change)
       print  change
edit retag flag offensive close merge delete

Comments

but it didn't work

what does this mean?

jayess gravatar image jayess  ( 2018-02-21 19:25:30 -0500 )edit

it's not publishing those RGB values to color_sensor, even though I am publishing it.i,e Background color is not changing to White(255,255,255)

bharadwaj26 gravatar image bharadwaj26  ( 2018-02-21 19:35:01 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-02-22 01:21:54 -0500

Hamid Didari gravatar image

updated 2018-02-22 01:22:19 -0500

for changing turtlesim Background color you must set param not publish via topic you do something like this in your code:

rospy.set_param('/background_b',100)
rospy.set_param('/background_r',100)
rospy.set_param('/background_g',100)

after that you must call

rosservice call /clear

in terminal or in your code

edit flag offensive delete link more

Comments

can't we publish the rgb values to the color_sensor and change color ,like we publish linear and angular data to cmd_vel in turtlesim to move the turtle

bharadwaj26 gravatar image bharadwaj26  ( 2018-02-27 12:56:22 -0500 )edit
0

answered 2020-09-09 16:21:41 -0500

updated 2020-10-17 06:08:26 -0500

turtlesim_node reads the values of the paramaters 'background_b' , 'background_r' and 'background_r' only after the calling the rosservice 'clear' . That's why, after updating the params, we need to call the '/clear' service.

      import rospy
      from std_srvs.srv import Empty


      def change_background_color_client():


              rospy.wait_for_service('/clear')

              try: 

                rospy.set_param('/turtlesim/background_r' , 255)
                rospy.set_param('/turtlesim/background_g' , 0)
                rospy.set_param('/turtlesim/background_b' , 0)

                change_background_color = rospy.ServiceProxy('/clear', Empty)

                resp = change_background_color()

                return resp

              except rospy.ServiceException as e:
               rospy.loginfo(f'Service failed with the exception {e}')


            if __name__ == "__main__" :

              change_background_color_client()
edit flag offensive delete link more

Comments

Could you please update your answer with an explanation of what your code does?

jayess gravatar image jayess  ( 2020-09-11 18:26:37 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-02-21 16:48:09 -0500

Seen: 1,117 times

Last updated: Oct 17 '20