Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. from geometry_msgs.msg import Twist
  5. from sensor_msgs.msg import LaserScan
  6. import time
  7.  
  8.  
  9. class RobotControl():
  10.  
  11. def __init__(self):
  12. rospy.init_node('robot_control_node', anonymous=True)
  13. self.vel_publisher = rospy.Publisher('cmd_vel', Twist, queue_size=1)
  14. self.laser_subscriber = rospy.Subscriber(
  15. 'scan', LaserScan, self.laser_callback)
  16. self.cmd = Twist()
  17. self.laser_msg = LaserScan()
  18. self.ctrl_c = False
  19. self.rate = rospy.Rate(1)
  20. rospy.on_shutdown(self.shutdownhook)
  21.  
  22. def publish_once_in_cmd_vel(self):
  23. """
  24. This is because publishing in topics sometimes fails the first time you publish.
  25. In continuos publishing systems there is no big deal but in systems that publish only
  26. once it IS very important.
  27. """
  28. while not self.ctrl_c:
  29. connections = self.vel_publisher.get_num_connections()
  30. if connections > 0:
  31. self.vel_publisher.publish(self.cmd)
  32. #rospy.loginfo("Cmd Published")
  33. break
  34. else:
  35. self.rate.sleep()
  36.  
  37. def shutdownhook(self):
  38. # works better than the rospy.is_shutdown()
  39. self.ctrl_c = True
  40.  
  41. def laser_callback(self, msg):
  42. self.laser_msg = msg
  43.  
  44. def get_laser(self, pos):
  45. time.sleep(1)
  46. return self.laser_msg.ranges[pos]
  47.  
  48. # def get_front_laser(self):
  49. # time.sleep(1)
  50. # return self.laser_msg.ranges[?]
  51.  
  52. # def get_laser_full(self):
  53. # time.sleep(1)
  54. # return self.laser_msg.ranges
  55.  
  56. def stop_robot(self):
  57. #rospy.loginfo("shutdown time! Stop the robot")
  58. self.cmd.linear.x = 0.0
  59. self.cmd.angular.z = 0.0
  60. self.publish_once_in_cmd_vel()
  61.  
  62. def move_straight(self):
  63.  
  64. # Initilize velocities
  65. self.cmd.linear.x = 0.5
  66. self.cmd.linear.y = 0
  67. self.cmd.linear.z = 0
  68. self.cmd.angular.x = 0
  69. self.cmd.angular.y = 0
  70. self.cmd.angular.z = 0
  71.  
  72. # Publish the velocity
  73. self.publish_once_in_cmd_vel()
  74.  
  75. def move_straight_time(self, motion, speed, time):
  76.  
  77. # Initilize velocities
  78. self.cmd.linear.y = 0
  79. self.cmd.linear.z = 0
  80. self.cmd.angular.x = 0
  81. self.cmd.angular.y = 0
  82. self.cmd.angular.z = 0
  83.  
  84. if motion == "forward":
  85. self.cmd.linear.x = speed
  86. elif motion == "backward":
  87. self.cmd.linear.x = - speed
  88.  
  89. i = 0
  90. # loop to publish the velocity estimate, current_distance = velocity * (t1 - t0)
  91. while (i <= time):
  92.  
  93. # Publish the velocity
  94. self.vel_publisher.publish(self.cmd)
  95. i += 1
  96. self.rate.sleep()
  97.  
  98. # set velocity to zero to stop the robot
  99. self.stop_robot()
  100.  
  101. s = "Moved robot " + motion + " for " + str(time) + " seconds"
  102. return s
  103.  
  104.  
  105. def turn(self, clockwise, speed, time):
  106.  
  107. # Initilize velocities
  108. self.cmd.linear.x = 0
  109. self.cmd.linear.y = 0
  110. self.cmd.linear.z = 0
  111. self.cmd.angular.x = 0
  112. self.cmd.angular.y = 0
  113.  
  114. if clockwise == "clockwise":
  115. self.cmd.angular.z = -speed
  116. else:
  117. self.cmd.angular.z = speed
  118.  
  119. i = 0
  120. # loop to publish the velocity estimate, current_distance = velocity * (t1 - t0)
  121. while (i <= time):
  122.  
  123. # Publish the velocity
  124. self.vel_publisher.publish(self.cmd)
  125. i += 1
  126. self.rate.sleep()
  127.  
  128. # set velocity to zero to stop the robot
  129. self.stop_robot()
  130.  
  131. s = "Turned robot " + clockwise + " for " + str(time) + " seconds"
  132. return s
  133.  
  134.  
  135. if __name__ == '__main__':
  136. #rospy.init_node('robot_control_node', anonymous=True)
  137. robotcontrol_object = RobotControl()
  138. try:
  139. robotcontrol_object.move_straight()
  140.  
  141. except rospy.ROSInterruptException:
  142. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement