Creating service server without class and with class

Hi there, I was trying to implement the service server using procedural method and then using classes method. However, i have a couple of doubts here,

  1. If we create the server using classes , we need to call the function at the last due to which the functioj get’s executed without calling it which is as opposed to the procedural method. Can we do something so that the function get’s executed only when we do rosservice call.

  2. Also, in the procedural method, we put request in the parenthesis of my_callback function whereas for class if we do the same, then we need to provide some value in the parenthesis so as to execute the function, otherwise it throws an error which is attached below.

    . My question is that why doesn’t it happen for procedural method. The picture with procedural and class approach have been attached below. Please give any suggestions as to what I am doing wrong or what can be done to make it work perfectly. I just put 0 for request variable and it works. and the class one is . Is this approach correct is is there another way to acheive the same?



You should not call the my_callback() method that you created in your class. you just have to define it like the procedural method.
so this line is not needed and is wrong!


after you created an object of your class (f1), just spin it like the procedural method. add rospy.spin() at the end of your code.
when your service object gets a request, it will call the defined callback (self.my_callback).

I hope this helps you.


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.