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
play.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
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 
22 from datetime import datetime
23 
24 ## \file play.py
25 ## \brief The node play.py implements the action corresponding to the command "Play".
26 ## @n The node subscribe to the circles detected in the Left and Right camera.
27 ## @n The Robot moves in order to keep the ball in both camera frames.
28 
29 ## \brief The class BallDetection implements the detection of the circles in the cameras and the robot behavior to keep the circles in sight
30 class BallDetection():
31 
32 
33  def __init__(self):
34 
35  ## Node rate
36  self.rate = rospy.get_param('rate',200)
37 
38  ## Initialization of the CircleArrayStamped variable in the Right callback
39  self.detect_right = 0
40  ## Initialization of the CircleArrayStamped variable in the Left callback
41  self.detect_left = 0
42 
43  ## Flag for Red Ball detected in Right camera
44  self.ball_right = False
45  ## Flag for Red Ball detected in Leftcamera
46  self.ball_left = False
47  ## Flag for Red Ball detected in Both cameras
48  self.ball = False
49 
50  ## Number of positive detection on Right camera
51  self.count_right = 0
52  ## Number of positive detection on Left camera
53  self.count_left = 0
54  ## Number of negative detection on Right camera
55  self.count_no_right = 0
56  ## Number of negative detection on Left camera
57  self.count_no_left = 0
58  ## Number of positive detection on Both cameras
59  self.count_ball = 0
60 
61  ## Subscriber to the topic /right/hough_circles/circles a message of type CircleArrayStamped that cointains the information about the circles detected in the Right camera.
62  self.sub_camera_right = rospy.Subscriber('right/hough_circles/circles', CircleArrayStamped, self.callback_camera_right,queue_size=1)
63  ## Subscriber to the topic /right/hough_circles/circles a message of type CircleArrayStamped that cointains the information about the circles detected in the Left camera.
64  self.sub_camera_left = rospy.Subscriber('left/hough_circles/circles', CircleArrayStamped, self.callback_camera_left,queue_size=1)
65  ## Publisher to the topic /miro_play a message of type platform_control which corresponds to the "Play" action.
66  self.pub_follow_ball = rospy.Publisher('miro_play',platform_control,queue_size=0)
67 
68 
69  ## Callback that receive the message that contains the information of the circles detected in the Right camera.
70  ## @n If the ball is detected for more than 3 times the flag self.ball_right is set to True.
71  ## @n If the ball is not detected for more than 3 times the flag self.ball_right is set to False.
72  def callback_camera_right(self, ball):
73 
74  self.detect_right = ball.circles
75 
76  if not self.detect_right:
77  self.count_no_right = self.count_no_right + 1
78  if self.count_no_right > 3:
79  self.count_right = 0
80  print "NO DETECTION IN RIGHT CAMERA"
81  self.ball_right = False
82 
83  else:
84  self.count_right = self.count_right + 1
85  if self.count_right > 3:
86  self.count_no_right = 0
87  print "BALL DETECTED IN RIGHT CAMERA"
88  self.ball_right = True
89 
90  ## Callback that receive the message that contains the information of the circles detected in the Left camera.
91  ## @n If the ball is detected for more than 3 times the flag self.ball_right is set to True.
92  ## @n If the ball is not detected for more than 3 times the flag self.ball_right is set to False.
93  def callback_camera_left(self, ball):
94 
95  self.detect_left = ball.circles
96 
97  if not self.detect_left:
98  self.count_no_left = self.count_no_left + 1
99  if self.count_no_left > 3:
100  self.count_left = 0
101  print "NO DETECTION IN LEFT CAMERA"
102  self.ball_left = False
103 
104  else:
105  self.count_left = self.count_left + 1
106  if self.count_left > 3:
107  self.count_no_left = 0
108  print "BALL DETECTED IN LEFT CAMERA"
109  self.ball_left = True
110 
111  ## Function that evaluate the detection of the ball and publish a message of type platform_control in order to keep the ball always in sight.
112  ## @n If the ball is detected in the Right camera, the robot will rotate towards right until the ball is detected also by the Left camera.
113  ## @n If the ball is detected in the Left camera, the robot will rotate towards left until the ball is detected also by the Right camera.
114  ## @n If the ball is detected in both cameras, the robot will go forward to reach the ball.
116 
117  r = rospy.Rate(self.rate)
118  q = platform_control()
119 
120  while not rospy.is_shutdown():
121  if self.ball_right and self.ball_left:
122  self.count_ball = self.count_ball + 1
123  # rospy.loginfo(self.count_ball)
124  if self.count_ball > 3:
125  self.ball = True
126  print "DETECTION COMPLETE"
127  q.body_vel.linear.x = 100.0
128  q.body_vel.angular.z = 0.0
129  q.lights_raw = [255,150,0,255,150,0,255,150,0,255,150,0,255,150,0,255,150,0]
130  self.pub_follow_ball.publish(q)
131 
132  else:
133  self.count_ball = 0
134  self.ball = False
135  #q.body_vel.linear.x = 0.0
136  #q.body_vel.angular.z = 0.0
137  print "NO COMPLETE DETECTION"
138  if self.ball_right:
139  q.body_vel.linear.x = 0.0
140  q.body_vel.angular.z = -0.2
141  elif self.ball_left:
142  q.body_vel.linear.x = 0.0
143  q.body_vel.angular.z = 0.2
144  else:
145  q.body_vel.linear.x = 0.0
146  q.body_vel.angular.z = 0.0
147 
148  self.pub_follow_ball.publish(q)
149  r.sleep()
150 
151 
152 if __name__== '__main__':
153  rospy.init_node('play')
154  ball = BallDetection()
155  ball.compared_detection()
156 
157 
158 
pub_follow_ball
Publisher to the topic /miro_play a message of type platform_control which corresponds to the "Play" ...
Definition: play.py:66
count_no_right
Number of negative detection on Right camera.
Definition: play.py:55
rate
Node rate.
Definition: play.py:36
detect_left
Initialization of the CircleArrayStamped variable in the Left callback.
Definition: play.py:41
ball_left
Flag for Red Ball detected in Leftcamera.
Definition: play.py:46
count_no_left
Number of negative detection on Left camera.
Definition: play.py:57
ball
Flag for Red Ball detected in Both cameras.
Definition: play.py:48
def callback_camera_right
Callback that receive the message that contains the information of the circles detected in the Right ...
Definition: play.py:72
def callback_camera_left
Callback that receive the message that contains the information of the circles detected in the Left c...
Definition: play.py:93
sub_camera_left
Subscriber to the topic /right/hough_circles/circles a message of type CircleArrayStamped that cointa...
Definition: play.py:64
The class BallDetection implements the detection of the circles in the cameras and the robot behavior...
Definition: play.py:30
count_left
Number of positive detection on Left camera.
Definition: play.py:53
count_right
Number of positive detection on Right camera.
Definition: play.py:51
count_ball
Number of positive detection on Both cameras.
Definition: play.py:59
detect_right
Initialization of the CircleArrayStamped variable in the Right callback.
Definition: play.py:39
sub_camera_right
Subscriber to the topic /right/hough_circles/circles a message of type CircleArrayStamped that cointa...
Definition: play.py:62
ball_right
Flag for Red Ball detected in Right camera.
Definition: play.py:44
def compared_detection
Function that evaluate the detection of the ball and publish a message of type platform_control in or...
Definition: play.py:115