demo.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #coding=utf-8
3 
4 import rospy, yaml, os, json,time
5 
6 from xbot_face.msg import FaceResult
7 from xbot_talker.srv import chat, play
8 from std_msgs.msg import String, UInt32, UInt8, Bool
9 from geometry_msgs.msg import Pose, PoseStamped
10 from actionlib_msgs.msg import GoalStatusArray
11 from move_base_msgs.msg import MoveBaseActionResult
12 from std_srvs.srv import Empty
13 
14 class demo():
15  """docstring for welcome"""
16  def __init__(self):
17 # 声明节点订阅与发布的消息
18 
19  # 发布目标点信息
20  self.move_base_goal_pub = rospy.Publisher('/move_base_simple/goal', PoseStamped, queue_size=1)
21  # 订阅人脸识别结果
22  self.face_result_sub = rospy.Subscriber('/xbot/face_result', FaceResult, self.faceCB)
23  # 订阅是否到达目标点结果
24  self.move_base_result_sub = rospy.Subscriber('/move_base/result', MoveBaseActionResult, self.move_base_resultCB)
25  # 请求清除costmap服务
26  self.clear_costmaps_srv = rospy.ServiceProxy('/move_base/clear_costmaps',Empty)
27  # 订阅是否收到visit信息
28  self.visit_sub = rospy.Subscriber('/demo/leave', Bool, self.visitCB)
29 
30  # 请求chat服务
31  self.chat_srv = rospy.ServiceProxy('/xbot/chat',chat)
32  # 请求播放服务
33  self.play_srv = rospy.ServiceProxy('/xbot/play',play)
34 
35 
36 # 记录机器人当前的目标点
37  self.current_goal = 0
38 
39  # self.VIP_name = 'wp'
40 
41  self.greet_last_time = time.time()
42 
43  self.getVIP = False
46 
47 # 读取一存储的讲解点字典文件,默认位于xbot_s/param/position_dic.yaml文件
48  self.kp_path = rospy.get_param('/demo/kp_path','/home/roc/ros_kinetic_ws/xbot-u/src/xbot_navi/param/kp.json')
49  self.greet_path = rospy.get_param('/demo/greet_path','/home/roc/ros_kinetic_ws/xbot-u/src/xbot_navi/param/greet.json')
50 # yaml_path = yaml_path + '/scripts/position_dic.yaml'
51  with open(self.kp_path, 'r') as json_file:
52  self.kp_list = json.load(json_file)
53  json_file.close()
54 
55  with open(self.greet_path,'r') as f:
56  self.greet_dict = json.load(f)
57  f.close()
58  rospy.spin()
59 
60  def greeting(self,name):
61  if name == 'unknown':
62  self.play_srv(False,2,'','您好,我还不认识您,如有需要请注册。')
63  else:
64  resp1 = self.play_srv(False,2,'',self.greet_dict[name]['greet_words'])
65  if resp1 and self.greet_dict[name]['isVIP']:
66  self.getVIP = True
67  self.chat_srv(True)
68  # self.pub_kp()
69 
70 
71 
72  def faceCB(self,msg):
73  if not self.getVIP:
74  if not msg.face_exist:
75  self.known_face_times = 0
76  self.unknown_face_times = 0
77  else:
78  if msg.confidence >0.55:
79  self.known_face_times+=1
80  self.unknown_face_times = 0
81  else:
82  self.unknown_face_times +=1
83  self.known_face_times = 0
84 
85 
86  if time.time()-self.greet_last_time>5:
87  if self.known_face_times >=5:
88  self.greeting(msg.name)
89  self.known_face_times = 0
90 
91 
92  if self.unknown_face_times >=10:
93  self.greeting('unknown')
94  self.unknown_face_times = 0
95 
96  self.greet_last_time = time.time()
97  else:
98  pass
99 
100  def pub_kp(self):
101  if self.current_goal < len(self.kp_list):
102  pos = self.kp_list[self.current_goal]['pose']
103  goal = PoseStamped()
104  goal.header.frame_id = 'map'
105  goal.pose.position.x = pos[0][0]
106  goal.pose.position.y = pos[0][1]
107  goal.pose.position.z = pos[0][2]
108  goal.pose.orientation.x = pos[1][0]
109  goal.pose.orientation.y = pos[1][1]
110  goal.pose.orientation.z = pos[1][2]
111  goal.pose.orientation.w = pos[1][3]
112  print goal
113  self.move_base_goal_pub.publish(goal)
114 
115  def visitCB(self, msg):
116  if msg.data:
117  end_talk = self.play_srv(False, 2, '', '好的,您请跟我来!')
118  if end_talk:
119  self.pub_kp()
120 
121 
122 # 导航程序对前往目标点的执行结果
123  def move_base_resultCB(self, result):
124  if result.status.status == 3:
125 # 成功到达目标点
126  kp = self.kp_list[self.current_goal]
127  if kp['play']:
128  resp = self.play_srv(False, 2, '', kp['play_words'])
129  if resp:
130  self.current_goal+=1
131  self.clear_costmaps_srv()
132  self.pub_kp()
133  else:
134  self.current_goal += 1
135  self.clear_costmaps_srv()
136  self.pub_kp()
137  elif result.status.status == 4:
138 # 到达目标点失败,slam发布abort信号给talker,talker会请求前方人员让一下,然后重新规划路径尝试去往目标点
139 # TODO:此时启用胸前深度摄像头确认前方障碍物情况再决定是否请求人员让一下
140  self.pub_kp()
141 
142 
143 
144 
145 if __name__ == '__main__':
146  rospy.init_node('demo_node')
147  try:
148  rospy.loginfo('demo node initialized...')
149  demo()
150  except rospy.ROSInterruptException:
151  rospy.loginfo('demo node initialize failed, please retry...')
move_base_goal_pub
Definition: demo.py:20
def move_base_resultCB(self, result)
Definition: demo.py:123
def visitCB(self, msg)
Definition: demo.py:115
play_srv
Definition: demo.py:33
unknown_face_times
Definition: demo.py:45
greet_dict
Definition: demo.py:56
def __init__(self)
Definition: demo.py:16
clear_costmaps_srv
Definition: demo.py:26
move_base_result_sub
Definition: demo.py:24
def greeting(self, name)
Definition: demo.py:60
greet_last_time
Definition: demo.py:41
greet_path
Definition: demo.py:49
kp_path
Definition: demo.py:48
chat_srv
Definition: demo.py:31
current_goal
Definition: demo.py:37
def faceCB(self, msg)
Definition: demo.py:72
visit_sub
Definition: demo.py:28
face_result_sub
Definition: demo.py:22
getVIP
Definition: demo.py:43
kp_list
Definition: demo.py:52
known_face_times
Definition: demo.py:44
def pub_kp(self)
Definition: demo.py:100


xbot_navi
Author(s):
autogenerated on Sat Oct 10 2020 03:27:50