MrYsLab / PyMata

A Python client class library for Interaction with Standard Firmata
GNU Affero General Public License v3.0
95 stars 40 forks source link

example in master-branch for ping is wrong #17

Closed dkurok closed 9 years ago

dkurok commented 9 years ago

Hello, I tested with ping/sonar and tried at first the ping_config_and_read. This does not work because there is a mixup of the PyMata-instance-variable (once called firmata and once called board). I changed the source to the following and did some additional comment on it; maybe it's better this way. Works fine on Arduino Uno with FirmataPlus under Windows (server 12) with Python 2.7 (Python(x,y) in my case). Here the working code:

#!/usr/bin/env python
"""
Copyright (c) 2013-2015 Alan Yorinks All rights reserved.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU  General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

You should have received a copy of the GNU  General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
import time
import sys
import signal

from PyMata.pymata import PyMata

# create a PyMata instance
board = PyMata("COM7", verbose=True)

def signal_handler(sig, frame):
    print('You pressed Ctrl+C')
    if board is not None:
        board.reset()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Configure up to for 6 SONAR modules
# here just one on pin 12 (echo and trigger are both on the same pin; see ping.png)
#with no callback and thus with no other parameters
board.sonar_config(12, 12)

time.sleep(1)

# Create a forever loop that will sequentially 
# print out the sonar data for the attached device

while 1:
    #data is an active_sonar_map with all sonars attached/configured
    #it is arranged as a dictonary where the pin# is the key
    #the value in the dictionary is a [] of [callback , value]. Callback is None, if no callback has been given
    #in the call of sonar_config (so here we only want the value)
    data = board.get_sonar_data()
    print('%d centimeters' % (data[12][1]))
    time.sleep(.2)
MrYsLab commented 9 years ago

Thanks for your input. I modified the examples. Please let me know if you see any other issues.