knmlprz / TrailblazerML

Collaboration with Legendary Rover scientific club
0 stars 0 forks source link

Start with ROS #83

Open Kleczyk opened 1 month ago

Kleczyk commented 1 month ago

Info

For the sake of learning and before getting started in ROS, everyone needs to do the task. i encourage you to expand the network of node connections to get a better feel for the topic. We need this before brainstorming. Solutions past in comment .

Description

The goal is to create two ROS 2 nodes, one that publishes data and another that subscribes to it via a topic. The nodes must be correctly built and configured in a package, and their connections should be visualized using rqt_graph.

Tasks

Possible Solution

Two nodes:

Images look this:Image Image

Dependencies

Blocked by

FWalkowicz commented 1 month ago

@Kleczyk czy po wykonaniu taska dawać tutaj jakiś ss że działa?

Kleczyk commented 1 month ago

@FWalkowicz tak tutaj. screen wystarczy

FWalkowicz commented 1 month ago

@Kleczyk dodaje całe rozwiązanie wraz z przykładowym kodem gdyby ktoś miał jakieś pytania albo mu nie działało.

<depend>rclpy</depend>  
<depend>std_msgs</depend>
mkdir ROS2

cd ROS2

mkdir src

ros2 pkg create my_robot_controller --build-type ament_python --dependencies rclpy

cd ROS2/src/my_robot_controller/my_robot_controller

touch my_first_node.py
touch my_second_node.py
import rclpy  
import random  
from std_msgs.msg import String  
from rclpy.node import Node  

class SpeedPublisher(Node):  
    def __init__(self):  
        super().__init__('speed_publisher')  
        self.speed_publisher = self.create_publisher(String, '/rover/speed', 10)  
        time_period = 1  
        self.timer = self.create_timer(time_period, self.timer_callback)  
        self.i = 1  

    def timer_callback(self):  
        msg = String()  
        msg.data = f'msg: {self.i} - rover speed is : {round(random.random(), 2)}'  
        self.speed_publisher.publish(msg)  
        self.get_logger().info(f'Publishing {msg.data}')  
        self.i += 1  

def main(args=None):  
    rclpy.init(args=args)  
    node = SpeedPublisher()  
    rclpy.spin(node)  
    rclpy.shutdown()  

if __name__ == '__main__':  
    main()

class SpeedSubscriber(Node):
def init(self):
super().init('speed_subscriber')
self.create_subscription(
String,
'rover/speed',
self.listener_callback,
10
)

    self.subscriptions  

def listener_callback(self, msg):  
    self.get_logger().info(f'Subscribed {msg.data}')  

def main(args=None):
rclpy.init(args=args)
node = SpeedSubscriber()
rclpy.spin(node)
rclpy.shutdown()

if name == 'main':
main()


- [x] Add correct entries to the `setup.py` file

```python
'console_scripts': [  
    "publisher_node = my_robot_controller.my_first_node:main",  
    "subscriber_node = my_robot_controller.my_second_node:main",  
],
Kleczyk commented 1 month ago

@FWalkowicz , @kosiarn @Sygnator @kubabartecki @bafaurazan @jKryczka jak tam idzie ?

Kleczyk commented 1 month ago

@kosiarn @Sygnator @kubabartecki @bafaurazan @jKryczka jak tam idzie ?

kosiarn commented 1 month ago

@Kleczyk sorki, inżynierka nieco wstrzymała... właśnie instaluję Ubuntu na wirtualce

Kleczyk commented 1 month ago

@kosiarn nic się nie dzieje super powodzianka z VM

bafaurazan commented 1 month ago

@Kleczyk na linux mint działa wszystko

sudo apt upgrade

sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers" --os=ubuntu:jammy

image

mkdir ros2_ws

cd ros2_ws

mkdir src

cd src

ros2 pkg create --build-type ament_python --license Apache-2.0 rover_control
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>rover_control</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="rafal@todo.todo">rafal</maintainer>
  <license>Apache-2.0</license>

  <exec_depend>rclpy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

pobranie przykładowego publisher-a i dostosowanie go

cd src/rover_control/rover_control/

wget https://raw.githubusercontent.com/ros2/examples/humble/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py
import rclpy
from rclpy.node import Node

from std_msgs.msg import String

class SpeedPublisher(Node):

    def __init__(self):
        super().__init__('speed_publisher')
        self.publisher_ = self.create_publisher(String, 'rover/speed', 10)
        timer_period = 1.0 
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0.0

    def timer_callback(self):
        msg = String()
        msg.data = '%.1f' % self.i
        self.publisher_.publish(msg)
        self.get_logger().info('"%s"' % msg.data)
        self.i += 0.1

def main(args=None):
    rclpy.init(args=args)

    my_speed_publisher = SpeedPublisher()

    rclpy.spin(my_speed_publisher)

    my_speed_publisher.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

pobranie przykładowego subscriber-a i dostosowanie go

cd src/rover_control/rover_control/

wget https://raw.githubusercontent.com/ros2/examples/humble/rclpy/topics/minimal_subscriber/examples_rclpy_minimal_subscriber/subscriber_member_function.py
import rclpy
from rclpy.node import Node

from std_msgs.msg import String

class SpeedSubscriber(Node):

    def __init__(self):
        super().__init__('speed_subscriber')
        self.subscription = self.create_subscription(
            String,
            'rover/speed',
            self.listener_callback,
            10)
        self.subscription 

    def listener_callback(self, msg):
        self.get_logger().info('"%s"' % msg.data)

def main(args=None):
    rclpy.init(args=args)

    my_speed_subscriber = SpeedSubscriber()

    rclpy.spin(my_speed_subscriber)

    my_speed_subscriber.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
entry_points={
        'console_scripts': [
                'publisher_node = rover_control.publisher_member_function:main',
                'subscriber_node = rover_control.subscriber_member_function:main',
        ],
    },

image

kosiarn commented 3 weeks ago

image coś działa aczkolwiek jeśli ktoś wie dlaczego przy każdej nowej instancji terminala muszę robić source setup.zsh paczki żeby ros ją widział przy ros2 run to byłbym wdzięczny za podzielenie się wiedzą :))))