currently I am writing a robot operating system (ros) node in python. I want to create a while loop which accepts user input on the one hand but is able to continue if no user input is available.
The idea of the following snippet is, that this python-script offers always 'start' or 'stop'. Another node is listening what string gets published. The user should be able to type in 0 or 1 at runtime to toggle the flag.
Here is my python code:
def main():
pub = rospy.Publisher('/start_stop', String, queue_size=10)
rospy.init_node('start_stop', anonymous = True);
rate=rospy.Rate(10) # 10hz
pubStr = "Start"
while not rospy.is_shutdown():
try:
input = raw_input()
if input == "0":
pubStr = "Stop"
elif input == "1":
pubStr = "Start"
except:
rospy.sleep(0.1)
rospy.loginfo(pubStr)
pub.publish(pubStr)
rate.sleep()
if __name__ == '__main__':
main();