Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #! usr#!/usr/bin/env python
  2.  
  3. import rospy
  4. from sensor_msgs.msg import LaserScan
  5. from geometry_msgs.msg import Twist
  6. from nav_msgs.msg import Odometry
  7. import tf.transformations
  8. import matplotlib.pyplot as plt
  9. import random
  10. import math
  11. import time
  12.  
  13. #PLOTTING THE ENVIRONMENT!!!
  14. global mapPointsX # global variable for x coordinates of points
  15. mapPointsX = []
  16. global mapPointsY # global variable for y coordinates of points
  17. mapPointsY = []
  18.  
  19. def plot_map():
  20. # Plots points (X, Y) from turtlebot odometry
  21. global mapPointsX
  22. global mapPointsY
  23.  
  24. print('mapPoints range: ', len(mapPointsX))
  25. plt.plot(mapPointsY,mapPointsX, 'ro')
  26. plt.axis([-3,3,-3,3])
  27. plt.show()
  28.  
  29. def shutdown():
  30.  
  31. # stop turtlebot
  32. rospy.loginfo("Stop TurtleBot")
  33.  
  34. # a default Twist has linear.x of 0 and angular.z of 0. So it'll stop TurtleBot
  35. pub.publish(Twist())
  36. # sleep just makes sure TurtleBot receives the stop command prior to shutting down the script
  37. rospy.sleep(1)
  38. plot_map() # plot the map
  39.  
  40. class RobotMap:
  41. global mapPointsX
  42. global mapPointsY
  43. def __init__(self):
  44. self.position = None
  45. self.angle = None
  46. self.lidarPoints = None
  47.  
  48.  
  49. def callback_lidar(self, msg):
  50. # stores the lidarPoints
  51. self.lidarPoints = msg.ranges
  52.  
  53.  
  54. def callback_odom(self, msg):
  55.  
  56. self.position = msg.pose.pose.position # stores the position from odometry
  57. quaternion = (
  58. msg.pose.pose.orientation.x,
  59. msg.pose.pose.orientation.y,
  60. msg.pose.pose.orientation.z,
  61. msg.pose.pose.orientation.w)
  62. euler = tf.transformations.euler_from_quaternion(quaternion) # translates the quaternion orientation of turtlebot to euler
  63. self.yaw = euler[2]
  64. print("position:",self.position)
  65. print('yaw: ', self.yaw)
  66.  
  67. self.store_data()
  68.  
  69. def store_data(self):
  70. # mapPoints.append([self.position.x, self.position.y])
  71. for i in range(360):
  72. x = self.position.x + self.lidarPoints[i] *math.cos(math.radians(i+self.yaw))
  73. y = self.position.y + self.lidarPoints[i] *math.sin(math.radians(i+self.yaw))
  74. y = -1 * y # this is done for the plot to be consistent with the coordinates of turtlebot
  75. mapPointsX.append(x)
  76. mapPointsY.append(y)
  77. # print('position: ',len(mapPoints))
  78.  
  79. #MOVEMENT OF ROBOT!!!
  80.  
  81.  
  82.  
  83. def callback(msg):
  84. print('=========================')
  85. movements = [[0.0, -1*math.pi/2], [0.0,math.pi/2], [-0.3, 0.0]] #linear and angular velocities for turning right, left, and moving back, respectively
  86.  
  87.  
  88.  
  89. print('s1 [0]')
  90. print(msg.ranges[0]) #distance at angle 0, which is directly in front of robot
  91.  
  92. print('s2 [90]')
  93. print(msg.ranges[90]) #distance at angle 90
  94.  
  95. print('s3 [180]')
  96. print(msg.ranges[180]) #distance at angle 180
  97.  
  98. print('s4 [270]')
  99. print(msg.ranges[270]) #distance at angle 270
  100.  
  101. print('s5 [359]')
  102. print(msg.ranges[359]) #distance at angle 359
  103.  
  104. if msg.ranges[0] < msg.ranges[180]: #CHECK FRONT AND BACK, REVERSE IF BACK > FRONT
  105. move.linear.x = -0.3
  106. move.angular.z = 0.0
  107.  
  108. elif msg.ranges[0] > msg.ranges[180]: #CHECK FRONT AND BACK, FORWARD IF FRONT > BACK
  109. move.linear.x = 0.3
  110. move.angular.z = 0.0
  111.  
  112. elif msg.ranges[90] < msg.ranges[270]: #CHECK SIDES, TURN RIGHT IF RIGHT > LEFT
  113. move.linear.x = 0.0
  114. move.angular.z = -1 * math.pi/2
  115.  
  116. elif msg.ranges[90] > msg.ranges[270]: #CHECK SIDES, TURN LEFT IF LEFT > RIGHT
  117. move.linear.x = 0.0
  118. move.angular.z = math.pi/2
  119.  
  120. elif msg.ranges[0] - msg.ranges[180] <= 0.3 and msg.ranges[90] - msg.ranges[270] <= 0.3: #CHECK ALL DISTANCES
  121. move.linear.x = 0.0
  122. move.angular.z = 0.0
  123. rospy.signal_shutdown()
  124.  
  125.  
  126. pub.publish(move)
  127.  
  128.  
  129.  
  130.  
  131. rospy.init_node('DP_World3x3')
  132. rospy.loginfo("To STOP turtlebot Ctrl + C")
  133. rospy.on_shutdown(shutdown)
  134.  
  135. robotMap = RobotMap()
  136.  
  137. sub = rospy.Subscriber('/scan', LaserScan, callback)
  138. sub2 = rospy.Subscriber('/scan', LaserScan, robotMap.callback_lidar)
  139. sub3 = rospy.Subscriber('/odom', Odometry, robotMap.callback_odom)
  140.  
  141. pub = rospy.Publisher('/cmd_vel', Twist)
  142. move = Twist()
  143. rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement