Skip to content

Commit 34867fd

Browse files
authoredApr 6, 2024
[ ROS2 PORTING ] : 1_chatter_listener and turtlesim under activities folder (#67)
* Changed chatter_listener * changed turtlesim * Update README.md
1 parent e80f9bc commit 34867fd

File tree

12 files changed

+331
-157
lines changed

12 files changed

+331
-157
lines changed
 

‎1_chatter_listener/CMakeLists.txt

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
cmake_minimum_required(VERSION 3.8)
2-
project(multiple_chatter_listener)
2+
project(chatter_listener)
33

44
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
55
add_compile_options(-Wall -Wextra -Wpedantic)
66
endif()
7-
8-
# find dependencies
7+
# Find dependencies
98
find_package(ament_cmake REQUIRED)
10-
find_package(rclcpp REQUIRED)
11-
find_package(std_msgs REQUIRED)
12-
find_package(geometry_msgs REQUIRED)
13-
# uncomment the following section in order to fill in
14-
# further dependencies manually.
15-
# find_package(<dependency> REQUIRED)
16-
add_executable(talk src/multi_publisher.cpp)
17-
ament_target_dependencies(talk rclcpp std_msgs geometry_msgs)
18-
19-
add_executable(listen src/multi_subscriber.cpp)
20-
ament_target_dependencies(listen rclcpp std_msgs geometry_msgs)
21-
22-
install(TARGETS
23-
talk
24-
listen
25-
DESTINATION lib/${PROJECT_NAME})
9+
find_package(rclpy REQUIRED)
10+
11+
# Install Python executables
12+
install(PROGRAMS
13+
scripts/talker.py
14+
scripts/listener.py
15+
DESTINATION lib/${PROJECT_NAME}
16+
)
17+
18+
# Install launch files
2619

2720
if(BUILD_TESTING)
2821
find_package(ament_lint_auto REQUIRED)
2922
# the following line skips the linter which checks for copyrights
30-
# comment the line when a copyright and license is added to all source files
31-
set(ament_cmake_copyright_FOUND TRUE)
23+
# uncomment the line when a copyright and license is not present in all source files
24+
#set(ament_cmake_copyright_FOUND TRUE)
3225
# the following line skips cpplint (only works in a git repo)
33-
# comment the line when this package is in a git repo and when
34-
# a copyright and license is added to all source files
35-
set(ament_cmake_cpplint_FOUND TRUE)
26+
# uncomment the line when this package is not in a git repo
27+
#set(ament_cmake_cpplint_FOUND TRUE)
3628
ament_lint_auto_find_test_dependencies()
3729
endif()
3830

3931
ament_package()
32+
33+
34+

‎1_chatter_listener/README.md

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
# talker and listener
2-
Steps to talker ans listener scripts :
3-
* If you haven't cloned the repo, clone the repo and rebuild the catkin workspace by following the instructions specified in the beginning.
4-
* Start `roscore` in a terminal.
2+
Steps to run talker and listener scripts :
3+
* To run the talker and listener first we need to copy the 1_chatter_listener to ros2_ws src
4+
55
```
6-
source /opt/ros/noetic/setup.bash
7-
roscore
6+
cp -r /home/(user_name)/MARIO/1_chatter_listener /home/(user_name)/ros2_ws/src
87
```
9-
* Open another terminal to run `talker.py`
8+
* To build the necesarry packages we need to install them so for that go to ros2_ws
109
```
11-
source ~/catkin_ws/devel/setup.bash
12-
rosrun chatter_listener talker.py
10+
cd ros2_ws
1311
```
14-
* Open another terminal to rub `listener.py`
12+
* Then to build run:
1513
```
16-
source ~/catkin_ws/devel/setup.bash
17-
rosrun chatter_listener listener.py
14+
colcon build
1815
```
19-
* To know more about it visit [this tutorial](http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29)
16+
* Now we need to source it
17+
```
18+
source install/setup.bash
19+
```
20+
* Now run the talker
21+
```
22+
ros2 run chatter_listener talker.py
23+
```
24+
* To run the listener `open another terminal` and again source it:
25+
```
26+
source install/setup.bash
27+
```
28+
* Now run the listener
29+
```
30+
ros2 run chatter_listener listener.py
31+
```
32+
* TALKER AND LISTENER
33+
<p align="centere"><img src ="assets/talker_listener.gif" width="1500" height="900"></p>
34+
830 KB
Loading

‎1_chatter_listener/package.xml

+11-71
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,20 @@
11
<?xml version="1.0"?>
2-
<package format="2">
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
34
<name>chatter_listener</name>
45
<version>0.0.0</version>
5-
<description>The chatter_listener package</description>
6+
<description>TODO: Package description</description>
7+
<maintainer email="arn1952004@gmail.com">atharva</maintainer>
8+
<license>TODO: License declaration</license>
69

7-
<!-- One maintainer tag required, multiple allowed, one person per tag -->
8-
<!-- Example: -->
9-
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
10-
<maintainer email="swapnil@todo.todo">swapnil</maintainer>
10+
<buildtool_depend>ament_cmake</buildtool_depend>
11+
<buildtool_depend>rclpy</buildtool_depend>
12+
<buildtool_depend>std_msgs</buildtool_depend>
1113

14+
<test_depend>ament_lint_auto</test_depend>
15+
<test_depend>ament_lint_common</test_depend>
1216

13-
<!-- One license tag required, multiple allowed, one license per tag -->
14-
<!-- Commonly used license strings: -->
15-
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
16-
<license>TODO</license>
17-
18-
19-
<!-- Url tags are optional, but multiple are allowed, one per tag -->
20-
<!-- Optional attribute type can be: website, bugtracker, or repository -->
21-
<!-- Example: -->
22-
<!-- <url type="website">http://wiki.ros.org/chatter_listener</url> -->
23-
24-
25-
<!-- Author tags are optional, multiple are allowed, one per tag -->
26-
<!-- Authors do not have to be maintainers, but could be -->
27-
<!-- Example: -->
28-
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
29-
30-
31-
<!-- The *depend tags are used to specify dependencies -->
32-
<!-- Dependencies can be catkin packages or system dependencies -->
33-
<!-- Examples: -->
34-
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
35-
<!-- <depend>roscpp</depend> -->
36-
<!-- Note that this is equivalent to the following: -->
37-
<!-- <build_depend>roscpp</build_depend> -->
38-
<!-- <exec_depend>roscpp</exec_depend> -->
39-
<!-- Use build_depend for packages you need at compile time: -->
40-
<!-- <build_depend>message_generation</build_depend> -->
41-
<!-- Use build_export_depend for packages you need in order to build against this package: -->
42-
<!-- <build_export_depend>message_generation</build_export_depend> -->
43-
<!-- Use buildtool_depend for build tool packages: -->
44-
<!-- <buildtool_depend>catkin</buildtool_depend> -->
45-
<!-- Use exec_depend for packages you need at runtime: -->
46-
<!-- <exec_depend>message_runtime</exec_depend> -->
47-
<!-- Use test_depend for packages you need only for testing: -->
48-
<!-- <test_depend>gtest</test_depend> -->
49-
<!-- Use doc_depend for packages you need only for building documentation: -->
50-
<!-- <doc_depend>doxygen</doc_depend> -->
51-
<buildtool_depend>catkin</buildtool_depend>
52-
<build_depend>roscpp</build_depend>
53-
<build_depend>rospy</build_depend>
54-
<build_depend>std_msgs</build_depend>
55-
<build_depend>urdf</build_depend>
56-
<build_depend>controller_manager</build_depend>
57-
<build_depend>joint_state_controller</build_depend>
58-
<build_depend>robot_state_publisher</build_depend>
59-
<build_export_depend>roscpp</build_export_depend>
60-
<build_export_depend>rospy</build_export_depend>
61-
<build_export_depend>std_msgs</build_export_depend>
62-
<build_export_depend>urdf</build_export_depend>
63-
<build_export_depend>controller_manager</build_export_depend>
64-
<build_export_depend>joint_state_controller</build_export_depend>
65-
<build_export_depend>robot_state_publisher</build_export_depend>
66-
<exec_depend>roscpp</exec_depend>
67-
<exec_depend>rospy</exec_depend>
68-
<exec_depend>std_msgs</exec_depend>
69-
<exec_depend>urdf</exec_depend>
70-
<exec_depend>controller_manager</exec_depend>
71-
<exec_depend>joint_state_controller</exec_depend>
72-
<exec_depend>robot_state_publisher</exec_depend>
73-
74-
75-
<!-- The export tag contains other, unspecified, tags -->
7617
<export>
77-
<!-- Other tools can request additional information be placed here -->
78-
18+
<build_type>ament_cmake</build_type>
7919
</export>
8020
</package>
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
# MIT License
3+
4+
# Copyright (c) 2024 Society of Robotics and Automation
5+
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
## A multiple listenr demo that subscribes std_msgs/Strings messages
24+
## from the 3 different 'talker' topics
25+
26+
import sys
27+
import rclpy
28+
from std_msgs.msg import String
29+
30+
def cb1(node, msg:String):
31+
node.get_logger().info("Heard from talker1 %s" %(msg.data))
32+
33+
def cb2(node, msg:String):
34+
node.get_logger().info("Heard from talker2 %s" %(msg.data))
35+
36+
def cb3(node, msg:String):
37+
node.get_logger().info("Heard from talker3 %s" %(msg.data))
38+
39+
def main(args=None):
40+
rclpy.init(args=sys.argv)
41+
node = rclpy.create_node("listener")
42+
sub1 = node.create_subscription(String, "talker_1", lambda msg: cb1(node, msg), 10)
43+
sub2 = node.create_subscription(String, "talker_2", lambda msg: cb2(node, msg), 10)
44+
sub3 = node.create_subscription(String, "talker_3", lambda msg: cb3(node, msg), 10)
45+
46+
try:
47+
rclpy.spin(node)
48+
except KeyboardInterrupt:
49+
pass
50+
rclpy.shutdown()
51+
52+
if __name__ == "__main__":
53+
main()

‎1_chatter_listener/scripts/talker.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
# MIT License
3+
4+
# Copyright (c) 2024 Society of Robotics and Automation
5+
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
24+
## A multiple talker demo that published std_msgs/Strings messages
25+
## to the 3 different 'talker' topics
26+
27+
import sys
28+
import rclpy
29+
from rclpy import qos
30+
from std_msgs.msg import String
31+
32+
33+
34+
def timer_call():
35+
msg = String()
36+
msg.data = "Hello from SRA%d %d" %(timer_call.id,timer_call.counter)
37+
if timer_call.id==1:
38+
pub1.publish(msg)
39+
elif timer_call.id == 2:
40+
pub2.publish(msg)
41+
elif timer_call.id == 3:
42+
pub3.publish(msg)
43+
44+
node.get_logger().info('Publishing: "%s"' % msg.data)
45+
timer_call.counter += 1
46+
if timer_call.counter > 50:
47+
timer_call.counter = 1
48+
timer_call.id += 1
49+
if timer_call.id > 3:
50+
timer_call.id = 1
51+
52+
53+
def main(args=None):
54+
rclpy.init(args=sys.argv)
55+
56+
global node,pub1,pub2,pub3
57+
node = rclpy.create_node("talker")
58+
pub1 = node.create_publisher(String, "talker_1", qos_profile=qos.qos_profile_parameters)
59+
pub2 = node.create_publisher(String, "talker_2", qos_profile=qos.qos_profile_parameters)
60+
pub3 = node.create_publisher(String, "talker_3", qos_profile=qos.qos_profile_parameters)
61+
timer_call.counter = 1
62+
timer_call.id = 1
63+
timer = node.create_timer(0.1,timer_call)
64+
65+
66+
67+
try:
68+
rclpy.spin(node) # Spin only one node to handle callbacks
69+
except KeyboardInterrupt:
70+
pass
71+
72+
rclpy.shutdown()
73+
74+
if __name__ == "__main__":
75+
main()
76+
77+
78+
79+
80+

‎activities/turtlesim/CMakeLists.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(circle)
3+
4+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
5+
add_compile_options(-Wall -Wextra -Wpedantic)
6+
endif()
7+
# Find dependencies
8+
find_package(ament_cmake REQUIRED)
9+
find_package(rclpy REQUIRED)
10+
find_package(geometry_msgs REQUIRED)
11+
find_package(turtlesim REQUIRED)
12+
# Install Python executables
13+
install(PROGRAMS
14+
scripts/draw_circle.py
15+
DESTINATION lib/${PROJECT_NAME}
16+
)
17+
18+
install(
19+
DIRECTORY scripts
20+
DESTINATION share/${PROJECT_NAME}
21+
)
22+
23+
24+
# Install launch files
25+
26+
if(BUILD_TESTING)
27+
find_package(ament_lint_auto REQUIRED)
28+
# the following line skips the linter which checks for copyrights
29+
# uncomment the line when a copyright and license is not present in all source files
30+
#set(ament_cmake_copyright_FOUND TRUE)
31+
# the following line skips cpplint (only works in a git repo)
32+
# uncomment the line when this package is not in a git repo
33+
#set(ament_cmake_cpplint_FOUND TRUE)
34+
ament_lint_auto_find_test_dependencies()
35+
endif()
36+
37+
ament_package()
38+

0 commit comments

Comments
 (0)
Please sign in to comment.