Getting Started Questions

I was trying to become more aquainted with MRL as a user and had some questions. I thought I would create a thread to save the answers for posterity.

First, I was trying the keyboardinput example (HERE). Long story short, it doesn't seem to work. Being unexperienced I don't really know why. It appears input() is never being called, but the keyboard commands appear in the java window.

Upon executing the example I get the readout below.

 

 
22632 [python] INFO  org.myrobotlab.service.Python  - exec #file : Keyboard.input.py
keyboard = Runtime.createAndStart("keyboard", "Keyboard")
python.subscribe("keyCommand", keyboard.getName(), "input")
print "subscribed"
 
def input():
    #never gets here
    print "input()"
    # print 'python object is ', msg_[service]_[method]
    cmd = msg_keyboard_keyCommand.data[0]
    print 'python data is ', cmd
 
    
    if (cmd == "A"):
      print "hello A !"
    elif (cmd == "B"):
      print "hello B !"
 
22739 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - loader for this class sun.misc.Launcher.AppClassLoader
22745 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - parent sun.misc.Launcher.ExtClassLoader
22750 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - system class loader sun.misc.Launcher$AppClassLoader@6bad186f
22756 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - parent should be nullsun.misc.Launcher.ExtClassLoader
22760 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - thread context sun.misc.Launcher.AppClassLoader
22761 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - thread context parent sun.misc.Launcher.ExtClassLoader
22764 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - refreshing classloader
22769 [Thread-10] INFO  class org.myrobotlab.framework.Service  - %s loading %d non-routable methods
22779 [Thread-10] INFO  class org.myrobotlab.net.CommunicationManager  - instanciating a org.myrobotlab.net.CommObjectStreamOverTCP
22782 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - returning org.myrobotlab.service.Keyboard
22784 [python] INFO  org.myrobotlab.service.Python  - exec from org.myrobotlab.service import Keyboard
keyboard = Runtime.getServiceWrapper("keyboard").service
 
22803 [python_outbox_0] ERROR class org.myrobotlab.net.CommunicationManager  - Runtime.getServiceWrapper could not return keyCommand.addListener for sender python 
22858 [keyboard] INFO  class org.myrobotlab.framework.Service  - adding addListener from keyboard.publishStatus to gui.getStatus
23040 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildGraph
23044 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-begin
23045 [gui] INFO  org.myrobotlab.control.ServiceGUI  - GUIServiceGUI service count 4
23051 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-end
23069 [gui] INFO  org.myrobotlab.service.GUIService  - org.myrobotlab.control.TabControl[,249,3,53x16,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=keyboard,verticalAlignment=CENTER,verticalTextPosition=CENTER]
 
 
Thanks in advance.
-Matthew
Alessandruino's picture

Hi Matthew.... I just fixed

Hi Matthew.... I just fixed the script..so refresh Keyboard service page, test it and let us know if it works for you... :D 

Ciaooo

Matthew's picture

Thanks Alessandruino. It

Thanks Alessandruino. It works fine now.

My next question is in regards to the Serial.asciiToChar.py example. I was trying to mimic Grog's blog post by connecting my arduino to MRL and printing some data in the python  window. I changed the COM port to the correct one for my Arduino and deleted the write stuff at the bottom. When I execute it, it says that the COM port is opened, but I don't see any data. If I connect Tera Term or the Arduino Serial Monitor I see it just fine.

I looked in the Serial javadoc but didn't see anything about the read method. 

 

Also, when I stop the python script, close Serial, and then restart the script it it gives me an error. I have to restart MRL for it to work again. 

NameError: name 'sleep' is not defined
 
at org.python.core.Py.NameError(Py.java:246)
at org.python.core.PyFrame.getname(PyFrame.java:257)
at org.python.pycode._pyx12.f$0(<string>:18)
at org.python.pycode._pyx12.call_function(<string>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1261)
at org.python.core.Py.exec(Py.java:1305)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:206)
at org.myrobotlab.service.Python$PIThread.run(Unknown Source)
------
 
525721 [Thread-20] ERROR class org.myrobotlab.framework.Service  - Python error
 
I feel like these are all caused by some small error on my part, but I don't know what. I'm just piecing together examples.
kmcgerald's picture

Hope this helps

As for the "NameError: name 'sleep' is not defined" error, did you include the "import time" at the top of the script?  I'm guessing that's where the sleep function/method comes from.

I have played with that example as well and although I can see data in the Serial "tab" that gets opened in the WebGui (are you using WebGUI or the old application GUI?) I don't see any of the data back in the the Python console.  I'm not sure if I messed it up or if it was broken.

Alessandruino's picture

So you have an arduino hooked

So you have an arduino hooked to the computer... And you would like to See serial data sent from what? Arduino is sending serial data?

Matthew's picture

kmcgerald, the strange thing

kmcgerald, the strange thing is that I do have the import time there. No errors the first time I run it, but if I stop it and run it again I get the error. It is just a direct copy from the Serial page except I removed the bottom where it writes stuff.

I was using the application GUI. Like you say, the python readout stays empty. On the WebGUI I don't even see the serial service. Wether I start the WebGUI before or after executing the python script, it never appears. I also tried running the script from the WebGUI python page without success.

Alessandruino, at the moment the Arduino is sending a number to the computer. Basically I was just trying to get MRL to display any data that comes in over serial in the python window.

Alessandruino's picture

in which way Arduino is

in which way Arduino is sending a number?are you using an Arduino sketch? btw it is normal that you have errors when you stop script...because in reality script never stops :D so there are sorts of "bad overwriting"...python doesn't like them...so you have to restart mrl or close all services before to restart script...

Alessandruino's picture

oh...in your Arduino sketch

oh...in your Arduino sketch are you sending at the same baud of MRL serial service? (9600 baud for example?)

if you could post arduino sketch, and MRL debug would be great!!

GroG's picture

Hi Matthew

Thanks for posting the questions in a post - it helps everyone ...

It looks like your trying all sorts of things which is great, even the WebGUI (you are brave!).

I know some of the confusion you may have to do with the concept of "stopping" a script.  
Script just change the state of MRL which is always running - rarely would you have the opportunity to stop a script - they run very fast and do things like open up serial connections, poll data, change stuff, etc.

So if one script opens a serial connection, and you "stop" the script - you can bet the serial connection is still open.

Additionally, the serial connection has always been a pain, especially when it comes to closing ports. Unfortunately, it is the nature of the RXTXLib 

A no-worky might shed some light on the problem now, maybe the sketch your using too 

Matthew's picture

Interesting about the script

Interesting about the script never stopping, but here are all my sketches. I used  57600 as the baud in both cases. 

Here is my Arduino sketch. (on Arduino Mega 2560 r3)

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
}
 
void loop() {
  // put your main code here, to run repeatedly: 
  Serial.print(12);
  delay(500);
}
 
Here is my MRL script. (version 1636.20130819.0643)
#file : Serial.asciiToChar.py
import time
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
def input():
 code = msg_serial_read.data[0]
 decoded = ''.join(chr(code))
 print decoded
#have python listening to serial
serial.addListener("read", python.name, "input") 
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM5", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
This is the debug from when I run the script initially after rebooting MRL.
31166 [python] INFO  org.myrobotlab.service.Python  - exec #file : Serial.asciiToChar.py
#file : Serial.asciiToChar.py
import time
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
def input():
 code = msg_serial_read.data[0]
 decoded = ''.join(chr(code))
 print decoded
#have python listening to serial
serial.addListener("read", python.name, "input") 
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM14", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
 
#write a series of bytes to the serial port
 
31267 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - loader for this class sun.misc.Launcher.AppClassLoader
31268 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - parent sun.misc.Launcher.ExtClassLoader
31269 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - system class loader sun.misc.Launcher$AppClassLoader@6bad186f
31270 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - parent should be nullsun.misc.Launcher.ExtClassLoader
31270 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - thread context sun.misc.Launcher.AppClassLoader
31270 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - thread context parent sun.misc.Launcher.ExtClassLoader
31270 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - refreshing classloader
31273 [Thread-10] INFO  class org.myrobotlab.framework.Service  - %s loading %d non-routable methods
31280 [Thread-10] INFO  class org.myrobotlab.net.CommunicationManager  - instanciating a org.myrobotlab.net.CommObjectStreamOverTCP
31281 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - returning org.myrobotlab.service.Serial
31282 [python] INFO  org.myrobotlab.service.Python  - exec from org.myrobotlab.service import Serial
serial = Runtime.getServiceWrapper("serial").service
 
31287 [serial] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishState to gui.getState
31288 [serial] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishStatus to gui.getStatus
31306 [Thread-10] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.read to python.input
31308 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - getSerialDevice COM14|57600|8|1|0
31309 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - Loaded class: class org.myrobotlab.serial.gnu.SerialDeviceFactoryGNU
31310 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - Got method: public org.myrobotlab.serial.SerialDevice org.myrobotlab.serial.gnu.SerialDeviceFactoryGNU.getSerialDevice(java.lang.String,int,int,int,int) throws org.myrobotlab.serial.SerialDeviceException
31346 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildGraph
31347 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-begin
31347 [gui] INFO  org.myrobotlab.control.ServiceGUI  - GUIServiceGUI service count 4
31349 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-end
31353 [gui] INFO  org.myrobotlab.service.GUIService  - org.myrobotlab.control.TabControl[,249,3,32x16,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=serial,verticalAlignment=CENTER,verticalTextPosition=CENTER]
31869 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - COM14
31874 [Thread-10] INFO  org.myrobotlab.serial.gnu.SerialDeviceGNU  - opening COM14
32230 [Thread-10] INFO  org.myrobotlab.serial.gnu.SerialDeviceGNU  - opened COM14
 

Grog, I'll send a no-worky. I just figured it was operator error and didn't know if that warrented one.

Matthew's picture

Except I did not have debug

Except I did not have debug on when I got that last one. Ok here's the real debug. The no static route message just repeats at the end.

27055 [AWT-EventQueue-0] DEBUG org.myrobotlab.framework.Outbox  - msg [python.exec (#file : Serial.asciiToChar.py
#file : Serial.asciiToChar.py
import time
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
def input():
 code = msg_serial_read.data[0]
 decoded = ''.join(chr(code))
 print decoded
#have python listening to serial
serial.addListener("read", python.name, "input") 
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM14", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
 
#write a series of bytes to the serial port
)]
27087 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - python configured to RELAY 
27090 [gui_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27091 [gui_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - python.msgBox +1 = 1
27093 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for gui.send
27093 [python] DEBUG org.myrobotlab.framework.Inbox  - python.msgBox -1 0
27102 [python] DEBUG class org.myrobotlab.framework.Service  - --invoking python.exec(#file : Serial.asciiToChar.py
#file : Serial.asciiToChar.py
import time
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
def input():
 code = msg_serial_read.data[0]
 decoded = ''.join(chr(code))
 print decoded
#have python listening to serial
serial.addListener("read", python.name, "input") 
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM14", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
 
#write a series of bytes to the serial port
) 1376971035205--
27104 [python] INFO  org.myrobotlab.service.Python  - exec #file : Serial.asciiToChar.py
#file : Serial.asciiToChar.py
import time
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
def input():
 code = msg_serial_read.data[0]
 decoded = ''.join(chr(code))
 print decoded
#have python listening to serial
serial.addListener("read", python.name, "input") 
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM14", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
 
#write a series of bytes to the serial port
 
27111 [python] DEBUG org.myrobotlab.framework.Outbox  - msg [.exec (null)]
27119 [python_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for python.exec
27130 [Thread-10] DEBUG class org.myrobotlab.service.Runtime  - Runtime.create - Class.forName
27130 [Thread-10] DEBUG class org.myrobotlab.service.Runtime  - Runtime.createService
27130 [Thread-10] DEBUG class org.myrobotlab.service.Runtime  - service serial does not exist
27131 [Thread-10] DEBUG class org.myrobotlab.service.Runtime  - ABOUT TO LOAD CLASS
27131 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - loader for this class sun.misc.Launcher.AppClassLoader
27131 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - parent sun.misc.Launcher.ExtClassLoader
27131 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - system class loader sun.misc.Launcher$AppClassLoader@311f56a3
27132 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - parent should be nullsun.misc.Launcher.ExtClassLoader
27133 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - thread context sun.misc.Launcher.AppClassLoader
27133 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - thread context parent sun.misc.Launcher.ExtClassLoader
27134 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - refreshing classloader
27136 [Thread-10] INFO  class org.myrobotlab.framework.Service  - %s loading %d non-routable methods
27136 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.main is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.write is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.write is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.write is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.write is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.readInt is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.readChar is not routable
27137 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.connect is not routable
27138 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.connect is not routable
27138 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getToolTip is not routable
27139 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.readLong is not routable
27139 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.readByte is not routable
27139 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.readString is not routable
27139 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.disconnect is not routable
27139 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishByteArray is not routable
27140 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getSerialDevice is not routable
27140 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.readByteArray is not routable
27140 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishString is not routable
27140 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getPortName is not routable
27140 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.serialEvent is not routable
27140 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getPortNames is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishLong is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishInt is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishByte is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishChar is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.isConnected is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.invoke is not routable
27141 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.invoke is not routable
27142 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.invoke is not routable
27142 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.run is not routable
27142 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getName is not routable
27142 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getSimpleName is not routable
27143 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getMethods is not routable
27143 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.load is not routable
27143 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.load is not routable
27143 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.in is not routable
27143 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.out is not routable
27144 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.out is not routable
27144 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.sleep is not routable
27144 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.save is not routable
27144 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.save is not routable
27144 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.save is not routable
27144 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getTypeName is not routable
27145 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.initialize is not routable
27145 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.error is not routable
27146 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.error is not routable
27146 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getHost is not routable
27146 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.setState is not routable
27148 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getHostName is not routable
27148 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.connect is not routable
27148 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.attach is not routable
27148 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.attach is not routable
27148 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.stopService is not routable
27148 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.startService is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.logException is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.warn is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.warn is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.info is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.info is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.subscribe is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.subscribe is not routable
27149 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.subscribe is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.allowExport is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.allowExport is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.hasDisplay is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.display is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.send is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.send is not routable
27150 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.send is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.isRunning is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.isReady is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getCFG is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getCFG is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getHostCFG is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.setCFG is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.setCFG is not routable
27151 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.setCFG is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.unsubscribe is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.addListener is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.addListener is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.addListener is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getMsg is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.proxySend is not routable
27152 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.sendBlocking is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.sendBlocking is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.sendBlocking is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getInbox is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getOutbox is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.setHost is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.noConnection is not routable
27153 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getComm is not routable
27154 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishState is not routable
27154 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getCFGDir is not routable
27154 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getLastError is not routable
27154 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.loadServiceDefaultConfiguration is not routable
27155 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.releaseService is not routable
27155 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getNotifyListKeySet is not routable
27155 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getNotifyList is not routable
27155 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.stackToString is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.loadGlobalMachineDefaults is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.createAndStartSubServices is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.preRoutingHook is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.preProcessHook is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getIntanceName is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getNewInstance is not routable
27156 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getNewInstance is not routable
27157 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getNewInstance is not routable
27157 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getNewInstance is not routable
27157 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.removeListener is not routable
27157 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.removeListener is not routable
27157 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.removeListener is not routable
27157 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.removeListener is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.removeAllListeners is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.startRecording is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.startRecording is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.stopRecording is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.loadRecording is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.createMessage is not routable
27158 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.createMessage is not routable
27159 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getThisThread is not routable
27159 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.setThisThread is not routable
27159 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.connectionBroken is not routable
27159 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getMethodToolTip is not routable
27159 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.copyShallowFrom is not routable
27159 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.broadcastState is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getServiceResourceFile is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.echoHeartbeat is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.startHeartbeat is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.stopHeartbeat is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.publishStatus is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getMessageSet is not routable
27160 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.wait is not routable
27161 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.wait is not routable
27161 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.wait is not routable
27161 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.equals is not routable
27162 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.toString is not routable
27162 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.hashCode is not routable
27162 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.getClass is not routable
27162 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.notify is not routable
27163 [Thread-10] DEBUG class org.myrobotlab.framework.Service  - method null.notifyAll is not routable
27171 [Thread-10] DEBUG class org.myrobotlab.framework.ConfigurationManager  - file Matthew-PC.serial.properties not found
27172 [Thread-10] INFO  class org.myrobotlab.net.CommunicationManager  - instanciating a org.myrobotlab.net.CommObjectStreamOverTCP
27174 [Thread-10] DEBUG org.myrobotlab.framework.Outbox  - msg [.registered (ServiceWrapper)]
27175 [Thread-10] INFO  class org.myrobotlab.service.Runtime  - returning org.myrobotlab.service.Serial
27178 [runtime_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27178 [runtime_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - python.msgBox +1 = 1
27178 [runtime_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27179 [runtime_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox +1 = 1
27178 [python] DEBUG org.myrobotlab.framework.Inbox  - python.msgBox -1 0
27180 [python] DEBUG class org.myrobotlab.framework.Service  - --invoking python.registered(ServiceWrapper) 1376971035325--
27181 [python] INFO  org.myrobotlab.service.Python  - exec from org.myrobotlab.service import Serial
serial = Runtime.getServiceWrapper("serial").service
 
27181 [python] DEBUG org.myrobotlab.framework.Outbox  - msg [.registered (null)]
27180 [gui] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox -1 0
27185 [Thread-12] DEBUG org.myrobotlab.framework.Outbox  - msg [.finishedExecutingScript (null)]
27186 [gui] DEBUG org.myrobotlab.framework.Outbox  - msg [serial.addListener (MRLListener)]
27186 [gui] DEBUG org.myrobotlab.framework.Outbox  - msg [serial.publishState (null)]
27185 [python_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for python.registered
27187 [python_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27187 [python_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox +1 = 1
27187 [gui] DEBUG org.myrobotlab.framework.Outbox  - msg [serial.addListener (MRLListener)]
27188 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - serial configured to RELAY 
27188 [gui_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27188 [gui_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - serial.msgBox +1 = 1
27188 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for gui.send
27188 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - serial configured to RELAY 
27188 [gui_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27189 [gui_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - serial.msgBox +1 = 2
27189 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for gui.send
27189 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - serial configured to RELAY 
27189 [gui_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27189 [serial] DEBUG org.myrobotlab.framework.Inbox  - serial.msgBox -1 1
27193 [gui_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - serial.msgBox +1 = 2
27193 [gui_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for gui.send
27194 [serial] DEBUG class org.myrobotlab.framework.Service  - --invoking serial.addListener(MRLListener) 1376971035337--
27194 [serial] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishState to gui.getState
27194 [serial] DEBUG org.myrobotlab.framework.Outbox  - msg [.addListener (null)]
27194 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.addListener
27195 [serial] DEBUG org.myrobotlab.framework.Inbox  - serial.msgBox -1 1
27197 [serial] DEBUG class org.myrobotlab.framework.Service  - --invoking serial.publishState(null) 1376971035337--
27197 [serial] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishState (Serial)]
27197 [serial] DEBUG org.myrobotlab.framework.Inbox  - serial.msgBox -1 0
27197 [serial] DEBUG class org.myrobotlab.framework.Service  - --invoking serial.addListener(MRLListener) 1376971035337--
27198 [serial] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishStatus to gui.getStatus
27198 [serial] DEBUG org.myrobotlab.framework.Outbox  - msg [.addListener (null)]
27198 [serial_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
27199 [serial_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox +1 = 2
27199 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.addListener
27204 [Thread-10] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.read to python.input
27207 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - getSerialDevice COM14|57600|8|1|0
27208 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - Loaded class: class org.myrobotlab.serial.gnu.SerialDeviceFactoryGNU
27208 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - Got method: public org.myrobotlab.serial.SerialDevice org.myrobotlab.serial.gnu.SerialDeviceFactoryGNU.getSerialDevice(java.lang.String,int,int,int,int) throws org.myrobotlab.serial.SerialDeviceException
27245 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildGraph
27246 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-begin
27246 [gui] INFO  org.myrobotlab.control.ServiceGUI  - GUIServiceGUI service count 4
27248 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-end
27252 [gui] INFO  org.myrobotlab.service.GUIService  - org.myrobotlab.control.TabControl[,249,3,32x16,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=serial,verticalAlignment=CENTER,verticalTextPosition=CENTER]
27254 [gui] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox -1 1
27255 [gui] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox -1 0
27629 [Thread-10] INFO  org.myrobotlab.serial.SerialDeviceFactory  - COM14
27633 [Thread-10] INFO  org.myrobotlab.serial.gnu.SerialDeviceGNU  - opening COM14
27974 [Thread-10] INFO  org.myrobotlab.serial.gnu.SerialDeviceGNU  - opened COM14
28288 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
28289 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
28290 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
28291 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
28785 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
28787 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
28788 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
28788 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
28976 [Thread-10] DEBUG org.myrobotlab.serial.gnu.SerialDeviceGNU  - setSerialPortParams 57600 8 1 0
29080 [Thread-10] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishState (Serial)]
29082 [serial_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
29083 [serial_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox +1 = 1
29088 [gui] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox -1 0
29288 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
29288 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
29289 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
29290 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
29787 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
29789 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
29790 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
29791 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
30082 [Thread-10] DEBUG org.myrobotlab.framework.Outbox  - msg [.finishedExecutingScript (null)]
30084 [python_outbox_0] DEBUG class org.myrobotlab.net.CommunicationManager  - sending local
30085 [python_outbox_0] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox +1 = 1
30085 [gui] DEBUG org.myrobotlab.framework.Inbox  - gui.msgBox -1 0
30288 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
30290 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
30304 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
30306 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
30790 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
30792 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
30793 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
30793 [serial_outbox_0] DEBUG org.myrobotlab.framework.Outbox  - no static route for serial.publishByte
31291 [Thread-13] DEBUG org.myrobotlab.framework.Outbox  - msg [.publishByte (Byte)]
 
GroG's picture

It's Still Under Development :D

Your repeating publishByte - is a method which is getting called everytime you print.  Its just not displaying :)

Here is a script which should work for you - change the COM port of course :)
The Serial.print is doing the ascii equivalent of 12 - so you should see it as 2 bytes (49 &50)
If you want to "see" a 12 - then you have a couple options, you can change your Arduino sketch to do Serial.write(12) instead of Serial.print(12) and it won't go through the ascii conversion, or you can change the pyhton "print data" to "print unichr(data)" ..  its software - there's a bazillion ways to do the same thing :D

Also in the script I put a if not connected then connect - which is nice for "re-running the script".

In the future there will be a simple "read" - but at the moment its completely event driven - when there is a byte it is "published" for all listeners 

Let us know if it works for you ;)

# This demo creates and starts an Arduino service
# Connects a serial device on Windows this would COMx 
# Sets the board type
# Then starts polling analog pin 17 which is Analog pin 3
 
# create an Arduino service named arduino
serial = Runtime.createAndStart("serial","Serial")
 
if not (serial.isConnected()):
    # connect to COM9 - 57600, 8, 1 0 (default)
    serial.connect("COM9") # TODO - make rentrant
 
 
# add a message route from arduino.publishPin --> python.input
serial.addListener("publishByte", python.name, "input")
 
# read data from pins which recieve data will be sent here 
def input():
    data = msg_serial_publishByte.data[0]
    # pin data is printed to the console
    print data 
 
Matthew's picture

Success

Ah yes. That script does work. Now I'll try the other direction. 

Also, the unichr() does work in that it displays a 1 and then a 2. Is there an easy way to combine the 2 bytes back into an int? Or is that work for the future? I know I've seen sketches for it in Arduino C, but I didn't know if there was something built in.

Thanks for your help! 

GroG's picture

Wouldn't you rather change

Wouldn't you rather change your sketch to do a Serial.write(12) instead of a Serial.print(12) ?  

There's less conversions on both side then :)

Matthew's picture

Yeah. 12 was just arbitrary.

Yeah. 12 was just arbitrary. I was just wondering in case I wanted to send bigger numbers. 

GroG's picture

Ya - you can send 0 - 255 :)

Bigger than that and you have to put them together byte by byte... or ascii char by char... :)

I'm working on several other methods - but they aren't ready yet...

publishChar publishInt publishLong publishByteArray publishString

Matthew's picture

Right. I was trying to do

Right. I was trying to do that myself, but it was not working.. Surprises there. Ha. I'll probably just wait for your methods.

My attempt. 

serial = Runtime.createAndStart("serial","Serial")
 
if not (serial.isConnected()):
    # connect to COM9 - 57600, 8, 1 0 (default)
    serial.connect("COM14") # TODO - make rentrant
 
 
# add a message route from arduino.publishPin --> python.input
serial.addListener("publishByte", python.name, "input")
 
 
entry = 1
# read data from pins which recieve data will be sent here 
def input():
    if entry == 1:
     data = msg_serial_publishByte.data[0]
     entry = 2
    if entry == 2:
     data2 = msg_serial_publishByte.data[0]
     entry = 1
     decoded = int( unichr(data) + unichr(data2) )
     print decoded
     
 
GroG's picture

I am curious as to your

I am curious as to your project.. could you elaborate?

Matthew's picture

At the moment I was just

At the moment I was just trying to figure out how MRL works. 

There is no particular project in mind at the moment. What I would like to do is create a sort of plug-and-play OpenCV script that I just outputs x and y coordinates of a face, tracker point, etc via serial that could then be modified from the OpenCV GUI without much knowledge of Python. This would allow anyone with a little bit of Arduino (or any other microcontroller I suppose) to get vision processing without having to know the ins and outs of OpenCV.

I was looking for an easy vision processing interface that could be plugged into existing projects and MRL seemed to be it.  The questions about the keyboard service and such were just me trying to figure out how all this works.

GroG's picture

Ok, :) Did you look at the

Ok, :)

Did you look at the face tracking scripts - some of them output to an Arduino the pan and tilt coordinates for tracking with servos.

Matthew's picture

I  saw a few of them. They

I  saw a few of them. They all appear to use the Arduino service. I wanted to avoid that and just use straight serial so it could be plugged into existing Arduino projects/sketches. 

Hopefully, I will be able to copy a lot of that work that has already been done. I remember seeing several cool find face sketches a while ago. I hope to look more into it this weekend when I get some time.

Matthew's picture

Ok GroG (or anyone else). I

Ok GroG (or anyone else). I have another debug for you. First, here's what it's supposed to do.

Open a serial port with an Arduino. Start opencv and add 2 filters. It should find a face and send the location to python where it will find the center of the box. It will then change the coordinates to a scale from 1-100 instead of 1-320 or 1-240. Then it will write an identifier for pan and write the pan coordinate. Then same for tilt.

I will send a no-worky. I figure it's probably a combination of python errors and calling the wrong methods for opencv. I mostly copied the Tracking.face.py example.

from org.myrobotlab.service import OpenCV
from org.myrobotlab.opencv import OpenCVData
from com.googlecode.javacv.cpp.opencv_core import CvPoint;
from org.myrobotlab.service import OpenCV
 
# create or get a handle to an OpenCV service
opencv = Runtime.createAndStart("opencv","OpenCV")
# reduce the size - face tracking doesn't need much detail
# the smaller the faster
opencv.addFilter("PyramidDown1", "PyramidDown")
# add the face detect filter
opencv.addFilter("FaceDetect1", "FaceDetect")
 
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
#serial.addListener("publishByte", python.name, "serialinput")
 
def input():
    global x
    global y
    global sposx
    global sposy
    global posx
    global posy
 
    #print 'found face at (x,y) ', msg_opencv_publishOpenCVData.data[0].x(), msg_opencv_publish.data[0].y()
    opencvData = msg_opencv_publishOpenCVData.data[0]
     # print 'python object is ', msg_[service]_[method]
 
 
    if (opencvData.getBoundingBoxArray().size() > 0) :
     rect = opencvData.getBoundingBoxArray().get(0)
     posx = rect.x
     posy = rect.y
 
     w = rect.width
     h = rect.height
     sposx = (w/2)
     sposy = (h/2)
     # Get your x and y in pixels. Origin is in top left corner
     x = (posx + sposx)  
     y = (posy + sposy)
     
     # Convert x,y pixels to (x,y) coordinates from top left. Note that 640 and 480 will need to be changed if pyramid down is used
     # This gets the position in a scale from 1, 100
     x = translate(x, 1, 320, 1, 100);
     y = translate(y, 1, 240, 1, 100);
     print x
     print y
     #write a series of bytes to the serial port
     serial.write(250) # pan code
     serial.write(x)
     serial.write(251) # tilt code
     serial.write(y)
 
     
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM15", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
 
# create a message route from opencv to python so we can see the coordinate locations
opencv.addListener("publishOpenCVData", python.name, "input", OpenCVData().getClass());
 
#opencv.setCameraIndex(0)
 
opencv.capture()
 
# Scale values. Mimics Arduino map() function.
def translate(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin
 
    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)
 
    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)
 

 

Alessandruino's picture

Matthew i noticed 2 issues

Matthew i noticed 2 issues : 

  1.  your x and y returned a float value... but to write it over serial you need an integer value so you have to replace :

              x = translate(x, 1, 320, 1, 100);

              y = translate(y, 1, 240, 1, 100);

              with

              x = int(translate(x, 1, 320, 1, 100)) ;

              y = int(translate(y, 1, 240, 1, 100));

This will force x,y value to an int

        2.  you have to replace this  opencv.addListener("publishOpenCVData", python.name, "input",OpenCVData().getClass());

             with this one opencv.addListener("publishOpenCVData", python.name, "input");

 

Let us know if it works for you...

GroG's picture

Your Back !  ;) Well first

Your Back !  ;)

Well first - 

opencv.addListener("publishOpenCVData", python.name, "input", OpenCVData().getClass());

complains that it OpenCVData wants at least 1 input for the constructor...

the "addListener" or "subscribe" is supposed to be "type parameter aware" - but really isn't - so you can leave the parameter defintion off :P  ... someday I may implement it - but not anytime soon

change it to 

opencv.addListener("publishOpenCVData", python.name, "input");

 

 
Matthew's picture

Those changes appear to fix

Those changes appear to fix it. Something in the transfer to the Arduino didn't seem to be working, but I haven't really wrung that part out yet.

Should the addListener code be changed in those examples? Or is it something with my particular use of it that I don't need the last parameter? That line was a direct copy from the facetracking example, and I noticed a few others had it as well. 

GroG's picture

I would take them out...

I would take them out...  right now its a simple name mapping

"foo" ---will map to ---> "bar"    ..  if the return type of foo and the input type of bar are incompatible you'll get a runtime error..

the last parameter was really supposed to help when there was an overloaded "bar" .. say one which takes Integers and another which takes Strings ...

you could differentiate it .. but now in retro-spect .. who wants that added complexity ? - the framework should figure it out for you.. so that we humans only need to specify the method names we want linked together ;)

Matthew's picture

Well I have had success. It

Well I have had success. It isn't what I would call done, but it will track a face. Here is the code I am running at the moment. It is still a work in progress.

MRL:

# create or get a handle to an OpenCV service
opencv = Runtime.createAndStart("opencv","OpenCV")
 
opencv.addFilter("Gray1", "Gray")        # Sometimes gray seems to help
# reduce the size - face tracking doesn't need much detail
# the smaller the faster
opencv.addFilter("PyramidDown1", "PyramidDown")
# add the face detect filter
opencv.addFilter("FaceDetect1", "FaceDetect")
 
 
#create a Serial service named serial
serial = Runtime.createAndStart("serial","Serial")
#serial.addListener("publishByte", python.name, "serialinput")
 
def input():
    global x
    global y
    global sposx
    global sposy
    global posx
    global posy
 
    #print 'found face at (x,y) ', msg_opencv_publishOpenCVData.data[0].x(), msg_opencv_publish.data[0].y()
    opencvData = msg_opencv_publishOpenCVData.data[0]
     # print 'python object is ', msg_[service]_[method]
 
 
    if (opencvData.getBoundingBoxArray().size() > 0) :
     rect = opencvData.getBoundingBoxArray().get(0)
     posx = rect.x
     posy = rect.y
 
     w = rect.width
     h = rect.height
     sposx = (w/2)
     sposy = (h/2)
     # Get your x and y in pixels. Origin is in top left corner
     x = (posx + sposx)  
     y = (posy + sposy)
     
     # Convert x,y pixels to (x,y) coordinates from top left. Note that 640 and 480 will need to be changed if pyramid down is used
     # This gets the position in a scale from 1, 100
     x = int(translate(x, 1, 320, 1, 100));
     y = int(translate(y, 1, 240, 1, 100));
     print 'x: ' ,  x
     print 'y: ' ,  y
     #write a series of bytes to the serial port
     serial.write(250) # pan code
     serial.write(x)
     serial.write(251) # tilt code
     serial.write(y)
 
     
#connect to a serial port COM4 57600 bitrate 8 data bits 1 stop bit 0 parity
serial.connect("COM15", 57600, 8, 1, 0)
#sometimes its important to wait a little for hardware to get ready
sleep(1)
 
# create a message route from opencv to python so we can see the coordinate locations
opencv.addListener("publishOpenCVData", python.name, "input");
 
#opencv.setCameraIndex(0)
 
opencv.capture()
 
# Scale values. Mimics Arduino map() function.
def translate(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin
 
    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)
 
    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)
 
 
Arduino:
 
#include <Servo.h> 
 
Servo pan;  // create servo object to control a servo 
Servo tilt;
 
int tiltval = 90;    
int panval = 90;
int positionval = 50;
int reading;
const int Delta = 1;
 
const int panMin = 5;
const int panMax = 175;
const int tiltMin = 5;
const int tiltMax = 175;
const int Tolerance = 10;
 
void setup() 
  Serial.begin(57600);
  Serial.write(44);
  pan.attach(9);  // attaches the servo on pin 9 to the servo object 
  tilt.attach(10);
 
void loop() 
  //Serial.println("test");
  while (Serial.available())
  {
    reading = Serial.read();
    //Serial.print(reading);
    if( reading == 250) // pan code
    {
      //Serial.println("yay");
      while(!Serial.available())  delay(1);                                         // Wait for position val
      positionval = Serial.read();                                                  //get the position value for pan
       if ( positionval > (50 + Tolerance))                                                          // if the change is greater that 2 degrees
      {       
          panval = panval - Delta;                                                   // Add delta to panval 
      }
      if ( positionval < (50 - Tolerance))
      {
        panval = panval + Delta;
      }
      //Serial.print(reading);
    }
 
    if( reading == 251) // tilt code
    {
      while(!Serial.available())  delay(1);
      positionval = Serial.read();            //get the position value for tilt
       if ( positionval > (50 + Tolerance) )                                                          // if the change is greater that 2 degrees
      {       
          tiltval = tiltval + Delta;                                                   // Add delta to tiltval 
      }
      if ( positionval < (50 - Tolerance))
      {
        tiltval = tiltval - Delta;
      }
      }
    }
  
  
  FixLimits();      // Fix limits to avoid overdriving servos
  
  
  // Write the angle to 
 
  tilt.write(tiltval);
  pan.write(panval);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
 
void FixLimits()
{
  if (tiltval > tiltMax) tiltval = tiltMax;
  if (tiltval < tiltMin) tiltval = tiltMin;
  if (panval > panMax) panval = panMax;
  if (panval < panMin) panval = panMin;
}
 

I have had a bit of a problem with it thinking other objects are faces. I am going to tweak things and play around with filters to try and improve it. In a cluttered, poorly lit dorm room I may just have to deal with it.

 

Matthew's picture

Now another question. I found

Now another question. I found in one of the examples how to set the inrange filter, but it doesn't appear to be working. What is the correct way to do this? This is what I have.

opencv.addFilter("InRange1", "InRange")
opencv.setFilterCFG("InRange1","hueMin", "0")
opencv.setFilterCFG("InRange1","hueMax", "248")
opencv.setFilterCFG("InRange1","saturationMin", "0")
opencv.setFilterCFG("InRange1","saturationMax", "256")
opencv.setFilterCFG("InRange1","valueMin", "62")
opencv.setFilterCFG("InRange1","valueMax", "178")
opencv.setFilterCFG("InRange1","useHue", True)
opencv.setFilterCFG("InRange1","useSaturation", True)
opencv.setFilterCFG("InRange1","useValue", True)
Also, how would one know what methods to call to get the OpenCv data. For instance, how to get lkoptical points or the box found by FindContours. I discovered opencvData.getBoundingBoxArray().get(0) for the face detect just by copying Alessandruino. Is there a method besides just copying people that know or do you pretty much have to go in and read the code?