fixed RuntimeError('Cannot capture')

This commit is contained in:
2018-06-23 13:29:10 +02:00
parent 126329bafd
commit 4536a4d1ac
2 changed files with 32 additions and 31 deletions

View File

@@ -21,8 +21,9 @@ class NaoImageReader(object):
self.res = self.RESOLUTIONS[res] self.res = self.RESOLUTIONS[res]
self.cam_id=cam_id self.cam_id=cam_id
self.vd = ALProxy('ALVideoDevice', ip, port) self.vd = ALProxy('ALVideoDevice', ip, port)
streamer_name = '_'.join(['lower' if cam_id else 'upper', str(res)])
self.sub = self.vd.subscribeCamera( self.sub = self.vd.subscribeCamera(
"video_streamer", cam_id, res, 13, fps streamer_name, cam_id, res, 13, fps
) )
def to_angles(self, x, y): def to_angles(self, x, y):
@@ -36,10 +37,8 @@ class NaoImageReader(object):
def get_frame(self): def get_frame(self):
result = self.vd.getImageRemote(self.sub) result = self.vd.getImageRemote(self.sub)
self.vd.releaseImage(self.sub) self.vd.releaseImage(self.sub)
if result == None: if result is None or result[6] is None:
raise RuntimeError('cannot capture') raise RuntimeError("Couldn't capture")
elif result[6] == None:
raise ValueError('no image data string')
else: else:
height, width = self.res height, width = self.res
return np.frombuffer(result[6], dtype=np.uint8).reshape( return np.frombuffer(result[6], dtype=np.uint8).reshape(

View File

@@ -37,7 +37,10 @@ class Striker(object):
self.last_speak = None self.last_speak = None
def speak(self, text): def speak(self, text):
if (self.tts_thread is None or not self.tts_thread.isAlive() ) and text!=self.last_speak: if (
(self.tts_thread is None or not self.tts_thread.isAlive())
and text != self.last_speak
):
self.tts_thread = Thread( self.tts_thread = Thread(
target=lambda text: self.speaker.say(str(text)), target=lambda text: self.speaker.say(str(text)),
args=(text,) args=(text,)
@@ -45,8 +48,6 @@ class Striker(object):
self.tts_thread.start() self.tts_thread.start()
self.last_speak = text self.last_speak = text
def ball_scan(self): def ball_scan(self):
"""Intelligently rotate the robot to search for stuff.""" """Intelligently rotate the robot to search for stuff."""
yaw = self.mover.get_head_angles()[0] yaw = self.mover.get_head_angles()[0]
@@ -68,7 +69,12 @@ class Striker(object):
def get_ball_angles_from_camera(self, cam): def get_ball_angles_from_camera(self, cam):
"""Detect the ball and return its angles in camera coordinates.""" """Detect the ball and return its angles in camera coordinates."""
try:
ball = self.ball_finder.find(cam.get_frame()) ball = self.ball_finder.find(cam.get_frame())
except RuntimeError as e:
print(e)
return None
if ball is None: if ball is None:
return None return None
@@ -153,7 +159,6 @@ class Striker(object):
self.mover.move_to(0, 0, yaw) self.mover.move_to(0, 0, yaw)
self.mover.wait() self.mover.wait()
def align_to_ball(self): def align_to_ball(self):
ball_angles = self.get_ball_angles_from_camera(self.lower_camera) ball_angles = self.get_ball_angles_from_camera(self.lower_camera)
if ball_angles is None: if ball_angles is None:
@@ -302,7 +307,6 @@ if __name__ == '__main__':
striker.mover.kick() striker.mover.kick()
striker.mover.rest() striker.mover.rest()
# perform normal state-machine if no input argument is given # perform normal state-machine if no input argument is given
# (see diagram above) # (see diagram above)
else: else:
@@ -328,8 +332,6 @@ if __name__ == '__main__':
print(ball_in_lower) print(ball_in_lower)
if (ball_in_lower is not None and ball_in_lower[1] > 0.28): if (ball_in_lower is not None and ball_in_lower[1] > 0.28):
#t = Thread(target=striker.speak, args=["Ball is close enough, stop approach"])
#t.start()
print('Ball is close enough, stop approach') print('Ball is close enough, stop approach')
# striker.speak("Ball is close enough stop approach") # striker.speak("Ball is close enough stop approach")
# striker.mover.stop_moving() # striker.mover.stop_moving()
@@ -337,7 +339,7 @@ if __name__ == '__main__':
state = 'simple_kick' state = 'simple_kick'
else: else:
print('Continue running') print('Continue running')
##striker.speak("Continue running") # striker.speak("Continue running")
striker.run_to_ball() striker.run_to_ball()
state = 'tracking' state = 'tracking'