2015-09-23 12:12:59 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
import rospy
|
|
|
|
|
import sys
|
|
|
|
|
from std_msgs.msg import Bool
|
|
|
|
|
from rtabmap_ros.msg import Goal
|
|
|
|
|
|
|
|
|
|
pub = rospy.Publisher('/rtabmap/goal_node', Goal, queue_size=1)
|
|
|
|
|
waypoints = []
|
|
|
|
|
currentIndex = 0
|
2018-12-07 18:31:59 -05:00
|
|
|
waitingTime = 1.0
|
2015-09-23 12:12:59 -04:00
|
|
|
|
|
|
|
|
def callback(data):
|
|
|
|
|
global currentIndex
|
2018-12-07 18:31:59 -05:00
|
|
|
global waitingTime
|
2015-09-23 12:12:59 -04:00
|
|
|
if data.data:
|
2018-12-07 18:31:59 -05:00
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": Goal '%s' reached! Publishing next goal in %.1f sec...", waypoints[currentIndex], waitingTime)
|
2015-09-23 12:12:59 -04:00
|
|
|
else:
|
2018-12-07 18:31:59 -05:00
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": Goal '%s' failed! Publishing next goal in %.1f sec...", waypoints[currentIndex], waitingTime)
|
2015-09-27 12:49:44 -04:00
|
|
|
|
|
|
|
|
currentIndex = (currentIndex+1) % len(waypoints)
|
2015-09-23 12:12:59 -04:00
|
|
|
|
2018-12-07 18:31:59 -05:00
|
|
|
# Waiting time before sending next goal
|
|
|
|
|
rospy.sleep(waitingTime)
|
2015-09-23 12:12:59 -04:00
|
|
|
|
|
|
|
|
msg = Goal()
|
2018-12-07 18:31:59 -05:00
|
|
|
try:
|
|
|
|
|
int(waypoints[currentIndex])
|
|
|
|
|
is_dig = True
|
|
|
|
|
except ValueError:
|
|
|
|
|
is_dig = False
|
|
|
|
|
if is_dig:
|
2015-09-27 12:49:44 -04:00
|
|
|
msg.node_id = int(waypoints[currentIndex])
|
|
|
|
|
msg.node_label = ""
|
|
|
|
|
else:
|
|
|
|
|
msg.node_id = 0
|
|
|
|
|
msg.node_label = waypoints[currentIndex]
|
2018-12-07 18:31:59 -05:00
|
|
|
|
|
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": Publishing goal '%s'! (%d/%d)", waypoints[currentIndex], currentIndex+1, len(waypoints))
|
2015-09-23 12:12:59 -04:00
|
|
|
msg.header.stamp = rospy.get_rostime()
|
|
|
|
|
pub.publish(msg)
|
|
|
|
|
|
|
|
|
|
def main():
|
2015-09-27 12:49:44 -04:00
|
|
|
rospy.init_node('patrol', anonymous=False)
|
2015-09-23 12:12:59 -04:00
|
|
|
rospy.Subscriber("/rtabmap/goal_reached", Bool, callback)
|
2018-12-07 18:31:59 -05:00
|
|
|
global waitingTime
|
|
|
|
|
waitingTime = rospy.get_param('~time', waitingTime)
|
2015-09-23 12:12:59 -04:00
|
|
|
rospy.sleep(1.) # make sure that subscribers have seen this node before sending a goal
|
|
|
|
|
|
2018-12-07 18:31:59 -05:00
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": Waypoints: [%s]", str(waypoints).strip('[]'))
|
|
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": time: %f", waitingTime)
|
|
|
|
|
|
2015-09-23 12:12:59 -04:00
|
|
|
# send the first goal
|
|
|
|
|
msg = Goal()
|
2018-12-07 18:31:59 -05:00
|
|
|
try:
|
|
|
|
|
int(waypoints[currentIndex])
|
|
|
|
|
is_dig = True
|
|
|
|
|
except ValueError:
|
|
|
|
|
is_dig = False
|
|
|
|
|
if is_dig:
|
2015-09-27 12:49:44 -04:00
|
|
|
msg.node_id = int(waypoints[currentIndex])
|
|
|
|
|
msg.node_label = ""
|
|
|
|
|
else:
|
|
|
|
|
msg.node_id = 0
|
|
|
|
|
msg.node_label = waypoints[currentIndex]
|
2015-09-23 12:12:59 -04:00
|
|
|
while rospy.Time.now().secs == 0:
|
2018-12-07 18:31:59 -05:00
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": Waiting clock...")
|
2015-09-23 12:12:59 -04:00
|
|
|
rospy.sleep(.1)
|
|
|
|
|
msg.header.stamp = rospy.Time.now()
|
2018-12-07 18:31:59 -05:00
|
|
|
rospy.loginfo(rospy.get_caller_id() + ": Publishing goal '%s'! (%d/%d)", waypoints[currentIndex], currentIndex+1, len(waypoints))
|
2015-09-23 12:12:59 -04:00
|
|
|
pub.publish(msg)
|
|
|
|
|
rospy.spin()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if len(sys.argv) < 3:
|
2018-12-07 18:31:59 -05:00
|
|
|
print("usage: patrol.py waypointA waypointB waypointC ... [_time:=1] [topic remaps] (at least 2 waypoints, can be node id, landmark or label)")
|
2015-09-23 12:12:59 -04:00
|
|
|
else:
|
|
|
|
|
waypoints = sys.argv[1:]
|
2018-12-07 18:31:59 -05:00
|
|
|
waypoints = [x for x in waypoints if not x.startswith('/') and not x.startswith('_')]
|
2015-09-23 12:12:59 -04:00
|
|
|
main()
|