def getState(self, uid): # Получаем позицию робота send = [-1] * 2 # initialize an array with -1 send[IndexSignalSend.type] = SignalType.getState send[IndexSignalSend.uid] = uid logging.debug("getState() robot_uid = %d" % uid) # get a reply of the form [uid, ambient_light] | distance_keys | distance_values recv = self.sendSignal(send) #logging.debug("Received %s" % recv) recv = recv.split(b'|') for i in range(len(recv)): # unpack ints in place recv[i] = vrep.simxUnpackInts(recv[i]) logging.debug("recv[%d] = %s" % (i, recv[i])) if (recv[0][IndexSignalReceive.uid] != uid): logging.critical("received the state from the wrong robot (req.uid = %d, response.uid = %d)" % (uid, recv[0][IndexSignalReceive.uid])) exit(1) # construct the distances dictionary (robot_uid: current_distance) distances = {recv[1][i]: recv[2][i] for i in range(len(recv[1]))} # remove distance from myself, as it is always 0 and is not needed del distances[uid] return { 'uid' : recv[0][IndexSignalReceive.uid], 'light' : recv[0][IndexSignalReceive.ambient_light], 'distances' : distances}
def setState(self, uid, motion, light): # Задаем текущее положение робота send = [-1] * 6 send[IndexSignalSend.type] = SignalType.setState send[IndexSignalSend.uid] = uid send[IndexSignalSend.motion] = motion send[IndexSignalSend.led_r] = light[0] send[IndexSignalSend.led_g] = light[1] send[IndexSignalSend.led_b] = light[2] recv = vrep.simxUnpackInts(self.sendSignal(send))
def getState(self, uid): """Return the current state of the kilobot, under the form of a structured dictionary :uid: the target kilobot's unique id :returns: structured dictionary that represents the state of the robot { uid : the target kilobot's unique id light : (val_now, val_previous) distances : {robot_uid: current_distance} } """ send = [-1] * 2 # initialize an array with -1 send[IndexSignalSend.type] = SignalType.getState send[IndexSignalSend.uid] = uid logging.debug("getState() robot_uid = %d" % uid) # get a reply of the form [uid, ambient_light] | distance_keys | distance_values recv = self.sendSignal(send) # logging.debug("Received %s" % recv) recv = recv.split(b"|") for i in range(len(recv)): # unpack ints in place recv[i] = vrep.simxUnpackInts(recv[i]) logging.debug("recv[%d] = %s" % (i, recv[i])) if recv[0][IndexSignalReceive.uid] != uid: logging.critical( "received the state from the wrong robot (req.uid = %d, response.uid = %d)" % (uid, recv[0][IndexSignalReceive.uid]) ) exit(1) # construct the distances dictionary (robot_uid: current_distance) distances = {recv[1][i]: recv[2][i] for i in range(len(recv[1]))} # remove distance from myself, as it is always 0 and is not needed del distances[uid] return { "uid": recv[0][IndexSignalReceive.uid], "light": recv[0][IndexSignalReceive.ambient_light], "distances": distances, }
def setState(self, uid, motion, light): """Set a current state for the end-effectors of the Kilobot (Motors and RGB-led) :uid: the target kilobot's unique id :motion: one of Motion(enum) values :light: [r, g, b] list with values from [0-3] interval :returns: TODO """ send = [-1] * 6 send[IndexSignalSend.type] = SignalType.setState send[IndexSignalSend.uid] = uid send[IndexSignalSend.motion] = motion send[IndexSignalSend.led_r] = light[0] send[IndexSignalSend.led_g] = light[1] send[IndexSignalSend.led_b] = light[2] # recv = self.sendSignal(send) recv = vrep.simxUnpackInts(self.sendSignal(send)) logging.debug("Received %s" % recv)