Created
December 13, 2018 17:00
-
-
Save TheProjectsGuy/17595286594fc9dc0fb2bb31a7ee515e to your computer and use it in GitHub Desktop.
RViZ Tutorial 1 using sensor_msgs/Temperature in ROS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <launch> | |
| <!-- Arguments to load --> | |
| <arg name="rvizconfig" default="$(find intro_pkg1)/rviz/TempConfig.rviz" doc="Pass the path and full name to RViZ configuration file" /> | |
| <!-- Nodes to launch --> | |
| <node name="TempSensor" pkg="intro_pkg1" type="TempSimSensor" /> | |
| <node name="rviz" pkg="rviz" type="rviz" args="-d $(arg rvizconfig)" required="true" /> | |
| </launch> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ros/ros.h" | |
| #include "std_msgs/Header.h" | |
| #include "sensor_msgs/Temperature.h" | |
| int main(int argc, char **argv) { | |
| // Initialize the ROS node | |
| ros::init(argc, argv, "TemperatureSensor", ros::init_options::AnonymousName); | |
| // Node handler | |
| ros::NodeHandle nodeHandler; | |
| // Publisher object | |
| ros::Publisher publisherObject = nodeHandler.advertise<sensor_msgs::Temperature>("temp_readings", 5); | |
| // Rate handler (5 Hz) | |
| ros::Rate rateHandler = ros::Rate(5); | |
| // Buffer variables | |
| float tempValue = 30.0; | |
| bool increment = true; | |
| // Publishing message | |
| sensor_msgs::Temperature msg; | |
| // Default properties | |
| msg.header.frame_id = "source"; | |
| msg.variance = 0.05; | |
| while(ros::ok()) { | |
| // Assign it time | |
| msg.header.stamp = ros::Time::now(); | |
| // Temperature value | |
| msg.temperature = tempValue; | |
| ROS_DEBUG("Temperature value : %f", tempValue); | |
| // Modify the temperature value | |
| tempValue += (increment) ? 0.1 : -0.1; | |
| if (tempValue >= 50 || tempValue <= 20) { | |
| increment = !increment; | |
| } | |
| // Publish the message | |
| publisherObject.publish(msg); | |
| // Increase sequence number | |
| msg.header.seq++; | |
| // Sleep for the rate satisfaction | |
| rateHandler.sleep(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment