Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import rospy
- from geometry_msgs.msg import Twist
- from sensor_msgs.msg import LaserScan
- import time
- class RobotControl():
- def __init__(self):
- rospy.init_node('robot_control_node', anonymous=True)
- self.vel_publisher = rospy.Publisher('cmd_vel', Twist, queue_size=1)
- self.laser_subscriber = rospy.Subscriber(
- 'scan', LaserScan, self.laser_callback)
- self.cmd = Twist()
- self.laser_msg = LaserScan()
- self.ctrl_c = False
- self.rate = rospy.Rate(1)
- rospy.on_shutdown(self.shutdownhook)
- def publish_once_in_cmd_vel(self):
- """
- This is because publishing in topics sometimes fails the first time you publish.
- In continuos publishing systems there is no big deal but in systems that publish only
- once it IS very important.
- """
- while not self.ctrl_c:
- connections = self.vel_publisher.get_num_connections()
- if connections > 0:
- self.vel_publisher.publish(self.cmd)
- #rospy.loginfo("Cmd Published")
- break
- else:
- self.rate.sleep()
- def shutdownhook(self):
- # works better than the rospy.is_shutdown()
- self.ctrl_c = True
- def laser_callback(self, msg):
- self.laser_msg = msg
- def get_laser(self, pos):
- time.sleep(1)
- return self.laser_msg.ranges[pos]
- # def get_front_laser(self):
- # time.sleep(1)
- # return self.laser_msg.ranges[?]
- # def get_laser_full(self):
- # time.sleep(1)
- # return self.laser_msg.ranges
- def stop_robot(self):
- #rospy.loginfo("shutdown time! Stop the robot")
- self.cmd.linear.x = 0.0
- self.cmd.angular.z = 0.0
- self.publish_once_in_cmd_vel()
- def move_straight(self):
- # Initilize velocities
- self.cmd.linear.x = 0.5
- self.cmd.linear.y = 0
- self.cmd.linear.z = 0
- self.cmd.angular.x = 0
- self.cmd.angular.y = 0
- self.cmd.angular.z = 0
- # Publish the velocity
- self.publish_once_in_cmd_vel()
- def move_straight_time(self, motion, speed, time):
- # Initilize velocities
- self.cmd.linear.y = 0
- self.cmd.linear.z = 0
- self.cmd.angular.x = 0
- self.cmd.angular.y = 0
- self.cmd.angular.z = 0
- if motion == "forward":
- self.cmd.linear.x = speed
- elif motion == "backward":
- self.cmd.linear.x = - speed
- i = 0
- # loop to publish the velocity estimate, current_distance = velocity * (t1 - t0)
- while (i <= time):
- # Publish the velocity
- self.vel_publisher.publish(self.cmd)
- i += 1
- self.rate.sleep()
- # set velocity to zero to stop the robot
- self.stop_robot()
- s = "Moved robot " + motion + " for " + str(time) + " seconds"
- return s
- def turn(self, clockwise, speed, time):
- # Initilize velocities
- self.cmd.linear.x = 0
- self.cmd.linear.y = 0
- self.cmd.linear.z = 0
- self.cmd.angular.x = 0
- self.cmd.angular.y = 0
- if clockwise == "clockwise":
- self.cmd.angular.z = -speed
- else:
- self.cmd.angular.z = speed
- i = 0
- # loop to publish the velocity estimate, current_distance = velocity * (t1 - t0)
- while (i <= time):
- # Publish the velocity
- self.vel_publisher.publish(self.cmd)
- i += 1
- self.rate.sleep()
- # set velocity to zero to stop the robot
- self.stop_robot()
- s = "Turned robot " + clockwise + " for " + str(time) + " seconds"
- return s
- if __name__ == '__main__':
- #rospy.init_node('robot_control_node', anonymous=True)
- robotcontrol_object = RobotControl()
- try:
- robotcontrol_object.move_straight()
- except rospy.ROSInterruptException:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement