Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 14 characters in body
Source Link
ksv
  • 27
  • 1
  • 5

I am using ROS-noetic and ROS2-foxy in the same machine. I created a package in ros2, and write this pythonobstacle_maneuver.py file,

I am using ROS-noetic and ROS2-foxy in the same machine. I created a package in ros2, and write this python file,

I am using ROS-noetic and ROS2-foxy in the same machine. I created a package in ros2, and write this obstacle_maneuver.py file,

Source Link
ksv
  • 27
  • 1
  • 5

Make sure to source your ROS 2 workspace after your ROS 1 workspace

I am using ROS-noetic and ROS2-foxy in the same machine. I created a package in ros2, and write this python file,

import rclpy from rclpy.node import Node from sensor_msgs.msg import LaserScan from geometry_msgs.msg import Twist class ObstacleAvoidingBot(Node): def __init__(self): super().__init__('Go_to_position_node') ## name of the node self.publisher = self.create_publisher(Twist, '/cmd_vel', 40) self.subscription=self.create_subscription(LaserScan,'/scan',self.get_scan_values,40) #periodic call timer_period = 0.2;self.timer = self.create_timer(timer_period, self.send_cmd_vel) ## Initializing Global values for linear velocity self.linear_vel = 0.22 ## dividing the area of laser scan (720/120) self.regions={'front_right':0.0,'front':0.0,'front_left':0.0, 'rear_left':0.0, 'back':0.0, 'rear_right':0.0} ## creating a message object to fit new velocities and publish them self.velocity=Twist() ## Subscriber Callback function def get_scan_values(self,scan_data): ## We have 360 data points and we are dividing them in 3 regions ## we say if there is something in the region get the smallest value self.regions = { 'front_right': min(scan_data.ranges[0:60]), 'front': min(scan_data.ranges[60:120]), 'front_left': min(scan_data.ranges[120:180]), 'rear_left': min(scan_data.ranges[180:240]), 'back': min(scan_data.ranges[240:300]), 'rear_right': min(scan_data.ranges[300:360]) } print(self.regions['front_right']," / ",self.regions['front']," / ",self.regions['front_left'], " / ",self.regions['rear_left'], " / ",self.regions['back'], " / ",self.regions['rear_right']) ## Callback Publisher of velocities called every 0.2 seconds def send_cmd_vel(self): ## angular and linear velocities are set into object self.velcity ## setting the linear velocity to be fixed and robot will keep on moving self.velocity.linear.x=self.linear_vel ## cases to make the robot change its angular velocity front_right_value = self.regions['front_right'] front_value = self.regions['front'] front_left_value = self.regions['front_left'] rear_left_value = self.regions['rear_left'] back_value = self.regions['back'] rear_right_value = self.regions['rear_right'] if all(value > 2 for value in [front_left_value, front_value, front_right_value, rear_right_value, back_value, rear_left_value]): self.velocity.angular.z = 0.0 # Area is total clear, move forward print("Forward") elif all(value > 2 for value in [front_value, front_right_value, rear_left_value, back_value, rear_right_value]) and front_left_value < 2: self.velocity.angular.z = 1.57 print("object in front_left, Moving front_right") elif all(value > 2 for value in [front_left_value, front_value, rear_right_value, rear_left_value, back_value]) and front_right_value < 2: self.velocity.angular.z = -1.57 print("object in front_right,Moving front_left") elif all(value > 2 for value in [front_value, rear_right_value, rear_left_value, back_value]) and front_right_value < 2 and front_left_value < 2: self.velocity.angular.z = 0.0 print("object in front_right and front_left ,Moving forward") elif all(value > 2 for value in [rear_right_value, rear_left_value, back_value]) and front_right_value < 2 and front_left_value < 2 and front_value < 2: self.velocity.angular.z = 3.14 print("object in front_right, front_left and front, going reverse") elif all(value > 2 for value in [rear_right_value]) and rear_left_value < 2 and front_left_value < 2 and front_value < 2 and front_right_value < 2 and back_value < 2: self.velocity.angular.z = 1.57 print("object in front_left, front, front_right, rear_left and back, Moving rear_right") elif all(value > 2 for value in [rear_left_value]) and rear_right_value < 2 and front_left_value < 2 and front_value < 2 and front_right_value < 2 and back_value < 2: self.velocity.angular.z = -1.57 print("object in front_left, front, front_right, rear_right and back, Moving rear_left") elif all(value > 2 for value in [back_value]) and rear_right_value < 2 and front_left_value < 2 and front_value < 2 and front_right_value < 2 and rear_left_value < 2: self.velocity.angular.z = 3.14 print("object in front_left, front, front_right, rear_right and rear_left_value, going reverse") elif all(value > 2 for value in [front_value]) and rear_right_value < 2 and front_left_value < 2 and back_value < 2 and front_right_value < 2 and rear_left_value < 2: self.velocity.angular.z = 0.0 print("object in front_left, back, front_right, rear_right and rear_left_value, going straight") else: #self.velocity.angular.z = 0.0 # Stop if none of the above conditions are met print("Robot stopped") ## lets publish the complete velocity self.publisher.publish(self.velocity) def main(args=None): rclpy.init(args=args) oab=ObstacleAvoidingBot() rclpy.spin(oab) rclpy.shutdown() if __name__ == '__main__': main() 

this is the setup.py file

from setuptools import setup import os from glob import glob package_name = 'my_rover' setup( name=package_name, version='0.0.0', packages=[package_name], data_files=[ ('share/ament_index/resource_index/packages', ['resource/' + package_name]), ('share/' + package_name, ['package.xml']), (os.path.join('share', package_name), glob('launch/*.py')), (os.path.join('share', package_name), glob('urdf/*')), ], install_requires=['setuptools'], zip_safe=True, maintainer='vasanth', maintainer_email='[email protected]', description='TODO: Package description', license='TODO: License declaration', tests_require=['pytest'], entry_points={ 'console_scripts': [ 'obstacle_avoidance = my_rover.obstacle_maneuver:main' ], }, ) 

when i run this command ros2 run my_rover obstacle_avoidance, after i gave source /opt/ros/foxy/src and source ~/colcon_ws/install/setup.bash, this is the error my terminal shows...

Traceback (most recent call last): File "/home/vasanth/colcon_ws/install/my_rover/lib/my_rover/obstacle_avoidance", line 11, in <module> load_entry_point('my-rover==0.0.0', 'console_scripts', 'obstacle_avoidance')() File "/home/vasanth/colcon_ws/install/my_rover/lib/python3.8/site-packages/my_rover/obstacle_maneuver.py", line 117, in main oab=ObstacleAvoidingBot() File "/home/vasanth/colcon_ws/install/my_rover/lib/python3.8/site-packages/my_rover/obstacle_maneuver.py", line 11, in __init__ self.publisher = self.create_publisher(Twist, '/cmd_vel', 40) File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/node.py", line 1144, in create_publisher check_for_type_support(msg_type) File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/type_support.py", line 20, in check_for_type_support ts = msg_type.__class__._TYPE_SUPPORT AttributeError: type object 'type' has no attribute '_TYPE_SUPPORT' This might be a ROS 1 message type but it should be a ROS 2 message type. Make sure to source your ROS 2 workspace after your ROS 1 workspace. 

and this shows in the terminal when i run source ~/colcon_ws/install/setup.bash

source ~/colcon_ws/install/setup.bash ROS_DISTRO was set to 'foxy' before. Please make sure that the environment does not mix paths from different distributions. ROS_DISTRO was set to 'noetic' before. Please make sure that the environment does not mix paths from different distributions. 

why it's shows both two workspaces at the same time?