Javadoc link

The Python Service allows development and execution of Python scripts which interact and control other Services.  The GUI provides a rudementary IDE for this Service.  You can find a large number of scripts in the community library pyrobotlab

Python can be used to "glue" Services together to form more complex systems or behaviors. It can also be easily used to transform data.  With the Python service you can create a number of other services, establish message routes, create complex methods, then save the entire thing to work on later.  

Imagine an infrared distance sensor constantly sending data to the this service.  Each time it gets one of these IR distance messages, the Python program can check to see if something is "too close" and if it is, the Python service can tell the Motor service to stop.

1.1 Python Basics

I won't go over the basics because there are alot of better Python tutorials out there.  Jython is a Python interpreter running inside a JVM and having complete access to all Java imports !   Which means its Python but can use any Java classes which you may want.  
With that said here are a few references:

1.2 Creating & Starting MyRobotLab Services through Python

Pretty easy really.  
First, pick the service your interested in - you can find a list of services and descriptions on the site here, or a more complete one in the Java Docs here.

Once you find the service you want to use just create and start it.
Like so:

from org.myrobotlab.service import Clock

clock = Runtime.create("clock01","Clock")
clock.startService()

line 1 - brings in a definition of a Clock service from MRL.
line 3 - creates a new Clock service named (of all things)  "clock01" !
line 4 - starts it.

If you copy and paste this small script in the Jython IDE, press the exec and a new tab named "clock01" should show up.


If you prefer fewer lines of code you can use createAndStart to do this in 2 lines of code instead of 3 :D

from org.myrobotlab.service import Clock

clock = Runtime.createAndStart("clock01","Clock")

These methods are reentrant, in that if they are executed again, they will not do anything silly.  For example if I hit the execute button again createAndStart will first check to see if a service named "clock01" is around.  If it already exists then is just passes back a reference to that clock.  If its not started it will start the clock.  If the clock is already started then it will do nothing.

1.3 Setting Service parameters and configuration.

Once you start a service you'll probably want to configure it.  In our ongoing example we are going to set some parameters of the clock we have just created.

from org.myrobotlab.service import Clock
 
clock = Runtime.createAndStart("clock01","Clock")
clock.setPulseDataString('I am a clock pulse')
clock.setPulseDataType(clock.PulseDataType.string)
clock.setInterval(500)
clock.broadcastState()

Line 3,4, & 5 set parameters of the clock.  They set the type of pulse, the data within the pulse and the interval respectively.  Line 6 is not necessary, except to update the gui.  Its a command which tells all the services interested in state changes that things have changed.  If this was not done, the gui would not be updated to the new values, but that is not necessary for the Clock service to run correctly.  Remember the GUIService is just another service, and all the other services are completely happy to run without the GUI.

At this point we have named and started a Clock service.  Told it to send a message who's data value will be a string.  The content of the string will be "I am a clock pulse", and set the interval of this pulse to be every 1/2 second.

Before we start the clock within the Clock service, we probably want it to display somewhere.

1.4 Logging Messages

Within MRL is the Logging service. The Logging service can log messages to a scrollable display. So we are going to send our clock pulse messages to the Logging service.  

1.5 Loading External Python Modules

You can load external python libraries simply by locating where your Python.jar is, making a directory named Lib and dumping python modules into it.  You'll need to restart MRL in order for Python to pick up new modules.

 

References 

 

References :

Example code (from branch develop):
#file : Python.py (github)
# start the service
python = runtime.start("python","Python")

GroG

9 years 9 months ago

Currently in the Python service you can open multiple files and execute them all against the same Python Service.

If you have a set of functions but you want to keep them somewhat modular, you just open the file up and execute it ..  the method definitionw will be there and ready for any of the other files to use.

execute the foobar.py library

and now all function are available in your main script

 

Another way is to call execfile in Python with the file ..  the file must be in the "root" or the myrobotlab directory. Or you must used "absolute path" to the file.

This is nice .. in that you don't need to graphically load or execute the foobar.py in order to use all its functions.

(remember : they have to be in the root of myrobotlab.. although the default behavior is to save to the .myrobotlab directory - I know its a little bizzare .. I'll fix it soon

Example configuration (from branch develop):
#file : Python.py (github)
!!org.myrobotlab.service.config.PythonConfig
listeners: null
modulePaths: [
  ]
peers: null
scriptRootDir: null
startScripts: [
  ]
stopScripts: [
  ]
type: Python