自動吹笛子機-2 <<
Previous Next >> W13 Pick and Place remoteApi
3D列印機
使用python remoteApi進行遠端控制
控制:
上下左右控制噴頭的上下左右
空白鍵往上
C鍵往下
圖檔
3D print remoteApi program
3D print remoteApi.ttt
程式參考
5/18 程式修改:將顯示的數值由公尺轉成毫米並且去掉多餘的小數點
import sim as vrep
import math
import random
import time
import keyboard
print ('Start')
# Close eventual old connections
vrep.simxFinish(-1)
# Connect to V-REP remote server
clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)
if clientID != -1:
print ('Conipconfigected to remote API server')
res = vrep.simxAddStatusbarMessage(
clientID, "40823214",
vrep.simx_opmode_oneshot)
if res not in (vrep.simx_return_ok, vrep.simx_return_novalue_flag):
print("Could not add a message to the status bar.")
opmode = vrep.simx_opmode_oneshot_wait
STREAMING = vrep.simx_opmode_streaming
vrep.simxStartSimulation(clientID, opmode)
ret,joint1=vrep.simxGetObjectHandle(clientID,"X",opmode)
ret,joint2=vrep.simxGetObjectHandle(clientID,"Y",opmode)
ret,joint3=vrep.simxGetObjectHandle(clientID,"Z",opmode)
dx=0
dy=0
dz=0
dt=0.005
cont=0
rangeR=0.013
max_h=0.35
vrep.simxSetJointTargetPosition(clientID,joint1,dx,opmode)
vrep.simxSetJointTargetPosition(clientID,joint2,dx,opmode)
vrep.simxSetJointTargetPosition(clientID,joint3,dx,opmode)
while True:
#Clockwise
if keyboard.is_pressed("8"):
dx=dx+dt
cont=float(math.pow(dx,2)+math.pow(dy,2))
if cont<rangeR:
vrep.simxSetJointTargetPosition(clientID,joint1,dx,opmode)
print(round(dx*1000,1),round(dy*1000,1),round(dz*1000,1))
else:
print("Out of range")
dx=dx-dt
if keyboard.is_pressed("2"):
dx=dx-dt
cont=float(math.pow(dx,2)+math.pow(dy,2))
if cont<rangeR:
vrep.simxSetJointTargetPosition(clientID,joint1,dx,opmode)
print(round(dx*1000,1),round(dy*1000,1),round(dz*1000,1))
else:
print("Out of range")
dx=dx+dt
if keyboard.is_pressed("4"):
dy=dy+dt
cont=float(math.pow(dx,2)+math.pow(dy,2))
if cont<rangeR:
vrep.simxSetJointTargetPosition(clientID,joint2,dy,opmode)
print(round(dx*1000,1),round(dy*1000,1),round(dz*1000,1))
else:
print("Out of range")
dy=dy-dt
if keyboard.is_pressed("6"):
dy=dy-dt
cont=float(math.pow(dx,2)+math.pow(dy,2))
if cont<rangeR:
vrep.simxSetJointTargetPosition(clientID,joint2,dy,opmode)
print(round(dx*1000,1),round(dy*1000,1),round(dz*1000,1))
else:
print("Out of range")
dy=dy+dt
if keyboard.is_pressed("space"):
dz=dz+dt
if dz<max_h:
vrep.simxSetJointTargetPosition(clientID,joint3,dz,opmode)
print(round(dx*1000,1),round(dy*1000,1),round(dz*1000,1))
else:
print("too high")
dz=dz-dt
if keyboard.is_pressed("c"):
dz=dz-dt
if dz<0:
print("too low")
dz=0
else:
vrep.simxSetJointTargetPosition(clientID,joint3,dz,opmode)
print(round(dx*1000,1),round(dy*1000,1),round(dz*1000,1))
else:
print ('Failed connecting to remote API server')
print ('End')
自動吹笛子機-2 <<
Previous Next >> W13 Pick and Place remoteApi