Skip to content

Instantly share code, notes, and snippets.

@jdumont0201
jdumont0201 / simple.cpp
Created May 4, 2018 11:12
Minimal ROS node
#include <ros/ros.h>
int main(int argc, char** argv) {
ros::init(argc, argv, "Hello, World");
ros::NodeHandle nh; ROS_INFO("Hello, World!");
ros::spinOnce(); return 0;
}
@jdumont0201
jdumont0201 / minimal.launch
Last active May 4, 2018 11:15
Minimal ROS Node Launcher
<launch>
<!-- ~/catkin_ws/src/my_package/launch/minimal.launch -->
<node pkg="my_package" type="simple" name="HelloWorld" output="screen"></node>
</launch>
@jdumont0201
jdumont0201 / minimal_keep_alive.cpp
Last active May 4, 2018 11:38
Minimal ROS node with keep-alive
#include <ros/ros.h>
int main(int argc, char** argv) {
ros::init(argc, argv, "ObiWan");
ros::NodeHandle nh;
ros::Rate loop_rate(2); // We create a Rate object of 2Hz
while (ros::ok()) { // Endless loop until Ctrl + C
ROS_INFO("Hello, World!");
ros::spinOnce();
loop_rate.sleep(); // We sleep the needed time to maintain the Rate fixed above
}
@jdumont0201
jdumont0201 / minimal_topic_publisher.cpp
Created May 4, 2018 11:17
Minimal ROS topic publisher
#include <ros/ros.h>
#include <std_msgs/Int32.h>
int main(int argc, char** argv) {
ros::init(argc, argv, "topic_publisher");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise&lt;std_msgs::Int32&gt;("counter", 1000);
ros::Rate loop_rate(2);
@jdumont0201
jdumont0201 / robot_speed_change.cpp
Created May 4, 2018 11:18
ROS Robot speed change
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int argc, char** argv) {
ros::init(argc, argv, "topic_publisher"); ros::NodeHandle nh;
ros::Publisher pub = nh.advertise("cmd_vel",1000);
ros::Rate loop_rate(2);
geometry_msgs::Twist twist;
twist.linear.x=0.5;
#include <ros/ros.h>
#include <std_msgs/Int32>
void counterCallback(const std_msgs::Int32::ConstPtr& msg)
{
ROS_INFO("%d", msg->data);
}
int main(int argc, char** argv) {
@jdumont0201
jdumont0201 / laser_sensor_subscriber.cpp
Created May 4, 2018 11:21
ROS Laser sensor subscriber
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
void counterCallback(const sensor_msgs::LaserScan::ConstPtr& msg){
ROS_INFO("LaserScan (val,angle)=(%f,%f", msg->range_min,msg->angle_min);
}
int main(int argc, char** argv) {
ros::init(argc, argv, "topic_subscriber");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("/kobuki/laser/scan", 1000, counterCallback);
@jdumont0201
jdumont0201 / minimal_service_caller.cpp
Created May 4, 2018 11:22
Minimal ROS Service caller
#include "ros/ros.h"
#include "gazebo_msgs/DeleteModel.h"
int main(int argc, char **argv){
ros::init(argc, argv, "service_client"); // Initialise a ROS node with the name service_client
ros::NodeHandle nh;
// Create the connection to the service /gazebo/delete_model
ros::ServiceClient delete_model_service = nh.serviceClient&lt;gazebo_msgs::DeleteModel&gt;("/gazebo/delete_model");
@jdumont0201
jdumont0201 / minimal_service_server.cpp
Created May 4, 2018 11:27
Minimal ROS Service server
#include "ros/ros.h" #include "std_srvs/Empty.h" // Import the service message header file generated from the Empty.srv message
bool my_callback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res){
//process some action
ROS_INFO("My_callback has been called");
return true;
}
int main(int argc, char **argv){
ros::init(argc, argv, "service_server");
@jdumont0201
jdumont0201 / CMakeLists.txt
Last active May 4, 2018 11:29
Custom service message CMake edits
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs message_generation )
...
add_service_files(
FILES
MyCustomServiceMessage.srv
)
...