graft_ukf_velocity.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Author: Chad Rockey
32  */
33 
34 #include <iostream>
35 #include <Eigen/Dense>
36 #include <ros/ros.h>
38 #include <graft/GraftSensor.h>
40 #include <graft/GraftImuTopic.h>
41 #include <graft/GraftUKFVelocity.h>
42 #include <graft/GraftState.h>
44 
46 
49 
50 nav_msgs::Odometry odom_;
51 
52 // tf
55 
56 std::string parent_frame_id_;
57 std::string child_frame_id_;
58 
59 void publishTF(const nav_msgs::Odometry& msg){
60  geometry_msgs::TransformStamped tf;
61  tf.header.stamp = msg.header.stamp;
62  tf.header.frame_id = msg.header.frame_id;
63  tf.child_frame_id = msg.child_frame_id;
64 
65  tf.transform.translation.x = msg.pose.pose.position.x;
66  tf.transform.translation.y = msg.pose.pose.position.y;
67  tf.transform.translation.z = msg.pose.pose.position.z;
68  tf.transform.rotation = msg.pose.pose.orientation;
69 
70  broadcaster_->sendTransform(tf);
71 }
72 
73 void timer_callback(const ros::TimerEvent& event){
74  double dt = ukfv.predictAndUpdate();
75 
76  graft::GraftState state = *ukfv.getMessageFromState();
77  state.header.stamp = ros::Time::now();
78  state_pub.publish(state);
79 
80  odom_.header.stamp = ros::Time::now();
81  odom_.header.frame_id = parent_frame_id_;
82  odom_.child_frame_id = child_frame_id_;
83  odom_.twist.twist.linear.x = state.twist.linear.x;
84  odom_.twist.twist.linear.y = state.twist.linear.y;
85  odom_.twist.twist.angular.z = state.twist.angular.z;
86 
87  // Update Odometry
88  double diff = pow(odom_.pose.pose.orientation.w, 2.0)-pow(odom_.pose.pose.orientation.z, 2.0);
89  double mult = 2.0*odom_.pose.pose.orientation.w*odom_.pose.pose.orientation.z;
90  double theta = atan2(mult, diff);
91  if(std::abs(odom_.twist.twist.angular.z) < 0.00001){ // There's no (or very little) curvature, apply the straight line model
92  odom_.pose.pose.position.x += odom_.twist.twist.linear.x*dt*cos(theta)-odom_.twist.twist.linear.y*dt*sin(theta);
93  odom_.pose.pose.position.y += odom_.twist.twist.linear.x*dt*sin(theta)+odom_.twist.twist.linear.y*dt*cos(theta);
94  } else { // Calculate components of arc distance and add distances.
95  double curvature_x = odom_.twist.twist.linear.x/odom_.twist.twist.angular.z;
96  double curvature_y = odom_.twist.twist.linear.y/odom_.twist.twist.angular.z;
97  double new_theta = theta + odom_.twist.twist.angular.z*dt;
98 
99  odom_.pose.pose.position.x += -curvature_x*sin(theta) + curvature_x*sin(new_theta);
100  odom_.pose.pose.position.x += -curvature_y*cos(theta) + curvature_y*cos(new_theta);
101  odom_.pose.pose.position.y += curvature_x*cos(theta) - curvature_x*cos(new_theta);
102  odom_.pose.pose.position.y += -curvature_y*sin(theta) + curvature_y*sin(new_theta);
103  theta = new_theta;
104  }
105  odom_.pose.pose.orientation.z = sin(theta/2.0);
106  odom_.pose.pose.orientation.w = cos(theta/2.0);
108  if(publish_tf_){
109  publishTF(odom_);
110  }
111 }
112 
113 int main(int argc, char **argv)
114 {
115  ros::init(argc, argv, "graft_ukf_velocity");
116  ros::NodeHandle n;
117  ros::NodeHandle pnh("~");
118  state_pub = pnh.advertise<graft::GraftState>("state", 5);
119  odom_pub = n.advertise<nav_msgs::Odometry>("odom_combined", 5);
120 
121  // Load parameters
122  std::vector<boost::shared_ptr<GraftSensor> > topics;
123  std::vector<ros::Subscriber> subs;
124  GraftParameterManager manager(n, pnh);
125  manager.loadParameters(topics, subs);
126 
127  publish_tf_ = manager.getPublishTF();
128 
130  child_frame_id_ = manager.getChildFrameID();
131 
132  // Set up the E
133  std::vector<double> initial_covariance = manager.getInitialCovariance();
134  std::vector<double> Q = manager.getProcessNoise();
135  ukfv.setAlpha(manager.getAlpha());
136  ukfv.setKappa(manager.getKappa());
137  ukfv.setBeta(manager.getBeta());
138  ukfv.setInitialCovariance(initial_covariance);
140  ukfv.setTopics(topics);
141 
142  odom_.pose.pose.position.x = 0.0;
143  odom_.pose.pose.position.y = 0.0;
144  odom_.pose.pose.position.z = 0.0;
145 
146  odom_.pose.pose.orientation.w = 1.0;
147  odom_.pose.pose.orientation.x = 0.0;
148  odom_.pose.pose.orientation.y = 0.0;
149  odom_.pose.pose.orientation.z = 0.0;
150 
151  odom_.twist.twist.linear.x = 0.0;
152  odom_.twist.twist.linear.y = 0.0;
153  odom_.twist.twist.linear.z = 0.0;
154  odom_.twist.twist.angular.x = 0.0;
155  odom_.twist.twist.angular.y = 0.0;
156  odom_.twist.twist.angular.z = 0.0;
157 
158  // Tf Broadcaster
160 
161  // Start loop
163 
164  // Spin
165  ros::spin();
166 }
GraftUKFVelocity::setKappa
void setKappa(const double kappa)
Definition: GraftUKFVelocity.cpp:319
GraftParameterManager::getChildFrameID
std::string getChildFrameID()
Definition: GraftParameterManager.cpp:338
GraftParameterManager::getBeta
double getBeta()
Definition: GraftParameterManager.cpp:404
ros::Publisher
GraftUKFVelocity::setAlpha
void setAlpha(const double alpha)
Definition: GraftUKFVelocity.cpp:315
GraftUKFVelocity::setBeta
void setBeta(const double beta)
Definition: GraftUKFVelocity.cpp:323
boost::shared_ptr
GraftParameterManager::getInitialCovariance
std::vector< double > getInitialCovariance()
Definition: GraftParameterManager.cpp:380
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
GraftParameterManager.h
ros.h
odom_
nav_msgs::Odometry odom_
Definition: graft_ukf_velocity.cpp:50
GraftParameterManager::loadParameters
void loadParameters(std::vector< boost::shared_ptr< GraftSensor > > &topics, std::vector< ros::Subscriber > &subs)
Definition: GraftParameterManager.cpp:163
GraftUKFVelocity
Definition: GraftUKFVelocity.h:51
ukfv
GraftUKFVelocity ukfv
Definition: graft_ukf_velocity.cpp:45
GraftUKFVelocity.h
GraftParameterManager::getPublishTF
bool getPublishTF()
Definition: GraftParameterManager.cpp:374
GraftParameterManager::getAlpha
double getAlpha()
Definition: GraftParameterManager.cpp:392
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
GraftParameterManager
Definition: GraftParameterManager.h:43
transform_broadcaster.h
publish_tf_
bool publish_tf_
Definition: graft_ukf_velocity.cpp:53
GraftUKFVelocity::setInitialCovariance
void setInitialCovariance(std::vector< double > &P)
Definition: GraftUKFVelocity.cpp:279
GraftImuTopic.h
GraftSensor.h
timer_callback
void timer_callback(const ros::TimerEvent &event)
Definition: graft_ukf_velocity.cpp:73
tf::TransformBroadcaster
child_frame_id_
std::string child_frame_id_
Definition: graft_ukf_velocity.cpp:57
GraftUKFVelocity::setTopics
void setTopics(std::vector< boost::shared_ptr< GraftSensor > > &topics)
Definition: GraftUKFVelocity.cpp:275
state_pub
ros::Publisher state_pub
Definition: graft_ukf_velocity.cpp:47
GraftOdometryTopic.h
parent_frame_id_
std::string parent_frame_id_
Definition: graft_ukf_velocity.cpp:56
ros::TimerEvent
main
int main(int argc, char **argv)
Definition: graft_ukf_velocity.cpp:113
publishTF
void publishTF(const nav_msgs::Odometry &msg)
Definition: graft_ukf_velocity.cpp:59
GraftParameterManager::getProcessNoise
std::vector< double > getProcessNoise()
Definition: GraftParameterManager.cpp:386
broadcaster_
boost::shared_ptr< tf::TransformBroadcaster > broadcaster_
Definition: graft_ukf_velocity.cpp:54
GraftUKFVelocity::predictAndUpdate
double predictAndUpdate()
Definition: GraftUKFVelocity.cpp:236
GraftParameterManager::getKappa
double getKappa()
Definition: GraftParameterManager.cpp:398
GraftParameterManager::getUpdateRate
double getUpdateRate()
Definition: GraftParameterManager.cpp:344
GraftUKFVelocity::getMessageFromState
graft::GraftStatePtr getMessageFromState()
Definition: GraftUKFVelocity.cpp:138
ros::spin
ROSCPP_DECL void spin()
tf
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
ros::Timer
GraftUKFVelocity::setProcessNoise
void setProcessNoise(std::vector< double > &Q)
Definition: GraftUKFVelocity.cpp:297
ros::NodeHandle
odom_pub
ros::Publisher odom_pub
Definition: graft_ukf_velocity.cpp:48
GraftParameterManager::getParentFrameID
std::string getParentFrameID()
Definition: GraftParameterManager.cpp:332
ros::Time::now
static Time now()


graft
Author(s): Chad Rockey
autogenerated on Wed Mar 2 2022 00:20:33