position_controller.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include "quad_can_driver/Attitude.h"
00003 #include <geometry_msgs/Vector3Stamped.h>
00004 #include "px_comm/OpticalFlow.h"
00005 
00006 #define PI 3.14159256
00007 
00008 double K;
00009 quad_can_driver::Attitude att_msg;
00010 ros::Publisher att_pub, OF_pub;
00011 
00012 //Low Pass Filter Variables
00013 bool initialized = false;
00014 
00015 typedef struct{
00016     ros::Time time_last;
00017     double last_x;
00018     double last_y;
00019     ros::Time time_current;
00020     double current_x;
00021     double current_y;
00022 } OF_str;
00023 
00024 OF_str OF;
00025 
00026 double Ts =0.01;
00027 double tau = 0.2;
00028 double alpha = Ts/tau;
00029 px_comm::OpticalFlow OF_Fil;
00030 
00031 
00032 
00033 /*void OFCallback(geometry_msgs::Vector3Stamped const &msg){
00034 
00035     double theta_d = -K*msg.vector.x;
00036     double phi_d = K*msg.vector.y;
00037 
00038     ROS_INFO("Phi_d: %f   Theta_d: %f", phi_d, theta_d);
00039 
00040     if(abs(theta_d)>0.068){
00041         theta_d=0.068*(theta_d/abs(theta_d));
00042     }
00043 
00044     if(abs(phi_d)>0.068){
00045         phi_d=0.068*(phi_d/abs(phi_d));
00046     }
00047 
00048     att_msg.pitch=theta_d;
00049     att_msg.roll=phi_d;
00050 
00051     att_pub.publish(att_msg);
00052 }*/
00053 
00054 void PX4Callback(px_comm::OpticalFlow const &msg){
00055 
00056     if(!initialized){
00057         OF.last_x = msg.velocity_x;
00058         OF.last_y = msg.velocity_y;
00059         OF.current_x = OF.last_x;
00060         OF.current_y = OF.last_y;
00061         initialized = true;
00062     } else{
00063 
00064         OF.last_x = OF.current_x;
00065         OF.last_y = OF.current_y;
00066 
00067         //Low Pass Filter
00068         OF.current_x = OF.last_x+alpha*(msg.velocity_x-OF.last_x);
00069         OF.current_y = OF.last_y+alpha*(msg.velocity_y-OF.last_y);
00070         OF_Fil.header.stamp=ros::Time::now();
00071         OF_Fil.velocity_x = OF.current_x;
00072         OF_Fil.velocity_y = OF.current_y;
00073         OF_pub.publish(OF_Fil);
00074 
00075         double theta_d = K*OF.current_x;
00076         double phi_d = K*OF.current_y;
00077 
00078         /*double theta_d = K*msg.velocity_x;
00079         double phi_d = K*msg.velocity_y;*/
00080 
00081         /*if(abs(theta_d)>0.068){
00082             theta_d=0.068*(theta_d/abs(theta_d));
00083         }
00084 
00085         if(abs(phi_d)>0.068){
00086             phi_d=0.068*(phi_d/abs(phi_d));
00087         }*/
00088 
00089         ROS_INFO("Phi_d: %f   Theta_d: %f", phi_d, theta_d);
00090         att_msg.pitch=theta_d;
00091         att_msg.roll=phi_d;
00092 
00093         att_pub.publish(att_msg);
00094     }
00095 }
00096 
00097 int main(int argc, char **argv){
00098 
00099     ros::init(argc, argv, "quad_position");
00100     ros::NodeHandle n, nh("~");
00101 
00102     //Read Parameters
00103     nh.param("P_gain", K, 0.16);
00104 
00105     //Subscribing
00106     //ros::Subscriber sub1 = n.subscribe("/quad_OF/O_Flow", 10, OFCallback);
00107     ros::Subscriber sub2 = n.subscribe("/px4flow/opt_flow", 1, PX4Callback);
00108 
00109     //Publishing
00110     att_pub = n.advertise<quad_can_driver::Attitude>("AttitudeCtrl", 1);
00111     OF_pub = n.advertise<px_comm::OpticalFlow>("/opt_flow_filt", 1);
00112 
00113     ros::spin();
00114 
00115     return 0;
00116 }


quad_position
Author(s): Henrique
autogenerated on Mon Jan 6 2014 11:48:32