Skip to content

Latest commit

 

History

History
225 lines (185 loc) · 5 KB

File metadata and controls

225 lines (185 loc) · 5 KB

Práctica 1

Antes de empezar importante:

source devel/setup.bash

Exercise 1

1. Clonar paquete teleop_twist_keyboard_cp

cd ~/catkin_ws/src
git clone https://github.com/methylDragon/teleop_twist_keyboard_cpp.git
cd ..
catkin clean
catkin_make
source devel/setup.bash

Para comprobar que funciona: `rosrun teleop_twist_keyboard_cpp teleop_twist_keyboard

2. Crear un nuevo paquete

  1. Abre Visual Studio y asegúrate de que tienes configurado tu espacio de trabajo de ROS (catkin_ws).
  2. Abre una terminal y navega a la carpeta src de tu espacio de trabajo:
cd ~/catkin_ws/src 
  1. Crea un nuevo paquete llamado first_package sin dependencias:
catkin_create_pkg first_package

3. Agregar la carpeta de lanzamiento

cd first_package
mkdir launch

4. Crear el archivo de lanzamiento

nano launch/start_stage.launch

Escribir:

<launch>
    <node pkg="stage_ros" type="stageros" name="stage" args="$(find first_package)/world/simple.world" />
    <node pkg="teleop_twist_keyboard_cpp" type="teleop_twist_keyboard" name="teleop_keyboard" output="screen">
        <param name="scale_linear" value="1.0"/>
        <param name="scale_angular" value="1.0"/>
    </node>
</launch>

5. Copiar el archivo mundo

Asegúrate de tener el archivo (simple.world) en la carpeta (world) dentro de tu paquete.

6. Ejecutar lanzamiento

roslaunch first_package start_stage.launch

Exercise 2

1. Crear un nuevo paquete con dependencias

cd ~/catkin_ws/src

Crea un nuevo paquete llamado second_package con las dependencias std_msgs, rospy, y roscpp:

catkin_create_pkg second_package std_msgs rospy roscpp

2. Crear archivo (helloWordl.cpp)

cd second_package
cd src
nano helloWorld.cpp

Contiene el siguiente código:

#include <ros/ros.h>
int main(int argc, char** argv)//includes ROS main header file
{
    ros::init(argc, argv, "helloWorld");//should be called before calling other ROS functions
    ros::NodeHandle nodeHandle; //The node handle is the access point for communications
    // with the ROS system (topics, services, parameters)
    ros::Rate loopRate(10); //ros::Rate is a helper class to run loops at a desired frequency
    unsigned int count = 0;
    while (ros::ok()) { //ros::ok() checks if a node should continue running Returns false
        //if Ctrl + C is received or ros::shutdown() has been called
        ROS_INFO_STREAM("Hello World " << count);//ROS_INFO() logs messages to the
        // filesystem
        ros::spinOnce();//ros::spinOnce() processes incoming messages via callbacks
        loopRate.sleep();
        count++;
    }
    return 0;
}

3. Modificar CMakeLists.txt

Añadir/Descomentar lo siguiente

add_executable(helloWorld src/helloWorld.cpp)

target_link_libraries(helloWorld
  ${catkin_LIBRARIES}
)

include_directories(
  include ${catkin_INCLUDE_DIRS}
)

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES second_package
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)

4. Compilar el paquete

cd ~/catkin_ws
catkin_make

5. Ejecutar el nodo

rosrun second_package helloWorld

6. Crear ficheros listener y publisher

Dentro de (~/catkin_ws/src/second_package/src) Fichero publisher.cpp

#include <ros/ros.h>
#include <std_msgs/String.h>

int main(int argc, char** argv)
{
    ros::init(argc, argv, "publisher_node");
    ros::NodeHandle nh; // Default node handle

    ros::Publisher pub = nh.advertise<std_msgs::String>("greetings", 10); // Topic name "greetings"
    ros::Rate loopRate(1); // 1 Hz

    unsigned int count = 0;
    while (ros::ok())
    {
        std_msgs::String msg;
        msg.data = "Autonomous Robot Course " + std::to_string(count);
        
        pub.publish(msg); // Publish the message
        ROS_INFO_STREAM("Published: " << msg.data);

        ros::spinOnce();
        loopRate.sleep();
        count++;
    }
    return 0;
}

Fichero listener.cpp

#include <ros/ros.h>
#include <std_msgs/String.h>

void messageCallback(const std_msgs::String::ConstPtr& msg)
{
    ROS_INFO("Received: %s", msg->data.c_str());
    // Reply to the message
    ROS_INFO("Hello, I am a student!");
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "listener_node");
    ros::NodeHandle nh; // Default node handle

    ros::Subscriber sub = nh.subscribe("greetings", 10, messageCallback); // Subscribe to the "greetings" topic

    ros::spin(); // Keep the node running
    return 0;
}

7. Modificar fichero CMakeLists.txt

add_executable(publisher_node src/publisher.cpp)

add_executable(listener_node src/listener.cpp)

target_link_libraries(publisher_node
  ${catkin_LIBRARIES}
)

target_link_libraries(listener_node
  ${catkin_LIBRARIES}
)

8. Compilar el paquete

cd ~/catkin_ws
catkin_make

9. Ejecutar

En una terminal:

rosrun second_package publisher_node

En otra terminal:

rosrun second_package listener_node

Exercise 3