00001
00002
00003
00004
00005
00006
00007
00008 #include <StateEstimators/ViconStateEstimator.hpp>
00009 #include <tk_state/StateEstimatorController.hpp>
00010
00011 #include <telekyb_base/ROS.hpp>
00012
00013 #include <telekyb_msgs/TKState.h>
00014
00015 PLUGINLIB_DECLARE_CLASS(tk_state, ViconStateEstimator, telekyb_state::ViconStateEstimator, TELEKYB_NAMESPACE::StateEstimator);
00016
00017 namespace telekyb_state {
00018
00019 ViconStateEstimatorOptions::ViconStateEstimatorOptions()
00020 : OptionContainer("ViconStateEstimator")
00021 {
00022 tViconSeTopicName = addOption<std::string>("tViconSeTopicName","TopicName of Vicon Sensor.","undef",true,true);
00023 Eigen::Matrix3d m = Eigen::Matrix3d::Zero();
00024 m.diagonal() << 1.0,-1.0,-1.0;
00025 tViconToNEDMatrix = addOption<Eigen::Matrix3d>("tViconToNEDMatrix","ConversionMatrix from Vicon to NED", m, false, true);
00026
00027 tViconVelFilterFreq = addOption<double>("tViconVelFilterFreq",
00028 "Frequency of Velocity Filter (Initial)", 40.0, false, true);
00029 tViconSmoothVelFilterFreq = addOption<double>("tViconSmoothVelFilterFreq",
00030 "Frequency of Smooth Velocity Filter (Initial)", 10.0, false, true);
00031 tViconAngFilterFreq = addOption<double>("tViconAngFilterFreq",
00032 "Frequency of Angular Velocity Filter (Initial)", 30.0, false, true);
00033
00034 tViconSampleTime = addOption<double>("tViconSampleTime",
00035 "Sampling Time of Vicon System. Default is 120Hz", 0.008333333333, false, true);
00036
00037 tViconPublishSmoothVel = addOption<bool>("tViconPublishSmoothVel",
00038 "Publish TKState with smoothed Velocity", false, false, true);
00039 }
00040
00041 void ViconStateEstimator::initVelocityFilters()
00042 {
00043 IIRFiltDeriv isDerivative;
00044 for(int i=0;i<3;i++){
00045 velFilter[i] = new IIRFilter(
00046 isDerivative,
00047 2.0*M_PI*options.tViconVelFilterFreq->getValue(),
00048 1.0,
00049 options.tViconSampleTime->getValue());
00050 }
00051
00052 for(int i=0;i<3;i++){
00053 smoothVelFilter[i] = new IIRFilter(
00054 isDerivative,
00055 2.0*M_PI*options.tViconSmoothVelFilterFreq->getValue(),
00056 1.0,
00057 options.tViconSampleTime->getValue());
00058 }
00059
00060
00061 for(int i=0;i<4;i++){
00062 angFilter[i] = new IIRFilter(
00063 isDerivative,
00064 2.0*M_PI*options.tViconAngFilterFreq->getValue(),
00065 1.0,
00066 options.tViconSampleTime->getValue());
00067 }
00068 }
00069
00070
00071 void ViconStateEstimator::velQuatToBodyOmega(const Quaternion& quat, const Quaternion& quatRates, Velocity3D& bodyOmega)
00072 {
00073 Eigen::Matrix<double, 3, 4> conversionMatrix;
00074 conversionMatrix << -quat.x() , quat.w(), quat.z(), -quat.y(),
00075 -quat.y() , -quat.z(), quat.w(), quat.x(),
00076 -quat.z() , quat.y(), -quat.x(), quat.w();
00077
00078 Eigen::Vector4d quatRatesVec;
00079 quatRatesVec(0) = quatRates.w();
00080 quatRatesVec.tail<3>() = quatRates.vec();
00081
00082 bodyOmega = (2.0 * conversionMatrix) * quatRatesVec;
00083
00084 }
00085
00086 void ViconStateEstimator::initialize()
00087 {
00088 nodeHandle = ROSModule::Instance().getMainNodeHandle();
00089
00090 initVelocityFilters();
00091 }
00092 void ViconStateEstimator::willBecomeActive()
00093 {
00094 viconSub = nodeHandle.subscribe<geometry_msgs::PoseStamped>(
00095 options.tViconSeTopicName->getValue(),1, &ViconStateEstimator::viconCallback, this);
00096
00097 if (options.tViconPublishSmoothVel->getValue()) {
00098 smoothVelPub = nodeHandle.advertise<telekyb_msgs::TKState>("SmoothedVelocity",1);
00099 }
00100 }
00101 void ViconStateEstimator::willBecomeInActive()
00102 {
00103 smoothVelPub.shutdown();
00104 viconSub.shutdown();
00105 }
00106
00107 void ViconStateEstimator::destroy()
00108 {
00109 for(int i=0;i<3;i++){
00110 delete velFilter[i];
00111 delete smoothVelFilter[i];
00112 }
00113
00114 for(int i=0;i<4;i++){
00115 delete angFilter[i];
00116 }
00117 }
00118
00119 std::string ViconStateEstimator::getName() const
00120 {
00121
00122 return "ViconStateEstimator";
00123 }
00124
00125 void ViconStateEstimator::viconCallback(const geometry_msgs::PoseStamped::ConstPtr& msg)
00126 {
00127
00128
00129 Position3D posVicon(msg->pose.position.x, msg->pose.position.y, msg->pose.position.z);
00130 Quaternion quatVicon(msg->pose.orientation.w, msg->pose.orientation.x, msg->pose.orientation.y, msg->pose.orientation.z);
00131
00132 Position3D posNED = options.tViconToNEDMatrix->getValue() * posVicon;
00133
00134
00135
00136
00137
00138 double output[3],outputMore[3];
00139 std::vector<double> input(1);
00140 input[0]=posNED(0);
00141 velFilter[0]->step(input, output[0]);
00142 smoothVelFilter[0]->step(input, outputMore[0]);
00143 input[0] = posNED(1);
00144 velFilter[1]->step(input, output[1]);
00145 smoothVelFilter[1]->step(input, outputMore[1]);
00146 input[0] = posNED(2);
00147 velFilter[2]->step(input, output[2]);
00148 smoothVelFilter[2]->step(input, outputMore[2]);
00149 Velocity3D velNED(output);
00150 Velocity3D smoothVelNED(outputMore);
00151
00152
00153
00154
00155
00156
00157 double outputQuat[4];
00158
00159 input[0] = quatVicon.w();
00160 angFilter[0]->step(input, outputQuat[0]);
00161 input[0] = quatVicon.x();
00162 angFilter[1]->step(input, outputQuat[1]);
00163 input[0] = quatVicon.y();
00164 angFilter[2]->step(input, outputQuat[2]);
00165 input[0] = quatVicon.z();
00166 angFilter[3]->step(input, outputQuat[3]);
00167
00168
00169 Quaternion quatRates(outputQuat[0], outputQuat[1], outputQuat[2], outputQuat[3]);
00170
00171
00172 Velocity3D angVelocity;
00173 velQuatToBodyOmega(quatVicon, quatRates, angVelocity);
00174
00175 Quaternion quatNED(quatVicon);
00176 quatNED.vec() = options.tViconToNEDMatrix->getValue() * quatNED.vec();
00177
00178
00179
00180 Velocity3D angVelocityNED = options.tViconToNEDMatrix->getValue() * angVelocity;
00181
00182
00183
00184
00185
00186 TKState tStateMsg;
00187 tStateMsg.time = Time(msg->header.stamp);
00188 tStateMsg.position = posNED;
00189 tStateMsg.linVelocity = velNED;
00190 tStateMsg.orientation = quatNED;
00191 tStateMsg.angVelocity = angVelocityNED;
00192
00193
00194 stateEstimatorController.activeStateCallBack(tStateMsg);
00195
00196
00197 if (options.tViconPublishSmoothVel->getValue()) {
00198 telekyb_msgs::TKState smoothVelMsg;
00199 tStateMsg.toTKStateMsg(smoothVelMsg);
00200 smoothVelMsg.header.stamp = ros::Time::now();
00201 smoothVelMsg.twist.linear.x = smoothVelNED(0);
00202 smoothVelMsg.twist.linear.y = smoothVelNED(1);
00203 smoothVelMsg.twist.linear.z = smoothVelNED(2);
00204 smoothVelPub.publish(smoothVelMsg);
00205 }
00206 }
00207
00208 }