Help

Hello. I'm a newbie in Python and MRL. I have 3 question at the moment. I'm so greatful if someone help me :)
1.  what is addListener? Can you explain that in detail?
2.  What is the different between addListener and subscribe?
3. why MRL does not have loop like arduino?

I'm so greatful if someone help me :)

GroG's picture

Hello eziovn123, A quick

Hello eziovn123,

A quick background might help...

To start with MRL is a (Service Oriented Architecture) - its a bunch of Services that live around in a framework ...  think of them as floating Legos

Each Service has a set of methods.
You can plug any method of one service into the method of another service (as long as it has an appropriate data type) ..

In this way you plug things together .. and make cool stuff

myService.addListener(otherService) ... is "plugging together"

otherService.subscribe(myService)  .. is similar but "plugging the other way around" :)

Loops ?  We donna need no stinking Loops !

Python - what you're probably programming in is "just another Service" in MRL.
With Python you can do anything you want including making an infiinite loop like the Arduino loop,
we don't try to restrict what you do in Python, you can make loops, threads, and any other kindof process flow the language supports.

MRL is asynchronous, and multi-threaded - it might be a little hard to deal with this, but its much like life .. which is not a tightly controlled loop :)

Oh look I spilled my coffee - see !  Asynchronous events in a massively multi-threaded environment ! :D

eziovn123's picture

Thank you very much. I have

Thank you very much. I have other questions.

-------------------------------------------------------------------------------------------------

# getting analog input back from arduino into Python
 
arduino = Runtime.createAndStart("arduino","Arduino")
arduino.connect("COM15")
 
readAnalogPin = 1
readDigitalPin = 7
 
# make friendly sample rate
arduino.setSampleRate(8000)
 
# add call back route
arduino.addListener("publishPin", "python", "publishPin")
 
# my call-back
def publishPin(pin):
  print pin.pin, pin.value, pin.type, pin.source
 
# get data from analog pin for 5 seconds
arduino.analogReadPollingStart(readAnalogPin)
sleep(5)
arduino.analogReadPollingStop(readAnalogPin) 
 
# get data from digital pin for 5 seconds
arduino.digitalReadPollingStart(readDigitalPin)
sleep(5)
arduino.digitalReadPollingStop(readDigitalPin) 
 
 
and ----------------------------------------------------------
 
from java.lang import String
from org.myrobotlab.service import Speech
from org.myrobotlab.service import Sphinx
from org.myrobotlab.service import Runtime
 
# create ear and mouth
ear = Runtime.createAndStart("ear","Sphinx")
mouth = Runtime.createAndStart("mouth","Speech")
opencv = Runtime.createAndStart("opencv","OpenCV")
opencv.addFilter("pdown","PyramidDown")
opencv.setDisplayFilter("pdown")
opencv.capture()
# start listening for the words we are interested in
ear.startListening("hello robot|take photo")
 
 
# set up a message route from the ear --to--> python method "heard"
ear.addListener("recognized", python.name, "heard");
 
def heard(data):
      print "heard ", data
      if (data == "hello robot"):
         mouth.speak("Hi Alessandro.")
      elif (data == "take photo"):          
           photoFileName = opencv.recordSingleFrame()
           print "name file is" , photoFileName
           mouth.speak("photo taken")
 
ear.attach("mouth")
 
------------------------------------------------------------------------
What is the different between
arduino.addListener("publishPin", "python", "publishPin")
and
ear.addListener("recognized", python.name, "heard");
and
myService.addListener(otherService) ?
Can we use subscribe instead of addListener ?
GroG's picture

The all important name is

The all important name is what you call thing which are created.  My name is "GroG".
The default python service's name is called "python".  It could have been "george" or "fred" or whatever you want it to be when the service was created.

So, there is no difference in this case.

Funny, neither is the prefered way either - where did you get this script ?
The preferred way is 

ear.addListener("recognized", python.getName(), "heard")

additionally to be complete, this script should not rely on the fact, the "default" behavior creates a Python service named "python".  Rather, it should have this line where the other services are being created & started.

python = Runtime.start("python","Python")

start will automatically create before starting.  If its called again and the service is already started, nothing bad happens, it just gives a reference back of the running service.

eziovn123's picture

Oh...OK. I understood. Tks a

Oh...OK. I understood. Tks a lots. 
Where can I get all the references of the service? ( I mean the codes or methods or ... I don't know how people call them)

GroG's picture

Start a webgui

Start a webgui service

http://127.0.0.1:7777/services 

will show you all the currently running services - when you click on one - it will show you all the possible methods

If your lucky you can press help and there will be Javadoc info.

eziovn123's picture

Merci beaucoup. You are my

Merci beaucoup. You are my savior.