-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvrep_env.py
133 lines (94 loc) · 4.68 KB
/
vrep_env.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import vrep
import numpy as np
import random
import time
class Environment:
def __init__(self):
# Launch the simulation with the given launchfile name
self.action_space = [i for i in range(3)] #F,L,R
self.reward_range = (-np.inf, np.inf)
self.sensors = 3
self.sensor_h=[0,0,0]
vrep.simxFinish(-1) # just in case, close all opened connections
self.clientID=vrep.simxStart('127.0.0.1',20000,True,True,5000,5)
if self.clientID!=-1: #check if client connection successful
print ('Connected to remote API server, clientID',self.clientID)
else:
print( 'Connection not successful')
sys.exit('Could not connect')
def set_vel(self,vl,vr):
errorCode=vrep.simxSetJointTargetVelocity(self.clientID,self.left_motor_handle,vl, vrep.simx_opmode_streaming)
errorCode=vrep.simxSetJointTargetVelocity(self.clientID,self.right_motor_handle,vr, vrep.simx_opmode_streaming)
def get_sensor_data(self):
sensor_val=np.array([])
for x in range(1,self.sensors + 1):
errorCode,detectionState,detectedPoint,detectedObjectHandle,detectedSurfaceNormalVector=vrep.simxReadProximitySensor(self.clientID,self.sensor_h[x-1],vrep.simx_opmode_buffer)
if detectionState == True :
sensor_val=np.append(sensor_val,np.linalg.norm(detectedPoint)) #get list of values
else:
sensor_val=np.append(sensor_val,np.inf)
# print("SENSOR data " ,errorCode,detectionState,detectedPoint,detectedObjectHandle,detectedSurfaceNormalVector)
return sensor_val
def get_observation(self):
sensorDistance = self.get_sensor_data()
discretized_ranges = np.empty((0,3))
min_range = 0.11
done = False
# convert sensor values to descrete values for state encoding
for i, item in enumerate(sensorDistance):
# if item < 0.25:
# discretized_ranges= np.append(discretized_ranges, 0)
# elif(item < 0.5):
# discretized_ranges= np.append(discretized_ranges, 1)
# elif(item < 0.75):
# discretized_ranges= np.append(discretized_ranges, 2)
if item < 0.35:
discretized_ranges= np.append(discretized_ranges, round(item,2))
else:
discretized_ranges= np.append(discretized_ranges, 0.35)
if (min_range > item > 0):
# print('Done = True , Sensor = ',i, 'val = ',item)
done = True
print("state = ",discretized_ranges)
return discretized_ranges,done
def init_sensors(self):
# _,robot = vrep.simxGetObjectHandle(self.clientID,'Pioneer_p3dx',vrep.simx_opmode_oneshot_wait)
# ret_val = vrep.simxSetObjectOrientation(self.clientID,robot ,robot, [0,0,random.choice([-0.05,0,0.05])],vrep.simx_opmode_oneshot)
errorCode,self.left_motor_handle = vrep.simxGetObjectHandle(self.clientID,'Pioneer_p3dx_leftMotor',vrep.simx_opmode_oneshot_wait)
errorCode,self.right_motor_handle = vrep.simxGetObjectHandle(self.clientID,'Pioneer_p3dx_rightMotor',vrep.simx_opmode_oneshot_wait)
# print(errorCode, " ++++++++++++++++++++++", self.left_motor_handle)
for x in range(1,self.sensors + 1):
errorCode,sensor_handle=vrep.simxGetObjectHandle(self.clientID,'Pioneer_p3dx_ultrasonicSensor'+str(x),vrep.simx_opmode_oneshot_wait)
self.sensor_h[x-1] = sensor_handle
sensor_val=np.array([])
for x in range(1,self.sensors + 1):
errorCode,detectionState,detectedPoint,detectedObjectHandle,detectedSurfaceNormalVector=vrep.simxReadProximitySensor(self.clientID,self.sensor_h[x-1],vrep.simx_opmode_streaming)
if detectionState == True :
sensor_val=np.append(sensor_val,np.linalg.norm(detectedPoint)) #get list of values
else:
sensor_val=np.append(sensor_val,np.inf)
def reset(self):
sensorDistance = self.init_sensors()
# vrep.simxStopSimulation(self.clientID,vrep.simx_opmode_blocking)
# ret_val = vrep.simxStartSimulation(self.clientID,vrep.simx_opmode_oneshot_wait)
message = 0
count = 0
while ((message &1) == 0):
count+=1
print("SIMULATION STOPPED")
result,message = vrep.simxGetInMessageInfo(self.clientID, vrep.simx_headeroffset_server_state )
ret_val = vrep.simxStartSimulation(self.clientID,vrep.simx_opmode_oneshot_wait)
print("message = ",message)
# if count != 3:
# print("reset")
# return count
print("SIMULATION STARTED ",count)
start_state,done = self.get_observation(sensorDistance)
return start_state
def stop(self):
error_code = vrep.simxStopSimulation(self.clientID,vrep.simx_opmode_blocking)
def load_scene(self):
# error_code = vrep.simxLoadScene(self.clientID,'MyScenes/square_demo'+str(random.choice([1,2]))+'.ttt',0xFF,vrep.simx_opmode_blocking)
error_code = vrep.simxLoadScene(self.clientID,'MyScenes/primitive_test.ttt',0xFF,vrep.simx_opmode_blocking)
def start(self):
error_code = vrep.simxStartSimulation(self.clientID,vrep.simx_opmode_oneshot_wait)