ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

why doesnt my while loop finish?

asked 2021-08-31 15:36:45 -0500

PGTKing gravatar image

updated 2021-08-31 18:18:13 -0500

miura gravatar image

My while loop iterates 2-5 times but doesn't get up to the 100 iterations I want it to. I need at least 100 iterations to extract enough data from my simulation. Here is my code, appreciate the help.

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 = 10
    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 < 100:
            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

Comments

I thought this was the part that said it would not loop 100 times. The loop statement looks like the problem content at first glance. When print is enabled, does it only print 2-5 times?

while i < 100:
    ...
    #print("the force is",z)
    ...
    i += 1
miura gravatar image miura  ( 2021-08-31 18:18:32 -0500 )edit

@miura thank for looking into my issue. Sorry I must need to clarify things, so when I make the while loop have 10 iterations and the num_trials loop have 10 iterations, the num_trials loop gets up to ~4 iterations. I eventually want to get up 100 iterations on each loop but am currently stuck here.

PGTKing gravatar image PGTKing  ( 2021-08-31 19:10:56 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-01 19:28:43 -0500

miura gravatar image

I think the problem is that you are running init_node with the same node name. Currently, the init_node is called every time you create an instance of Robotreacher. The following may solve the problem.

class Robotreacher:
    def __init__(self):
        # rospy.init_node('robot_control_node', anonymous=True)
# ...
rospy.init_node('robot_control_node', anonymous=True) # added
robotcontrolloop2()
edit flag offensive delete link more

Comments

I tried your solution, but output is still acting the same, I tried running the program again, but this time printing the force and I got up to 8/10 num_trials. You think it could be a processing issue? I have an i7 chip 2019 laptop

PGTKing gravatar image PGTKing  ( 2021-09-02 14:27:48 -0500 )edit

I don't think the performance of the machine is a problem, but it would be good to see what happens with other machines.

I'd like to work with you a little more on this. Are you getting the results of the following function?

print("Average: {}".format(np.mean(scores))))

I'm concerned that something may be causing it to terminate abnormally in the middle. Checking the log file as well as the output to the terminal may give you some insight.

miura gravatar image miura  ( 2021-09-02 18:31:19 -0500 )edit

yeah, I get the mean score if I reduce the num_trials down to 2 or 3 and the while loop down to ~5 iterations. Never tried checking log file before, will try to run the program without the gazebo gui as well.

PGTKing gravatar image PGTKing  ( 2021-09-02 21:56:23 -0500 )edit

If you don't get an average score, it means you have an unintended end in the middle. Another possibility is that Robotreacher's init has not finished.

It is possible that the loop is not terminated at

while self.observation_check() is False:

You may want to experiment by changing the loop to not loop once.

miura gravatar image miura  ( 2021-09-03 20:54:23 -0500 )edit

removed

while self.observation_check() is False:

still running into same problem of loop not running after 2-3 iterations

how do you find an unitended end in a log file?

The program sometimes gets to the end if it has a low number of iterations. I am able to get the average score in these situations.

PGTKing gravatar image PGTKing  ( 2021-09-08 12:36:57 -0500 )edit

The last log file you ran will be in the "~/.ros/log/latest" directory. (If you are trying to print to a terminal, there may not be a log file. )

miura gravatar image miura  ( 2021-09-08 18:38:17 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-08-31 15:36:45 -0500

Seen: 135 times

Last updated: Sep 01 '21