Skip to content

Commit 0260819

Browse files
committed
added the message in the node and wrote a new callback function for the cmd_vel subscriber
1 parent a8b5aca commit 0260819

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

neato_node/nodes/neato.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
from sensor_msgs.msg import LaserScan, Range
4141
from std_srvs.srv import SetBool, SetBoolResponse
42-
from neato_node.msg import Button, Sensor
42+
from neato_node.msg import Button, Sensor, Movement
4343
from neato_node.srv import SetLed, SetLedResponse, PlaySound, PlaySoundResponse
4444
from geometry_msgs.msg import Quaternion
4545
from geometry_msgs.msg import Twist
@@ -61,7 +61,7 @@ def __init__(self):
6161

6262
self.robot = Botvac(self.port, self.lds)
6363

64-
rospy.Subscriber("cmd_vel", Twist, self.cmdVelCb)
64+
rospy.Subscriber("cmd_dist", Movement, self.cmdMovementCb)
6565
self.scanPub = rospy.Publisher('base_scan', LaserScan, queue_size=1)
6666
self.odomPub = rospy.Publisher('odom', Odometry, queue_size=1)
6767
self.buttonPub = rospy.Publisher('button', Button, queue_size=1)
@@ -77,8 +77,9 @@ def __init__(self):
7777
rospy.Service('play_sound', PlaySound, self.playSound)
7878
rospy.Service('set_lds', SetBool, self.setLDS)
7979

80-
self.cmd_vel = [0, 0]
81-
self.old_vel = self.cmd_vel
80+
self.cmd_vel = 0
81+
self.cmd_dist = [0, 0]
82+
self.old_dist = self.cmd_dist
8283
self.encoders = [0, 0]
8384

8485
def spin(self):
@@ -132,8 +133,10 @@ def spin(self):
132133
self.robot.setMotors(0, 0, 0);
133134
self.cmd_vel = [0, 0]
134135
elif self.cmd_vel != self.old_vel:
135-
self.robot.setMotors(self.cmd_vel[0], self.cmd_vel[1], max(abs(self.cmd_vel[0]), abs(self.cmd_vel[1])))
136-
136+
self.robot.setMotors(self.cmd_dist[0], self.cmd_dist[1], self.cmd_vel)
137+
# reset command distance and speed
138+
self.robot.cmd_dist = [0,0]
139+
self.robot.cmd_vel = 0
137140
# wait, then do it again
138141
r.sleep()
139142

@@ -163,6 +166,15 @@ def cmdVelCb(self,req):
163166
x = x*self.robot.max_speed/k; th = th*self.robot.max_speed/k
164167
self.cmd_vel = [int(x-th), int(x+th)]
165168

169+
def cmdMovementCb(self,req):
170+
k = req.vel
171+
# sending commands higher than max speed will fail
172+
if k > self.robot.max_speed:
173+
k = self.robot.max_speed
174+
ros.logwarn("You have set the speed to more than the maximum speed of the neato. For safety reasons it is set to %d", self.robot.max_speed)
175+
self.robot.cmd_vel = k
176+
self.robot.cmd_dist = [req.x_dist, req.y_dist]
177+
166178
def publish_odom(self, odom):
167179
# get motor encoder values
168180
left, right = self.robot.getMotors()

0 commit comments

Comments
 (0)