Newbie Help

I am trying to get the UltrasonicSensor working via MRL and keep getting a Null Exception when adding the RangeListener.  I've copied the various versions of code that I've seen.  Anyone has complete code that works?   

 

from org.myrobotlab.service import Arduino
from org.myrobotlab.service import UltrasonicSensor
from org.myrobotlab.service import Runtime
from org.myrobotlab.service import Python
from org.myrobotlab.service import WebGui 
from time import sleep
 
# comPort = "/dev/ttyUSB0"
comPort = "COM3"
 
init=0
if init == 0:
    # Create the Python service
    pythonEngine = Runtime.start("pythonEngine","Python")
    sleep(2)
 
    # Create the uno service and connect
    uno = Runtime.createAndStart("uno", "Arduino")
    sleep(2)
    #uno.connect("COM3")
    sleep(2)
 
    # Create the ultrasonic sensor service
    sr04 = Runtime.createAndStart("sr04", "UltrasonicSensor")
    sleep(2)
    #print "DNA: " sr04.gtDNA() 
    #sr04.reserveRootAs("sr04.arduino", "uno");
    #sr04.startService()
    
    #gui = Runtime.createAndStart("gui","GuiService")
else:
    # access the services
    pythonEngine = Runtime.getService("pythonEngine")
    uno = Runtime.getService("uno")
    sr04 = Runtime.getService("sr04")
 
    #gui = Runtime.getService("GuiService")
    sleep(2)
 
    
 
sr04.attach(uno, 2, 3)
print "echo pin", sr04.getEchoPin()
 
Some of my debug output from a python script
 
 
[python.interpreter.18] [INFO] starting python pythonEngine
[python.interpreter.18] [INFO] started python pythonEngine
[python.interpreter.18] [INFO] log info -------------Python is : pythonEngine
[python.interpreter.18] [INFO] log info -------------isRunning: True
[python.interpreter.18] [INFO] log info -------------Has Display: org.myrobotlab.service.Python
[python.interpreter.18] [INFO] looking for /resource/Python/pythonConsole.py
[python.interpreter.18] [INFO] exec(String) 
import sys
from javax.swing import JFrame
from javax.swing import JPanel
from javax.swing import JLabel
from javax.swing import JTextArea
from javax.swing import JScrollPane
from javax.swing import JTabbedPane
from javax.swing import WindowConstants
from java.awt import BorderLayout
from java.lang import Boolean
 
# myService is a local variable
# created to point to this Python
# service
 
class Console:
 
  def __init__(self):
    self.stdout = None
    self.stderr = None
    
  def write(self,string):
    myService.invoke("publishStdOut", string)
    
  def attach(self):
    if (self.stdout == None):    
      self.stdout = sys.stdout
      self.stderr = sys.stderr
      sys.stdout = self
      sys.stderr = self
   
  def flush(self):
    pass
    
  def detach(self):
    if (self.stdout != None):
      sys.stdout = self.stdout
      sys.stderr = self.stderr
      self.stdout = None
      self.stderr = None
    
console = Console()
console.attach()
 
[python.interpreter.18] [INFO] connect to port COM3 115200|8|1|0
[python.interpreter.18] [INFO] already connected to COM3 - disconnect first to reconnect
[python.interpreter.18] [INFO] uno COM3 getVersion
[python.interpreter.18] [INFO] uno info uno.serial connected on COM3 responded version 40 ... goodtimes...
[python.interpreter.18] [INFO] publishVersion 40
[AWT-EventQueue-0] [INFO] getState Arduino
[uno] [INFO] uno COM3 getVersion
[uno] [INFO] uno info uno.serial connected on COM3 responded version 40 ... goodtimes...
[uno] [INFO] publishVersion 40
[python.interpreter.18] [INFO] log info -------------------------Arduino getName is : uno
[python.interpreter.18] [INFO] log info InfoFIXME - meta data needs to be re-infused into instance  sr04  SR04
[python.interpreter.18] [INFO] log info PINGER
[python.interpreter.18] [INFO] log info 0
[python.interpreter.18] [INFO] log info PINGED
[python.interpreter.18] [ERROR] python error PyException - null Traceback (most recent call last):
  File "<string>", line 68, in <module>
at org.myrobotlab.service.UltrasonicSensor.startRanging(UltrasonicSensor.java:145)
 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 
at java.lang.reflect.Method.invoke(Unknown Source)
 
 
java.lang.NullPointerException: java.lang.NullPointerException
 
at org.python.core.Py.JavaError(Py.java:546)
at org.python.core.Py.JavaError(Py.java:537)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
at org.python.core.PyObject.__call__(PyObject.java:478)
at org.python.core.PyObject.__call__(PyObject.java:482)
at org.python.core.PyMethod.__call__(PyMethod.java:141)
at org.python.pycode._pyx36.f$0(<string>:72)
at org.python.pycode._pyx36.call_function(<string>)
at org.python.core.PyTableCode.call(PyTableCode.java:167)
at org.python.core.PyCode.call(PyCode.java:18)

 

moz4r's picture

Hi Jim ! Can you try the last

Hi Jim ! Can you try the last build, with this piece of code , I succesful get it worky :

 

http://34.201.4.170/deploy/develop/myrobotlab.jar

https://raw.githubusercontent.com/MyRobotLab/pyrobotlab/master/service/U...

http://myrobotlab.org/service/UltraSonicSensor
 

SurferJim's picture

Thank you.  I will give it a

Thank you.  I will give it a try.  

SurferJim's picture

UltraSonic

Hi,

I checked and I have the latest builds from the myrobotlab download page in terms of the C code and the Java code.   I also know that I can connect to the SR04 sensor via the uno as it can reply to the getName(), getDescription(), and getPings() methods.  The issue is when I call, sr04.addRangeListener(python). The exception says its receiving a null, and this looks like the UltraSonicSensor.java code is trying to attach the Service via the AddMRLListener().

I run this code from the python service window in mrl gui placing it into the editor window and pressing the execute icon at the top of the page. From what I understand, the

def onRange(distance):
  print "distance ", distance, " cm"

is a callback method.  I see that the Messaging Framework in MRL should route the callback to the Python Module and call upon the onRange() function.   Now that I have the code in Eclipse, I may compile and run it locally, so I can add more debug messages if I cannot isolate the issue.  I will run tonight after my day job, but assume that my messaging is not working properly and will focus there first.

Nice job with the code, Grog, nice clean code and design.  

 

moz4r's picture

Hi Jim , can you send a

Hi Jim , can you send a "noworky" from help command. After you reproduced the problem.

I'm not sure you have the last build ( the build from download section is not the last )

I saw in your post upper : publishVersion 40 , this is an old mrlcomm version

this is the last beta build : http://34.201.4.170/deploy/develop/myrobotlab.jar

SurferJim's picture

MRLComm

Hi,

 

Thanks Moz4r, I have the jar.  Am I to assume if I use GitHub, I will be getting old software?  I would like to get a jar which has the java files so I can setup an Eclipse environment.  Can you point me in the right direction?  Also, where are the latest files to compile MRLComm in the Arduino IDE, as I downloaded off of GitHub and noticed that the documentation tags matched, so I just left my current uno installation alone.

Thanks for the assistance! 

Mats's picture

Eclipse setup and the latest version

The instructions to setup Eclipse can be found here: http://myrobotlab.org/quick_start

Follow the instructions for Developer. 

The master branch is currently on level 1.0.1758 ( Lamiak ) i.e. almost a year old.

Wh are using the develop branch, and it's on level 1.0.2590 and ticking. 

So if you want to be at the latest bleeding edge, and perhaps also help developing, then you should use the develop branch.

If you just want the latest jar, it can be found at the top of the shoutbox ( latest build ).

 

SurferJim's picture

Thanks again

Hi,

I used the new JAR that you sent and installed JDK 1.8 as it didn't like the 1.7v I was using.  That's probably why message broadcasting was not working.   When I launch the jar, using java -jar myrobotlab.jar I received a python statup error.   I removed my current python installation from my path, and tried again thinking there was a colision, nope :-(.    At your request, I sent a noworky last evening around 9pm EST.  I noticed in the log it was looking for python home under the myrobotlab directory structure.  I will try again after I get back home from my day job.  Thanks again for the help :-)   

SurferJim's picture

Not giving up yet

Hi,

I downloaded the code from GitHub and am running under eclipse.  Pretty nice!  However, I am getting a different error now.   I examined the console log and have a clean start with INFO only messages when starting up the GUIService, Python, and having it talk to the Arduino .   When the SR04 is instantiated, we see below.   I sent a NoWorky a few mins ago, 7:20 EST.  

 
-------------------------------
 
19:18:22.203 [python] INFO c.m.s.Python [Python.java:533] exec(String) 
from org.myrobotlab.service import UltrasonicSensor
sr04 = Runtime.getService("sr04")
 
 
19:18:22.266 [AWT-EventQueue-0] INFO  c.myrobotlab.framework.Service - subscribe [sr04/publishStatus ---> gui/getStatus]
19:18:22.266 [AWT-EventQueue-0] INFO c.m.f.Service [Service.java:2002] subscribe [sr04/publishStatus ---> gui/getStatus]
19:18:25.219 [python.interpreter.3] ERROR c.myrobotlab.framework.Service - python error PyException - null Traceback (most recent call last):
  File "<string>", line 40, in <module>
AttributeError: 'NoneType' object has no attribute 'info'
 
at org.python.core.Py.AttributeError(Py.java:205)
at org.python.core.PyObject.noAttributeError(PyObject.java:1013)
at org.python.core.PyObject.__getattr__(PyObject.java:1008)
at org.python.pycode._pyx3.f$0(<string>:60)
at org.python.pycode._pyx3.call_function(<string>)
at org.python.core.PyTableCode.call(PyTableCode.java:167)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1386)
at org.python.core.Py.exec(Py.java:1430)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:267)
at org.myrobotlab.service.Python$PIThread.run(Python.java:161)
 
GroG's picture

Hi SurferJim..   seems like

Hi SurferJim..   seems like you already made progress ...  installing jdk 1.8, installing eclipse, checking out the source (develop branch I hope) 

Nice avatar picture ! 

Lets start with virtual hardware .. cuz virtual hardware only catches on virtual fire ;)

Starting with Runtime & F11 in eclipse make sure you entered the following command line parameters in eclipse menu Run->Debug Configurations (you should only have to do this once - it will save you from some issues in the future)

After MRL starts (even in eclipse) you should install all services once...

tab runtime -> system -> install all. This will take a while, but only needs to be done only once.  Pour and enjoy your favorite beverage....

After its done, you'll need to restart MRL.

Now you can hit the python tab -> examples

This starts a browser and directs you to a list of 163+ exciting SERVICE SCRIPTS.  These are the same ones you can access through the "pretty" matrix page - http://myrobotlab.org/matrix.php?branch=develop (the little script symbol will bring up a service script example)

Let's look at UltraSonicsensor

https://github.com/MyRobotLab/pyrobotlab/blob/develop/service/UltrasonicSensor.py

most of these script have the amazing quality, when you add 

virtual = True

They magically work without any real hardware ... if there is one you find that does not work this way, let us know.  It means the auto-magical elves missed one - and they will be beaten mercilessly.

Let's do it again - but right click -> undock the sr04 tab..

Neato ...

Now lets get crazy and run the PingDar script ..

WHOOHOO .. that worky too .. looks like we don't need to be the auto-magical elves !

You can learn a lot from the matrix set of python scripts.  You can play with them, tweak them, experiment, and break things without having to worry about hardware exploding ...

Have fun !

SurferJim's picture

Hey

Thanks Grog. , I have a clean compile with the develop branch and have MRL running locally   I had some issues with some Utf characters, but edited them out for now   Thanks for the help   Where do I get the latest MRL agent that runs on the arduino?  Like, version 57, as I’m currently on vers 40 as my logfile informed me

GroG's picture

After a

After a (runtime->system->install all) you should have it under
%running_or_installation_directory%/resource/Arduino/MrlComm

SurferJim's picture

Thanks Grog!   It's been

Thanks Grog!   It's been quite an adventure for me this week as I had been developing under Java 1.7 and Kepler.    When my environment works, I don't mess around with it much, I just code and code.  I am now on JDK 8 and running on Oxygen.  I even updated my BioMight project as well, so it was a nice motivator to bring myself current :-)  I will run the vitual arduino, as suggested, and update my Arduino controller this eve after I return from my day job. Can't wait to see it work!   Gotta say, I like the code's  architecture.  Have a great weekend :-)

 

Update ----

For those who wish to dig into the code, when you setup Eclipse, make sure that under Window->Preferences->Java->Compiler that you have JDK 1.8 Compliace set.  Under the Build Path, ensure you have the 1.8 JRE Library so that everything compiles cleanly.  A few files use UTF-8 encoding to cover multiple characters.  I just edited them out for now, as I'm only using English as the moment.  Thanks to everyone for the help.  I now have the UltrasonicSensor working as expected.  On to better things like teaching my robotic inmoov arm to recognize and pet my dog  :-)