ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

Your program structure needs a bit of a change so that it will do what you want. Here are some points:

  • You should initialise your node and create your subscribers once only.
  • The rospy.spin() function will spin forever until you close the node down manually.

Bearing these in mind you will need to change your code so that inside main you initialise your node once and create the subscriber once. Then you'll need to create the rospy.Rate object. Finally still within main you'll need to add your while loop.

This while loop will include the rate.sleep() to keep it at the desired frequency. Within this loop you can call your error function, control function and finally your serial connection.

Note, the body of the while loop may well execute before the callback function so you'll have to make sure your code is safe to do this.

Hope this helps.

Your program structure needs a bit of a change so that it will do what you want. Here are some points:

  • You should initialise your node and create your subscribers once only.
  • The rospy.spin() function will spin forever until you close the node down manually.

Bearing these in mind you will need to change your code so that inside main you initialise your node once and create the subscriber once. Then you'll need to create the rospy.Rate object. Finally still within main you'll need to add your while loop.

This while loop will include the rate.sleep() to keep it at the desired frequency. Within this loop you can call your error function, control function and finally your serial connection.

Note, the body of the while loop may well execute before the callback function so you'll have to make sure your code is safe to do this.

Hope this helps.

UPDATE:

Just a style node for you code. You should generally make the start point of code within main, at the moment you do all your initialisation in the global scope of the script before main is reached. Although this will work it's not considered good practice. If you move the following lines and place them between try: and the while statement in main it will tidy this up.

rospy.init_node('serial_connection', anonymous = True)

print (ser.name)
time.sleep(2)

print(sending_data)

rospy.Subscriber('real_position', String, callback)
rate = rospy.Rate(1) #isws na min xreiazetai