Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import serial
- import time
- import numpy.linalg
- import math
- class Microphone(object): # Microphone class
- def __init__(self, x, y, limit):
- self.X = x
- self.Y = y
- self.low_limit = limit
- self.counter = 0
- def handle_adc_msg(msg, mics):
- words = msg.split(' ')
- mics[(ord(msg[1]) - ord('0'))].counter = int(words[1])
- mics[(ord(msg[1]) - ord('0'))].raw = msg
- def reset_mics(mics):
- for m in range(0,len(mics)):
- mics[m].counter = -1
- def do_hard_math_2d(mics, W):
- N = [None] * len(mics)
- for i in range(0, len(mics)):
- N[i] = float(mics[i].counter - mics[0].counter)
- print N
- print "C: " + str('%.2f' % C) + "\nN: " + str([ round(e, 2) for e in N ]) + "\n------------------------------"
- Tao = [None] * (len(N)-1)
- for i in range(0, len(Tao)):
- Tao[i] = N[i+1]-N[0]
- print "Tao: " + str([ round(e, 2) for e in Tao ])
- D = [None] * len(Tao)
- for i in range(0, len(D)):
- D[i] = Tao[i] * C
- print "D: " + str([round(e, 2) for e in D])
- W = [None] * len(D)
- for i in range(0, len(W)):
- W[i] = ( math.pow(D[i],2) -math.pow(mics[i+1].X,2) +math.pow(mics[0].X,2) -math.pow(mics[i+1].Y,2) +math.pow(mics[0].Y,2) ) / 2
- print "W: " + str([round(e, 2) for e in W])
- A = numpy.zeros((len(mics)-1,3))
- for i in range(0, len(mics)-1):
- A[i][0] = mics[0].X - mics[i+1].X
- A[i][1] = mics[0].Y - mics[i+1].Y
- A[i][2] = D[i]
- print("A: \n" + '\n'.join([''.join(['{:10}'.format(round(item, 2)) for item in row]) for row in A]))
- A_plus = numpy.linalg.pinv(A)
- print("A+: \n" + '\n'.join([''.join(['{:10}'.format(round(item, 2)) for item in row]) for row in A_plus]))
- X = A_plus.dot(numpy.array(W).transpose())
- print "\nX: " + str([round(e, 2) for e in X])
- #ERROR = math.pow(X[2], 2) - ( math.pow(knock.X, 2) + math.pow(knock.Y, 2) )
- #print("ERROR: " + str(round(ERROR)))
- def knock_calibration_2d(port, microphones, W):
- print " ------------------------- Knock calibration ----------------------------- "
- i = 0
- while True:
- if port.in_waiting > 0:
- msg = str(port.readline()).strip()
- if msg.startswith("ACK:"): # received an ACK message
- continue
- elif msg.startswith("A"): # received an ADC value
- words = msg.split(' ')
- microphones[(ord(msg[1]) - ord('0'))] = int(words[1])
- i = i + 1
- if i == len(microphones):
- break
- print "Raw calibration counter values: " + str([e for e in microphones])
- L = W / math.sqrt(2) # W=47
- T = 0.0
- for j in range(1, len(microphones)):
- T = T + (microphones[j].counter - microphones[0].counter)
- T = T / (len(microphones) - 1)
- C = L / T
- print "Normalized calibration counter values: " + str([e for e in microphones])
- print "L=" + str(L) + " T=" + str(T) + " C=" + str(C)
- return 1
- def main():
- # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ INIT ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- BAUD_RATE = 115200
- PORT_NUMBER = 'COM5'
- WAIT_ALL_MICS_TIME = 0.5 # sec
- W = 47
- port = serial.Serial(PORT_NUMBER, BAUD_RATE)
- msg = ""
- first_read_time = time.time()
- is_first_mics = True
- msg_count = 0
- adc_read_success = False
- microphones = [
- Microphone( 0, 0, 50),
- Microphone( W/2, W/2, 50),
- Microphone( W/2, -W/2, 50),
- Microphone( -W/2, -W/2, 50),
- Microphone( -W/2, W/2, 50)
- ]
- C = knock_calibration_2d(port, microphones, W)
- while True: # main loop
- if port.in_waiting > 0:
- msg = str(port.readline()).strip()
- if msg.startswith("ACK:"): # received an ACK message
- continue
- elif msg.startswith("A"): # received an ADC calue
- # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- if not is_first_mics:
- if time.time() - first_read_time < WAIT_ALL_MICS_TIME: # we are in time to collect more mics value
- if msg_count < len(microphones):
- print "new value has added to the microphones " + msg
- msg_count = msg_count + 1
- handle_adc_msg(msg, microphones)
- if msg_count == len(microphones):
- do_hard_math_2d(microphones, W)
- is_first_mics = True
- continue
- else:
- print "Runned out of time. New round started "
- is_first_mics = True
- if is_first_mics:
- print "Read first mics val in the round: " + msg
- first_read_time = time.time()
- reset_mics(microphones)
- msg_count = 0
- is_first_mics = False # here we read the first mics val so the others are not first val anymore
- msg_count = msg_count + 1
- handle_adc_msg(msg, microphones)
- continue
- # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- elif msg.startswith("S"): # received an ADC SUM value
- continue
- else: # received an unknown message
- continue
- if adc_read_success:
- adc_read_success = False
- reset_mics(microphones)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement