diff --git a/bluetoothControlApp.py b/bluetoothControlApp.py new file mode 100644 index 0000000000000000000000000000000000000000..96edfc957745dd18f4910e8d96a576b251c30fb3 --- /dev/null +++ b/bluetoothControlApp.py @@ -0,0 +1,56 @@ +from distanceMeasure import TFmini, Button +from piAndroidBluetooth import serialBluetooth +import os +import time +import threading +import serial + +tf = TFmini() +button0 = Button(11) +button1 = Button(13) +button2 = Button(15) +tf.open() +duration = 0.1 +blue = None + + +while True: + try: + while blue == None: + blue = serialBluetooth() + while True: + try: + line = blue.read() + if(line != ""): + duration = float(line) + print(line) + print(duration) + if button0.isPressed(): + distance = tf.getDistance() + if blue.isOpen() : + blue.write("0,"+str(distance)+",0") + print("0,"+str(distance)+",0") + time.sleep(duration) + elif button1.isPressed(): + distance = tf.getDistance() + if blue.isOpen() : + blue.write("1,"+str(distance)+",0") + print("1,"+str(distance)+",0") + time.sleep(duration) + elif button2.isPressed(): + distance = tf.getDistance() + if blue.isOpen() : + blue.write("2,"+str(distance)+",0") + print("2,"+str(distance)+",0") + time.sleep(2); + except Exception: + break; + print("closing connection") + blue.close() + blue = None + time.sleep(10) + + except KeyboardInterrupt: # Ctrl+C + tf.close() + blue.close() + diff --git a/distanceMeasure/button.py b/distanceMeasure/button.py new file mode 100644 index 0000000000000000000000000000000000000000..611de0fbe4f65c3258e5842ea393815a973ef344 --- /dev/null +++ b/distanceMeasure/button.py @@ -0,0 +1,24 @@ +import RPi.GPIO as GPIO + +class Button: + + def __init__(self, pin): + self.pin = pin + GPIO.setwarnings(False) # Ignore warning for now + GPIO.setmode(GPIO.BOARD) # Use physical pin numbering + GPIO.setup(self.pin , GPIO.IN, pull_up_down=GPIO.PUD_DOWN) + + def isPressed(self): + return GPIO.input(self.pin)==GPIO.HIGH + +if __name__ == '__main__': + button1 = Button(11) + button2 = Button(13) + button3 = Button(15) + while True: # Run forever + if button1.isPressed(): + print("Button1 was pushed!") + if button2.isPressed(): + print("Button2 was pushed!") + if button3.isPressed(): + print("Button3 was pushed!") diff --git a/distanceMeasure/modeSelector.py b/distanceMeasure/modeSelector.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/distanceMeasure/tfmini.py b/distanceMeasure/tfmini.py new file mode 100644 index 0000000000000000000000000000000000000000..34cb9687f833fc293f941756795427b15c509d19 --- /dev/null +++ b/distanceMeasure/tfmini.py @@ -0,0 +1,45 @@ +#Reference : https://github.com/TFmini/TFmini-RaspberryPi/blob/master/TFmini_RPi/tfmini.py +# -*- coding: utf-8 -* +import serial +import time + +class TFmini: + + def __init__(self): + self.ser = serial.Serial("/dev/ttyAMA0", 115200) + + def open(self): + if self.ser.is_open == False: + self.ser.open() + + def getDistance(self): + self.ser.reset_input_buffer() + while True: + count = self.ser.in_waiting + if count > 8: + recv = self.ser.read(9) + if recv[0] == 'Y' and recv[1] == 'Y': # 0x59 == 89 is 'Y' + low = int(recv[2].encode('hex'), 16) + high = int(recv[3].encode('hex'), 16) + distance = low + high * 256 + return distance + self.ser.reset_input_buffer() + + def close(self): + if self.ser != None: + self.ser.close() + + + +if __name__ == '__main__': + from button import Button + tfmini = TFmini() + button = Button(15) + try: + tfmini.open() + while True: + if button.isPressed(): + print(tfmini.getDistance()) + time.sleep(0.1) + except KeyboardInterrupt: # Ctrl+C + tfmini.close() diff --git a/piAndroidBluetooth/serialBluetooth.py b/piAndroidBluetooth/serialBluetooth.py new file mode 100644 index 0000000000000000000000000000000000000000..ac0fab2affca1ac70f1357012d938e635ad24c26 --- /dev/null +++ b/piAndroidBluetooth/serialBluetooth.py @@ -0,0 +1,50 @@ +import serial +import time +import os + +class serialBluetooth: + def __init__(self): + print("creating") + if os.path.exists("/dev/rfcomm0"): + self.ser = serial.Serial('/dev/rfcomm0', timeout=0) + else: + self.ser = None + + def open(self): + if self.ser == None: + return false + if self.isOpen() == False: + self.ser.open() + return true + + + def isOpen(self): + if self.ser == None: + return False + return self.ser.isOpen()==1 + + def write(self, message): + self.ser.write(message) + + def read(self): + return self.ser.readline() + + def close(self): + if self.isOpen() == True: + self.ser.close() + + +if __name__ == "__main__": + term = 0.15 + bluetooth = serialBluetooth() + f = open("./testing.txt","r") + for i in f.read().split("\n"): + print(i + " sending " + str(term)) + bluetooth.write(i) + time.sleep(term) + f.close() + while True: + message = raw_input() + bluetooth.write(message) + print(bluetooth.read()) + diff --git a/sampleApp.py b/sampleApp.py new file mode 100644 index 0000000000000000000000000000000000000000..88863c99a76cb067d9a8cf1b1314bcaf5623c8df --- /dev/null +++ b/sampleApp.py @@ -0,0 +1,38 @@ +from audioPlayer import pyaudioPlayer +from signalGenerator import sineGenerator, TextureAdder +from distance2Frequency import MaxMinConverter, derivativeConverter +import numpy as np +import scipy.io.wavfile as wave +import time + +duration = 0.05 +fs = 48000 + +flc = MaxMinConverter(10,300,220,880) +vlc = MaxMinConverter(50,250,0.2,0.9) +fdc = derivativeConverter(2,200,110,220,440,880) +player = pyaudioPlayer(fs) +sg = sineGenerator(fs) +ta = TextureAdder() + +volume = 0.75 + +try: + signal = np.empty(0) + #f = open("samples/realSamples/chair.txt", "r") + #f = [0.001,0.0025,0.005, 0.01, 0.025,0.5,0.1,0.25,0.5,1,2.5,5,10] + f = range(440, 880, 10) + for frequency in f: + #distance = int(distance) + #frequency = fdc.convert(distance) + #volume = vlc.convert(distance) + tempSignal = ta.addTexture(sg.generate(frequency,volume,duration), "bulb") + signal = np.append(signal,tempSignal) + + wave.write("bulb.wav",fs, signal) + #player.appendBuffer(signal) + #player.play() + #time.sleep(10) + +except KeyboardInterrupt: # Ctrl+C + del player diff --git a/showMapping.py b/showMapping.py new file mode 100644 index 0000000000000000000000000000000000000000..df27b0992e30a493dc90eae1eedb7d64c9c66713 --- /dev/null +++ b/showMapping.py @@ -0,0 +1,34 @@ +import matplotlib.pyplot as plt +import math + +class Calculator: + + def calculateLinear(self, a_x, b_x, a_y, b_y, x): + return (b_y - a_y)/(b_x-a_x)*(x-a_x)+a_y + + def exponentialA(self, a_x, b_x, a_y, b_y): + return math.log2(b_y/a_y)/(b_x-a_x) + + def calculateExponential(self, k, a, a_x, x): + return k*math.pow(2,a*(x - a_x)) + + def logarithmicA(self, a_x, b_x, a_y, b_y): + return (b_y-a_y)/math.log2(b_x - a_x+1) + + def calculateLogarithmic(self, a,a_x,a_y, x): + return a*math.log2(x - a_x+1)+a_y + +minD = 10 +maxD = 100 +minF = 440 +maxF = 1760 + +calculator = Calculator() + +f = range(10, 100, 1) +a = calculator.logarithmicA(minD, maxD, maxF, minF) +res = [calculator.calculateLogarithmic(a, minD, maxF, i) for i in f] +plt.plot(f, res) +plt.xlabel('distance') +plt.ylabel('frequency') +plt.show() diff --git a/signalGenerator/__init__.py b/signalGenerator/__init__.py index c1e4fffb700a036e562ef7c7938c7777da9aed07..806b9e26a80a31489fde30502f7828b36842f1e0 100644 --- a/signalGenerator/__init__.py +++ b/signalGenerator/__init__.py @@ -1 +1,2 @@ from .sineGenerator import sineGenerator +from .textureAdder import TextureAdder \ No newline at end of file diff --git a/signalGenerator/textureAdder.py b/signalGenerator/textureAdder.py new file mode 100644 index 0000000000000000000000000000000000000000..41004da97617390a63f64b73de75ddb003bb0851 --- /dev/null +++ b/signalGenerator/textureAdder.py @@ -0,0 +1,48 @@ +from math import sin, radians, log10 +import matplotlib.pyplot as plt + +class TextureAdder: + + bulb = [sin(radians(i/32*180)) for i in range(33)] + horn = [log10(i) for i in range(2, 11)] + + def straight(self, signal): + return signal + + def addTexture(self, signal, texture): + if texture == "straight": + return signal + + t = [] + + if texture == "bulb": + t = self.bulb + elif texture == "horn": + t = self.horn + else: + return signal + + start = 0 + end = 0 + before = 0 + for i, s in enumerate(signal): + if before * s <0: + end = i + signal[start:end] *= t[(int)((start+end)/2/len(signal)*len(t))] + start = end + before = s + signal[start: len(signal)] *= t[-1] + return signal + +if __name__ == "__main__": + from sineGenerator import sineGenerator + + sg = sineGenerator() + duration = 0.05 + ta = TextureAdder() + while(True): + frequency = int(input()) + signal = sg.generate(frequency,1, duration) + signal = ta.addTexture(signal, "bulb") + plt.plot(range(len(signal)), signal) + plt.show() \ No newline at end of file diff --git a/testingSamples/bulb.wav b/testingSamples/bulb.wav new file mode 100644 index 0000000000000000000000000000000000000000..a1adbf2fde4f93c6339026c6c95e71f1e781c27b Binary files /dev/null and b/testingSamples/bulb.wav differ diff --git a/testingSamples/frequencyRangeDown.wav b/testingSamples/frequencyRangeDown.wav new file mode 100644 index 0000000000000000000000000000000000000000..4d39727f05e8af02437056d83f8ab49fe01734cd Binary files /dev/null and b/testingSamples/frequencyRangeDown.wav differ diff --git a/testingSamples/frequencyRangeUp.wav b/testingSamples/frequencyRangeUp.wav new file mode 100644 index 0000000000000000000000000000000000000000..544f5068c630638a19193c2d437dda5fb47b3767 Binary files /dev/null and b/testingSamples/frequencyRangeUp.wav differ diff --git a/testingSamples/frequencySensitivity.wav b/testingSamples/frequencySensitivity.wav new file mode 100644 index 0000000000000000000000000000000000000000..fd10cf636ff45b930521f5d4d719641ae272afcb Binary files /dev/null and b/testingSamples/frequencySensitivity.wav differ diff --git a/testingSamples/horn.wav b/testingSamples/horn.wav new file mode 100644 index 0000000000000000000000000000000000000000..f8aae0345e77e79054512a6fc5d84d1ffd4f5a27 Binary files /dev/null and b/testingSamples/horn.wav differ diff --git a/testingSamples/straight.wav b/testingSamples/straight.wav new file mode 100644 index 0000000000000000000000000000000000000000..48588f12080b6add7ee59a1dd0b35c890aa4b150 Binary files /dev/null and b/testingSamples/straight.wav differ