MiRo-Training
The aim of the project is to develop a software architecture for interacting with Miro using vocal and gestural commands.
 All Classes Files Functions Variables
command_recognition.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ################################################################
4 
5 import rospy
6 from std_msgs.msg import String,Bool,Int32
7 from sensor_msgs.msg import Image,CompressedImage,Range,Imu
8 from geometry_msgs.msg import Twist,Pose
9 
10 import miro_msgs
11 from miro_msgs.msg import platform_config,platform_sensors,platform_state,platform_mics,platform_control,core_state,core_control,core_config,bridge_config,bridge_stream
12 
13 import opencv_apps
14 from opencv_apps.msg import CircleArrayStamped
15 
16 import math
17 import numpy
18 import time
19 import sys
20 from miro_constants import miro
21 from datetime import datetime
22 
23 ## \file command_recognition.py
24 ## \brief The node command_recognition.py recognize vocal command converted as String and publish the associated robot's action
25 ## @n The vocal command "Miro" bring the robot in a default configuration and enables the processing of further commands.
26 ## @n Different commands are managed. Each command publishes a message of type platform_control with the corresponding action.
27 ## @n The action are handled independently by separated nodes.
28 ## @n The current node subscribes to them and, when the associated command is received, publishes on MiRo the msg content.
29 
30 ##\brief The class CommandRecognition handles the different incoming commands and publish on the robot the corresponding action
32 
33  ## Constructor
34  def __init__(self):
35 
36  ## Node rate
37  self.rate = rospy.get_param('rate',200)
38 
39  #topic root
40  ## Allow to switch from real robot to simulation from launch file
41  self.robot_name = rospy.get_param ( '/robot_name', 'rob01')
42  topic_root = "/miro/" + self.robot_name
43  print "topic_root", topic_root
44 
45  ## Initialization of the enabling command
46  self.activate = False
47  ## Initialization of the string to evaluate
48  self.command = String()
49 
50  #------------------ ADD OBJECTS OF NEW COMMANDS ----------------------#
51 
52  ## Platform control Object that represents the sleep action ("Sleep")
53  self.q_sleep = platform_control()
54  ## Platform control Object that represents the miro action when it is scolded ("Bad")
55  self.q_sad = platform_control()
56  ## Platform control Object that represents the miro action when it follows the Ball ("Play")
57  self.q_play = platform_control()
58  ## platform_control object that rapresents the Gesture Based Behavior ("Let's go out")
59  self.q_gbb = platform_control()
60  ## Platform control Object that represents the miro action when it is praised ("Good")
61  self.q_good = platform_control()
62  ## Platform control Object that represents the miro action when it is praised ("Kill")
63  self.q_kill = platform_control()
64 
65  ## Subscriber to the topic /speech_to_text a message of type String that cointains the vocal command converted in text
66  self.sub_speech_to_text = rospy.Subscriber('/speech_to_text', String, self.callback_receive_command,queue_size=1)
67 
68  #------------------ ADD SUBSCRIBERS TO NEW COMMANDS -------------------#
69  ## Subscriber to the topic /miro_sleep a message of type platform_control that rapresents the action corresponting to the command "Sleep"
70  self.sub_sleep_action = rospy.Subscriber('/miro_sleep', platform_control, self.callback_sleep_action,queue_size=1)
71  ## Subscriber to the topic /miro_sad a message of type platform_control that rapresents the action corresponting to the command "Bad"
72  self.sub_sad_action = rospy.Subscriber('/miro_sad', platform_control, self.callback_sad_action,queue_size=1)
73  ## Subscriber to the topic /miro_follow a message of type platform_control that rapresents the action corresponting to the command "Play"
74  self.sub_play_action = rospy.Subscriber('/miro_play', platform_control, self.callback_play_action,queue_size=1)
75  ## Subscriber to the topic /miro_dance a message of type platform_control that rapresents the action corresponting to the command "Let's go out"
76  self.sub_gbb = rospy.Subscriber('/gbb', platform_control, self.callback_gbb,queue_size=1)
77  ## Subscriber to the topic /miro_good a message of type platform_control that rapresents the action corresponting to the command "Good"
78  self.sub_good_action = rospy.Subscriber('/miro_good', platform_control, self.callback_good_action,queue_size=1)
79  ## Subscriber to the topic /miro_good a message of type platform_control that rapresents the action corresponting to the command "Kill"
80  self.sub_kill_action = rospy.Subscriber('/miro_kill', platform_control, self.callback_kill_action,queue_size=1)
81 
82  ## Publisher to the topic /platform/control a message of type platform_control which execute Miro actions
83  self.pub_platform_control = rospy.Publisher(topic_root + "/platform/control", platform_control, queue_size=0)
84 
85 
86  ## Callback function that receive and save the user's voice command as text
87  def callback_receive_command(self, text):
88 
89  self.command = text.data
90 
91  #------------------ ADD CALLBACK FOR NEW COMMANDS -------------------#
92  ## Callback that receives the action to be executed when the vocal command is "Sleep"
93  def callback_sleep_action(self, sleep):
94 
95  self.q_sleep = sleep
96 
97  ## Callback that receives the action to be executed when the vocal command is "Bad"
98  def callback_sad_action(self, sad):
99 
100  self.q_sad = sad
101 
102  ## Callback that receives the action to be executed when the vocal command is "Play"
103  def callback_play_action(self, play):
104 
105  self.q_play = play
106  self.q_play.body_config = [0.0,0.0,0.0,0.0]
107  self.q_play.body_config_speed = [0.0,-1.0,-1.0,-1.0]
108 
109  ## Callback that receives the action to be executed when the vocal command is "Let's go"
110  def callback_gbb(self,gbb):
111 
112  self.q_gbb = gbb
113 
114  ## Callback that receives the action to be executed when the vocal command is "Good"
115  def callback_good_action(self, good):
116 
117  self.q_good = good
118 
119  ## Callback that receives the action to be executed when the vocal command is "Kill"
120  def callback_kill_action(self, kill):
121 
122  self.q_kill = kill
123 
124  ## Function that check the incoming commands and publish the corresponding action
125  ## @n The command "Miro" brings the robot in a default configuration the first time is used. The variable activate is set to True and enables the evauation of the other commands.
126  ## @n The command "Bad" is executed only if self.active is True and publish the action managed by the node bad.py
127  ## @n The command "Good" is executed only if self.active is True and publish the action managed by the node good.py
128  ## @n The command "Play" is executed only if self.active is True and publish the action managed by the node play.py
129  ## @n The command "Let's go out" is executed only if self.active is True and publish the action managed by the node gbb_miro.py
130  ## @n The command "Sleep" is executed only if self.active is True and publish the action managed by the node sleep.py.
131  ## @n The variable activate is set to False and Miro remains in sleep mode until a new command "Miro" is received.
133 
134  q = platform_control()
135  q.eyelid_closure = 1.0
136 
137  r = rospy.Rate(self.rate)
138  count_bad = 0
139  count_miro = 0
140  count_sleep = 0
141 
142  while not rospy.is_shutdown():
143 
144  #ACTIVATION COMMAND
145 
146  if self.command == "Miro" or self.command == " Miro" or self.command == "miro" or self.command == " miro":
147  count_miro = 0
148  count_miro = count_miro +1
149  rospy.loginfo(count_miro)
150  count_sleep = 0
151 
152  if count_miro == 1:
153  q.eyelid_closure = 0.0
154  q.lights_raw = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
155  q.tail = -1.0
156  q.ear_rotate = [0.0,0.0]
157  q.body_config = [0.0,0.0,0.0,0.0]
158  q.body_config_speed = [0.0,-1.0,-1.0,-1.0]
159  self.pub_platform_control.publish(q)
160 
161  self.activate = True
162 
163  # SLEEP
164  if self.activate and self.command == "Sleep" or self.command == " Sleep" or self.command == "sleep" or self.command == " sleep":
165  count_miro = 0
166  count_bad = 0
167  q = self.q_sleep
168  self.pub_platform_control.publish(q)
169  self.activate = False
170  count_sleep = 1
171  print "Sleep"
172 
173  # BAD
174  elif self.activate and (self.command == "Bad" or self.command == " Bad" or self.command == "bad" or self.command == " bad"):
175  count_bad = count_bad + 1
176  rospy.loginfo(count_bad)
177  if count_bad < 2000:
178  q = self.q_sad
179  self.pub_platform_control.publish(q)
180  else:
181  q.body_vel.linear.x = 0.0
182  q.body_vel.angular.z = 0.0
183  q.lights_raw = [255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0]
184  self.pub_platform_control.publish(q)
185  print "Bad"
186 
187  # PLAY
188  elif self.activate and (self.command == "Play" or self.command == " Play" or self.command == "play" or self.command == " play"):
189  count_bad = 0
190  q = self.q_play
191  self.pub_platform_control.publish(q)
192 
193  # LET'S GO OUT
194  elif self.activate and (self.command == "Let's go out" or self.command == " Let's go out" or self.command == "let's go out" or self.command == " let's go out"):
195  count_bad = 0
196  q.body_config = [0.0,0.0,0.0,0.0]
197  q.body_config_speed = [0.0,-1.0,-1.0,-1.0]
198  q = self.q_gbb
199  self.pub_platform_control.publish(q)
200  print "Let's go out"
201 
202  # GOOD
203  elif self.activate and (self.command == "Good" or self.command == " Good" or self.command == "good" or self.command == " good"):
204  count_bad = 0
205  q = self.q_good
206  self.pub_platform_control.publish(q)
207  print "Good"
208 
209  # KILL
210  elif self.activate and (self.command == "Kill" or self.command == " Kill" or self.command == "kill" or self.command == " kill"):
211  count_bad = 0
212  q = self.q_kill
213  self.pub_platform_control.publish(q)
214  print "Kill"
215 
216  # HANDLING OF DIFFERENT COMMANDS
217  elif count_sleep == 1 and (not self.activate and not self.command == "Sleep"):
218  rospy.loginfo(count_sleep)
219  q.eyelid_closure = 1
220  self.pub_platform_control.publish(q)
221 
222  r.sleep()
223 
224 if __name__== '__main__':
225 
226  rospy.init_node('command_recognition')
227  sb = CommandRecognition()
228  sb.switching_commands()
robot_name
Allow to switch from real robot to simulation from launch file.
activate
Initialization of the enabling command.
q_gbb
platform_control object that rapresents the Gesture Based Behavior ("Let's go out") ...
sub_good_action
Subscriber to the topic /miro_good a message of type platform_control that rapresents the action corr...
def callback_sleep_action
Callback that receives the action to be executed when the vocal command is "Sleep".
def switching_commands
Function that check the incoming commands and publish the corresponding action The command "Miro" b...
sub_sad_action
Subscriber to the topic /miro_sad a message of type platform_control that rapresents the action corre...
def callback_good_action
Callback that receives the action to be executed when the vocal command is "Good".
sub_play_action
Subscriber to the topic /miro_follow a message of type platform_control that rapresents the action co...
q_sleep
Platform control Object that represents the sleep action ("Sleep")
pub_platform_control
Publisher to the topic /platform/control a message of type platform_control which execute Miro action...
command
Initialization of the string to evaluate.
sub_sleep_action
Subscriber to the topic /miro_sleep a message of type platform_control that rapresents the action cor...
sub_gbb
Subscriber to the topic /miro_dance a message of type platform_control that rapresents the action cor...
def callback_gbb
Callback that receives the action to be executed when the vocal command is "Let's go"...
def callback_play_action
Callback that receives the action to be executed when the vocal command is "Play".
q_good
Platform control Object that represents the miro action when it is praised ("Good") ...
q_kill
Platform control Object that represents the miro action when it is praised ("Kill") ...
q_play
Platform control Object that represents the miro action when it follows the Ball ("Play") ...
sub_kill_action
Subscriber to the topic /miro_good a message of type platform_control that rapresents the action corr...
q_sad
Platform control Object that represents the miro action when it is scolded ("Bad") ...
The class CommandRecognition handles the different incoming commands and publish on the robot the cor...
def callback_receive_command
Callback function that receive and save the user's voice command as text.
def callback_kill_action
Callback that receives the action to be executed when the vocal command is "Kill".
def callback_sad_action
Callback that receives the action to be executed when the vocal command is "Bad". ...
sub_speech_to_text
Subscriber to the topic /speech_to_text a message of type String that cointains the vocal command con...