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

conver a python script in to a python class and make the callback function a class member

asked 2019-12-17 03:59:42 -0500

stevensu1838 gravatar image

Hi all,

I'd love to use my callback function to process some data and then deliver some result that will be used by other functions in the same python script. And I learned that one common way to do this is to use define a class and make the callback function a class. I am going to show you both of the original script and the one I tried to rewrite in class form. Can you please tell me what i did wrong in my class? I know it is a big favor. Hope you can help. My class throws errors like

ted@vra1:~/dual_ws$ rosrun robotiq_s_model_control SModelSimpleController.py _topic:=/left_hand/command
  File "/home/ted/dual_ws/src/robotiq/robotiq_s_model_control/nodes/SModelSimpleController.py", line 81
    def genCommand(char, self.command):
                             ^
SyntaxError: invalid syntax
ted@vra1:~/dual_ws$

My original script which has no errors in shown below:

import roslib; roslib.load_manifest('robotiq_s_model_control')
import rospy
from robotiq_s_model_articulated_msgs.msg import SModelRobotOutput 
from time import sleep
from geometry_msgs.msg import Twist


def callback(data):

print("Message received")
print(data.linear.x) 

def genCommand(char, command):
    """Update the command according to the character entered by the user."""  



if char == 'a':
    command = SModelRobotOutput()
    command.rACT = 1
    command.rGTO = 1
    command.rSPA = 255
    command.rFRA = 150

if char == 'r':
    command = SModelRobotOutput()
    command.rACT = 0

if char == 'c':
    command.rPRA = 255

if char == 'o':
    command.rPRA = 0

if char == 'b':
    command.rMOD = 0

if char == 'p':
    command.rMOD = 1

if char == 'w':
    command.rMOD = 2

if char == 's':
    command.rMOD = 3

#If the command entered is a int, assign this value to rPRA
try: 
    command.rPRA = int(char)
    if command.rPRA > 255:
        command.rPRA = 255
    if command.rPRA < 0:
        command.rPRA = 0
except ValueError:
    pass                    

if char == 'f':
    command.rSPA += 25
    if command.rSPA > 255:
        command.rSPA = 255

if char == 'l':
    command.rSPA -= 25
    if command.rSPA < 0:
        command.rSPA = 0


if char == 'i':
    command.rFRA += 25
    if command.rFRA > 255:
        command.rFRA = 255

if char == 'd':
    command.rFRA -= 25
    if command.rFRA < 0:
        command.rFRA = 0

return command


def askForCommand(command):
    """Ask the user for a command to send to the gripper."""    

currentCommand  = 'Simple S-Model Controller\n-----\nCurrent command:'
currentCommand += ' rACT = '  + str(command.rACT)
currentCommand += ', rMOD = ' + str(command.rMOD)
currentCommand += ', rGTO = ' + str(command.rGTO)
currentCommand += ', rATR = ' + str(command.rATR)
currentCommand += ', rGLV = ' + str(command.rGLV)
currentCommand += ', rICF = ' + str(command.rICF)
currentCommand += ', rICS = ' + str(command.rICS)
currentCommand += ', rPRA = ' + str(command.rPRA)
currentCommand += ', rSPA = ' + str(command.rSPA)
currentCommand += ', rFRA = ' + str(command.rFRA)

#We only show the simple control mode
currentCommand += ', rPRB = ' + str(command.rPRB)
currentCommand += ', rSPB = ' + str(command.rSPB)
currentCommand += ', rFRB = ' + str(command.rFRB)
currentCommand += ', rPRC = ' + str(command.rPRC)
currentCommand += ', rSPC = ' + str(command.rSPC)
currentCommand += ', rFRC = ' + str(command.rFRC)
currentCommand += ', rPRS = ' + str(command.rPRS)
currentCommand += ', rSPS = ' + str(command.rSPS)
currentCommand += ', rFRS = ' + str(command.rFRS)
print currentCommand
strAskForCommand  = '-----\nAvailable commands\n\n'
strAskForCommand += 'r: Reset\n'
strAskForCommand += 'a: Activate\n'
strAskForCommand += 'c: Close\n'
strAskForCommand += 'o: Open\n'
strAskForCommand += 'b: Basic mode\n'
strAskForCommand += 'p ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-12-17 08:52:21 -0500

mgruhler gravatar image

In the end, this is not a ROS but a python question.

You cannot have self.command as a parameter to a function.

You might want to read a bit up on what self means in python, there are a lot of pages out there about this. This seems to be the main reason for your issues...

some more hints:

  • it might make sense to have self as the first parameter of your class methods (i.e. def fct(self, var1, var2))
  • self.command below in the main doesn't make sense. You are not "inside" the class...
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-12-17 03:59:42 -0500

Seen: 386 times

Last updated: Dec 17 '19