this my service_server
#! /usr/bin/env python
import rospy
from services_quiz.srv import BB8CustomServiceMessage, BB8CustomServiceMessageResponse # you import the service message python classes
from geometry_msgs.msg import Twist
def my_callback(request):
#print("My_callback has been called")
rospy.loginfo("move_bb8_in_square_custom has been called")
for i in range(request.repetitions):
move = Twist()
rospy.sleep(1)
for j in range(4):
move.linear.x = request.side
move.angular.z = 0.4
pub.publish(move)
rospy.loginfo("insede the loop")
rospy.loginfo("outside the loop")
move.linear.x = 0.0
move.angular.z = 0.0
rospy.loginfo("stoping BB8")
rospy.loginfo("Finished service move_bb8_in_square_custom")
print("Request Data==> repetitons="+str(request.repetitions),"side"+ str(request.side))
my_response = BB8CustomServiceMessageResponse()
my_response.success = True
return my_response # the service Response class, in this case BBCustomServiceMessageResponse
rospy.init_node('service_server')
my_service = rospy.Service('/move_bb8_in_square_custom', BB8CustomServiceMessage , my_callback) # create the Service called my_service with the defined callback
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
# move = Twist()
# rate = rospy.Rate(1)
rospy.loginfo("Service /move_bb8_in_square_custom Ready")
# rospy.spin() # maintain the service open.
while not rospy.is_shutdown():
# Wait for a service request
request = rospy.wait_for_service('/move_bb8_in_square_custom', timeout=None)
# If the service request is received, call the callback function
if request is not None:
response = my_callback(request)
# If the service client exits cleanly, break out of the loop
if rospy.is_shutdown():
break
rospy.loginfo("Service /move_bb8_in_square_custom Shutdown")
this is service_client
#! /usr/bin/env python
import rospy
from services_quiz.srv import BB8CustomServiceMessage, BB8CustomServiceMessageRequest # you import the service message python classes
# Initialise a ROS node with the name service_client
rospy.init_node('service_client')
# Wait for the service client to be running
rospy.wait_for_service('/move_bb8_in_square_custom')
# Create the connection to the service
service_client = rospy.ServiceProxy('/move_bb8_in_square_custom', BB8CustomServiceMessage)
# Create an object of type
# Make two small squares
request_object = BB8CustomServiceMessageRequest()
request_object.repetitions = 2
request_object.side = 1.0
rospy.loginfo("Doing Service Call...")
result = service_client(request_object) # Send through the connection the path to the trajectory file to be executed
rospy.loginfo(str(result)) # Print the result given by the service called
rospy.loginfo("END of Service call...")
# Wait for a second
rospy.sleep(1)
# Make a big square
request_object.repetitions = 1
request_object.side = 1.5
rospy.loginfo("Doing Service Call...")
result = service_client(request_object) # Send through the connection the path to the trajectory file to be executed
rospy.loginfo(str(result)) # Print the result given by the service called
rospy.loginfo("END of Service call...")
Problems:
- BB8 made continuous squares and not stoping(probably loop trouble, i dont understand what exactly)
- BB8 not repeat small square 2 time(probably also related to loop or service call)
- How to make it stop after each side to make real square, because mine looks like oval
Thanks