Skip to content

Commit 654f998

Browse files
committed
Initial commit
0 parents  commit 654f998

File tree

7 files changed

+2425
-0
lines changed

7 files changed

+2425
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

parking_tank.ttt

4.16 MB
Binary file not shown.

remoteApi.dll

69 KB
Binary file not shown.

start.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import vrep
2+
import math
3+
import sys
4+
import time
5+
import numpy as np
6+
from tank import *
7+
import matplotlib.pyplot as plt
8+
9+
vrep.simxFinish(-1)
10+
clientID = vrep.simxStart('127.0.0.1', 19999, True, True, 5000, 5)
11+
12+
if clientID != -1:
13+
print("Connected to remote API server")
14+
else:
15+
print("Not connected to remote API server")
16+
sys.exit("Could not connect")
17+
18+
tank = Tank(clientID)
19+
20+
err_code, ps_handle = vrep.simxGetObjectHandle(clientID, "Proximity_sensor", vrep.simx_opmode_blocking)
21+
22+
t = time.time()
23+
24+
vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot)
25+
26+
while (time.time()-t) < 25:
27+
err_code, detectionState, detectedPoint, detectedObjectHandle, detectedSurfaceNormalVector = vrep.simxReadProximitySensor(clientID, ps_handle, vrep.simx_opmode_streaming)
28+
distance = np.linalg.norm(detectedPoint)
29+
if distance == 0:
30+
continue
31+
velocity = tank.readVelocity()
32+
33+
vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot)

tank.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import vrep
2+
3+
class Tank:
4+
def __init__(self, ID):
5+
self.clientID = ID
6+
# get handles to robot drivers
7+
err_code, self.left_front_handle = vrep.simxGetObjectHandle(self.clientID,'left_front', vrep.simx_opmode_blocking)
8+
err_code, self.left_back_handle = vrep.simxGetObjectHandle(self.clientID,'left_back', vrep.simx_opmode_blocking)
9+
err_code, self.right_back_handle = vrep.simxGetObjectHandle(self.clientID,'right_back', vrep.simx_opmode_blocking)
10+
err_code, self.right_front_handle= vrep.simxGetObjectHandle(self.clientID,'right_front', vrep.simx_opmode_blocking)
11+
12+
self.side_handles=[]
13+
for l in 'rl':
14+
for i in range(1,7):
15+
err_code, handle= vrep.simxGetObjectHandle(self.clientID,'sj_'+l+'_'+str(i) , vrep.simx_opmode_blocking)
16+
self.side_handles.append(handle)
17+
18+
#initial velocity
19+
self.leftvelocity=0
20+
self.rightvelocity=0
21+
self.MaxVel=10
22+
self.dVel=1
23+
24+
def stop(self):
25+
#set divers to stop mode
26+
force =0
27+
err_code = vrep.simxSetJointForce(self.clientID, self.left_front_handle, force, vrep.simx_opmode_oneshot)
28+
err_code = vrep.simxSetJointForce(self.clientID, self.left_back_handle, force, vrep.simx_opmode_oneshot)
29+
err_code = vrep.simxSetJointForce(self.clientID, self.right_back_handle, force, vrep.simx_opmode_oneshot)
30+
err_code = vrep.simxSetJointForce(self.clientID, self.right_front_handle, force, vrep.simx_opmode_oneshot)
31+
32+
force =10
33+
for h in self.side_handles:
34+
err_code = vrep.simxSetJointForce(self.clientID, h, force, vrep.simx_opmode_oneshot)
35+
36+
#break
37+
self.leftvelocity=10
38+
self.rightvelocity=10
39+
vrep.simxSetJointTargetVelocity(self.clientID,self.left_front_handle,self.leftvelocity,vrep.simx_opmode_streaming)
40+
vrep.simxSetJointTargetVelocity(self.clientID,self.left_back_handle,self.leftvelocity,vrep.simx_opmode_streaming)
41+
vrep.simxSetJointTargetVelocity(self.clientID,self.right_back_handle,self.rightvelocity,vrep.simx_opmode_streaming)
42+
vrep.simxSetJointTargetVelocity(self.clientID,self.right_front_handle,self.rightvelocity,vrep.simx_opmode_streaming)
43+
44+
def go(self):
45+
#set divers to go mode
46+
force =10
47+
err_code = vrep.simxSetJointForce(self.clientID, self.left_front_handle, force, vrep.simx_opmode_oneshot)
48+
err_code = vrep.simxSetJointForce(self.clientID, self.left_back_handle, force, vrep.simx_opmode_oneshot)
49+
err_code = vrep.simxSetJointForce(self.clientID, self.right_back_handle, force, vrep.simx_opmode_oneshot)
50+
err_code = vrep.simxSetJointForce(self.clientID, self.right_front_handle, force, vrep.simx_opmode_oneshot)
51+
52+
force =0
53+
for h in self.side_handles:
54+
err_code = vrep.simxSetJointForce(self.clientID, h, force, vrep.simx_opmode_oneshot)
55+
56+
def setVelocity(self):
57+
#verify if the velocity is in correct range
58+
if self.leftvelocity > self.MaxVel:
59+
self.leftvelocity = self.MaxVel
60+
if self.leftvelocity < -self.MaxVel:
61+
self.leftvelocity = -self.MaxVel
62+
if self.rightvelocity > self.MaxVel:
63+
self.rightvelocity = self.MaxVel
64+
if self.rightvelocity < -self.MaxVel:
65+
self.rightvelocity = -self.MaxVel
66+
67+
#send value of velocity to drivers
68+
#vrep.simxSetJointTargetVelocity(clientID,left_front_handle,leftvelocity,vrep.simx_opmode_streaming)
69+
vrep.simxSetJointTargetVelocity(self.clientID,self.left_back_handle,self.leftvelocity,vrep.simx_opmode_streaming)
70+
vrep.simxSetJointTargetVelocity(self.clientID,self.right_back_handle,self.rightvelocity,vrep.simx_opmode_streaming)
71+
#vrep.simxSetJointTargetVelocity(clientID,right_front_handle,rightvelocity,vrep.simx_opmode_streaming)
72+
73+
#Move the tank forward
74+
#None - increases velocity by 1, if velocities of wheels are different they are equalized
75+
#velocity - takes values from <-10,10> and sets them as velocity for both wheels in forward direction
76+
def forward(self, velocity=None):
77+
self.go()
78+
if velocity!=None:
79+
self.leftvelocity=velocity
80+
self.rightvelocity=velocity
81+
else:
82+
self.rightvelocity=self.leftvelocity=(self.leftvelocity+self.rightvelocity)/2
83+
self.leftvelocity+=self.dVel
84+
self.rightvelocity+=self.dVel
85+
self.setVelocity()
86+
87+
#Move the tank backward
88+
#None - decreases velocity by 1, if velocities of wheels are different they are equalized
89+
#velocity - takes values from <-10,10> and sets them as velocity for both wheels in backward direction
90+
def backward(self, velocity=None):
91+
self.go()
92+
if velocity!=None:
93+
self.leftvelocity=-velocity
94+
self.rightvelocity=-velocity
95+
else:
96+
self.rightvelocity=self.leftvelocity=(self.leftvelocity+self.rightvelocity)/2
97+
self.leftvelocity-=self.dVel
98+
self.rightvelocity-=self.dVel
99+
self.setVelocity()
100+
101+
#Turns left the tank
102+
#None - increases velocity of rightwheel by 1, decreases velocity of leftwheel by 1
103+
#velocity - takes values from <-10,10> and sets it as velocity for right wheel
104+
#in forward direction and oposite value of velocity for left wheel in backward direction
105+
def turn_left(self, velocity=None):
106+
self.go()
107+
if velocity!=None:
108+
self.leftvelocity =-velocity
109+
self.rightvelocity= velocity
110+
else:
111+
self.leftvelocity -=self.dVel
112+
self.rightvelocity+=self.dVel
113+
self.setVelocity()
114+
115+
#Turns right the tank
116+
#None - increases velocity of leftwheel by 1, decreases velocity of rightwheel by 1
117+
#velocity - takes values from <-10,10> and sets it as velocity for left wheel
118+
#in forward direction and oposite value of velocity for right wheel in backward direction
119+
def turn_right(self, velocity=None):
120+
self.go()
121+
if velocity!=None:
122+
self.leftvelocity = velocity
123+
self.rightvelocity=-velocity
124+
else:
125+
self.leftvelocity +=self.dVel
126+
self.rightvelocity-=self.dVel
127+
self.setVelocity()
128+
def readVelocity(self):
129+
velocity = (self.rightvelocity+self.leftvelocity)/2
130+
return velocity

0 commit comments

Comments
 (0)