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

9 years 6 months ago

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

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 ?

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.

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)