Hi, I write 2 code: publish twist and convert velocities for each wheels
- Publish Twist
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
class PublishTwist():
def __init__(self):
self.publisher = rospy.Publisher("/twist_vels", Twist, queue_size=1)
self.twistVels = Twist()
self.rate10Hz = rospy.Rate(10)
self.rate1Hz = rospy.Rate(1)
self.ctrlC = False
rospy.on_shutdown(self.shutdownhook)
def shutdownhook(self):
self.ctrlC = True
self.twistVels.linear.x = 0
self.twistVels.angular.z = 0
self.publisher.publish(self.twistVels)
rospy.loginfo("Stop")
def publish(self, linearSpeed=0.2, angularSpeed=0.2):
self.twistVels.linear.x = linearSpeed
self.twistVels.angular.z = angularSpeed
i = 0
while self.ctrlC is False:
self.publisher.publish(self.twistVels)
rospy.loginfo("twistVels published - {0}".format(i))
i = i + 1
rospy.sleep(3)
rospy.loginfo("Wake up")
if name == “main”:
# rospy.init_node(“publish_twist_node”, anonymous=True)
rospy.init_node(“publish_twist_node”)
publishTwist = PublishTwist()
try:
publishTwist.publish(linearSpeed=0.2, angularSpeed=0.2)
except rospy.ROSInterruptException as e:
print(e)
- Convert velocities
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import Float64
class SubscribeTwist():
def __init__(self):
self.subscriber = rospy.Subscriber("/twist_vels", Twist, self.callback)
self.twistVels = Twist()
self.vLeftPublisher = rospy.Publisher("/left_wheel_controller/command", Float64, queue_size=1)
self.vLeft = Float64()
self.vRightPublisher = rospy.Publisher("/right_wheel_controller/command", Float64, queue_size=1)
self.vRight = Float64()
self.rate10Hz = rospy.Rate(10)
self.rate1Hz = rospy.Rate(1)
self.ctrlC = False
rospy.on_shutdown(self.shutdownhook)
self.L = 0.3
self.R = 0.1
def shutdownhook(self):
self.ctrlC = True
self.vLeft.data = 0
self.vRight.data = 0
self.vLeftPublisher.publish(self.vLeft)
self.vRightPublisher.publish(self.vRight)
rospy.loginfo("Stop")
def callback(self, msg):
linearXVel = msg.linear.x
angularZVel = msg.angular.z
self.vLeft.data = (2*linearXVel + angularZVel*self.L)/(2*self.R)
self.vRight.data = (2*linearXVel - angularZVel*self.L)/(2*self.R)
rospy.loginfo("callback is called")
def publish(self):
i = 0
while self.ctrlC is False:
self.vLeftPublisher.publish(self.vLeft)
self.vRightPublisher.publish(self.vRight)
rospy.loginfo("leftWheelCommand published - {0}".format(i))
rospy.loginfo("rightWheelCommand published - {0}".format(i))
i = i + 1
rospy.sleep(3)
rospy.loginfo("Wake up")
if name == “main”:
# rospy.init_node(“publish_twist_node”, anonymous=True)
rospy.init_node(“subscribe_twist_node”)
subscribeTwist = SubscribeTwist()
try:
# rospy.spin()
subscribeTwist.publish()
except rospy.ROSInterruptException as e:
print(e)
So, when linearSpeedX=0.2, angularSpeedX=0.2. Robot will turn left. The speed of left side has to be smaller than the right side.
But When I check my program.
→ linearSpeedX=0.2, angularSpeedX=0.2. Robot will turn left
→ But the speed of left side is higher than the right side. ???
Could you help me to understand this case?