I tried the content based on the code you provided as is and did not get any errors regarding the service messages.
Most errors happen due to improper compilation,so compile using below cmds.
cd catkin_ws
catkin_make
source devel/setup.bash
during compilation check for any errors in that web shell
Anyways, the error i did see in you code is as follows:
cmd.linear.x = 0.3
cmd.angular.z = 0.31
cmd.linear.y = 0.3
cmd.angular.z = 0.31
cmd.linear.x = -0.3
cmd.angular.z = 0.31
cmd.linear.y = -0.3
cmd.angular.z = 0.31
r= 0
while r <= request.repetitions :
pub.publish(cmd)
rate.sleep()
r=r+1
You see ,here cmd.linear.x = 0.3
, changes to cmd.linear.x = -0.3
, the value gets updated to the new value you assigned and 0.3 is ignored. this error is repeated for all the values of x, y and z. You need to publish the values using pub.Publish() else there is nothing happening with the value you assigned.
Also, to move linearly, you only need to make changes in X, and Z is to be used only when you need it to rotate there is no need for Y.
In the case, above, unless you mentioned pub.publish(cmd) every time, only the very last values of x=-0.3 and Z=0.31 will be published. You also need to tell how long you want a command to run using rospy.sleep(time in secs). everytime.
The correct way is as follows:
def my_callback(request):
print("callback called")
r=0
while r <= request.repetitions :
print("callback called")
#move forward
print("moving forward")
cmd.linear.x=0.2
cmd.angular.z=0
pub.publish(cmd)
rospy.sleep(2)
#stop
print("stop")
cmd.linear.x=0
cmd.angular.z=0
pub.publish(cmd)
rospy.sleep(2)
#rotate
print("Rotate")
cmd.linear.x=0
cmd.angular.z=0.2
pub.publish(cmd)
#modify the time 2 below to till the right angle rotation is achived
rospy.sleep(2)
#stop
print("stop")
cmd.linear.x=0
cmd.angular.z=0
pub.publish(cmd)
rospy.sleep(2)
#update r
r=r+1
response.success = True
return response # the service Response class, in this case EmptyResponse
PLEASE note that the above code is only to show that the robot can move forward and rotate based on the code and not the complete solution. Since this is a quiz, i can’t reveal everything at the start, but if you are stuck, I will definitely help out.
You still need to modify the code to make the robot rotate at right angle by tweaking the rospy.sleep(modify_value) and modifying the cmd.linear,x value and corresponding rospy.sleep() values to make sure that the distance traveled is based on the side value provided.
The python file
#! /usr/bin/env python
import rospy
from services_quiz.srv import BB8CustomServiceMessage, BB8CustomServiceMessageResponse
from geometry_msgs.msg import Twist
def my_callback(request):
print("callback called")
r=0
while r <= request.repetitions :
print("callback called")
#move forward
print("moving forward")
cmd.linear.x=0.2
cmd.angular.z=0
pub.publish(cmd)
rospy.sleep(2)
#stop
print("stop")
cmd.linear.x=0
cmd.angular.z=0
pub.publish(cmd)
rospy.sleep(2)
#rotate
print("Rotate")
cmd.linear.x=0
cmd.angular.z=0.2
pub.publish(cmd)
#modify the time 2 below to till the right angle rotation is achived
rospy.sleep(2)
#stop
print("stop")
cmd.linear.x=0
cmd.angular.z=0
pub.publish(cmd)
rospy.sleep(2)
#update r
r=r+1
response.success = True
return response # the service Response class, in this case EmptyResponse
rospy.init_node('service_move_bb8_in_square_custom_server')
my_service = rospy.Service('/move_bb8_in_square_custom', BB8CustomServiceMessage, my_callback)
response = BB8CustomServiceMessageResponse()
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
while pub.get_num_connections()<1:
pass
#pub2 = rospy.Publisher('/counter', Int32, queue_size=1)
cmd= Twist()
#count=Int32()
rate = rospy.Rate(5)
rospy.spin()
All the other files remain unchanged.
Let me know if this solution worked out, (just in case, why dont you start over with a fresh package, while deleting the previous package in case the error persists)