Help with sub=rospy.subscriber() in action client

Hello,
I am trying to build my action client for the rosject and have the code below. However I am getting invalid syntax error.:

user:~$ rosrun turtle2 actionclient.py
File “/home/user/catkin_ws/src/turtle2/scripts/actionclient.py”, line 31
sub = rospy.Subscriber(’/scan’, LaserScan, callback)
^
SyntaxError: invalid syntax

Not sure why this would be an invalid syntax, the descriptor is unfortunatly vauge. Ive used this line in code before and its worked. What am I doing incorrect?

Code
#! /usr/bin/env python

import rospy
import actionlib
from turtlemvmt.action import turtlebotAction, turtlebotGoal
import rospy
import actionlib
from turtlemvmt.action import turtlebotAction, turtlebotFeedback, turtlebotResult
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
from std_msgs.msg import String
import actionlib
import actionlib_tutorials.msg

#Get information from /scan into turtlebotGoal

def feedback_cb(msg):
print (‘Feedback received:’, msg)

def call_server():

client = actionlib.SimpleActionClient('turtlebotactionserver', turtlebotAction)

client.wait_for_server()

goal = turtlebotGoal()

def callback(msg):
    print ("distance is:" (msg.ranges[180])
    (msg.ranges[180]) = goal

sub = rospy.Subscriber('/scan', LaserScan, callback)
rospy.spin()

client.send_goal(goal, feedback_cb=feedback_cb)

client.wait_for_result()

result = client.get_result()

return result

if name == ‘main’:

try:
    rospy.init_node('action_client')
    result = call_server()
    print 'The result is:', result
except rospy.ROSInterruptException as e:
    print 'Something went wrong:', e

It might be as simple as

sub = rospy.Subscriber('/scan', LaserScan, callback=callback)

just like you do with the feedback_cb.

Also I don’t know if your entry duplicated the imports, but just in case you don’t need to import rospy, actionlib or any other package more than once.

Unfortunately, I still get :
user:~$ rosrun turtle2 actionclient.py
File “/home/user/catkin_ws/src/turtle2/scripts/actionclient.py”, line 33
sub = rospy.Subscriber(’/scan’, LaserScan, callback=callback)
^
SyntaxError: invalid syntax

If I remember correctly the turtlebot scan topic was /kobuki/laser/scan so check the topics you can do a simple rostopic list or you can look for it using grep like rostopic list | grep 'kobuki' and this will show everything with kobuki in it. If you still have problems take a screen shot of the errors this would be very helpful if there’s something else going on. Also your print statement is incorrect you need more parentheses to close off the print. Go back and lookup the print statement in python to see what you need to fix their.

1 Like

just fixing the parenthesis seemed to keep the problem at bay. Will repost if error resurfaces. Thank you!