관리 메뉴

팜테크(FAMTECH)

[nCode] 파이썬과 연동해서 진동, 내구, 소음, 피로 분석(Python) #2 본문

팜테크 제품 리뷰(Review)/HBM nCode

[nCode] 파이썬과 연동해서 진동, 내구, 소음, 피로 분석(Python) #2

FAMTECH 2022. 7. 1. 10:51

 

 

 

 

"관련제품 문의는 로고 클릭 또는 공지사항의 연락처를 통해 하실 수 있습니다."

 

 

 

 

앞시간에 간단하게 nCode에서 파이썬의 기본 예제 "Hello World" 출력에 대해 알아보았습니다(https://famtech.tistory.com/113). 이번 실습에서는 실제 측정한 가속도(Acceleration) 값을 파이썬을 사용해서 -1배 변환 출력하는 방법에 대해 알아보겠습니다.

 

상세 코드 분석은 팜테크 오프라인 교육으로 진행하니 관심 있으시면 참가신청 해주시면 됩니다.

 

 

 

nCode 가속도 값  파이썬 변환 목적 및 전체 구조

 

이번 프로젝트의 목적은 아래의 방법들에 대한 습득에 있습니다. 

 

  • nCode의 입출력, 메타(meta) 정보, 특성(attribute) 정보 파이썬(Python) Script에 연결하는 방법
  • 파이썬(Python) 데이터 변경 방법

 

아래의 예제는 하나의 채널만 파이썬으로 변경하시면 현실에서 많은 채널에 몇가지 요소만 변경해서 사용하고 싶을 때 nCode 모듈로 설정하려면 많은 모듈을 사용해서 작업해야 하지만 파이썬의 경우 Script 모듈 하나로 처리가 가능 한 것이 Script의 장점입니다. 

 

 

전체 구조는 위와 같습니다.

 

  • 왼편의 Time Series 가속도 측정값을 Script 모듈과 연결
  • Script에서 파이썬 코드를 통해 파형을 변환
  • 변환된 그래프와 메타/특성 정보 테이블 출력

 

파이썬 코드 작성을 위한 Script 모듈은 GlyphBuilder 카테고리의 Scripting 모듈에서 사용 가능 합니다.

 

 

 

 

 

 

nCode 가속도 값 변환 파이썬 코드 분석

 

전체 코드>>

def glyphscript(es):
	# Get input time series object from first TSIn pad
	tsin = es.GetInputTimeSeries(0)

	# Set output time series object for first TSOut pad
	tsout = es.GetOutputTimeSeries(0)

	# Set multiplication factor
	factor = -1

	# Now multiply the input channels by the factor
	doMultiply(tsin, tsout, factor)

	# All done - return to GlyphWorks without any error message
	return ''

def doMultiply(tsin, tsout, factor):
	# Find the number of input channels
	numChans = tsin.GetChannelCount()	

	# Initialize the output channels
	tsout.SetChannelCount(numChans)

	# Copy the test attributes & meta data over
	# -1 arguments on chanindex indicate test attributes and metadata instead of channel-specific
	tsout.CopyAttributes(tsin, -1, -1)
	tsout.CopyMetaData(tsin, -1, -1)

	# Access the metadata object of the output time series so we can add our own metadata later
	meta = tsout.GetMetaData()

	# Loop all channels
	iChan = 0
	while iChan < numChans:
		# Copy channel attributes and metadata from input channel to output channel
		tsout.CopyAttributes(tsin, iChan, iChan)
		tsout.CopyMetaData(tsin, iChan, iChan)

		# Find number of datapoints in this channel
		numPoints = tsin.GetPointCount(iChan)

		# Loop through all data points in this channel
		iPoint = 0
		while iPoint < numPoints:
			# Get a single time point's value for this input channel
			oldValue = tsin.GetValue(iChan, iPoint)

			# Perform multiplication
			newValue = oldValue * factor

			# Write new value to output channel
			tsout.PutValue(iChan, iPoint, newValue)

			# move on to point
			iPoint = iPoint + 1
		
		# Now add some meta data
		meta.SetItem(iChan, 'ScriptProperties', 'Function', 'string', 'Multiply by a constant')
		meta.SetItem(iChan, 'ScriptProperties', 'MultiplicationFactor', 'double', factor)

		# move on to next channel
		iChan = iChan + 1

 

코드 구성은 기본 파이썬과 다릅니다. nCode에서 제공하는 파이썬 Script는 외부에서 함수에 대한 정의와 실행을 자동으로 구현 하므로 함수(Function)에 대한 정의만으로도 구현이 가능 합니다.

 

위 코드는 크게 glyphscript라는 함수와 doMultiply라는 두개의 함수로 구성됩니다. 

 

  • glyphscript 함수: 입출력 값 정보 처리
  • doMultiply 함수: 입력 정보를 받아 배수 처리

 

입출력에 필요한 nCode 파이썬 내장함수(Method)들에 대한 입력값 및 정의는 아래 그림과 같이 nCode 소프트웨어 매뉴얼에서 확인 가능 합니다.

 

 

상세 코드 분석 및 사용법은 팜테크 오프라인 교육에서 진행합니다.

 

 

 

 

 

Comments