odometry.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 
33 #include <atlascar_base/odometry.h>
34 #include <sensor_msgs/Imu.h>
35 #include "geometry_msgs/Vector3Stamped.h"
36 #include "geometry_msgs/QuaternionStamped.h"
37 #include "geometry_msgs/Pose.h"
38 #include <sensor_msgs/JointState.h>
39 
40 #include "geometry_msgs/Pose.h"
41 #include "geometry_msgs/Quaternion.h"
42 #include "tf/transform_datatypes.h"
43 
44 //SAVE FILE
45 #include <stdio.h>
46 #include <fstream>
47 #include <iostream>
48 // FILE * pFile;
49 
50 #define PFLN {printf("%s %d\n",__FILE__, __LINE__);}
52 atlascar_base::AtlascarStatus base_status;
53 atlascar_base::AtlascarVelocityStatus base_velocity;
54 bool message_receive=false;
55 bool bool_init=true;
56 ros::Time t_i, t_i_1;
58 
59 void VelocityMessageHandler(const atlascar_base::AtlascarVelocityStatus& msg)
60 {
61  base_velocity=msg;
62  new_status_message=true;
63  if(bool_init)
64  {
65  t_i_1=msg.header.stamp;
66  t_i=msg.header.stamp;
67  bool_init=false;
68  }else
69  {
70  t_i_1=t_i;
71  t_i=msg.header.stamp;
72  }
73 }
74 void StatusMessageHandler(const atlascar_base::AtlascarStatus& msg)
75 {
76  base_status=msg;
77 }
78 
79 int main(int argc, char** argv)
80 {
81  ros::init(argc, argv, "atlascar_odometry");
82  ros::NodeHandle n;
83  ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("/vhc/odometry", 50);
84 // ros::Publisher odom_pub_no_filter = n.advertise<nav_msgs::Odometry>("/atc/base/odometry_no_filter", 50);
85  tf::TransformBroadcaster odom_broadcaster;
86 
87 // Subscriber subscribeStatusMessages = n.subscribe ("/atc/base/status/plc", 1, StatusMessageHandler);
88  Subscriber subscribeStatusMessages = n.subscribe ("/vhc/plc/status", 1, StatusMessageHandler);
89  Subscriber subscribeVelocityStatusMessages = n.subscribe ("/vhc/velocity/status", 1, VelocityMessageHandler);
90 // Subscriber subscribeVelocityStatusMessages = n.subscribe ("/velocity_status", 1, VelocityMessageHandler);
91 
92  double x = 0.0;
93  double y = 0.0;
94  double yaw = 0.0;
95 
96  double vx = 0.0;
97  double vy = 0.0;
98  double vth = 0.0;
99 
100  double vl,phi;
101  double l;
102 
103  n.param("wheel_base",l,DEFAULT_WHEEL_BASE);
104 
105  new_status_message=false;
106 
107  ros::Rate r(20.0);
108 
109  ROS_INFO("Starting to spin ...");
110 
111  //KALMAN FILTER
112  non_holonomic_ekfilter filter;
113 
114  Vector z_i(2);
115  Vector u(0);
116  filter.SetWheelBase(l);
117 
118  nav_msgs::Odometry odom;
119 
120  double phi_tmp=0;
121 
122  while(n.ok())
123  {
124  spinOnce();
125  r.sleep();
126 
127  if(!new_status_message)
128  continue;
129 
130  new_status_message=false;
131  message_receive=false;
132 
133 
134  vl=base_velocity.velocity;
135  phi=base_status.steering_wheel;
136 
137 
138  if (phi ==0)
139  phi=phi_tmp;
140  else
141  phi_tmp=phi;
142 
143 
144  phi= phi +0.28801; //liceu
145 
146  if (phi <=0)
147  {
148  phi = phi*0.766;
149  phi=phi*-1;
150  }
151  else if (phi>0)
152  {
153  phi= phi*0.770;
154  phi=phi*-1;
155  }
156  //corrrection velocity car like robot
157  double w=1.27;
158  double instant_radious=fabs(l/tan(phi));
159  double v_corrected;
160  if(phi>=0)
161  v_corrected=vl*(instant_radious/(instant_radious-w/2));
162  else
163 
164  v_corrected=vl*(instant_radious/(instant_radious+w/2));
165  //kalmen input measures
166  z_i(1)=v_corrected;
167  z_i(2)=phi;
168 
169 
170 
171  //v
172  double dt = (t_i - t_i_1).toSec();
173  double delta_x;
174  double delta_y;
175  double delta_yaw;
176 
177 
178  delta_x=cos(yaw)*cos(phi)*v_corrected*dt;
179  delta_y=sin(yaw)*cos(phi)*v_corrected*dt;
180  //delta_yaw=sin(phi/l)*v_corrected*dt; //erro no l
181  delta_yaw=sin(phi)*v_corrected*dt/l;
182 
183 
184  x += delta_x;
185  y += delta_y;
186  yaw += delta_yaw;
187 
188 //
189  //KALMAN FILTER
190  filter.SetTimeInterval(dt);
191  filter.step(u,z_i);
192 
193  Vector x_estimated=filter.getX();
194  Matrix covariance_matrix =filter.calculateP();
195  //end KALMAN FILTER
196 
197  //since all odometry is 6DOF we'll need a quaternion created from yaw
198  geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(x_estimated(3));
199 //
200 // //first, we'll publish the transform over tf
201 // geometry_msgs::TransformStamped odom_trans;
202 // odom_trans.header.stamp = t_i;
203 //
204 // odom_trans.header.frame_id = "/world";
205 // odom_trans.child_frame_id = "/atc/vehicle/rear_axis";
206 //
207 // odom_trans.transform.translation.x = x_estimated(1);
208 // odom_trans.transform.translation.y = x_estimated(2);
209 // odom_trans.transform.translation.z = 0.0;
210 // odom_trans.transform.rotation = odom_quat;
211 
212  //file save to file
213 
214 
215  //send the transform
216 // odom_broadcaster.sendTransform(odom_trans);
217 
218  //next, we'll publish the odometry message over ROS
219  nav_msgs::Odometry odom;
220  odom.header.stamp =t_i;
221  odom.header.frame_id = "/world";
222 
223  //set the position
224  odom.pose.pose.position.x = x_estimated(1);
225  odom.pose.pose.position.y = x_estimated(2);
226  odom.pose.pose.position.z = 0.0;
227  odom.pose.pose.orientation = odom_quat;
228  odom.pose.covariance[0]=covariance_matrix(1,1);
229  odom.pose.covariance[7]=covariance_matrix(2,2);
230  odom.pose.covariance[35]=covariance_matrix(3,3);
231  //cout << "possss " << odom.pose.pose.position.x << endl;
232 
233  //set the velocity
234  odom.child_frame_id = "/atc/vehicle/rear_axis";
235  odom.twist.twist.linear.x = vx;
236  odom.twist.twist.linear.y = vy;
237  odom.twist.twist.angular.z = vth;
238 
239 
240  //publish the message
241  odom_pub.publish(odom);
242 
243 
244 
245 
246  /*
247  //no filter
248  nav_msgs::Odometry odom_no_filter;
249  odom_no_filter.header.stamp =t_i;
250  odom_no_filter.header.frame_id = "/world";
251 
252  //set the position
253  odom_no_filter.pose.pose.position.x = x;
254  odom_no_filter.pose.pose.position.y = y;
255  odom_no_filter.pose.pose.position.z = 0.0;
256  odom_no_filter.pose.pose.orientation = tf::createQuaternionMsgFromYaw(yaw);
257  odom_no_filter.pose.covariance[0]=covariance_matrix(1,1);
258  odom_no_filter.pose.covariance[7]=covariance_matrix(2,2);
259  odom_no_filter.pose.covariance[35]=covariance_matrix(3,3);
260 
261 
262  //set the velocity
263  odom.child_frame_id = "/atc/vehicle/rear_axis";
264  odom.twist.twist.linear.x = vx;
265  odom.twist.twist.linear.y = vy;
266  odom.twist.twist.angular.z = vth;
267 
268 
269  //publish the message
270  odom_pub_no_filter.publish(odom_no_filter);*/
271  }
272 }
int contador_1
Definition: odometry.cpp:57
bool new_status_message
Definition: odometry.cpp:51
The documentation of this file is a responsibility of its current developer, Pedro Salvado...
void SetWheelBase(double l_)
Definition: odometry.h:87
atlascar_base::AtlascarVelocityStatus base_velocity
Definition: odometry.cpp:53
bool bool_init
Definition: odometry.cpp:55
#define DEFAULT_WHEEL_BASE
Definition: odometry.h:38
bool message_receive
Definition: odometry.cpp:54
void VelocityMessageHandler(const atlascar_base::AtlascarVelocityStatus &msg)
Definition: odometry.cpp:59
non_holonomic_ekfilter::Matrix Matrix
Definition: odometry.h:284
atlascar_base::AtlascarStatus base_status
Definition: odometry.cpp:52
ros::Time t_i_1
Definition: odometry.cpp:56
int contador_
Definition: odometry.cpp:57
ros::Time t_i
Definition: odometry.cpp:56
void SetTimeInterval(double dt_)
Definition: odometry.h:93
void StatusMessageHandler(const atlascar_base::AtlascarStatus &msg)
Definition: odometry.cpp:74
int main(int argc, char **argv)
Definition: odometry.cpp:79
non_holonomic_ekfilter::Vector Vector
Definition: odometry.h:283


atlascar_base
Author(s): Jorge Almeida, Sérgio Pinho, Miguel Oliveira, Pedro Salvado, Andre Oliveira and Pedro Pinheiro
autogenerated on Mon Mar 2 2015 01:31:23