Esempio n. 1
0
    def __init__(self, num_slaves, motion="walk"):
        np.random.seed(int(time.time()))
        self.num_slaves = num_slaves
        self.motion = motion

        RNNConfig.instance().loadData(motion)

        # initialize kinematic poses(root and character poses)
        self.rootPose = []
        self.rootPosePrev = []

        self.characterPose = []
        self.controlPrediction = []
        self.initialCharacterPose = np.zeros(RNNConfig.instance().yDimension,
                                             dtype=np.float32)
        self.initialControlPrediction = np.zeros(
            RNNConfig.instance().xDimension, dtype=np.float32)
        for _ in range(self.num_slaves):
            self.rootPose.append(Pose2d())
            self.rootPosePrev.append(Pose2d())
            self.characterPose.append(self.initialCharacterPose)
            self.controlPrediction.append(self.initialControlPrediction)

        self.model = None
        self.isModelLoaded = False

        # parameter for root height
        self.target_height = 88.

        # parameter for fall over and stand up
        self.fallOverState = [0] * self.num_slaves
        self.fallOverCount = [0] * self.num_slaves
        self.standUpCount = [40] * self.num_slaves

        self.isWalk = True

        # random target parameters
        self.target_dist_lower = 300.0
        self.target_dist_upper = 350.0
        self.target_angle_upper = math.pi * 0.9
        self.target_angle_lower = math.pi * (-0.9)

        # trajectory save
        self.log_trajectory = []
        self.log_target_trajectory = []

        # initialize targets
        self.targets = []
        for i in range(self.num_slaves):
            self.targets.append(self.randomTarget(i))
Esempio n. 2
0
    def resetProperties(self):
        # reset root and character poses
        self.characterPose = np.array(self.characterPose)
        for i in range(self.num_slaves):
            self.rootPose[i] = Pose2d()
            self.rootPosePrev[i] = Pose2d()
            self.characterPose[i] = self.initialCharacterPose
            self.controlPrediction[i] = self.initialControlPrediction

        self.fallOverState = [0] * self.num_slaves
        self.fallOverCount = [0] * self.num_slaves
        self.standUpCount = [40] * self.num_slaves

        # reset state
        if self.model is not None:
            self.model.resetState(self.num_slaves)
Esempio n. 3
0
    def getOriginalTrajectory(self, frame, origin_offset=0):
        x_dat = loadData("../motions/{}/data/0_xData.dat".format(self.motion))
        y_dat = loadData("../motions/{}/data/0_yData.dat".format(self.motion))

        x_dat = x_dat[1 + origin_offset:frame + 1 + origin_offset]
        y_dat = y_dat[1 + origin_offset:frame + 1 + origin_offset]

        x_dat = np.array([
            RNNConfig.instance().xNormal.denormalize_and_fill_zero(x)
            for x in x_dat
        ])
        y_dat = np.array([
            RNNConfig.instance().yNormal.denormalize_and_fill_zero(y)
            for y in y_dat
        ])

        self.resetProperties()

        trajectories = []
        targets = []

        for x, y in zip(x_dat, y_dat):
            localPose = Pose2d([x[0] * 100, x[1] * 100])
            targets.append(
                np.array(self.rootPose[0].localToGlobal(localPose).p) / 100)
            trajectories.append(self.getGlobalPositions(y, 0))

        trajectories = np.asarray(trajectories, dtype=np.float32)
        targets = np.asarray(targets, dtype=np.float32)
        return trajectories, targets
Esempio n. 4
0
 def get_target(self):
     p = self.viewer.pickPoint()
     
     target = Pose2d([p[0]/MOTION_SCALE, -p[2]/MOTION_SCALE])
     target = self.controller.pose.relativePose(target)
     target = target.p
     t_len = v_len(target)
     if (t_len > 80):
         ratio = 80/t_len
         target[0] *= ratio
         target[1] *= ratio
     return target
Esempio n. 5
0
    def __init__(self, folder):
        self.config = rnn.Configurations.get_config(folder)
        self.config.load_normal_data(folder)
        self.pose = Pose2d()

        self.model = self.config.model(1, 1)
        self.sess = tf.Session()
        saver = tf.train.Saver()
        self.sess.run(tf.global_variables_initializer())
        saver.restore(self.sess, "%s/train/ckpt" % (folder))
        #         saver.restore(self.sess, "%s/train/ckpt"%(folder))
        #         saver.save(self.sess, "%s/train2/ckpt"%(folder))

        self.state = None
        self.current_y = [[0] * self.config.y_normal.size()]
Esempio n. 6
0
    def setDynamicPose(self, dpose, index=0):
        new_rootPose = Pose2d().transform([dpose[3], dpose[0], dpose[2]])
        pose_delta = self.rootPosePrev[index].relativePose(new_rootPose)
        new_characterPose = deepcopy(
            RNNConfig.instance().yNormal.denormalize_and_fill_zero(
                self.characterPose[index]))

        new_characterPose[0:2] = dpose[52:54]  # foot contact
        new_characterPose[2] = pose_delta.rotatedAngle()  # root rotate
        new_characterPose[3:5] = pose_delta.p  # root translate
        new_characterPose[5] = dpose[1]  # root height
        new_characterPose[6:63] = dpose[54:111]  # positions
        new_characterPose[63:111] = dpose[4:52]  # orientations

        self.rootPose[index] = new_rootPose
        self.rootPosePrev[index] = new_rootPose
        self.characterPose[index] = RNNConfig.instance(
        ).yNormal.normalize_and_remove_zero(new_characterPose)
Esempio n. 7
0
    def randomTarget(self, index):
        target_dist = np.random.uniform(self.target_dist_lower,
                                        self.target_dist_upper)
        target_angle = np.random.uniform(self.target_angle_lower,
                                         self.target_angle_upper)
        local_target = [
            target_dist * math.cos(target_angle),
            target_dist * math.sin(target_angle)
        ]
        local_pose = Pose2d(local_target)
        target = self.rootPose[index].localToGlobal(local_pose).p
        if self.motion == "walkrunfall":
            target = target + [self.target_height]
        elif self.motion == "chicken":
            target[0] /= 100
            target[1] /= 100
        else:
            print(
                "policy/rnn/RNNManager.py/randomTarget: use default target generation"
            )

        return np.array(target, dtype=np.float32)
Esempio n. 8
0
    def convertAndClipTarget(self, targets):
        # clip target and change to local coordinate
        targets = deepcopy(targets)
        for j in range(self.num_slaves):
            if self.motion == "walkrunfall":
                self.updateCharacterFallState(j)

                t = targets[j][:2]
                t = Pose2d(t)
                t = self.rootPose[j].relativePose(t)
                t = t.p
                t_len = math.sqrt(t[0] * t[0] + t[1] * t[1])

                t_angle = math.atan2(t[1], t[0])
                t_angle = np.clip(t_angle, -0.4 * np.pi, 0.4 * np.pi)

                if (self.isWalk):
                    clip_len = 60
                else:
                    clip_len = 250

                t_len = np.clip(t_len, 0.0, clip_len)

                t[0] = np.cos(t_angle) * t_len
                t[1] = np.sin(t_angle) * t_len

                # targets[j][0] = t[0]
                # targets[j][1] = t[1]
                # targets[j][2] = self.target_height
                if (self.fallOverState[j] == 0):
                    if (self.standUpCount[j] < 40):
                        targets[j][0] = 60
                        targets[j][1] = 0
                        targets[j][2] = self.target_height
                    else:
                        targets[j][0] = t[0]
                        targets[j][1] = t[1]
                        targets[j][2] = self.target_height
                else:
                    if (self.fallOverCount[j] < 70):
                        pred = RNNConfig.instance(
                        ).xNormal.denormalize_and_fill_zero(
                            self.controlPrediction[j])
                        targets[j][0] = RNNConfig.instance().xNormal.mean[0]
                        targets[j][1] = RNNConfig.instance().xNormal.mean[1]
                        targets[j][2] = 0  #0#self.target_height
                    else:
                        if (RNNConfig.instance().useControlPrediction):
                            pred = RNNConfig.instance(
                            ).xNormal.denormalize_and_fill_zero(
                                self.controlPrediction[j])
                            targets[j][0] = pred[
                                0]  # self.config.x_normal.mean[0]
                            targets[j][1] = pred[
                                1]  # self.config.x_normal.mean[1]
                            targets[j][2] = pred[2]  # min(88, pred[2]+20)
                        else:
                            targets[j][0] = 0  # self.config.x_normal.mean[0]
                            targets[j][1] = 0  # self.config.x_normal.mean[1]
                            targets[j][
                                2] = self.target_height  #min(88, self.config.y_normal.de_normalize_l(self.current_y[j])[5]+20)

                targets[j] = RNNConfig.instance(
                ).xNormal.normalize_and_remove_zero(targets[j])
            elif self.motion == "chicken":
                self.updateCharacterFallState(j)

                t = targets[j][:2]
                t = Pose2d([t[0] * 100, t[1] * 100])
                t = self.rootPose[j].relativePose(t)
                t = t.p
                t[0] /= 100
                t[1] /= 100
                t_len = math.sqrt(t[0] * t[0] + t[1] * t[1])

                t_angle = math.atan2(t[1], t[0])
                t_angle = np.clip(t_angle, -0.6 * np.pi, 0.6 * np.pi)

                clip_len = 0.6

                t_len = np.clip(t_len, 0.0, clip_len)

                t[0] = np.cos(t_angle) * t_len
                t[1] = np.sin(t_angle) * t_len

                targets[j][0] = t[0]
                targets[j][1] = t[1]
                targets[j] = RNNConfig.instance(
                ).xNormal.normalize_and_remove_zero(targets[j])

        return np.array(targets, dtype=np.float32)