example_omnibase_odometry.cpp
Go to the documentation of this file.
1 
10 #include <ros/ros.h>
11 #include <geometry_msgs/Twist.h>
12 #include "sensor_msgs/JointState.h"
13 #include "hebiros/FeedbackMsg.h"
14 
15 #include <ros/console.h>
16 
17 using namespace hebiros;
18 
19 // Global Variables
20 sensor_msgs::JointState feedback;
21 sensor_msgs::JointState commands;
22 double rate_of_command = 60;
23 double buffer = 0;
24 bool feedback_init = false;
25 
26 
27 void feedback_callback(sensor_msgs::JointState data) {
28  /* Callback function which keeps up to date with feedback from modules */
29  feedback = data;
30  feedback_init = true;
31 }
32 
33 
34 int main(int argc, char ** argv) {
35 
36  // Initialize ROS node
37  ros::init(argc, argv, "example_omnibase_odometry");
38 
39  ros::NodeHandle node;
40 
41  ros::Rate loop_rate(rate_of_command);
42 
46 
47  /* Update this with the group name for your modules */
48  std::string group_name = "omniGroup";
49 
50  // Create a subscriber to receive feedback from group
51  // Register a callback that keeps the feedback updated
52  ros::Subscriber feedback_subscriber = node.subscribe(
53  "/hebiros/"+group_name+"/feedback/joint_state", 100, feedback_callback);
54 
55  // Create a publisher to post the calculated odometry to a topic
56  ros::Publisher odometry_publisher = node.advertise<geometry_msgs::Twist>(
57  "/hebiros/"+group_name+"/odometry", 100);
58 
59  feedback.position.resize(3);
60  commands.velocity.resize(3);
61 
65 
66  /* Declare variables to be used for calculations */
67  double d0; double d1; double d2;
68  double dx; double dy;
69  double dtheta0; double dtheta1; double dtheta2;
70  sensor_msgs::JointState prevPose;
71  sensor_msgs::JointState currPose;
72  geometry_msgs::Twist output;
73  bool startup_complete = false;
74 
75  /* Base dimensions */
76  double wheelRadius = 0.0762; // m
77  double baseRadius = 0.235; // m (radius from base center to wheel center)
78 
79  /******** MAIN LOOP **********/
80 
81  while (ros::ok()) {
82 
83  if ( !startup_complete ) {
84  /* Get the initial position of the wheels at startup */
85  if (feedback_init == true) {
86  prevPose = feedback;
87  startup_complete = true;
88  }
89 
90  } else {
91  /* get the latest pose for the base */
92  currPose = feedback;
93 
94  /* Determine change in position for each individual wheel */
95  /* Units: meters */
96  d0 = (currPose.position[0] - prevPose.position[0]) * wheelRadius; //wheel1
97  d1 = (currPose.position[1] - prevPose.position[1]) * wheelRadius; //wheel2
98  d2 = (currPose.position[2] - prevPose.position[2]) * wheelRadius; //wheel3
99 
100 
101  /* Use an average from all wheels to determine change in body angle */
102  /* Units: Radians*/
103  dtheta0 = d0 / (baseRadius);
104  dtheta1 = d1 / (baseRadius);
105  dtheta2 = d2 / (baseRadius);
106  output.angular.z += (dtheta0 + dtheta1 + dtheta2) / 3;
107 
108  /* Determine movement of the base in current frame of reference */
109  /* Units: Meters*/
110  dx = (d0/2 + d1/2 - d2) * 2/3;
111  dy = (d0 * (-sqrt(3)/2) + d1 * (sqrt(3)/2)) * 2/3;
112 
113  /* Map movement into the original frame of reference */
114  /* Units: Meters*/
115  output.linear.x += dy * sin(output.angular.z)
116  + dx * cos(output.angular.z);
117 
118  output.linear.y += dy * cos(output.angular.z)
119  - dx * sin(output.angular.z);
120 
121  /* Broadcast the latest odometry estimate in Twist format to node */
122  odometry_publisher.publish(output);
123 
124  /* Store the current pose for comparison to next recorded pose */
125  prevPose = currPose;
126  }
127 
128  ros::spinOnce();
129  loop_rate.sleep();
130  }
131 
132  return 0;
133 }
134 
sensor_msgs::JointState feedback
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void feedback_callback(sensor_msgs::JointState data)
int main(int argc, char **argv)
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
double rate_of_command
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool feedback_init
sensor_msgs::JointState commands
EIGEN_DEVICE_FUNC const CosReturnType cos() const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
EIGEN_DEVICE_FUNC const SinReturnType sin() const
ROSCPP_DECL void spinOnce()


hebiros_advanced_examples
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:13:22