Hello guys i wanted to try the wireless piano (first time experementing with MRL) thats posted on the website by Alessandruino but i c/p code to python, i've installed arduino and jfugue extensions on MRL but and i've set my Arduino Mega2560 to COM4, Bps 9600, data bits 8, parity none, stop bits 1, flow control none but when i try to upload the example code on the board it says:

296260 [python] INFO  org.myrobotlab.service.Python  - exec #file : JFugue.pingPiano.py
import time

#create a Serial service named serial
jf = Runtime.createAndStart("jf","JFugue")
serial = Runtime.createAndStart("serial","Serial")
count = 0

if not serial.isConnected():
    #connect to a serial port COM3 57600 bitrate 8 data bits 1 stop bit 0 parity
    serial.connect("COM4", 9600, 8, 1, 0)
    #have python listening to serial
    serial.addListener("publishByte", python.name, "input") 
def input():
 global count
 newByte = int(serial.readByte())
 #we have reached the end of a new line
 if (newByte == 10) :
    distanceString = ""
    while (newByte != 13):
        newByte = serial.readByte()
        distanceString += chr(newByte)

    distance = int(distanceString)
    print distance
    # count is used to avoid loop : one note couldn't be repeated
    if (distance < 20 and count != 1) :
        jf.play('C')
        count = 1
    elif (distance > 30 and distance < 50 and count != 2):
        jf.play('D')
        count = 2
    elif (distance > 60 and distance < 80 and count != 3):
        jf.play('E')
        count = 3
296289 [Thread-199] WARN  class org.myrobotlab.framework.Service  - startService request: service jf is already running
296290 [Thread-199] WARN  class org.myrobotlab.framework.Service  - startService request: service serial is already running
296291 [Thread-199] INFO  org.myrobotlab.serial.SerialDeviceFactory  - getSerialDevice COM4|9600|8|1|0
296291 [Thread-199] INFO  org.myrobotlab.serial.SerialDeviceFactory  - Loaded class: class org.myrobotlab.serial.gnu.SerialDeviceFactoryGNU
296291 [Thread-199] 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
296297 [Thread-199] ERROR org.myrobotlab.service.Serial  - could not get serial device
296298 [Thread-199] WARN  class org.myrobotlab.framework.Service  - attempting to add duplicate MRLListener {"outMethod":"publishByte""name":"python""inMethod":"input""paramType":"[Ljava.lang.Class;@23259fb6"}
 
The only variable i changed was COM3 to COM4, should i change anything else? i know the sollution must be easy and i feel really stupid :P Thanks in advance!!!

zampier

10 years 5 months ago

Try restart the MRL and let the script start the processes

GroG

10 years 5 months ago

The Errors you are see are because you are running the script twice,
so it's trying to re-create the Services and re-open the serial port...  

This should not be a problem though.

Some things to check.

1. Make sure the MRLComm.ino was loaded into your Arduino successfully - you can do this with MRL's Arduino service - but after an upload you should restart MRL.  Or you can use the Arduino IDE to upload the script.  

2. If things still don't work after that send a No-Worky to us.. and our "crack" team will look into the problem :D http://myrobotlab.org/content/helpful-myrobotlab-tips-and-tricks-0#noWorky

OH !  (just saw the connect) so there may be some confusion  MRL needs MRLComm.ino loaded on your arduino..

The default connection parameters of the script are 57600, 8, 1, 0 -  did you change this in MRLComm.ino ?

Default connection can be done without parameters e.g. serial.connect("COM4") in the Python

Curious why you want to connect at 9600 ?

And change your Avatar .. You are not scared gopher ! :D

Hello back and thanx for the instant reply!
To begin with, i once tried changing the bitrate to 57600 but i couldnt change it and also i'm not really sure about what it would happen if i increase it.
Secondly, well secondly i want to know how to change it because i cant do anything if i dont have the correct parameters :D
Cheers!

PS. Is there a "map" of which pins i have to use for the ultrasonic sensor? (I have the HC-SR04 and it has 4 pins)

Alessandruino

10 years 5 months ago

In reply to by gseonr

hi gseonr, have you ever used your sensor without mrl? what i mean is : have you an arduino sketch which is worky for your sensor? 

It would be good to make it works just using the arduino first....

Test this arduino sketch and tell us if it works for you!!

/*HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8
 
 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 And modified further by ScottC here: http://arduinobasics.blogspot.com/
 on 10 Nov 2012.
 */
 
 
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
 
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
 
void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
 
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

Hey man, thanks for the reply, i've used ultrasonic sensors a lot in my previous projects so i dont think that this is the part that i mess it up :P

Alessandruino

10 years 5 months ago

In reply to by gseonr

this project is made by an arduino sketch and a python script...i need to know if that arduino sketch works for you... thanks

Just tried the arduino sketch, works just fine :)

To make ur life easier can u tell me in a few words what i have to do to make this project work? for example 
 

1) load the sketch on arduino
 

2) C/p the python code to MRL

3) etc etc etc

Alessandruino

10 years 5 months ago

In reply to by gseonr

Ya...now that we know arduino sketch works we are ready to take-off :D

1) load that arduino sketch in your arduino

2)modify the python script with your arduino com port

3) run the script

4) let us know :D

So i just did it and this is the log:
 

38813 [python] INFO  org.myrobotlab.service.Python  - exec #file : JFugue.pingPiano.py
import time
#create a Serial service named serial
jf = Runtime.createAndStart("jf","JFugue")
serial = Runtime.createAndStart("serial","Serial")
count = 0
if not serial.isConnected():
    #connect to a serial port COM3 57600 bitrate 8 data bits 1 stop bit 0 parity
    serial.connect("COM4")
    #have python listening to serial
    serial.addListener("publishByte", python.name, "input") 
def input():
 global count
 newByte = int(serial.readByte())
 #we have reached the end of a new line
 if (newByte == 10) :
    distanceString = ""
    while (newByte != 13):
        newByte = serial.readByte()
        distanceString += chr(newByte)
    distance = int(distanceString)
    print distance
    # count is used to avoid loop : one note couldn't be repeated
    if (distance < 20 and count != 1) :
        jf.play('C')
        count = 1
    elif (distance > 30 and distance < 50 and count != 2):
        jf.play('D')
        count = 2
    elif (distance > 60 and distance < 80 and count != 3):
        jf.play('E')
        count = 3
38865 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - loader for this class sun.misc.Launcher.AppClassLoader
38867 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - parent sun.misc.Launcher.ExtClassLoader
38867 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - system class loader sun.misc.Launcher$AppClassLoader@fae7b85
38868 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - parent should be nullsun.misc.Launcher.ExtClassLoader
38868 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - thread context sun.misc.Launcher.AppClassLoader
38868 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - thread context parent sun.misc.Launcher.ExtClassLoader
38868 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - refreshing classloader
38872 [Thread-11] INFO  class org.myrobotlab.framework.Service  - %s loading %d non-routable methods
38877 [Thread-11] INFO  class org.myrobotlab.net.CommunicationManager  - instanciating a org.myrobotlab.net.CommObjectStreamOverTCP
38881 [gui] ERROR class org.myrobotlab.framework.Service  - ------
java.lang.ClassNotFoundException: org.myrobotlab.control.JFugueGUI
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.myrobotlab.service.GUIService.getNewInstance(Unknown Source)
at org.myrobotlab.service.GUIService.createTabbedPanel(Unknown Source)
at org.myrobotlab.service.GUIService.addTab(Unknown Source)
at org.myrobotlab.control.RuntimeGUI.registered(Unknown Source)
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)
at org.myrobotlab.reflection.Instantiator.invokeMethod(Unknown Source)
at org.myrobotlab.service.GUIService.preProcessHook(Unknown Source)
at org.myrobotlab.framework.Service.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
------
 
38885 [gui] WARN  org.myrobotlab.service.GUIService  - could not construct a org.myrobotlab.control.JFugueGUI object - creating generic template
38882 [python] INFO  org.myrobotlab.service.Python  - exec from org.myrobotlab.service import JFugue
jf = Runtime.getServiceWrapper("jf").service
 
38897 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildGraph
38898 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-begin
38898 [gui] INFO  org.myrobotlab.control.ServiceGUI  - GUIServiceGUI service count 5
38900 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-end
38906 [gui] INFO  org.myrobotlab.service.GUIService  - org.myrobotlab.control.TabControl[,313,3,7x16,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=jf,verticalAlignment=CENTER,verticalTextPosition=CENTER]
40126 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - returning org.myrobotlab.service.JFugue
40127 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - loader for this class sun.misc.Launcher.AppClassLoader
40127 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - parent sun.misc.Launcher.ExtClassLoader
40127 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - system class loader sun.misc.Launcher$AppClassLoader@fae7b85
40127 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - parent should be nullsun.misc.Launcher.ExtClassLoader
40128 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - thread context sun.misc.Launcher.AppClassLoader
40128 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - thread context parent sun.misc.Launcher.ExtClassLoader
40129 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - refreshing classloader
40129 [jf] INFO  class org.myrobotlab.framework.Service  - adding addListener from jf.publishState to gui.getState
40130 [jf] INFO  class org.myrobotlab.framework.Service  - adding addListener from jf.publishStatus to gui.getStatus
40131 [Thread-11] INFO  class org.myrobotlab.framework.Service  - %s loading %d non-routable methods
40133 [Thread-11] INFO  class org.myrobotlab.net.CommunicationManager  - instanciating a org.myrobotlab.net.CommObjectStreamOverTCP
40134 [Thread-11] INFO  class org.myrobotlab.service.Runtime  - returning org.myrobotlab.service.Serial
40135 [python] INFO  org.myrobotlab.service.Python  - exec from org.myrobotlab.service import Serial
serial = Runtime.getServiceWrapper("serial").service
 
40137 [gui] ERROR class org.myrobotlab.reflection.Instantiator  - Error
40142 [Thread-11] INFO  org.myrobotlab.serial.SerialDeviceFactory  - getSerialDevice COM4|57600|8|1|0
40150 [serial] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishState to gui.getState
40151 [Thread-11] INFO  org.myrobotlab.serial.SerialDeviceFactory  - Loaded class: class org.myrobotlab.serial.gnu.SerialDeviceFactoryGNU
40153 [serial] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishStatus to gui.getStatus
40154 [Thread-11] 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
40160 [Thread-11] INFO  org.myrobotlab.serial.SerialDeviceFactory  - COM4
40160 [Thread-11] INFO  org.myrobotlab.serial.gnu.SerialDeviceGNU  - opening COM4
40176 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildGraph
40176 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-begin
40177 [gui] INFO  org.myrobotlab.control.ServiceGUI  - GUIServiceGUI service count 6
40179 [gui] INFO  org.myrobotlab.control.ServiceGUI  - buildLocalServiceGraph-end
40186 [gui] INFO  org.myrobotlab.service.GUIService  - org.myrobotlab.control.TabControl[,341,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]
40190 [Thread-11] INFO  org.myrobotlab.serial.gnu.SerialDeviceGNU  - opened COM4
41234 [Thread-11] INFO  class org.myrobotlab.framework.Service  - adding addListener from serial.publishByte to python.input
41445 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte
41449 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte
41450 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte
41451 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte
41452 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte
41453 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte
41652 [python_input] INFO  org.myrobotlab.service.Python  - setting data msg_serial_publishByte

The last line goes forever as long i dont stop it, and no sound is coming from my speakers nor does the python tab next to java tab show anything :(
 

gseonr

10 years 5 months ago

In reply to by gseonr

I sovled issues with it! i just had to use ("COM4" 8 1 0) instead of the original ("COM4") i was typing, thanks guys!

GroG

10 years 5 months ago

Yay ! ...

Sorry about the confusion.. I thought you were using the Arduino service.. but I see in the script it is using the Serial service .. my bad :(

serial.connect("COM4") without any parameters connects at 57600 rate 8 data 1 stop 0 parity ...