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

Need help with ros code

asked 2021-07-19 13:07:56 -0500

PGTKing gravatar image

updated 2021-07-20 03:13:31 -0500

Hi all, I am making a robot that tries the solve the reacher problem made by openai. here is link

Here is my code

#!/usr/bin/env python3

import rospy
from std_msgs.msg import Float64
import math
import random
from sensor_msgs.msg import JointState
import numpy as np
import time
import threading
from std_srvs.srv import Empty
import sys

#start_time = time.time()
#target1 = random.randint(10, 62)/10

reset_simulation = rospy.ServiceProxy('/gazebo/reset_simulation', Empty)

#rospy.init_node('reset_world')

#rospy.wait_for_service('/gazebo/reset_world')
reset_world = rospy.ServiceProxy('/gazebo/reset_world', Empty)

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(1)
        self.reward = None
        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(-8, 8, 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):
        self.joint1 = abs(data.position[0])%(2*math.pi)
        #self.joint2 = data.position[1]
        #reward = None
        #if self.observation_check() is True:
            #reward = (time.time() - start_time)
            #reset_world()
            #reset_simulation()
            #sys.exit("restarting simulation")
            #return reward


        #else:
            #pass

        #print(self.joint1)
        return self.joint1

    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 robotcontrolloop():
    i=0
    training_samplex, training_sampley, training_samplez = [], [], []
    while i < 10:
        #start_time = time.time()
        r = Robotreacher()
        r
        training_samplex.append(r.give_force())
        training_sampley.append(r.joint_callback())
        if r.what_is_reward() is not None:
            training_samplez.append(r.what_is_reward())
            i += 1
    '''
    if RobotControl() is not None:
        reset_world()
    '''

robotcontrolloop()

The robot I am getting is

training_sampley.append(r.joint_callback())
TypeError: joint_callback() missing 1 required positional argument: 'data'

I tried inputting data but its not working. Thanks in advance.

edit retag flag offensive close merge delete

Comments

6

Your screen name is offensive and has racist overtones. @gvdhoorn I request that this post be deleted.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-07-19 17:00:10 -0500 )edit

I agree. The post itself is not offensive, but the screen name is. @pregnantghettoteen: Please update your screen name ASAP. @gvdhoorn@tfoote : What should we do with this post in the meantime? (BTW: I just edited code block formatting just now, wording is unchanged)

Martin Günther gravatar image Martin Günther  ( 2021-07-20 03:15:39 -0500 )edit
2

@Mike Scheutzow The username is a reaction to overly pc world which I think is detrimental to free speech, I use it for all my social media websites. I understand this is a private website so, will change it according to the rules set by the mods. This is my one a only rebuttle, if you still want me to change my username, please let me know.

PGTKing gravatar image PGTKing  ( 2021-07-20 14:08:57 -0500 )edit
3

Yes, I would like you to change your username. Thank you.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-07-20 14:53:43 -0500 )edit
1

I also agree with @Mike Scheutzow and @Martin Günther

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-07-21 11:17:20 -0500 )edit

@pregnantghettoteen: It has been 7 days, and you have still not changed your user name. Please do so immediately, otherwise I will remove your post in 2 days. I get it that it was not meant to be racist, but it makes people uncomfortable, and I think it is not overly PC to ask you to change it. Thank you.

Martin Günther gravatar image Martin Günther  ( 2021-07-27 05:10:54 -0500 )edit
2
PGTKing gravatar image PGTKing  ( 2021-07-27 10:22:55 -0500 )edit

Thank you!

Martin Günther gravatar image Martin Günther  ( 2021-07-28 02:32:38 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-07-28 02:45:02 -0500

updated 2021-07-28 02:45:30 -0500

The error that you are getting is caused by this line:

    training_sampley.append(r.joint_callback())

You don't provide any arguments to the joint_callback function, but it expects a single argument called data. You probably shouldn't be calling joint_callback yourself anyway, since it's the callback function that is called by rospy to pass the sensor_msgs/JointState message.

edit flag offensive delete link more

Comments

Other issues with this code: you should 1) create Robotreacher object only once, 2) slow down execution inside the while loop using a rospy.Rate object, 3) do not return an object from the joint_callback method.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-07-28 07:20:23 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-07-19 13:07:56 -0500

Seen: 219 times

Last updated: Jul 28 '21