Behold Auto-Magic Elves & Humans Working Together !

That's right, humans can now use and play with the service script at the bottom of every service page.
So for every service, there is a (or soon to be) nice service page - which describes what the service is used for AND has a nice example script at the bottom in Python (soon to be other languages).

An example (only one I've verified) is our UltrasonicSensor service.
It's service page is here http://myrobotlab.org/service/UltrasonicSensor

And below is the script on the develop branch of pyrobotlab/servce/UltrasonicSensor.py


#file : service/UltrasonicSensor.py edit raw
#########################################
# UltrasonicSensor.py
# description: ranging sensor
# categories: data, finance
# more info @: http://myrobotlab.org/service/UltrasonicSensor
#########################################
 
# config
port = "COM15"
 
# start the service
python = Runtime.start("python","Python")
sr04 = Runtime.start("sr04", "UltrasonicSensor")
arduino = Runtime.start("arduino", "Arduino")
 
if ('virtual' in globals() and virtual):
    virtualArduino = Runtime.start("virtualArduino", "VirtualArduino")
    virtualArduino.connect(port)
 
# initializing
arduino.connect(port)
sr04.attach(arduino, 12, 11)
sr04.addRangeListener(python)
 
def onRange(distance):
  print "distance ", distance, " cm"
 
# event driven ranging
# start ranging for 5 seconds - the publishRange(distance) will be
# called for every attempt to range - this SHOULD NOT INTERFERE WITH SERVOS
# YAY !
# the even ranging DOES NOT USE Arduino's pulseIn() method -
# at the moment range() & ping() do
sr04.startRanging()
sleep(5)
sr04.stopRanging()
 
# range can also be retreieved in a blocking call
print "on demand range is ", sr04.range()
 
# you can also ping - ping does not do any calculations
# it simply returns the duration in microseconds of the ping
print "ping ", sr04.ping(), " mircoseconds"

The test system will run the file and prepend this line to the service script 

virtual = True

and then the Auto-Magic Elf will endlessly test - no conflict - no confusion - no special rules
just 3 lines of code (possibly a couple more if multiple virtual Arduinos or Serial ports are needed)

...goodtimes....

btw this auto-magic elf pointed out at least 6 bugs which i have corrected now in the script ...


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kwatters's picture

auto testing elf harmony

This is excellent.  Now we just need good script examples for all of our services ;) 

I was wondering, do you think the automagic elf could auto-prepend more of the boiler plate stuff like

 

if ('virtual' in globals() and virtual):
    virtualArduino = Runtime.start("virtualArduino", "VirtualArduino")
    virtualArduino.connect(port)
 
?

and we can just use a convention for all scripts to use "COM99" or something like that for their arduino..

o/w every script needs to create the virtual arduino and connect to the symbolic port.

 

 

GroG's picture

Ya .. usually .. more simple

Ya .. usually .. more simple is better .. but sometimes its finding a sweet-spot between 

Tranparency & Ease of Use

With what you propose - your having "Ease of Use" - because the end user won't have to look at this code they potentially won't be using.  Most the time I would agree with this.  

However, its a single if condition, and always "off" for the end user, unless they turn it on.  But its a good "seed" for users without hardware.  Not only do auto-magic elves benefit from virtual hardware, but potentially "real" users can benefit greatly from virtual hardware.

So, I (in this case) prefer Transparency of potentially using virtual hardware vs hiding everything from the user.