"""
A sample showing how to make a Python script as an app.
"""
__version__ = "0.0.8"
__copyright__ = "Copyright 2015, Aldebaran Robotics"
__author__ = 'YOURNAME'
__email__ = 'YOUREMAIL@aldebaran.com'
import stk.runner
import stk.events
import stk.services
import stk.logging
import math
import time
import almath
from interpreter import Tile
# Define a ARuco object
# Define tile object
class Activity(object):
"A sample standalone app, that demonstrates simple Python usage"
APP_ID = "com.aldebaran.nao-maze"
def __init__(self, qiapp):
self.qiapp = qiapp
self.events = stk.events.EventHelper(qiapp.session)
self.s = stk.services.ServiceCache(qiapp.session)
self.logger = stk.logging.get_logger(qiapp.session, self.APP_ID)
self.DXAruco = self.qiapp.session.service("DXAruco")
self.signal_id = self.DXAruco.signal.connect(self.barcodeDetected)
def on_start(self):
self.events.connect_decorators(self)
self.logger.info("@@@ LIFECYCLE/on_start")
self.sleep()
## ForcePerception/HumanDetected
@stk.events.on("MiddleTactilTouched")
def onMiddleTactilTouched(self, touched):
if touched:
self.logger.info("@@@ EVENTS/Gesture")
self.s.ALTextToSpeech.say("A button on my skull was clicked on")
self.logger.info("@@@ EVENTS/eventNaoSkullWasSingleTouched : SingleMiddle")
if self.isRobotSleeping:
self.play()
else:
self.endGame()
def sleep(self):
self.logger.info("@@@ LIFECYCLE/sleep")
self.s.ALTextToSpeech.say("Going to sleep")
# Set the proper state
self.isRobotSleeping = True
# Reset the variables & models
# Tell user to put me on start tile
self.s.ALTextToSpeech.say("Please place me on the start tile")
def endGame(self):
self.logger.info("@@@ LIFECYCLE/sleep")
self.stopDetection()
self.sleep()
def play(self):
self.logger.info("@@@ LIFECYCLE/play")
# Set the proper state
self.isRobotSleeping = False
self.taskInProgress = False
# Tell user I am starting to play
self.s.ALTextToSpeech.say("Switched on play mode. Starting to read arucos in front of me")
# Launch detection
self.launchDetection()
def launchDetection(self):
self.logger.info("@@@ LIFECYCLE/launchDetection")
# Subscribe the DX Aruco service
self.params = self.DXAruco.get_default_parameters()
self.DXAruco.subscribe(self.params)
#self.s.ALMotion.setStiffnesses("Head", 1.0)
# Simple command for the HeadYaw joint at 10% max speed
names = "HeadPitch"
angles = 20.0*almath.TO_RAD
fractionMaxSpeed = 0.1
self.s.ALMotion.angleInterpolationWithSpeed(names, angles, fractionMaxSpeed)
def barcodeDetected(self, inputArucosDico):
self.logger.info("@@@ LIFECYCLE/barcodeDetected")
if not self.taskInProgress:
self.taskInProgress = True
self.tile = Tile()
if len(inputArucosDico) > 1:
self.s.ALTextToSpeech.say("Too many barcodes")
else:
for key, value in inputArucosDico.iteritems():
#self.logger.info("@@@ keyStan: %s" % key)
#self.logger.info("@@@ valueStan = : %s" % value)
#self.logger.info("@@@ valueStan x = : %s" % value['robot2target'][0])
self.tile.aruco.index = key
self.tile.aruco.positionRobotToTarget.x = value['robot2target'][0]
self.tile.aruco.positionRobotToTarget.y = value['robot2target'][1]
self.tile.aruco.positionRobotToTarget.z = value['robot2target'][2]
self.tile.aruco.positionRobotToTarget.wx = value['robot2target'][3]
self.tile.aruco.positionRobotToTarget.wy = value['robot2target'][4]
self.tile.aruco.positionRobotToTarget.wz = value['robot2target'][5]
break
self.tile.actAccordingly(self.logger, self.s, self.tile.aruco)
#self.s.ALTextToSpeech.say("I am in the middle of something")
self.taskInProgress = False
def stopDetection(self):
self.logger.info("@@@ LIFECYCLE/stopDetection")
self.DXAruco.unsubscribe()
# Unsubscribe from the DX Aruco service
def stop(self):
"Standard way of stopping the application."
pass
def on_stop(self):
"Cleanup"
self.endGame()
self.logger.info("Application finished.")
self.events.clear()
if __name__ == "__main__":
stk.runner.run_activity(Activity)
评论1