TouchDesigner/python
모터 제어 timer serial
잉_민
2022. 10. 10. 21:07
728x90
반응형
# me - this DAT.
# timerOp - the connected Timer CHOP
# cycle - the cycle index
# segment - the segment index
# fraction - the time in fractional form
#
# interrupt - True if the user initiated a premature
# interrupt, False if a result of a normal timeout.
#
# onInitialize(): if return value > 0, it will be
# called again after the returned number of frames.
def onInitialize(timerOp):
return 0
def onReady(timerOp):
return
def onStart(timerOp):
return
def onTimerPulse(timerOp, segment):
return
def whileTimerActive(timerOp, segment, cycle, fraction):
return
def onSegmentEnter(timerOp, segment, interrupt):
return
def onSegmentExit(timerOp, segment, interrupt):
return
def onCycleStart(timerOp, segment, cycle):
return
def onCycleEndAlert(timerOp, segment, cycle, alertSegment, alertDone, interrupt):
return
def makeNum(i) :
#2자리 수 0채우기
number= str(i+1).zfill(2)
#print(number)
#한자리로 만들기
num_list = list(map(int, str(number)))
num_str=[str(x) for x in num_list]
res = toBin(num_str[0])+','+toBin(num_str[1])
return res
def makeRGB(value) :
value=int(value)
#2자리 수 0채우기
number= str(value).zfill(3)
#print(number)
#한자리로 만들기
num_list = list(map(int, str(number)))
#res=[str(x) for x in num_list]
return num_list[0],num_list[1],num_list[2]
def toBin(dat) :
#아스키 utf-8 이법 바이너리
#print('#motor#',dat)
return bin(ord(str(dat)))
def sendResult(i,MotorDAT,rgb) :
num=makeNum(i)
motor = toBin(MotorDAT)
#rgb value 한자리 씩bin으로 바꾸기
rgb_res=''
for i in range(9) :
x= str(rgb[i])
rgb_res = rgb_res + toBin(x)+','
#print('#',rgb_res)
result = '00000010'+','+ num +','+motor+','+rgb_res+'00000011'
print(result)
return result
def onCycle(timerOp, segment, cycle):
n = op('chopto2') #motor
r = op('chopto3') #RGB
#print('start')
num_col= r.numCols
#print(num_col)
motor=[]
rgb=[]
RGBres=[]
num=0
#motor값 불러오기
for i in range(90):
m=n[0,i]
motor.append(m)
for i in range(num_col):
#rgb
rg=r[0,i]
rgb.append(rg)
for i in range(num_col):
if i%3==0 and i<269:
num=int(i/3)
#print(num)
RGBres= makeRGB(rgb[i])+makeRGB(rgb[i+1])+makeRGB(rgb[i+2])
result=sendResult(num,motor[num],RGBres)
print(result)
op('serial1').send(result)
return
def onDone(timerOp, segment, interrupt):
return
chop 에서 timer를 만들면

이렇게 타이머 callback DAT가 생긴다
별표눌러서 위의 코드를 적어준다.
여기서 chopto2 는 모터 90개고
chopto3는 각 모터에 보낼 rgb값이라 3개씩 90*3 = 270개다 ..

원래는 초록색으로 받은 값을 타이머를 사용해chopto해줘서 과부하 없게 하지만 나는 임시 테이블을 활용했다.
serial을 활용해서 포트넘버로 보내는 형식이다.
리스트를 스트링으로 혹은 int로
각변수의 자료형을 설정해주는게 어려웠다..

원하는 바이너리형식으로 잘 변환되어 출력된다.
728x90
반응형