Arduino-ROS, example - Odometry Publisher

In this example, we will see how to send an odom message from an Arduino node to a PC. This example can be used in a robot for computing odom and send to ROS Navigation stack as the input. The motor encoders can be used for computing odom and can send to PC. In this example, we will see how to send odom for a robot which is moving in a circle, without taking the motor encoder values.

/*  
 * rosserial Planar Odometry Example 
 */ 
 
#include <ros.h> 
#include <ros/time.h> 
#include <tf/tf.h> 
#include <tf/transform_broadcaster.h> 
 
ros::NodeHandle  nh; 
//Transform broadcaster object 
geometry_msgs::TransformStamped t; 
tf::TransformBroadcaster broadcaster; 
 
double x = 1.0; 
double y = 0.0; 
double theta = 1.57; 
 
char base_link[] = "/base_link"; 
char odom[] = "/odom"; 
 
void setup() 
{ 
  nh.initNode(); 
  broadcaster.init(nh); 
} 
 
void loop() 
{   
  // drive in a circle 
  double dx = 0.2; 
  double dtheta = 0.18; 
 
  x += cos(theta)*dx*0.1; 
  y += sin(theta)*dx*0.1; 
  theta += dtheta*0.1; 
 
  if(theta > 3.14) 
    theta=-3.14; 
     
  // tf odom->base_link 
  t.header.frame_id = odom; 
  t.child_frame_id = base_link; 
   
  t.transform.translation.x = x; 
  t.transform.translation.y = y; 
   
  t.transform.rotation = tf::createQuaternionFromYaw(theta); 
  t.header.stamp = nh.now(); 
   
  broadcaster.sendTransform(t); 
  nh.spinOnce(); 
   
  delay(10); 
} 

After uploading the code, run roscore and rosserial_node.py. We can view tf and odom in RViz. Open RViz and view the tf as shown next. We will see the odom pointer moving in a circle on RViz as follows:

Figure 16 : Visualizing odom data from Arduino
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.212.102