0
$\begingroup$

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,

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?

$\endgroup$

1 Answer 1

0
$\begingroup$

When you build workspaces it keeps track of what is already sourced on your path and will attempt to make sure that it includes those underlays so that you can have the same experience.

I suspect that you have unintentionally had both distros sourced at different times when you built the other one. And in it trying to activate the underlays you get that error message.

In general you should not be mixing distros within a workspace. The only time that is recommended is when you're specifically building the bridge, and that's only for a very specific subset of packages.

I would recommend first making sure that you don't have a setup file in you default envronment such as your ~/.bashrc and then be very careful about keeping your terminal environments either clearly foxy or clearly noetic. It only takes one build with the wrong environment sourced to potentially cross contaminate your workspace.

To clear things, you will want to clear your workspaces of all built artifacts and generated content and make sure to start over building your sequence of overlays an a clear and separate manner that you won't mistakenly switch between environments.

$\endgroup$
6
  • $\begingroup$ Thanks for your reply, I understand your response partially. so, I have to delete the setup file ~/.bashrc? or what i have to do? $\endgroup$ Commented Sep 9, 2023 at 6:27
  • $\begingroup$ No don't delete that file. But make sure that it's not sourcing a ROS environment. Like '/opt/ROS/noetic/setup.bash' but you will need to clear your build and install spaces and start new terminals. I recommend reading up on how environment variables work too. $\endgroup$ Commented Sep 9, 2023 at 17:47
  • $\begingroup$ so, let me explain how i made my workspace. For ROS-noetic i named the workspace like this catkin_ws, for ROS2-foxy i named colcon_ws. The folder you mentioned "build" and "install" to clear, noetic has build and devel folder, foxy has build and install folder, which one i have to clear? $\endgroup$ Commented Sep 11, 2023 at 6:34
  • $\begingroup$ You need to clear all of those. But that's not your whole state. You need to keep track of what order you sourced setup files in what terminals. Aka /opt/ROS/foxy/setup.bash cannot be sourced in the terminal before you build our use anything in your catkin workspace. And vice versa for noetic and colcon workspace. $\endgroup$ Commented Sep 11, 2023 at 7:16
  • $\begingroup$ alright, let me try and update here. $\endgroup$ Commented Sep 11, 2023 at 14:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.