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>
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
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
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
00037
00038 if (n_.getParam("/mode", modeset))
00039 {
00040
00041 }
00042 else
00043 {
00044 ROS_ERROR("Failed to get param 'my_param'");
00045 }
00046 vztglobal = msg.linear.z;
00047
00048 if (modeset == "0")
00049 {
00050 ROS_INFO("mode 0");
00051
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
00065
00066
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
00070
00071 }
00072 }
00073
00074 void Pose_Callback(const geometry_msgs::PoseStamped& msgPose)
00075 {
00076
00077
00078 if (n_.getParam("/mode", modeset))
00079 {
00080
00081 }
00082 else
00083 {
00084 ROS_ERROR("Failed to get param 'my_param'");
00085 }
00086
00087 if (modeset == "1")
00088 {
00089
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
00102 int msglen;
00103 char buf[2048];
00104 float ex = transform.getOrigin().x();
00105 float ey = transform.getOrigin().y();
00106 float ez = transform.getOrigin().z();
00107
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
00124
00125
00126
00127
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
00134
00135
00136 float F[3] = {0.0,0.0,0.0};
00137
00138 F[1] = 0.7 * ex + 0.005 * evx + axt;
00139 F[2] = -0.7 * ey - 0.005 * evy + ayt;
00140 F[3] = 0.72 + (0.1 * ez) + (0.000 * evz) + azt;
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
00175
00176
00177
00178
00179
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
00183
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
00207 std::string modeset;
00208
00209
00210
00211 tf::TransformListener listener;
00212
00213 };
00214
00215 int main(int argc, char **argv)
00216 {
00217
00218
00219 ros::init(argc, argv, "ardrone_ground_node_trajectorycontrol");
00220
00221 ROS_INFO_STREAM("Start ROS node!");
00222
00223
00224 CommunicationToDrone CommunicationObject;
00225 ros::Rate loop_rate(200);
00226
00227 ros::spin();
00228
00229 return 0;
00230 }
00231
00232
00233
00234