why doesnt my while loop finish?
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()
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?
@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.