Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 613167c285 |
@@ -0,0 +1,9 @@
|
||||
DC Voltage DC_BIAS
|
||||
AC Amplitude AC_AMP
|
||||
Frequency Range Max FREQ_START
|
||||
Frequency Range Min FREQ_STOP
|
||||
Points Per Decades PPD
|
||||
Point Spacing SCALE
|
||||
Delay DELAY
|
||||
Average AVERAGE_NUM
|
||||
Current Range RTIA
|
||||
-88
@@ -1,88 +0,0 @@
|
||||
import psycopg2
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy import signal
|
||||
import numpy as np
|
||||
|
||||
# data_spin_histogram
|
||||
def histogram_slope(x_data, y_data, slope =0, bins =0, plot =True):
|
||||
if len(x_data)!=len(y_data):
|
||||
print('Scale of x_data and y_data are different.')
|
||||
return False
|
||||
elif bins == 0:
|
||||
bins = round(len(x_data)/2)
|
||||
arr_list = [y_data[i] - slope*x_data[i] for i in range(len(x_data))]
|
||||
cons_max = max(arr_list)
|
||||
cons_min = min(arr_list)
|
||||
hist_range = np.linspace(cons_min, cons_max, bins+1)
|
||||
hist_range_mid = [(hist_range[i]+hist_range[i+1])/2 for i in range(len(hist_range)-1)]
|
||||
hist_range[-1] = hist_range[-1]+1
|
||||
hist_list = [len([j for j in arr_list if j>=hist_range[i] and j<hist_range[i+1]]) for i in range(len(hist_range)-1)]
|
||||
peek_temp_list,_ = signal.find_peaks(hist_list)
|
||||
peek_temp_list = list(peek_temp_list)
|
||||
print(peek_temp_list)
|
||||
# peek_temp_list[0] = list(peek_temp_list[0])
|
||||
if np.sign(np.diff(hist_list))[0] < 0:
|
||||
# print( np.sign(np.diff(hist_list))[0])
|
||||
peek_temp_list = [0] + peek_temp_list
|
||||
if np.sign(np.diff(hist_list))[-1] > 0:
|
||||
# print(np.sign(np.diff(hist_list))[-1])
|
||||
peek_temp_list = peek_temp_list+[len(hist_list)-1]
|
||||
print(peek_temp_list)
|
||||
|
||||
peek_list = [i for i in peek_temp_list if hist_list[i] >= len(x_data)/bins*3]
|
||||
print(peek_list)
|
||||
results_half = signal.peak_widths(hist_list,peek_list, rel_height=0.8)
|
||||
peak_min,peak_max = [cons_min + (cons_max-cons_min)/bins*j for j in results_half[2]],[cons_min + (cons_max-cons_min)/bins*j for j in results_half[3]]
|
||||
print(peak_min,peak_max)
|
||||
peak_80=[]
|
||||
for i in range(len(peak_min)):
|
||||
peak_filt_list = [j for j in arr_list if j>=peak_min[i] and j<=peak_max[i]]
|
||||
print(np.mean(peak_filt_list))
|
||||
print(len(peak_filt_list))
|
||||
peak_80.append(np.mean(peak_filt_list))
|
||||
|
||||
print(results_half[2])
|
||||
|
||||
print(results_half)
|
||||
plt.plot(peak_80, [hist_list[j] for j in peek_list], "y")
|
||||
if plot:
|
||||
plt.hist(arr_list, bins)
|
||||
plt.plot([hist_range_mid[i] for i in peek_list], [hist_list[j] for j in peek_list], "xb")
|
||||
plt.plot(peak_80, [hist_list[j] for j in peek_list], "xr")
|
||||
# plt.hlines(results_half[1],[ hist_list[int(j)] for j in results_half[2]],[ hist_list[int(j)] for j in results_half[2]], color="g")
|
||||
plt.savefig("histogrim.png")
|
||||
return hist_list, hist_range_mid, peek_list
|
||||
|
||||
def run():
|
||||
conn = psycopg2.connect(database="postgres", user="biopro", password="BioProControlBox", host="127.0.0.1", port="5432")
|
||||
cur = conn.cursor()
|
||||
id = 14304
|
||||
|
||||
sql_str = f'select "raw_data" from "recording_data_metas" WHERE id = {id}'
|
||||
cur.execute(sql_str)
|
||||
data = cur.fetchall()[0][0]
|
||||
channel_data = []
|
||||
for i in ['1','2']:
|
||||
channel_data_temp = []
|
||||
for j in data[i]:
|
||||
# print(j)
|
||||
sql_str = f'select "data" from "{i}_recording_data_raws" WHERE id = {j}'
|
||||
cur.execute(sql_str)
|
||||
raw_data = cur.fetchall()[0][0]
|
||||
raw_data = raw_data.replace('"***"',' ').split(" ")
|
||||
channel_data_temp =channel_data_temp + [int(raw_data[k]) for k in range(len(raw_data)) if k%2]
|
||||
# print(len(channel_data_temp))
|
||||
channel_data.append(channel_data_temp)
|
||||
# print(channel_data[1][-1])
|
||||
# print(channel_data[0][:500])
|
||||
plt.plot(channel_data[0],channel_data[1],'o',markersize=2)
|
||||
plt.hlines(1e6,2.4e6,2.6e6,color='g')
|
||||
plt.savefig('test.png')
|
||||
plt.close()
|
||||
|
||||
x_data, y_data ,peek_list = histogram_slope(channel_data[0],channel_data[1],bins = 200)
|
||||
print([y_data[i] for i in peek_list])
|
||||
print(peek_list, channel_data)
|
||||
return channel_data, peek_list
|
||||
# print([y_data])
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.9 KiB |
@@ -5,21 +5,43 @@ import time
|
||||
import sys
|
||||
import json
|
||||
|
||||
# from deviceManager import DeviceManager
|
||||
# from requests import Requests
|
||||
from deviceManager import DeviceManager
|
||||
from requests import Requests
|
||||
from mqtt import MqttThread
|
||||
from db_test import run
|
||||
|
||||
class Main():
|
||||
def __init__(self, controller_id = 'b8:27:eb:18:f8:cc', mqtt_ip = '192.168.2.1') -> None:
|
||||
# setup mqtt thread
|
||||
def __init__(self, controller_id = 'b8:27:eb:0c:d3:11', mqtt_ip = '192.168.5.53') -> None:
|
||||
self._deviceManager = DeviceManager()
|
||||
self._mqttThread = MqttThread(self, controller_id, mqtt_ip, 1883, 'test')
|
||||
self._mqttThread.run()
|
||||
|
||||
self._mqttRequest = Requests(self._mqttThread)
|
||||
self._mqttRequest.get_device()
|
||||
|
||||
def get_analysis_data(self, input:str):
|
||||
raw_data, peek_list = run()
|
||||
self._mqttThread.publish(raw_data)
|
||||
|
||||
def get_device_info_all(self, device: str) -> str:
|
||||
deviceDict = json.loads(device)
|
||||
# until get device info
|
||||
self._deviceManager.set_device(deviceDict)
|
||||
|
||||
# instruction example 1
|
||||
# self._mqttRequest.device_parameter_array_set(self._deviceManager.get_mac_address(), 'BLE_WRITE', '040005')
|
||||
# self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'ble_instru_send')
|
||||
|
||||
# instruction example 2
|
||||
# self._mqttRequest.device_parameter_array_set(self._deviceManager.get_mac_address(), 'BLE_WRITE', '01')
|
||||
# self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'ble_instru_send')
|
||||
# self._mqttRequest.device_parameter_get(self._deviceManager.get_mac_address(), 'ADC_VALUE_I')
|
||||
|
||||
#instruction example 3
|
||||
self._mqttRequest.device_parameter_set(self._deviceManager.get_mac_address(), 'MODE', 4)
|
||||
self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'start')
|
||||
|
||||
def device_parameter_value(self, data: str) -> None:
|
||||
dataDict = json.loads(data)
|
||||
print(dataDict)
|
||||
if dataDict['data'].get('ADC_VALUE_I') is not None:
|
||||
value = format(dataDict['data']['ADC_VALUE_I'], '040x')
|
||||
print(value)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# if len(sys.argv) < 3:
|
||||
|
||||
@@ -50,9 +50,9 @@ class MqttThread():
|
||||
self._mqtt_client.loop_start()
|
||||
|
||||
def on_connect(self, client: mqtt.Client, user_data, flags, rc: int):
|
||||
topic = f'{self.__controller_ID }_data_analysis/#'
|
||||
print('subscribe topic', topic)
|
||||
self._mqtt_client.subscribe(topic)
|
||||
topic = self.__controller_ID + '_user'
|
||||
self._mqtt_client.subscribe(self.__controller_ID + '/+/+')
|
||||
self._mqtt_client.subscribe(self.__controller_ID + '/data_server/device_data_stream/+/+')
|
||||
self._connected = True
|
||||
|
||||
def on_disconnect(self, client: mqtt.Client, user_data, rc):
|
||||
@@ -63,24 +63,18 @@ class MqttThread():
|
||||
|
||||
def publish(self, payload: str, wait_for_ack = False) -> int:
|
||||
QoS = 2 if wait_for_ack else 0
|
||||
topic = f'{self.__controller_ID}/data_analysis'
|
||||
message = json.dumps(payload)
|
||||
print('publish', topic, message)
|
||||
message_info = self._mqtt_client.publish(topic, message, QoS)
|
||||
|
||||
message_info = self._mqtt_client.publish(self.__controller_ID + '_user', json.dumps(payload), QoS)
|
||||
print(self.__controller_ID + '_user', payload)
|
||||
if wait_for_ack:
|
||||
print(" > awaiting ACK for {}".format(message_info.mid))
|
||||
message_info.wait_for_publish()
|
||||
print(" < received ACK for {}".format(message_info.mid))
|
||||
|
||||
def on_message(self, client: mqtt.Client, user_data, message):
|
||||
print('on_message', message.topic, message.payload)
|
||||
topic = message.topic.split('/')[1]
|
||||
payload = None
|
||||
|
||||
try:
|
||||
payload = message.payload.decode('utf-8')
|
||||
# print('callback from', topic, message.topic, payload)
|
||||
print(message.topic, payload)
|
||||
except:
|
||||
print('error')
|
||||
|
||||
|
||||
+3
-26
@@ -8,8 +8,8 @@ class Requests():
|
||||
def get_device(self) -> None:
|
||||
self._mqtt_client.publish({
|
||||
'header': 'get_device_info_all/0'
|
||||
})
|
||||
return
|
||||
}, True)
|
||||
return
|
||||
|
||||
def device_parameter_array_set(self, device: str, name: str, data: str) -> None:
|
||||
dataArray = [0 for i in range(16)]
|
||||
@@ -51,27 +51,4 @@ class Requests():
|
||||
self._mqtt_client.publish({
|
||||
'header': 'device_instruction/0',
|
||||
'device': device,
|
||||
})
|
||||
|
||||
def hardware_scan(self) -> None:
|
||||
self._mqtt_client.publish({
|
||||
'header': 'hardware_scan/0',
|
||||
})
|
||||
|
||||
def hardware_device(self) -> None:
|
||||
self._mqtt_client.publish({
|
||||
'header': 'hardware_device/0',
|
||||
})
|
||||
|
||||
def hardware_device_connect(self, device_name, device_address, serial_number) -> None:
|
||||
self._mqtt_client.publish({
|
||||
'header': 'hardware_device_connect/0',
|
||||
'content': {
|
||||
'device_name': device_name,
|
||||
'device_address': self.mac_tuple_to_str(device_address),
|
||||
'serial_number': serial_number
|
||||
}
|
||||
})
|
||||
|
||||
def mac_tuple_to_str(self, mac_array):
|
||||
return ':'.join('{:02x}'.format(b) for b in mac_array)
|
||||
})
|
||||
Reference in New Issue
Block a user