Hello, I’m on the final stage of the ROS project for the 5 days ROS course in Python. I found that my code works when I separately run the action server node and then use the axclient. However, when I’m having trouble with adding service client to my code. I keep getting this error:
Traceback (most recent call last):
File “/home/user/catkin_ws/src/follow_wall/scripts/pubVel.py”, line 42, in
client.send_goal(goal, feedback_cb=feedback)
NameError: name ‘feedback’ is not defined
Here is my code where I am trying to include the client, followed by the code for my action server. Please help!!
#! /usr/bin/env python
import rospy
import actionlib
from follow_wall.msg import OdomRecordAction, OdomRecordGoal, OdomRecordResult, OdomRecordFeedback
from follow_wall.srv import FindWall, FindWallRequest
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
move = Twist()
def callback(msg):
if(msg.ranges[360] > 100):
move.linear.x = 0.4
move.angular.z = 0.0
elif(msg.ranges[360] < 0.5):
move.linear.x = 0.05
move.angular.z = 1.0
elif(msg.ranges[180] > 0.3):
move.linear.x = 0.1
move.angular.z = -0.5
elif(msg.ranges[180] < 0.2):
move.linear.x = 0.1
move.angular.z = 0.5
else:
move.linear.x = 0.1
move.angular.z = 0.0
pub.publish(move)
def feedback_cb(feedback):
rospy.loginfo("Distance traveled was: " + feedback.current_total)
#Action client initialization
rospy.init_node('record_odom_client')
client = actionlib.SimpleActionClient('/record_odom', OdomRecordAction)
client.wait_for_server()
goal = OdomRecordGoal()
client.send_goal(goal, feedback_cb=feedback)
#Service client initialization
rospy.init_node('find_wall_custom_client')
rospy.wait_for_service('/find_wall')
find_wall_service_client = rospy.ServiceProxy('/find_wall', FindWall)
find_wall_service_object = FindWallRequest()
rospy.loginfo("Utilizing /find_wall service")
result = find_wall_service_client(find_wall_service_object)
print("Success of result of wall-finding service:")
print(result)
rospy.loginfo("END of service call!")
#Publisher/subscriber initialization
rospy.Subscriber('/scan', LaserScan, callback)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
while pub.get_num_connections() < 1:
# wait for a connection to publisher
pass
rospy.spin()
#! /usr/bin/env python
import rospy
import actionlib
from nav_msgs.msg import Odometry
from follow_wall.msg import OdomRecordFeedback, OdomRecordResult, OdomRecordAction
from std_msgs.msg import Empty
from geometry_msgs.msg import Point
class OdomRecordClass(object):
# create messages that are used to publish feedback/result
_feedback = OdomRecordFeedback()
_result = OdomRecordResult()
position_x = 0.0
position_y = 0.0
orientation_theta = 0.0
_dist_one_lap = 30.0
def __init__(self):
# creates the action server
rospy.loginfo("Initializing the action server...")
self._as = actionlib.SimpleActionServer("record_odom", OdomRecordAction, self.goal_callback, False)
self._as.start()
rospy.loginfo("Action server initialized...")
#create the subscriber
rospy.loginfo("creating the /odom subscriber")
_sub_odom = rospy.Subscriber('/odom', Odometry, self.odom_callback)
while _sub_odom.get_num_connections() < 1:
pass
rospy.loginfo("/odom subscriber created.")
def goal_callback(self, goal):
# this callback is called when the action server is called.
result = OdomRecordResult()
my_list = []
rate = rospy.Rate(1)
dist = 0
success = True
i = 0
a = []
while dist <= self._dist_one_lap:
rospy.loginfo("Saving odometry readings...")
#Check if goal is cancelled
if self._as.is_preempt_requested():
success = False
self._as.set_preempted()
break
#Saving odometry readings
self._odom_readings = Point()
self._odom_readings.x = self.position_x
self._odom_readings.y = self.position_y
self._odom_readings.z = self.orientation_theta
result.list_of_odoms.append(self._odom_readings)
rospy.loginfo(result.list_of_odoms)
#travelled distance
dist += 0.4
self._feedback.current_total = dist
self._as.publish_feedback(self._feedback)
#loop variables
i+=1
rate.sleep()
if success:
self._as.set_succeeded(result)
rospy.loginfo("Finishing the action server.")
#odometry callback
def odom_callback(self, msg):
self.position_x = msg.pose.pose.position.x
self.position_y = msg.pose.pose.position.y
self.orientation_theta = msg.pose.pose.orientation.z
if __name__ == '__main__':
rospy.init_node('record_odom_node')
OdomRecordClass()
rospy.spin()