Hello,
Please can someone help me look through this code? I have been up till 3am but I still can’t move the robot using the side and repetitions correctly. I have checked the similar questions asked but still no progress. I have tried various methods and this is my code at the moment, thank you.
Service_server
#! /usr/bin/env python
import rospy
from services_quiz.srv import BB8CustomServiceMessage, BB8CustomServiceMessageResponse
from geometry_msgs.msg import Twist
def my_callback(request):
rospy.loginfo("The service bb8 has been called")
j = 0
while j < request.repetitions:
if request.side == 1.0:
i = 0
while(i < 4.0):
rospy.loginfo("running square")
move_square.linear.x = size
pub.publish(move_square)
rospy.Rate.sleep()
move_square.linear.x = 0.0
pub.publish(move_square)
rospy.Rate.sleep()
move_square.angular.z = 0.9
pub.publish(move_square)
rospy.Rate.sleep()
move_square.angular.z = 0.0
pub.publish(move_square)
rospy.Rate.sleep()
i+=1
elif request.side == 2.0:
i = 0
while(i < 4.0):
rospy.loginfo("running square")
move_square.linear.x = size
pub.publish(move_square)
rospy.Rate.sleep()
move_square.linear.x = 0.0
pub.publish(move_square)
rospy.Rate.sleep()
move_square.angular.z = 0.9
pub.publish(move_square)
rospy.Rate.sleep()
move_square.angular.z = 0.0
pub.publish(move_square)
rospy.Rate.sleep()
i+=1
j+=1
move_square.linear.x = 0.0
move_square.angular.z = 0.0
pub.publish(move_square)
rospy.loginfo("Finished first small square ")
my_response = BB8CustomServiceMessageResponse()
my_response.success = True
return my_response
rospy.init_node(‘service_move_bb8_in_square_custom_server’)
my_service = rospy.Service(’/move_bb8_in_square_custom’, BB8CustomServiceMessage, my_callback)
pub = rospy.Publisher(’/cmd_vel’, Twist, queue_size=1)
move_square = Twist()
rate = rospy.Rate(1)
rospy.loginfo(“The service /move_bb8_in_square_custom is ready”)
rospy.spin()
Service_client
#! /usr/bin/env python
import rospkg
import rospy
from services_quiz.srv import BB8CustomServiceMessage, BB8CustomServiceMessageRequest
rospy.init_node(‘service_move_bb8_in_square_custom_client’)
rospy.wait_for_service(’/move_bb8_in_square_custom’)
move_bb8_in_square_service_client = rospy.ServiceProxy(’/move_bb8_in_square_custom’, BB8CustomServiceMessage)
move_bb8_in_square_object = BB8CustomServiceMessageRequest()
move_bb8_in_square_object.repetitions = 2
i = 0
while i < move_bb8_in_square_object.repetitions:
if i ==0:
move_bb8_in_square_object.side = 1.0
elif i ==1:
move_bb8_in_square_object.side = 2.0
i+=1
rospy.loginfo(“Doing service call”)
result = move_bb8_in_square_service_client(move_bb8_in_square_object)
rospy.loginfo(str(result))
rospy.loginfo(“End of Service call…”)