def __init__(self, max_actions=600): self.max_actions = max_actions self.portNumb = recoverPorts()[0] self.actions = [] self.score = 0 self.hasfallen = False # self.LUMSpeed = 0 # self.LLMSpeed = 0 # self.RUMSpeed = 0 # self.RLMSpeed = 0 self.clientID = vrep.simxStart('127.0.0.1', self.portNumb, True, True, 5000, 5) if self.clientID != -1 : print ("Connection to the simulator has been established") self.robotController = robotController(self.clientID) # LUM,LLM,RUM,RLM,head = recoverRobotParts(self.clientID) # self.LUM = LUM # self.LLM = LLM # self.RUM = RUM # self.RLM = RLM # self.head = head #Set simulation to be Synchonous instead of Asynchronous vrep.simxSynchronous(self.clientID, True) #Start simulation if it didn't start vrep.simxStartSimulation(self.clientID,vrep.simx_opmode_blocking) self.robotController.resetRobot() # setVelocity(self.clientID,self.LUM,self.LUMSpeed) # setVelocity(self.clientID,self.LLM,self.LLMSpeed) # setVelocity(self.clientID,self.RUM,self.RUMSpeed) # setVelocity(self.clientID,self.RLM,self.RLMSpeed) #Esto es necesario porque la primera observación siempre es incorrecta self.observation_space() vrep.simxSynchronousTrigger(self.clientID)
def runModel(portNumber, secuenceList): #Establish connection clientID = vrep.simxStart('127.0.0.1', portNumber, True, True, 5000, 5) #Verify connection if clientID == -1 : print("Connection to the simluator api could not be established") print("Make sure the simulator is up and running on port {}".format(portNumber)) else: evolutionModel = EvolutionModel() robotcontroller = robotController(clientID) #Set simulation to be Synchronous instead of Asynchronous vrep.simxSynchronous(clientID, True) #Setting Time Step to 50 ms (miliseconds) dt = 0.05 #Start simulation vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot) runInfo = [] for secuence in secuenceList: runtime = 0 observationTrace = [] for instruction in secuence: robotcontroller.moveRobot(instruction[0]) #This is what makes the simulation Synchronous initialTime = 0.0 actualTime = initialTime + dt runtime += dt #Condition to stop simulation done = False vrep.simxSynchronousTrigger(clientID) n = 1 while (n <= instruction[1] and not done): n+=1 #Make de simulation run one step (dt determines how many seconds pass by between step and step) vrep.simxSynchronousTrigger(clientID) #Advance time in my internal counter actualTime = actualTime + dt runtime += dt observations = robotcontroller.observablePositions() observationTrace.append({'Observation': observations, 'runtime' : runtime}) done = evolutionModel.isDone(observations) runInfo.append({'instructions' : secuence, 'observations' : observationTrace, 'Score' : evolutionModel.getScore(observationTrace)}) robotcontroller.resetRobot() #Stop_Start_Simulation vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking) #This sleep is necesary for the simulation to finish stopping before starting again time.sleep(2) robotcontroller.resetRobot() vrep.simxStartSimulation(clientID, vrep.simx_opmode_blocking) robotcontroller.resetRobot() #Should always end by finishing connetions vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking) vrep.simxFinish(clientID) return runInfo
def main(): #Maing sure no connection is active vrep.simxFinish(-1) #Default Port Numeber #TODO cambiar esto para que se lea de un archivo de configuración portNumb = 19997 #Establish connection clientID = vrep.simxStart('127.0.0.1', portNumb, True, True, 5000, 5) #Verify connection if clientID == -1 : print("Connection to the simluator api could not be established") print("Make sure the simulator is up and running on port 19997") else: #Start simulation #TODO verificar si es más conveniente iniciar la simulación antes o después de recuperar la configuración del robot vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot) robotcontroller = robotController(clientID) print("Write 0 to stop simulation or a number between 1 and {} to move the robot: ".format(len(robotcontroller.movements))) print([(i+1,v['name']) for i,v in enumerate(robotcontroller.movements)]) stillRun = True snoop = Snoop() savedSequences = [] while stillRun: nextAction = input("Write a movement option: ") try: nextAction = int(nextAction) except: nextAction = -1 while nextAction: if robotcontroller.isValid(nextAction): snoop.next_action(nextAction) robotcontroller.moveRobot(nextAction) nextAction = input("Write other movement option or 0 ") nextAction = int(nextAction) secuence = snoop.end() # print(secuence) saveRun = input("Do you want to save this run? y/n ") if saveRun == "y": savedSequences.append(secuence) continueRuning = input("Want to continue traying? y/n ") if continueRuning == "n": stillRun = False else: #Restart Simulation vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking) time.sleep(2) vrep.simxStartSimulation(clientID, vrep.simx_opmode_blocking) #Finish simulation and connections vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking) vrep.simxFinish(clientID) #Save Secuences into file sr.recordSecuences(savedSequences, "savedSequences.txt")