-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_manager.py
160 lines (131 loc) · 4.87 KB
/
simulation_manager.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import vrep
import time
import numpy as np
import scipy
import sys
import ctypes
import matplotlib
from random import *
from function_names import *
from quadcopter import *
import controller
SYNC = True
firstPass = True
runtime = 0.0
def getTime():
return runtime
def results(x,y):
print("Error Code:{} Value:{}".format(x,y))
Q_BASE = 'Quadricopter_base'
Rotor_1 = 'Quadricopter_propeller_respondable1'
ONE_SHOT = vrep.simx_opmode_oneshot_wait
vrep_mode = vrep.simx_opmode_oneshot
def sync(cid):
vrep.simxSynchronousTrigger(cid)
def getTargetStart(cid,target):
start_pos = [0]
while (max(start_pos) == 0):
controller.controller_motor(cid, copter, target)
err, start_pos = vrep.simxGetObjectPosition(cid, target, -1, mode())
return err, np.asarray(start_pos)
def mode():
global firstPass
return vrep.simx_opmode_buffer if firstPass else vrep.simx_opmode_streaming
def connect():
cid=vrep.simxStart('127.0.0.1',19997,True,True,5000,5) # Connect to V-REP
if cid != -1:
print ('Connected to V-REP remote API serv'
'\er, client id: %s' % cid)
vrep.simxStartSimulation( cid, vrep.simx_opmode_oneshot )
if SYNC:
vrep.simxSynchronous( cid, True )
else:
print ('Failed connecting to V-REP remote API server')
exit()
return cid
print ('Program started')
# copter = Quadcopter(4,False,None,True,None,cid)
# copter = AdaptiveController.__init__(copter,True,0.001,None)
#
# print(copter)
#Start Grabbing Handles
functions = {}
args = {}
real_fun(functions)
#1-15 are examples along a sphere, one at top of sphere, one in middle, one in bottom,
#and then three rings at centred .25d, .5d, .75d with radius = 2
k = functions.keys()
k.sort()
iter = 1
for key in k:
state_file = open('Trajectories/{}.txt'.format(key+"state"),'w+')
action_file = open('Trajectories/{}.txt'.format(key+"action"),'w+')
vrep.simxFinish(-1) # just in case, close all opened connections
time.sleep(1)
cid = connect()
time.sleep(1)
vrep.simxLoadScene(cid,'/home/elias/etragas@gmail.com/_Winter2015/CSC494/Scenes/Base_Quad.ttt',0,mode())
vrep.simxStartSimulation(cid,mode())
runtime = 0
err, copter = vrep.simxGetObjectHandle(cid, "Quadricopter_base",
vrep.simx_opmode_oneshot_wait)
err, target = vrep.simxGetObjectHandle(cid, "Quadricopter_target",
vrep.simx_opmode_oneshot_wait)
err, front_camera = vrep.simxGetObjectHandle(cid, 'Quadricopter_frontCamera', vrep.simx_opmode_oneshot)
err, lin, ang = vrep.simxGetObjectVelocity(cid, copter, vrep.simx_opmode_streaming)
err, orgpos = getTargetStart(cid,target)
real_args(args,orgpos )
args["addxToZ"] = [orgpos, 2.5]
args["fuckIt"] = [orgpos, 2.5]
#This prepared the data
print(err)
#while(1):
while(runtime <= 10):
error, pos = vrep.simxGetObjectPosition(cid,copter,-1,mode())
args["AsinToZ"] = [pos, getTime]
controller.target_move(cid,target,functions[key], args[key])
commands = controller.controller_motor(cid,copter,target)
vrep.simxSynchronousTrigger(cid)
state_file.write("{}\n".format(",".join([str(x) for x in controller.getDif(cid,copter,target)])))
action_file.write("{}\n".format(" ".join([str(x)[1:-2] for x in commands])))
runtime += .05
firstPass = False
vrep.simxStopSimulation(cid, mode())
vrep.simxSynchronousTrigger(cid)
firstPass = True
print("meow")
# packedData=vrep.simxPackFloats([0,0,0,0])
# err = vrep.simxSetStringSignal(cid, "rotorTargetVelocities",packedData,vrep_mode)
# sync(cid)
# err, res, image = vrep.simxGetVisionSensorImage(cid,front_camera,0,vrep.simx_opmode_streaming)
# results(err,res)
#
#
# err, res, image = vrep.simxGetVisionSensorImage(cid,front_camera,0,vrep.simx_opmode_oneshot_wait)
# print(err)
# print(image)
# print(res)
#
# err = vrep.simxSetStringSignal(clientID, "rotorTargetVelocities",
# raw_bytes,
# vrep_mode)
#
#
# errorCode, rotor1_joint = vrep.simxGetObjectHandle(clientID,'Quadricopter_propeller_joint1', ONE_SHOT)
# results(errorCode,rotor1_joint)
# rotor1_force = vrep.simxSetJointTargetVelocity(clientID,rotor1_joint,200,vrep.simx_opmode_streaming)
# results(errorCode,rotor1_force)
# time.sleep(2)
#
# #errorCode, rotor1_force = vrep.simxjoint(clientID,rotor1_joint,vrep.simx_opmode_buffer)
#
# results(errorCode,rotor1_force)
#
# #
# # vrep.simxset
# # vrep.simxSetJointTargetVelocity(clientID,rotor1,100000,vrep.simx_opmode_streaming)
# # vrep.simx
# #vrep.simxGetObjectVelocity(clientID,Q_BASE,ONE_SHOT)
# print(rotor1_force)
#
# print("")