Hello,
I just started with ROS and Gazebo and was trying to control the robot in the simulation via python.
However, I appear to be unable to create an object of type ApplyJointEffortRequest, which is neccessary for the /gazebo/apply_joint_effort service call.
I’ve tired all kinds of modifications to my code, but I don’t understand how to resolve this issue. Other people online who had the _request_class attribute problem simply had to rename their class (because it was the same name as the service class) but this isn’t the case with my code. I basically followed this Construct YouTube Video ([ROS Q&A] 056]([ROS Q&A] 056 - How to use GetModelState service from Gazebo in python - YouTube)) and it worked perfectly fine, but when I added my own function (here simply called party) to control the robot instead of just reading its values, I encountered the problem.
Here’s a snippet in case anyone can help me:
from gazebo_msgs.srv import GetModelState, ApplyJointEffortRequest
from rospy import rostime as rt
import rospy
class BodyPart:
def __init__(self, model_name, body_name):
self._model_name = model_name
self._body_name = body_name
@property
def model_name(self):
return self._model_name
@property
def body_name(self):
return self._body_name
class GazeboModStates:
_body_parts = {
0: BodyPart("cobotta", "base_link"),
1: BodyPart("cobotta", "J1"),
2: BodyPart("cobotta", "J2"),
3: BodyPart("cobotta", "J3"),
4: BodyPart("cobotta", "J4"),
5: BodyPart("cobotta", "J5"),
6: BodyPart("cobotta", "J6"),
}
def get_info(self):
try:
model_state = rospy.ServiceProxy("/gazebo/get_model_state", GetModelState)
for body in self._body_parts.values():
msg = model_state(body.model_name, body.body_name)
print("")
# print("Status.success = ", msg.success)
print(str(body.model_name) + "-" + str(body.body_name))
pos = msg.pose.position
print("Position: " + str(pos.x) + " | " + str(pos.y) + " | " + str(pos.z))
rot = msg.pose.orientation
print("Rotation: " + str(rot.x) + " | " + str(rot.y) + " | " + str(rot.z) + " | " + str(rot.w))
except rospy.ServiceException as e:
rospy.loginfo("Get Model State service call failed: {0}".format(e))
@staticmethod
def party():
try:
kwargs = {"joint_name": "joint_1",
"effort": 10,
"start_time": rt.Time(secs=0, nsecs=0),
"duration": rt.Duration(secs=10, nsecs=0)}
# this causes the AttributeError
ajer = ApplyJointEffortRequest(**kwargs)
rospy.ServiceProxy("/gazebo/apply_joint_effort", ajer)
except rospy.ServiceException as e:
rospy.loginfo("Get Model State service call failed: {0}".format(e))
def menu_loop():
cobot = GazeboModStates()
while True:
# noinspection PyUnresolvedReferences
cmd = str(raw_input("\nEnter command. q to quit, i for joint info, p for party\n"))
if cmd == "q":
print("\nQuitting...")
return
elif cmd == "i":
cobot.get_info()
elif cmd == "p":
cobot.party()