Exemplo n.º 1
0
def main():
    # 操作するArduinoを決定
    board = Arduino(Arduino.AUTODETECT)
    # サンプリング周期を10msに固定
    board.samplingOn(10)
    # pin8 をアナログピンのinputに設定
    digital_8 = set_pin(board, pin=8, mode="i", data="digital")
    time.sleep(2)
    while True:
        # センサから取得した値を0~1024(int)で出力
        print(digital_8.read())
        time.sleep(0.5)
    # アナログピンのサンプリングを終了する
    board.samplingOff()
class AnalogPrinter:
    def __init__(self):
        # sampling rate: 50Hz
        self.samplingRate = 100
        self.timestamp = 0
        self.board = Arduino(PORT)

    def start(self):
        self.board.analog[0].register_callback(self.myPrintCallback)
        self.board.samplingOn(1000 / self.samplingRate)
        self.board.analog[0].enable_reporting()

    def myPrintCallback(self, data):
        print("%f,%f" % (self.timestamp, data))
        self.timestamp += (1 / self.samplingRate)

    def stop(self):
        self.board.samplingOff()
        self.board.exit()
Exemplo n.º 3
0
class Reader:
    class Analog:
        def __init__(self, pin, _board):
            ### base ----------------
            self._board = _board
            self.timestamp = 0
            ### data-----------------
            self.pin = pin
            self.value = 0
            self.callback = None
            ### ---------------------
        def print_value(self, data):
            print("%f,%f" % (self.timestamp, data))
            self.timestamp += (1 / self.samplingRate)
            self.set_value(data)

        def set_value(self, data):
            self.value = data

    def __init__(self, port='/dev/ttyUSB0'):
        ### base ----------------------
        self.port = port
        self.board = Arduino(self.port)
        ### data ---------------------
        self.analog = {}
        self.samplingRate = 10
        ### setup --------------------
        self.board.samplingOn(100 / self.samplingRate)

    ### void setup -------------------------------------------------------------
    def start(self, pin=0, callback=None):
        self.analog[pin] = self.Analog(pin, self.board)
        if callback: self.analog[pin].callback = callback
        else: self.analog[pin].callback = self.analog[pin].set_value
        self.board.analog[pin].register_callback(self.analog[pin].callback)
        self.board.analog[pin].enable_reporting()

    ### void draw --------------------------------------------------------------
    def stop(self):
        self.board.samplingOff()
        self.board.exit()
"""
# -------------  Initialization of the Script -------------

    - fs : sampling frequency, defined for every application and limited by the system specifications.         
    - PORT : communication port, detected automatically so we don't have to care about the specific COM port.   
    - app : global QT application object for plotting.                                                        
    - running = signals to all threads in endless loops that we'd like to run these                            

"""

fs = 100
PORT = Arduino.AUTODETECT
app = QtGui.QApplication(sys.argv)
running = True
board = Arduino(PORT)  # Get the Arduino board.
board.samplingOn(1000 / fs)  # Set the sampling rate in the Arduino


class IIR2Filter(object):
    """
    given a set of coefficients for the IIR filter this class creates an object that keeps the variables needed for the
    IIR filtering as well as creating the function "filter(x)" with filters the input data.

    Attributes:
        @:param coefficients: input coefficients of the IIR filter as an array of 6 elements where the first three
        coefficients are for the FIR filter part of the IIR filter and the last three coefficients are for the IIR part
        of the IIR filter.
    """
    def __init__(self, coefficients):
        self.myCoefficients = coefficients
        self.IIRcoeff = self.myCoefficients[3:6]
Exemplo n.º 5
0
# sampling rate: 100Hz
samplingRate = 100

# called for every new sample which has arrived from the Arduino
def callBack(data):
    # send the sample to the plotwindow
    # add any filtering here:
    # data = self.myfilter.dofilter(data)
    realtimePlotWindow.addData(data)

# Get the Ardunio board.
board = Arduino(PORT)

# Set the sampling rate in the Arduino
board.samplingOn(1000 / samplingRate)

# Register the callback which adds the data to the animated plot
board.analog[0].register_callback(callBack)

# Enable the callback
board.analog[0].enable_reporting()

# show the plot and start the animation
plt.show()

# needs to be called to close the serial port
board.exit()

print("finished")
Exemplo n.º 6
0
        board.digital[yellow_LED].write(1)

    ch1 = board.analog[1].read()
    if ch1:
        Plot2.addData(ch1)
        ch1_filtered = abs(IIR_Y.dofilter(ch1))
        Plot4.addData(ch1_filtered)
        if ch1_filtered > THRESHOLD:
            board.digital[red_LED].write(1)


# Get the Ardunio board.
board = Arduino(PORT)

# Set the sampling rate in the Arduino
board.samplingOn(1000 / Fs)

# Register the callback which adds the data to the animated plot
board.analog[0].register_callback(callBack)

# Enable the callback
board.analog[0].enable_reporting()
board.analog[1].enable_reporting()

# showing all the windows
app.exec_()

# needs to be called to close the serial port
board.exit()

print("Finished")
Exemplo n.º 7
0
from pyfirmata2 import Arduino
from time import sleep
import matplotlib.pyplot as plt

signal = []

def my_cb(data):
    # print(data)
    signal.append(data)
    #pass

board = Arduino(Arduino.AUTODETECT)

board.samplingOn(10)  # ms

board.analog[0].register_callback(my_cb)
board.analog[0].enable_reporting()

lenPin = board.get_pin('d:3:p')

lum = 0
delta_lum = 0.05;

for t in range(50):
    sleep(0.5)
    if lum > 1.0 or lum < 0:
        delta_lum = -delta_lum
    lum += delta_lum
    lenPin.write(lum)

lenPin.write(0)
Exemplo n.º 8
0
    # send the sample to the qtwindow
    # add any filtering here:
    # data = self.myfilter.dofilter(data)
    now = time.time()
    if (now - time_pre) > 0.010:
        qt_display.addData(data)


time_pre = time.time()  #initiate time
#sleep 5 seconds to calculate sample rate

# Get the Ardunio board.
board = Arduino(PORT)

# Set the sampling rate in the Arduino
board.samplingOn(1000 / sampling_rate)

# Register the callback which adds the data to the animated plot
board.analog[0].register_callback(callBack)

# Enable the callback
board.analog[0].enable_reporting()

# show the plot and start the animation
print("sampling rate: ", sampling_rate, "Hz")

# start display window
display_window = QtGui.QApplication(sys.argv)
display_window.exec_()

# #save data episode for better design filter
Exemplo n.º 9
0
class ZombieController(object):
    PIN_MIRROR = 2
    PIN_BUTTON = 0
    PIN_SPARKS = 13

    PIN_DATA_MIRROR_ON = 0
    PIN_DATA_MIRROR_OFF = 1

    def __init__(self):
        self.main_quest = None
        # noinspection PyBroadException
        try:
            self.board = Arduino('COM11')
            self.board.samplingOn(50)

            self.mirror_pin: Pin = self.board.digital[ZombieController.PIN_MIRROR]
            self.sparks_pin: Pin = self.board.digital[ZombieController.PIN_SPARKS]

            self.button_pin: Pin = self.board.analog[ZombieController.PIN_BUTTON]
            self.button_pin.mode = INPUT
            self.button_pin.register_callback(self.big_red_button_pressed)
            self.button_pin.enable_reporting()
            self.board_missing = False
        except Exception:
            logging.error("Arduino in ZombieBox is not responding!!!")
            self.board_missing = True

    def reset(self):
        if self.board_missing:
            return

        self.button_pin.enable_reporting()

    def mirror(self, turn_on=True):
        if self.board_missing:
            return

        self.mirror_pin.write(0 if turn_on else 1)

    def blink(self):
        if self.board_missing:
            return

        self.sparks_pin.write(1)
        self.board.pass_time(0.05)
        self.sparks_pin.write(0)

    def sparkle(self):
        if self.board_missing:
            return

        for i in range(20):
            self.sparks_pin.write(random.randint(0, 1))
            self.board.pass_time(0.02)
        self.sparks_pin.write(0)

    def register_in_lua(self, main_quest):
        self.main_quest = main_quest
        return self

    def big_red_button_pressed(self, data):
        if data > 0.02:
            print(f"Activated on {data}")
            # self.button_pin.disable_reporting()
            if self.main_quest:
                self.main_quest['on_zombie_activated'](self.main_quest)
Exemplo n.º 10
0
class AnalogPrinter:
    
    def __init__(self):
        # sampling rate: 10Hz
        self.samplingRate = 10
        self.timestamp = 0
        self.gas = 0
        self.flama = 0
        self.luz = 0

    def start_gas(self):
        self.board = Arduino(PORT)
        self.board.analog[0].register_callback(self.myPrintCallback)
        self.board.samplingOn(1000 / self.samplingRate)
        self.board.analog[0].enable_reporting()

    def start_flama(self):
        self.board = Arduino(PORT)
        self.board.analog[1].register_callback(self.myPrintCallback_2)
        self.board.samplingOn(1000 / self.samplingRate)
        self.board.analog[1].enable_reporting()

    def start_luz(self):
        self.board = Arduino(PORT)
        self.board.analog[2].register_callback(self.myPrintCallback_3)
        self.board.samplingOn(1000 / self.samplingRate)
        self.board.analog[2].enable_reporting()

    def myPrintCallback(self, data):
        data1 = data * (5 / 1023) * 1000
        self.timestamp += 1 / self.samplingRate
        if data1 <= 0.2:
            print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",)
            self.board.digital[2].write(0)
            print("no hay gas")
            self.gas = 0
        else:
            print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",)
            self.board.digital[2].write(1)
            print("aqui hubo gas!")
            self.gas = 1

    def myPrintCallback_2(self, data):
        data1 = data * (5 / 1023) * 1000
        self.timestamp += 1 / self.samplingRate
        if data1 >= 2:
            print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es mayor al muestreo",)
            self.board.digital[3].write(0)
            self.flama = 1
            print("no hay fuego")
        else:
            print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",)
            self.board.digital[3].write(1)
            print("fuego!!")
            self.flama = 1

    def myPrintCallback_3(self, data):
        data1 = data * (5 / 1023) * 1000
        self.timestamp += 1 / self.samplingRate
        if data1 >= 1:
            print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",)
            self.board.digital[4].write(0)

            if self.gas == 1 :
                self.board.digital[2].write(1)

            if self.flama == 1:
                self.board.digital[3].write(1)

            if self.luz == 1:
                self.board.digital[4].write(1)

            if self.luz == 1 and self.gas == 1 and self.flama == 1:
                self.board.digital[5].write(1)

        else:
            print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es mayor al muestreo",)
            self.board.digital[4].write(1)
            self.luz = 1

            if self.gas == 1 :
                self.board.digital[2].write(1)

            if self.flama == 1:
                self.board.digital[3].write(1)

            if self.luz == 1 and self.gas == 1 and self.flama == 1:
                self.board.digital[5].write(1)
            

    def stop(self):
        self.board.samplingOff()
        self.board.exit()
Exemplo n.º 11
0
from pyfirmata2 import Arduino
from time import sleep


def my_cb(data):
    print(data)


board = Arduino(Arduino.AUTODETECT)

board.samplingOn(12)  # ms

board.analog[0].register_callback(my_cb)
board.analog[0].enable_reporting()

# board.pass_time(1)

lenPin = board.get_pin('d:11:p')

for t in range(200):
    sleep(0.050)
    lenPin.write(.8)
    #print("READ", board.analog[0].read())

#https://forum.arduino.cc/index.php?topic=70641.0

board.exit()
            )  # Call the addandfilter function to commence the filtering

    lowpass = sig.butter(
        N=2, Wn=0.5 / 50, btype='lowpass', output='sos'
    )  # Butterworth filter to create a lowpass with 0.5 Hz cutoff
    lowpass1 = sig.butter(
        N=2, Wn=1 / 50, btype='lowpass', output='sos'
    )  # Butterworth filter to create a lowpass with 1 Hz cutoff

    iir = IIRFilters.IIRFilter(
        lowpass)  # Send the sos coefficients to the filter
    iir2 = IIRFilters.IIRFilter(
        lowpass)  # Send the sos coefficients to the filter
    iir3 = IIRFilters.IIRFilter(
        lowpass1)  # Send the sos coefficients to the filter
    iir4 = IIRFilters.IIRFilter(
        lowpass1)  # Send the sos coefficients to the filter

    board = Arduino(PORT)  # Initialize board to look up for pins
    board.samplingOn(1000 /
                     samplingRate)  # Set the sampling rate for the board
    board.analog[0].register_callback(
        callBack)  # Register the callBack to a Port
    board.analog[0].enable_reporting()  # Enable port reporting for pin 0
    board.analog[1].enable_reporting()  # Enable port reporting for pin 1
    board.analog[2].enable_reporting()  # Enable port reporting for pin 2
    form.Portshow(str(board))  # Call the function to show the Port
    app.exec_()  # Execute our application
    board.exit()  # Close the board
    print("DONE")