檔案下載:https://github.com/mdekmol/cd2019a-task1-2019cda_t1_g6/tree/gh-pages/vrep%E6%AA%94%E6%A1%88/handfootball(pc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| import vrepimport sys, mathimport keyboard# child threaded script: # 內建使用 port 19997 若要加入其他 port, 在 serve 端程式納入#simExtRemoteApiStart(19999) vrep.simxFinish(-1) clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)KickBallV = 360 n=1U_KickBallVel = (math.pi/180)*KickBallVD_KickBallVel = -(math.pi/180)*KickBallVif clientID!= -1: print("Connected to remote server")else: print('Connection not successful') sys.exit('Could not connect')errorCode,Sphere_handle=vrep.simxGetObjectHandle(clientID,'Sphere',vrep.simx_opmode_oneshot_wait)errorCode,lmov_handle=vrep.simxGetObjectHandle(clientID,'lmov', vrep.simx_opmode_oneshot_wait)errorCode,rmov_handle=vrep.simxGetObjectHandle(clientID,'rmov',vrep.simx_opmode_oneshot_wait)errorCode,lrev_handle=vrep.simxGetObjectHandle(clientID,'lrev',vrep.simx_opmode_oneshot_wait)errorCode,rrev_handle=vrep.simxGetObjectHandle(clientID,'rrev',vrep.simx_opmode_oneshot_wait)errorCode,R_handle=vrep.simxGetObjectHandle(clientID,'R',vrep.simx_opmode_oneshot_wait)errorCode,L_handle=vrep.simxGetObjectHandle(clientID,'L',vrep.simx_opmode_oneshot_wait)vrep.simxSetJointTargetVelocity(clientID,lmov_handle,0,vrep.simx_opmode_oneshot_wait)vrep.simxSetJointTargetVelocity(clientID,rmov_handle,0,vrep.simx_opmode_oneshot_wait)vrep.simxSetJointTargetVelocity(clientID,lrev_handle,0,vrep.simx_opmode_oneshot_wait)vrep.simxSetJointTargetVelocity(clientID,rrev_handle,0,vrep.simx_opmode_oneshot_wait)def speed(handle,speed): errorCode = vrep.simxSetJointTargetVelocity(clientID,handle,speed,vrep.simx_opmode_oneshot_wait)vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot_wait)#vrep.simxSetJointTargetVelocity(clientID,P1_handle,5,vrep.simx_opmode_oneshot_wait)while True: errorCode,position_LB=vrep.simxGetObjectPosition(clientID,lrev_handle,-1,vrep.simx_opmode_oneshot) errorCode,position_S=vrep.simxGetObjectPosition(clientID,Sphere_handle,-1,vrep.simx_opmode_oneshot) BB =position_S[1] - position_LB[1] #左右 BBB =position_S[0] - position_LB[0]#前後 #print(position_S) if BBB <0.07: speed(lrev_handle, D_KickBallVel) elif BBB >= 0.07: speed(lrev_handle, U_KickBallVel) else: pass try: if keyboard.is_pressed('5'): vrep.simxSetJointTargetVelocity(clientID,rrev_handle,U_KickBallVel,vrep.simx_opmode_oneshot_wait) elif keyboard.is_pressed('0'): vrep.simxSetJointTargetVelocity(clientID,rrev_handle,D_KickBallVel,vrep.simx_opmode_oneshot_wait) elif keyboard.is_pressed('3'): vrep.simxSetJointTargetVelocity(clientID,rmov_handle,-1,vrep.simx_opmode_oneshot_wait) elif keyboard.is_pressed('2'): vrep.simxSetJointTargetVelocity(clientID,rmov_handle,0,vrep.simx_opmode_oneshot_wait) elif keyboard.is_pressed('1'): vrep.simxSetJointTargetVelocity(clientID,rmov_handle,1,vrep.simx_opmode_oneshot_wait) else: pass except: break Mv = BB*2.3 vrep.simxSetJointTargetVelocity(clientID,lmov_handle,Mv,vrep.simx_opmode_oneshot_wait) |
沒有留言:
張貼留言