kalman_filter.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <ros/ros.h>
3 #include <std_msgs/Float32.h>
4 #include <xbot_msgs/Battery.h>
5 using namespace std;
7  {
8 public:
9  kalman_filter(float q,float r)
10  {
11  Q=q;
12  R=r;//如果观测噪声大,我们就不太相信它,将R设大,告诉滤波器不太相信观测值,更相信预测值。
13  A=1.0;
14  H=1.0;
15 
16  pub = nh.advertise<xbot_msgs::Battery>("/mobile_base/sensors/battery_filtered", 1);
17  sub = nh.subscribe("/mobile_base/sensors/battery",3,&kalman_filter::call_back,this);
18  }
19 
20  void call_back(const xbot_msgs::Battery measure)
21  {
22  X=measure;
23  ros::param::get("Q_noise",Q);
24  ros::param::get("R_noise",R);
25  //prediction
26  //x_raw = A*x_last;
27  x_raw = A*x;
28  //p_raw=A*p_last*A+Q;
29  p_raw=A*p*A+Q;
30 // cout<<"Q is :"<<Q<<endl;
31 // cout<<"R is :"<<R<<endl;
32  //measurment
33  K=p_raw*H/(H*p_raw*H+R);
34  x=x_raw+K*(measure.battery_percent-H*x_raw);
35  p=(1-K*H)*p_raw;
36  //update
37  //x_last=x;
38  //p_last=p;
39  //pub
40  X.battery_percent=x;
41 
42  pub.publish(X);
43  }
44 
45 
46 
47 private:
48  float x;
49  float x_raw;
50  //float x_last;
51  float p;
52  float p_raw;
53  //float p_last;
54  float R;
55  float Q;
56  float K;
57  float A;
58  float H;
62  xbot_msgs::Battery X;
63  };
64 
65 int main(int argc,char** argv) {
66  cout<<"Lets start kalmanfilter!\n"<<endl;
67  ros::init(argc,argv,"kalmanfilter");
68  kalman_filter kalman_filter1(0.1,0.75);
69  ros::spin();
70 }
71 
void call_back(const xbot_msgs::Battery measure)
xbot_msgs::Battery X
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Publisher pub
ros::NodeHandle nh
kalman_filter(float q, float r)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
ros::Subscriber sub
ROSCPP_DECL void spin()
int main(int argc, char **argv)


xbot_tools
Author(s):
autogenerated on Sat Oct 10 2020 03:28:22