Antes de empezar importante:
source devel/setup.bash
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
- Abre Visual Studio y asegúrate de que tienes configurado tu espacio de trabajo de ROS (
catkin_ws
). - Abre una terminal y navega a la carpeta
src
de tu espacio de trabajo:
cd ~/catkin_ws/src
- Crea un nuevo paquete llamado first_package sin dependencias:
catkin_create_pkg first_package
cd first_package
mkdir launch
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>
Asegúrate de tener el archivo (simple.world) en la carpeta (world) dentro de tu paquete.
roslaunch first_package start_stage.launch
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
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;
}
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
)
cd ~/catkin_ws
catkin_make
rosrun second_package helloWorld
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;
}
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}
)
cd ~/catkin_ws
catkin_make
En una terminal:
rosrun second_package publisher_node
En otra terminal:
rosrun second_package listener_node