communication_node_pid_semitrajectorycontrol3.cpp
Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <stdio.h>
00005 #include "udp.h"
00006 #include "type.h"
00007 #include "util.h"
00008 #include "geometry_msgs/Twist.h"
00009 #include "geometry_msgs/PoseStamped.h"
00010 #include "std_msgs/Char.h"
00011 #include <pthread.h>
00012 #include <ctype.h>    /* For tolower() function */
00013 #include <math.h>
00014 #include <ros/ros.h>
00015 #include <tf/transform_listener.h>
00016 
00017 class CommunicationToDrone
00018 {
00019 public:
00020   
00021   CommunicationToDrone()
00022   {
00023     //Topic you want to subscribe
00024     sub_ = n_.subscribe("Robot_1/pose", 1, &CommunicationToDrone::Pose_Callback, this);
00025     mannualsub_ = n_.subscribe("cmd_vel", 1, &CommunicationToDrone::cmd_vel_Callback, this);
00026 
00027     //if(udpClient_Init(&udp,"192.168.43.100",7777)) diep("udpClient_Init");
00028     if(udpClient_Init(&udp,"192.168.1.1",7777)) diep("udpClient_Init");
00029 
00030     current_time = ros::Time::now();
00031     last_time = ros::Time::now();
00032   }
00033 
00034   void cmd_vel_Callback(const geometry_msgs::Twist& msg)
00035   {
00036     //n_.getParam("/mode", modeset);
00037     //printf("mode [%s] \n",modeset.c_str()); 
00038     if (n_.getParam("/mode", modeset))
00039     {
00040       //ROS_INFO("Got param: %s", modeset.c_str());
00041     }
00042     else
00043     {
00044       ROS_ERROR("Failed to get param 'my_param'");
00045     }
00046     vztglobal = msg.linear.z;
00047 
00048     if (modeset == "0") // Manual Control
00049     { 
00050         ROS_INFO("mode 0");
00051         // udp_struct udp;
00052         int msglen;
00053         char buf[2048];
00054         float ext = 0.0;
00055         float eyt = 0.0;
00056         float ezt = 0.0;
00057         float evxt = msg.linear.x;
00058         float evyt = msg.linear.y;
00059         float evzt = msg.linear.z;
00060         float axt = 0.0;
00061         float ayt = 0.0;
00062         float azt = 0.0;
00063         float yawratet = msg.angular.z;
00064         //printf("Pose x [%.5f] \n",QuadrotorPose.position.x);
00065   
00066         //msglen=sprintf(buf, (char*)" %.3f,%.3f,%.3f,%.3f ", updown,frontback,leftright,yawrotate);
00067         msglen=sprintf(buf, (char*)" %.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%s ",ext,eyt,ezt,evxt,evyt,evzt,axt,ayt,azt,yawratet,modeset.c_str());
00068         if (udpClient_Send(&udp, buf, msglen)) diep("send");
00069         //printf(" %.3f,%.3f,%.3f,%.3f \n",frontback,leftright,yawrotate,updown);   
00070         //printf("xt %.3f, yt %.3f, zt %.3f, vxt %.3f, vyt %.3f, vzt %.3f, axt %.3f, ayt %.3f, azt %.3f, rollt %.3f, pitcht %.3f, yawt %.3f, rollratet %.3f, pitchratet %.3f, yawratet %.3f \n", xt,yt,zt,vxt,vyt,vzt,axt,ayt,azt,rollt,pitcht,yawt,rollratet,pitchratet,yawratet);
00071         }
00072   }
00073 
00074   void Pose_Callback(const geometry_msgs::PoseStamped& msgPose) // Trajectory Control
00075   {
00076     //n_.getParam("/mode", modeset);
00077     //printf("mode [%s] \n",modeset.c_str()); 
00078     if (n_.getParam("/mode", modeset))
00079     {
00080       //ROS_INFO("Got param: %s", modeset.c_str());
00081     }
00082     else
00083     {
00084       ROS_ERROR("Failed to get param 'my_param'");
00085     }
00086 
00087     if (modeset == "1")
00088     { 
00089         //ROS_INFO("mode 1");
00090         tf::StampedTransform transform;
00091         try{
00092                 listener.lookupTransform("/Robot_1/base_link","/goal",   
00093                                ros::Time(0), transform);
00094         }
00095         catch (tf::TransformException ex){
00096                 ROS_ERROR("%s",ex.what());
00097                 ros::Duration(1.0).sleep();
00098         }
00099     
00100         
00101         // udp_struct udp;
00102         int msglen;
00103         char buf[2048];
00104         float ex = transform.getOrigin().x(); //msgPose.Position.x; w.r.t robot frame
00105         float ey = transform.getOrigin().y(); //msgPose.Position.y;
00106         float ez = transform.getOrigin().z();
00107         //float ezt = 0;
00108         current_time = ros::Time::now();
00109         float dt = (current_time - last_time).toSec();
00110         geometry_msgs::Twist twistframes;
00111         try{
00112                 listener.lookupTwist("/Robot_1/base_link","/goal",   
00113                                ros::Time(0), ros::Duration(dt), twistframes);
00114         }
00115         catch (tf::TransformException ex){
00116                 ROS_ERROR("%s",ex.what());
00117                 ros::Duration(1.0).sleep();
00118         }
00119         last_time = current_time;
00120         float evx = twistframes.linear.x;       
00121         float evy = twistframes.linear.y;
00122         float evz = twistframes.linear.z;
00123         //float evzt = vztglobal;
00124         //ROS_INFO("evt[%.3f,%.3f,%.3f] dt%.3f,", evx,evy,evz,dt); 
00125          
00126         
00127         //float vzt = vztglobal; 
00128         float axt = 0.0;
00129         float ayt = 0.0;
00130         float azt = 0.0;
00131         float yawratet = 1 * tf::getYaw(transform.getRotation());
00132 
00133         //ROS_INFO("ert[%.3f,%.3f,%.3f,%.3f]", ex,ey,ez,yawratet);
00134 
00135          
00136         float F[3] = {0.0,0.0,0.0}; 
00137   
00138         F[1] =  0.7 * ex + 0.005 * evx + axt; //0.005
00139         F[2] =  -0.7 * ey - 0.005 * evy + ayt;
00140         F[3] = 0.72 + (0.1 * ez) + (0.000 * evz) + azt; //4.03191;.
00141 
00142         if(F[1]>=0.2)
00143         {
00144                 F[1] = 0.2;
00145         }
00146         if(F[1]<=-0.2)
00147         {
00148                 F[1] = -0.2;
00149         }
00150         if(F[2]>=0.2)
00151         {
00152                 F[2] = 0.2;
00153         }
00154         if(F[2]<=-0.2)
00155         {
00156                 F[2] = -0.2;
00157         }
00158 
00159         if(F[3]>=0.95)
00160         {
00161                 F[3] = 0.95;
00162         }
00163         if(yawratet>=0.1)
00164         {
00165                 yawratet = 0.1;
00166         }
00167         if(yawratet<=-0.1)
00168         {
00169                 yawratet = -0.1;
00170         }
00171 
00172         ROS_INFO("dt%.3f_F[%.5f,%.5f,%.5f]_ert[%.3f,%.3f,%.3f,%.3f]_evt[%.3f,%.3f,%.3f]",dt,F[1],F[2],F[3],ex,ey,ez,yawratet,evx,evy,evz);
00173 
00174         //printf("Pose x [%.5f] \n",QuadrotorPose.position.x);
00175         //printf("Pose x [%.5f] \n",xt);
00176         //printf("Pose y [%.5f] \n",yt);
00177         //printf("Pose vzt [%.5f] \n",vzt);
00178         //ROS_INFO("Pose yaw [%.5f] \n",yawt);
00179         //msglen=sprintf(buf, (char*)" %.3f,%.3f,%.3f,%.3f ", updown,frontback,leftright,yawrotate);
00180         msglen=sprintf(buf, (char*)" %.3f,%.3f,%.3f,%.3f,%s ",F[3],F[1],F[2],yawratet,modeset.c_str());
00181         if (udpClient_Send(&udp, buf, msglen)) diep("send");
00182         //printf(" %.3f,%.3f,%.3f,%.3f \n",frontback,leftright,yawrotate,updown);
00183         //printf("xt %.3f, yt %.3f, zt %.3f, vxt %.3f, vyt %.3f, vzt %.3f, axt %.3f, ayt %.3f, azt %.3f, rollt %.3f, pitcht %.3f, yawt %.3f, rollratet %.3f, pitchratet %.3f, yawratet %.3f \n", xt,yt,zt,vxt,vyt,vzt,axt,ayt,azt,rollt,pitcht,yawt,rollratet,pitchratet,yawratet);
00184         }
00185   }
00186 
00187   void diep(const char *s)
00188   {
00189     perror(s);
00190     exit(1);
00191   }
00192 
00193 private:
00194   udp_struct udp;
00195   float vztglobal;
00196   float pre_ext;
00197   float pre_eyt;
00198   float pre_ezt;
00199   ros::Time current_time;
00200   ros::Time last_time;
00201 
00202   ros::NodeHandle n_; 
00203   ros::Publisher pub_;
00204   ros::Subscriber sub_;
00205   ros::Subscriber mannualsub_;
00206   //ros::NodeHandle nh;
00207   std::string modeset;
00208   //modeset = "0";
00209   //geometry_msgs::Pose QuadrotorPose;
00210   //
00211   tf::TransformListener listener;
00212 
00213 };//End of class SubscribeAndPublish
00214 
00215 int main(int argc, char **argv)
00216 {
00217 
00218   //Initiate ROS
00219   ros::init(argc, argv, "ardrone_ground_node_trajectorycontrol");
00220   // Broadcast a simple log message
00221   ROS_INFO_STREAM("Start ROS node!");
00222 
00223   //Create an object of class CommunicationToDrone that will take care of everything
00224   CommunicationToDrone CommunicationObject;
00225   ros::Rate loop_rate(200);
00226 
00227   ros::spin();
00228 
00229   return 0;
00230 }
00231 
00232   //udpClient_Close(&udp);
00233 
00234 


ardrone2islab
Author(s): Trung Nguyen , Oscar De Silva
autogenerated on Thu Jun 6 2019 20:53:51