1
0
Fork 0
This commit is contained in:
YujinChu 2023-09-11 05:41:27 +00:00
parent edd1801581
commit c40e686cad
2 changed files with 58 additions and 45 deletions

View File

@ -162,12 +162,16 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
#print("read thread get even") #print("read thread get even")
evt.clear() evt.clear()
prev_a_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
prev_b_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] #prev_a_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#prev_b_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
ai_array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ai_array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
random_index = random.randint(0, len(my_array) -1) random_index = random.randint(0, len(my_array) -1)
ai_array = [random.randint(1,50000) for _ in range(16)] ai_array = [random.randint(1,50000) for _ in range(16)]
prev_a_ai = ai_array
prev_b_ai = ai_array
#value list on -> do control data #value list on -> do control data
#추<><ECB694>에 do power on 할 리스트는 어디선가 읽어서 와야 해요 #추<><ECB694>에 do power on 할 리스트는 어디선가 읽어서 와야 해요
#do1 do2 나눠야 해요 #do1 do2 나눠야 해요
@ -194,13 +198,14 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
# PORT A # PORT A
port_a_do_value = list(b[:16]) port_a_do_value = list(b[:16])
try: try:
a_ai_data=port_A_AI.read_registers(0x32, 32) #a_ai_data=port_A_AI.read_registers(0x32, 32)
a_ai_data=ai_array
# suc_cnt_ai = suc_cnt_ai +1 # suc_cnt_ai = suc_cnt_ai +1
except: except:
isSuccess = False isSuccess = False
if isSuccess: if isSuccess:
prev_a_ai = ai_array priv_a_ai = ai_array
try: try:
port_A_DO.write_bits(0, port_a_do_value) port_A_DO.write_bits(0, port_a_do_value)
@ -211,8 +216,9 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
# PORT B # PORT B
port_b_do_value = list(b[17:32]) port_b_do_value = list(b[17:32])
try: try:
b_ai_data=port_B_AI.read_registers(0x32, 32) #b_ai_data=port_B_AI.read_registers(0x32, 32)
prev_b_ai = ai_array b_ai_data = ai_array
#prev_b_ai = ai_array
# suc_cnt_ai = suc_cnt_ai +1 # suc_cnt_ai = suc_cnt_ai +1
except: except:
isSuccess = False isSuccess = False
@ -230,8 +236,8 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
queue_a.put([timenow,ai_array, port_a_do_value]) queue_a.put([timenow,ai_array, port_a_do_value])
queue_b.put([timenow,ai_array, port_b_do_value]) queue_b.put([timenow,ai_array, port_b_do_value])
else: else:
queue_a.put([0,ai_array, port_a_do_value]) queue_a.put([0,prev_a_ai, port_a_do_value])
queue_b.put([0,ai_array, port_b_do_value]) queue_b.put([0,prev_a_ai, port_b_do_value])

View File

@ -13,7 +13,7 @@ import paho.mqtt.client as mqtt
import numpy as np import numpy as np
from multiprocessing import shared_memory from multiprocessing import shared_memory
import argparse import argparse
import random
''' '''
전달해야할 데이터 전달해야할 데이터
@ -26,8 +26,8 @@ tube 번호: 1 - 128 , chamber / slot
상태: 정상이냐, 초기화, 불량 상태: 정상이냐, 초기화, 불량
ER1: 1초동안 판별, 판별할 동안 counting 예정 2V 이하라면 CNT UP ER1: 1초동안 판별, 판별할 동안 counting 예정 2V 이하라면 CNT UP
if) er: 10 되었음 (1 유지) -> 0으로 변환, ER2 cnt up if) er: 10 되었음 (1 유지) -> 0으로 변환, ER2 cnt up
if) 2v 이상인 경우가 발생했다 -> 0으로 변환 if) 2v 이상인 경우가 발생했다 -> 0으로 변환
ER2: OFF 횟수 ER2: OFF 횟수
@ -63,7 +63,7 @@ with open(f"{DEVICE_PATH}/config.yaml","r") as f:
with open(f"{ROOT_PATH}/config.json","r") as f: with open(f"{ROOT_PATH}/config.json","r") as f:
#with open(f"{args.config}/config.json","r") as f: #with open(f"{args.config}/config.json","r") as f:
data = json.load(f) data = json.load(f)
print(data) print(data)
print(data["1"]) print(data["1"])
@ -77,7 +77,7 @@ a = np.array(a)
shm = shared_memory.SharedMemory(create=True, size=a.nbytes, name="DO-shm") shm = shared_memory.SharedMemory(create=True, size=a.nbytes, name="DO-shm")
b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf) b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
b[:] = a[:] b[:] = a[:]
############################################################################### ###############################################################################
@ -85,7 +85,7 @@ b[:] = a[:]
############################################################################### ###############################################################################
#MQTT_TOPIC = info['mqtt']['topic'] #MQTT_TOPIC = info['mqtt']['topic']
MQTT_TOPIC = dev_info['assetcode'] MQTT_TOPIC = f"/device-data/{dev_info['assetcode']}"
MQTT_ID = info['mqtt']['id'] MQTT_ID = info['mqtt']['id']
MQTT_PW = info['mqtt']['pw'] MQTT_PW = info['mqtt']['pw']
MQTT_HOST_IP = info['mqtt']['host_ip'] MQTT_HOST_IP = info['mqtt']['host_ip']
@ -164,12 +164,15 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
prev_a_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] prev_a_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
prev_b_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] prev_b_ai = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
ai_array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
random_index = random.randint(0, len(my_array) -1)
ai_array = [random.randint(1,50000) for _ in range(16)]
#value list on -> do control data #value list on -> do control data
#추후에 do power on 할 리스트는 어디선가 읽어서 와야 해요 #추<EFBFBD><EFBFBD>에 do power on 할 리스트는 어디선가 읽어서 와야 해요
#do1 do2 나눠야 해요 #do1 do2 나눠야 해요
''' '''
| AI1 | AI2 | DO1 | DO2 | | AI1 | AI2 | DO1 | DO2 |
@ -178,12 +181,12 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
Success | 1 | 0 | x | x | Success | 1 | 0 | x | x |
Success | 1 | 1 | x | x | => 유효한 데이터 Success | 1 | 1 | x | x | => 유효한 데이터
최소 한개 이상 실패 했을 최소 한개 이상 실패 했을
-> retry 해볼 것이냐 -> retry 해볼 것이냐
-> 데이터를 어떻게 것이냐 -> 데이터를 어떻게 것이냐
-> 이전 데이터가 있다면, 이전 데이터를 가지고 더미 데이터로 사용하는 방법 => 안하기로 했었음 -> 이전 데이터가 있다면, 이전 데이터를 가지고 더미 데이터로 사용하는 방법 => 안하기로 했었음
''' '''
timenow = int(time.time()*1000) timenow = int(time.time()*1000)
isSuccess = True isSuccess = True
@ -197,26 +200,26 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
isSuccess = False isSuccess = False
if isSuccess: if isSuccess:
prev_a_ai = a_ai_data prev_a_ai = ai_array
try: try:
port_A_DO.write_bits(0, port_a_do_value) port_A_DO.write_bits(0, port_a_do_value)
# suc_cnt_do = suc_cnt_do +1 # suc_cnt_do = suc_cnt_do +1
except: except:
None None
# PORT B # PORT B
port_b_do_value = list(b[17:32]) port_b_do_value = list(b[17:32])
try: try:
b_ai_data=port_B_AI.read_registers(0x32, 32) b_ai_data=port_B_AI.read_registers(0x32, 32)
prev_b_ai = b_ai_data prev_b_ai = ai_array
# suc_cnt_ai = suc_cnt_ai +1 # suc_cnt_ai = suc_cnt_ai +1
except: except:
isSuccess = False isSuccess = False
if isSuccess: if isSuccess:
prev_b_ai = b_ai_data prev_b_ai = ai_array
try: try:
port_B_DO.write_bits(0, port_b_do_value) port_B_DO.write_bits(0, port_b_do_value)
# suc_cnt_do = suc_cnt_do +1 # suc_cnt_do = suc_cnt_do +1
@ -224,15 +227,15 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
None None
if isSuccess: if isSuccess:
queue_a.put([timenow,prev_a_ai, port_a_do_value]) queue_a.put([timenow,ai_array, port_a_do_value])
queue_b.put([timenow,prev_b_ai, port_b_do_value]) queue_b.put([timenow,ai_array, port_b_do_value])
else: else:
queue_a.put([0,prev_a_ai, port_a_do_value]) queue_a.put([0,ai_array, port_a_do_value])
queue_b.put([0,prev_b_ai, port_b_do_value]) queue_b.put([0,ai_array, port_b_do_value])
# lock.release() # lock.release()
@ -245,7 +248,7 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
##데이터 생성 할 때 ##데이터 생성 할 때
# ai1, ai2 # ai1, ai2
# -> ai1 [32] -> [curr, volt, curr, volt ...] # -> ai1 [32] -> [curr, volt, curr, volt ...]
''' '''
data_for_list = { data_for_list = {
"slot" : 1, "slot" : 1,
@ -260,9 +263,9 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
} }
} }
lists.append(data_for_list) lists.append(data_for_list)
### 여기서 보냄 ### 여기서 보냄
# 10번 시그널 으면 # 10번 시그널 <EFBFBD>으면
# #
# list mqtt send # list mqtt send
# list clear # list clear
if read_cnt_ch0 % 10 == 0: if read_cnt_ch0 % 10 == 0:
@ -282,6 +285,10 @@ def cwt_thread(evt, chip_index, port_a_ai_id , port_a_do_id, port_b_ai_id , port
# time.sleep(1) # time.sleep(1)
''' '''
###############################################################################
# dummy data thread #
###############################################################################
############################################################################### ###############################################################################
# data process thread # # data process thread #
@ -303,7 +310,7 @@ def data_process_thread(data_queue, ch, chamber):
do_temp = [] do_temp = []
for d in do: for d in do:
do_temp.append(int(d)) do_temp.append(int(d))
do = do_temp.copy() do = do_temp.copy()
#================================================== #==================================================
@ -322,7 +329,7 @@ def data_process_thread(data_queue, ch, chamber):
else: else:
voltage.append(int(i)) voltage.append(int(i))
#전압값으로 error count check #전압값으로 error count check
voltage_cnt = 0 voltage_cnt = 0
for i in voltage: for i in voltage:
@ -362,7 +369,7 @@ def data_process_thread(data_queue, ch, chamber):
else: else:
data_list_for_send_2.append(data) data_list_for_send_2.append(data)
if queue_count == 10: if queue_count == 10:
queue_count = 0 queue_count = 0
mqtt_msg = {} mqtt_msg = {}
@ -372,8 +379,8 @@ def data_process_thread(data_queue, ch, chamber):
"assetCode":"NQ-R04-TEST-003", #나중에는 config에서 셋팅되는 값, or model 값으로 변경 (asset) "assetCode":"NQ-R04-TEST-003", #나중에는 config에서 셋팅되는 값, or model 값으로 변경 (asset)
"dataType":"DATA", "dataType":"DATA",
"data": { "data": {
"channel": ch, # 채널 입력 "channel": ch, # 채널 입력 <EFBFBD>
"chamber":chamber, #chamber 도 입력 "chamber":chamber, #chamber 도 입력 <EFBFBD>
"slot": ch, #슬롯 값은 chamber와 ch 값으로 만든다 "slot": ch, #슬롯 값은 chamber와 ch 값으로 만든다
"list": data_list_for_send_1 "list": data_list_for_send_1
} }
@ -386,8 +393,8 @@ def data_process_thread(data_queue, ch, chamber):
"assetCode":"NQ-R04-TEST-003", #나중에는 config에서 셋팅되는 값, or model 값으로 변경 (asset) "assetCode":"NQ-R04-TEST-003", #나중에는 config에서 셋팅되는 값, or model 값으로 변경 (asset)
"dataType":"DATA", "dataType":"DATA",
"data": { "data": {
"channel": ch, # 채널 입력 "channel": ch, # 채널 입력 <EFBFBD>
"chamber":chamber, #chamber 도 입력 "chamber":chamber, #chamber 도 입력 <EFBFBD>
"slot": ch, #슬롯 값은 chamber와 ch 값으로 만든다 "slot": ch, #슬롯 값은 chamber와 ch 값으로 만든다
"list": data_list_for_send_2 "list": data_list_for_send_2
} }
@ -395,7 +402,7 @@ def data_process_thread(data_queue, ch, chamber):
queue_select_count = 0 queue_select_count = 0
data_list_for_send_1.clear() data_list_for_send_1.clear()
# print(type(mqtt_msg['data']['list'][0]['do'][0])) # print(type(mqtt_msg['data']['list'][0]['do'][0]))
data_json_str = json.dumps(mqtt_msg, indent=4) data_json_str = json.dumps(mqtt_msg, indent=4)
# print(data_json_str) # print(data_json_str)
@ -475,7 +482,7 @@ if __name__ == '__main__':
chip0_evt = threading.Event() chip0_evt = threading.Event()
chip1_evt = threading.Event() chip1_evt = threading.Event()
read_data_chip0 = threading.Thread(target=cwt_thread,args=(chip0_evt, 0, 1,2, 5,6, ch1_queue, ch2_queue)) #현재 4개씩 연결되어 있어서 테스트 용 read_data_chip0 = threading.Thread(target=cwt_thread,args=(chip0_evt, 0, 1,2, 5,6, ch1_queue, ch2_queue)) #현재 4 개씩 연결되 어 있어서 테스트 용
read_data_chip1 = threading.Thread(target=cwt_thread,args=(chip1_evt, 1, 9,10, 13,14, ch3_queue, ch4_queue)) read_data_chip1 = threading.Thread(target=cwt_thread,args=(chip1_evt, 1, 9,10, 13,14, ch3_queue, ch4_queue))
data_process_ch1 = threading.Thread(target=data_process_thread,args=(ch1_queue, 1, 1)) data_process_ch1 = threading.Thread(target=data_process_thread,args=(ch1_queue, 1, 1))