Question: User import rclpy import math import random import array from rclpy . node import Node from geometry _ msgs . msg import Twist from nav

User
import rclpy
import math
import random
import array
from rclpy.node import Node
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from sensor_msgs.msg import LaserScan
class MyRobot(Node):
def __init__(self):
super().__init__('myrobot_node')
qos_policy = rclpy.qos.QoSProfile(
reliability=rclpy.qos.ReliabilityPolicy.BEST_EFFORT,
history=rclpy.qos.HistoryPolicy.KEEP_LAST, depth=1)
self.sub1= self.create_subscription(LaserScan, 'scan', self.scan_callback, qos_profile=qos_policy)
self.sub2= self.create_subscription(
Odometry,
'odom',
self.odom_callback,
qos_profile=qos_policy
)
self.pub = self.create_publisher(Twist,'cmd_vel', 10)
self.cmd_vel_pub = self.create_publisher(Twist,'cmd_vel', 10) # Added missing publisher
timer_period =0.1 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.my_x =0
self.my_y =0
self.my_angle =0
self.my_ls = LaserScan()
self.my_ls.ranges = array.array('f',[0.0]*360)
self.my_position = "forward"
self.target_angle =0
def scan_callback(self, msg):
self.my_ls.ranges = msg.ranges
# print ('l ={2:5.2f}, f ={1:5.2f}, r ={0:5.2f}'.format(self.my_ls.ranges[15], self.my_ls.ranges[0], self.my_ls.ranges[345]))
def odom_callback(self, msg):
self.my_x = msg.pose.pose.position.x
self.my_y = msg.pose.pose.position.y
# convert quaternion to Euler angles
q0= msg.pose.pose.orientation.w
q1= msg.pose.pose.orientation.x
q2= msg.pose.pose.orientation.y
q3= msg.pose.pose.orientation.z
self.my_angle = math.atan2(2*(q0* q3+ q1* q2),(1-2*(q2* q2+ q3* q3)))*180/ math.pi
# print('x ={0:5.2f}, y ={1:5.2f}, th ={2:5.2f}'.format(self.my_x, self.my_y, self.my_angle))
self.save_odometry_data()
def turn(self, target_angle):
move = Twist()
if self.my_angle < target_angle:
move.angular.z =0.1
else:
move.angular.z =0.0
return True # Signal that rotation is complete
return False
def timer_callback(self):
move = Twist()
# Implement obstacle avoidance logic here based on laser scan data
# For example, if an obstacle is too close, stop the robot
if (self.my_ls.ranges[0]<0.5 or self.my_ls.ranges[15]<0.5 or self.my_ls.ranges[345]<0.5) and self.my_position == "forward":
self.my_position = "left" # random.choice(['left', 'right'])
if self.my_position == "left":
self.target_angle = self.my_angle -90
if self.my_position == "right":
self.target_angle = self.my_angle +45
elif self.my_ls.ranges[0]>0.5:
self.my_position = "forward"
print(self.my_position)
if self.my_position == 'forward':
move.linear.x =0.1 # Speed to be adjust
move.angular.z =0.0
if self.my_position == 'left':
move.linear.x =0.0
move.angular.z =0.5 # The turning angle to be adjust
if self.my_angle > self.target_angle:
self.my_position == "forward"
elif self.my_position == 'right':
move.linear.x =0.0
move.angular.z =-0.5 # The turning angle to be adjust
if self.my_angle < self.target_angle:
self.my_position == "forward"
self.cmd_vel_pub.publish(move)
def save_odometry_data(self):
with open("src/ce215_pkg/ce215_pkg/myodom.txt","a") as myfile:
myfile.write(f"{self.my_x},{self.my_y},{self.my_angle}
")
def main(args=None):
rclpy.init(args=args)
myrobot_node = MyRobot()
rclpy.spin(myrobot_node)
myrobot_node.destroy_node()
rclpy.shutdown()
if __name__=='__main__':
main() Explain the code shown above in full description for each part of the command. This code is used for moving the turtlebot3 burger robot in gazebo simulation without hitting obstacles to prevent from crashing. Needs to cover the whole map from the Rviz simulation.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!