My program is not finishing if the number of iterations are to high

asked 2021-09-08 13:20:33 -0500

PGTKing gravatar image

updated 2021-09-08 13:55:46 -0500

jayess gravatar image

I am trying to make the rrbot do something similar to this. My rrbot receives random forces to one of its joints and I receive the positional data from the joint and then a score representing how long the joint was in the target joint position. The simulations were the rrbot has the collected the highest scores are saved and put into a machine learning programing to determine how much effort should be given to a joint in order for it to meet an intended target.

My Problem: The code gets stuck after 2 or 3 iterations and I don't understand why. I heard I can use roslog to determine where the code gets stuck

Here is my code:

class Robotreacher:
    def __init__(self):
        rospy.init_node('robot_control_node', anonymous=True)
        self.talker = rospy.Publisher('/rrbot/joint1_position_controller/command', Float64, queue_size=10)
        rospy.Subscriber('/rrbot/joint_states', JointState, self.joint_callback)
        self.force = None
        self.joint1 = 0
        self.target1 = random.randint(10, 62)/10
        self.start_time = time.time()
        self.rate = rospy.Rate(10)
        self.reward = 0
        print("the target is", self.target1)
        print("the reward is", self.reward)



        while self.observation_check() is False:
            self.give_force()
            self.rate.sleep()


    def give_force(self):
        self.force =  random.randrange(-12, 12, 1)  # Changes force every time, right before publishing
        self.talker.publish(self.force)
        #print("the force is ", self.force)
        return self.force            

    def joint_callback(self, data):
        #print(data)
        self.joint1 = abs(data.position[0])%(2*math.pi)


    def observation_check(self):
        target2 = self.target1 - 0.2
        if target2 < self.joint1 < self.target1:
            done = True
        else:
            done = False
        return done

    def what_is_reward(self):
        #reward = None
        if self.observation_check() is True:
            self.reward = (time.time() - self.start_time)

        return self.reward
        print("the reward is", self.reward)

    def get_joint(self):
        return self.joint1
        #print(self.joint1)


def robotcontrolloop2():

    num_trials = 3
    min_score = 0
    trainingZ, trainingP = [], []
    scores = []
    for _ in range(num_trials):
        i=0
        training_samplex = []
        r = Robotreacher()
        #w = r.give_force()
        #print("this is force", w)
        training_samplez = []
        training_samplep = []
        score = 0

        while i < 10:
            z = r.give_force()
            one_hot_action = np.zeros(25)
            one_hot_action[z] = 1
            #print(one_hot_action)
            print("the force is",z)
            time.sleep(0.7)
            p = r.get_joint()
            print("the position is", p)
            training_samplez.append(one_hot_action)
            #self.rate.sleep()
            training_samplep.append(p)
            score += r.what_is_reward()
            i += 1
        #print(training_z)
        #print(training_p)
        print(score)
        if score > min_score:
            scores.append(score)
            trainingZ += training_samplez
            trainingP += training_samplep

    trainingZ, trainingP = np.array(trainingZ), np.array(trainingP)
    print("Average: {}".format(np.mean(scores)))
    #print("Median: {}".format(np.median(scores)))
    return trainingZ, trainingP

robotcontrolloop2()
edit retag flag offensive close merge delete