From dbc640fdb5d52631b71eb8e4b9761808218de1bf Mon Sep 17 00:00:00 2001 From: 13195980010 <1512161085@qq.com> Date: Fri, 20 May 2022 04:42:19 -0700 Subject: [PATCH] re --- src/include/Djitellowhite.h | 80 ++++++++++++++++++ src/include/Hardwarelistener.h | 6 ++ src/main/Djitellowhite.cpp | 146 +++++++++++++++++++++++++++++++++ src/main/Djitellowhite.o | Bin 0 -> 15992 bytes src/main/Triplet.cpp | 45 ++++++++++ src/main/keyboard.cpp | 57 +++++++++++++ src/main/libDjitellowhite.so | Bin 0 -> 18152 bytes src/main/server.cpp | 58 +++++++++++++ 8 files changed, 392 insertions(+) create mode 100644 src/include/Djitellowhite.h create mode 100644 src/include/Hardwarelistener.h create mode 100644 src/main/Djitellowhite.cpp create mode 100644 src/main/Djitellowhite.o create mode 100644 src/main/Triplet.cpp create mode 100644 src/main/keyboard.cpp create mode 100755 src/main/libDjitellowhite.so create mode 100644 src/main/server.cpp diff --git a/src/include/Djitellowhite.h b/src/include/Djitellowhite.h new file mode 100644 index 0000000..bfaa936 --- /dev/null +++ b/src/include/Djitellowhite.h @@ -0,0 +1,80 @@ +#ifndef _DJITELLOWHITE_H +#define _DJITELLOWHITE_H +#include +#include +#include +#include +#include +#include +/*Library for interacting with DJI Ryze Tello drones.*/ + + +//coding = utf - 8 + +/* """C++ wrapper to interact with the Ryze Tello drone using the official Tello api. + Tello API documentation: + [1.3](https://dl-cdn.ryzerobotics.com/downloads/tello/20180910/Tello%20SDK%20Documentation%20EN_1.3.pdf) +*/ + +#define RESPONSE_TIMEOUT 7 //in seconds +#define TAKEOFF_TIMEOUT 20 //in seconds +#define FRAME_GRAB_TIMEOUT 3 +#define TIME_BTW_COMMANDS 0.1 //in seconds +#define TIME_BTW_RC_CONTROL_COMMANDS 0.001 //in seconds +#define RETRY_COUNT 3 //number of retries after a failed command + + +//Video stream, server socket +#define VS_UDP_IP "0.0.0.0" +#define VS_UDP_PORT 11111 + +#define CONTROL_UDP_PORT 8889 +#define STATE_UDP_PORT 8890 + +#define BUFFER_SIZE 1024 +//Constants for video settings +#define BITRATE_AUTO 0 +#define BITRATE_1MBPS 1 +#define BITRATE_2MBPS 2 +#define BITRATE_3MBPS 3 +#define BITRATE_4MBPS 4 +#define BITRATE_5MBPS 5 +#define RESOLUTION_480P "low" +#define RESOLUTION_720P "high" +#define FPS_5 "low" +#define FPS_15 "middle" +#define FPS_30 "high" +#define CAMERA_FORWARD 0 +#define CAMERA_DOWNWARD 1 +//日志模块暂时不写 + +class Tello { +private: + int sock; + bool stream_on; + unsigned long host; +public: + Tello(const char* IP); + ~Tello(); + void set_conf(int socket); + unsigned long gethost(); + void get_connect(sockaddr_in serveraddr); + void takeoff(sockaddr_in serveraddr); + void land(sockaddr_in serveraddr); + void forward(sockaddr_in serveraddr); + void back(sockaddr_in serveraddr); + void left(sockaddr_in serveraddr); + void right(sockaddr_in serveraddr); + void up(sockaddr_in serveraddr); + void down(sockaddr_in serveraddr); + void cw(sockaddr_in serveraddr); + void ccw(sockaddr_in serveraddr); + bool send_message(char* msg,sockaddr_in server_addr,int timeout = RESPONSE_TIMEOUT); + static void *udp_response_receiver(void* arg); + void get_video(sockaddr_in serveraddr); +}; + + + + +#endif diff --git a/src/include/Hardwarelistener.h b/src/include/Hardwarelistener.h new file mode 100644 index 0000000..599fa82 --- /dev/null +++ b/src/include/Hardwarelistener.h @@ -0,0 +1,6 @@ +#ifndef _HARDWARELISTENER_H +#define _HARDWARELISTENER_H +#include "Djitellowhite.h" +int listenkeyboard(Tello T,sockaddr_in serveraddr); +int scanKeyboard(); +#endif diff --git a/src/main/Djitellowhite.cpp b/src/main/Djitellowhite.cpp new file mode 100644 index 0000000..c8a8f52 --- /dev/null +++ b/src/main/Djitellowhite.cpp @@ -0,0 +1,146 @@ +#include"Djitellowhite.h" +#include +#include + + +#include +#include +#include + +#include +#include + +using namespace cv; +using namespace std; +Tello::Tello(const char* IP) { + this->stream_on=false; + this->host = inet_addr(IP); + cout << "create successfully!" << endl; + +} +Tello::~Tello() {} +void Tello::set_conf(int socket){ + this->sock = socket; +} +void Tello::get_connect(sockaddr_in serveraddr) { + char tmp[10] = {"command"}; + send_message(tmp,serveraddr,0);// 0 -> timeout + cout << "connect successfully!" <host; +} +void Tello::takeoff(sockaddr_in serveraddr) { + char tmp[10] = {"takeoff"}; + send_message(tmp,serveraddr,0);// 0 -> timeout + cout << "takeoff successfully!" < timeout + cout << "land successfully!" <sock,buffer,strlen(buffer),0,(struct sockaddr *)&server_addr, sizeof(server_addr)); + return true; +} +void* Tello::udp_response_receiver(void* arg){ + //int m_SockServer; //创建socket对象 + //sockaddr_in serveraddr; //创建sockaddr_in对象储存自身信息(当有多个端口,可以多个绑定) + //sockaddr_in serveraddrfrom; + + //serveraddr.sin_family = AF_INET; //设置服务器地址家族 + //serveraddr.sin_port = htons(8889); //设置服务器端口号 + //serveraddr.sin_addr.s_addr = inet_addr("0.0.0.0"); + //m_SockServer = socket(AF_INET, SOCK_DGRAM, 0); //创建一个临时变量并赋值给m_SockServer + //int i = bind(m_SockServer, (sockaddr*)&serveraddr, sizeof(serveraddr)); //把名字和套接字绑定 + //std::cout << "bind:" << i << std::endl; + + int socket = *(int *)arg; + + cout << "receive start" << endl; + while(1){ + char buffer[20]; + int iret; + memset(buffer,0,sizeof(buffer)); + + iret = recv(socket, buffer,20,MSG_WAITALL); + if(iret > 0 && iret <10){ + printf("Message from tello: %s\n", buffer); + } + } +} +void Tello::forward(sockaddr_in serveraddr){ + char tmp[15] = {"forward 20"}; + send_message(tmp,serveraddr,0);// 0 -> timeout + cout << "forward successfully!" < timeout + cout << "back successfully!" < timeout + cout << "left successfully!" < timeout + cout << "right successfully!" < timeout + cout << "up successfully!" < timeout + cout << "down successfully!" < timeout + cout << "cw successfully!" < timeout + cout << "ccw successfully!" <stream_on = true; + + cout << "vid"; + VideoCapture cap("udp://@0.0.0.0:11111"); + cout << "cap"; + if(!cap.isOpened()){ + cout << "fuck off"; + return; + } + + Mat frame; + cout << "continue!"; + while(1){ + cap>>frame; + if(frame.empty()) + break; + imshow("video",frame); + waitKey(20); + + } + cap.release(); + + +} diff --git a/src/main/Djitellowhite.o b/src/main/Djitellowhite.o new file mode 100644 index 0000000000000000000000000000000000000000..12a96388bd5e770804c9fdacdd369f86edde1e40 GIT binary patch literal 15992 zcmdU$e{fXSb;s{&5q9tolsd6pgFS1H-S`)4t&sRtoCu`FPmussi)@0ipO@7W+92(2 zcHbgI!9R+X@p?xI;|Uojb=|ms#Lkat+N7Cc;ut107PJ}HO*1uaNkdzwhMFN{8WZZ! zsOQ{!&hFW}`+{kwO}U=cd+&44J@tu^l+IiL~6;C_m_}N!F zXDPUzc1qL7!AzCDgVZVYU+PSCAaJJc#*2=0Ft%CK(ko8sbq#kPKDfK$9X#)Tp!T^` z{296|5B{vwQWLl9WG5&&;*|czDM9}4I;FpKO7R0uY4FgHQ=WCpt(bc$el~W%*_+Ca zjU_Wf_iy&vFFQQ^Yf*mzI;Z2OMxm1buj}`*lh6mDZoYl|7c(6^#&WzTE+ zGIazl-KELz!Nu+sQO+J}jEN`J?l+hIa>*qtXa*D`eTDV^ce zH__*c7jgXbln%X!eragz#j)m;S7T?nT=&jOXR&%kUH>h|50zCZe&oH5DQ7BvY#$EW z+Hy@5kCo#`Uc^DKV@X&kestH)qw3+q@b`U>9K^&9j88Lg^{~)=`t2`O$LR(1ioL_x z{CF}y6oIjKVmkiQwpOK}i@c-MEAgMpVR+(yV;EXB``%`YP07@j27y@pWZxzk}sY*F6OX?^*aUo@kP)phO*p_p#xJ%yPLbqaYd=mnkDa^~W_5fC z{t{n;@{?%tHI4Qhfs1?v9>))(f}i)2W7!PIe`7lSPt>V@jFa~B;tTx&YA=Ig}}b~0J(v!;sn{& z?&@rf>i?F-FsM{Ar-qC0!G|j>s5w8KDS9_5u=lcJ-3u#IT#1$jk3+9Et5LjxCFF5j z?ZV15aEdfUqy>5mN^H7MP=mog8246EXq4vi^y-J-4HUD+9`b<;vpEs-T zHbtjrtIvlQd~L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ef~X65nRq!F*=9D_Qks6_F6<78J_ttzgyV5f_Dop{qz#YGCd^h6T*K|@Ce*v zKp!81ALq;KO60c)4!1gBx5P5=xvHV&ymuo%z%@emXiHs5lTN{@sx)~>aFVRaqk^L^ z4qu=fWccIrvI7>+(DN~iKS|GgP|be$oGU`(k2#-nby}RyxgN4OpRYY?aXz1V#^QY5 z^peH-yy>XL`Me3wM=`KYK5v?jKyW^9T5fSZZ<6auG~x569hNhQtC~?`QJdHr&(e>g(iHyf3w9a{V-aX81s{xR225!%yoqCV527)g-sP- zMTe4pQbjiv3M!hL3?Lb0plZB};;G7L^jB?EM)7~+@-k5FOcoA#;NP7s(3{cp z3|i685&DoAcRX4qM`X7W$!ozQ2KsPGa;Ks;+})oNsX6!NAv` z^o}vB{EHS$KVPIfUwV9pW03h@56_&AuR;0qTqo1tZ~^IuFmnhz2cmu)N1QKTBfxNo z(=rY2OyH&|$jK6-XgIN%!ha8dzB((!-0Z#CT-RS|u9!dO!R5KuVD(#Y#r$d55moA6 ztb93wrXzk-cfuX|3`%d?+s`pbi_9+9amxP@D*@|=BswgGzi2YsaxgO2#NS~qmA{sm z+5|C;(tn212op3+;{ zs~=H9{6>+%s!sJHL^jI*+7^?Fx>3c2ozD@7!7gUEe45wp$0vPHKTf&_48Jd0e8_;d z82EYvr8l(tcL$}f5_8Atm`4HNmWNe*^(du}upwPpKjT%Dzn%UIl)lqySUdd+NZ+Xa za#Vjga*XYVZN}-j{jfZC-l**uRyKrI+8@Aq4&NVkdW^Ot0)c-+fs;LiKcf90^Hi;O Ve#So4DE%?|P`(cZieNpe{{pwkw4ML} literal 0 HcmV?d00001 diff --git a/src/main/Triplet.cpp b/src/main/Triplet.cpp new file mode 100644 index 0000000..9c0ef65 --- /dev/null +++ b/src/main/Triplet.cpp @@ -0,0 +1,45 @@ +#include"Djitellowhite.h" +#include"Hardwarelistener.h" +#include +using namespace std; +int main(void){ + Tello T("192.168.10.1"); + int socket_fd, err; + + if((socket_fd = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))==-1) + { + cout << "initialized error" << endl; + cout << 0; + return -1; + } + + //cout << 1 << socket_fd; + T.set_conf(socket_fd); + + pthread_t thread_receive; + err = pthread_create(&thread_receive,nullptr,T.udp_response_receiver,&socket_fd); + sleep(3); + struct sockaddr_in serveraddr; + memset(&serveraddr,0,sizeof(serveraddr)); + serveraddr.sin_family=AF_INET; + serveraddr.sin_addr.s_addr=T.gethost(); + serveraddr.sin_port=htons(CONTROL_UDP_PORT); + + + + if(err!=0) + { + cout << err << " " << "can't create thread"; + } + + T.get_connect(serveraddr); + //sleep(1); + //T.takeoff(serveraddr); + T.get_video(serveraddr); + sleep(5); + //T.land(serveraddr); + + pthread_join(thread_receive,NULL); + listenkeyboard(T,serveraddr); + return 0; +} diff --git a/src/main/keyboard.cpp b/src/main/keyboard.cpp new file mode 100644 index 0000000..dcf3ade --- /dev/null +++ b/src/main/keyboard.cpp @@ -0,0 +1,57 @@ +#include +#include +#include "Hardwarelistener.h" +#include + +using namespace std; +int scanKeyboard() +{ + + int in; + + struct termios new_settings; + struct termios stored_settings; + //设置终端参数 + tcgetattr(0,&stored_settings); + new_settings = stored_settings; + new_settings.c_lflag &= (~ICANON); + new_settings.c_cc[VTIME] = 0; + tcgetattr(0,&stored_settings); + new_settings.c_cc[VMIN] = 1; + tcsetattr(0,TCSANOW,&new_settings); + in = getchar(); + tcsetattr(0,TCSANOW,&stored_settings); + + return in; + +} + +//测试函数 +int listenkeyboard(Tello T, sockaddr_in serveraddr){ + while(1){ + switch(scanKeyboard()) + { + case (int)'w': + cout << "forward"<< endl; + T.forward(serveraddr); + break; + case (int)'s': + cout << "back" << endl; + T.back(serveraddr); + break; + case (int)'a': + cout << "left" <N+|;A>n7Zw_KW;il z+cA}~0Ji=ED|998HE6p%x+8A-lx~kHw|5`xC@z;<_%yH6?G-x@b9KC!>hQ{Vk`Ed) zcdcy{P&-0>(WhI=r)Zw!iSX0keRkm0>5pXkPXFrRe|c_A)%m#)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ltuEoUp{8Uc8d?|$38~N;NVE!X zsH+{$6h$tUlpy&lfoUp_F(HeGqX9C|wT`GIymIdIqUvplfu%|!928!peSxK^7uvB< zzzPU&xK&TKtw;&9Ipssmy2^s3sAp`-%|SU!gq; zEI(x7=UAp5jWgl>3+C~$b)G#x?=qdkikjl~m{*pA!|Ti#x6k`FOt)xlwsW_C59GAf z!uGtc!_>e!g9`1Bl;Dr;dB0^nFj8TAe$Hmf`$ABWg?u>6AAzAg8n)->ai;X#M1}j$ zdQ4j(qdgzy`MI5GgS1xtCmrs;hD9eTf%)Aod)`-ITB+M-eeKq6<}sH&?>{hQ{bKe{y6kzM zf~h<>HU?#XOuy~6*NZ%#rVsE2p!UIb?DD%Vd*1h8%KM|N&*zc(7qvZ~f8o&*P4{S) zi7TAq?;#+J?Rg)C=~HZ|sk{9*T=u-*!nA=MYkM9)?Dj2GXpcf)ruM!J|33~qPFbJr znf@CDlO6WFe>1dDS+HI)`wt~%Pl z5`M=4AWW;P Y&h4@;JrftQZ@fck(En@TEG}03A3X&HVE_OC literal 0 HcmV?d00001 diff --git a/src/main/server.cpp b/src/main/server.cpp new file mode 100644 index 0000000..581b3e7 --- /dev/null +++ b/src/main/server.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define PORT 8890 + +void udp_server(int sockfd) +{ + + socklen_t len; + char buf[1024] = {0}; + struct sockaddr_in server_addr; + int n; + int opt = 1; + len = sizeof(server_addr); + memset(&buf, 0, sizeof(buf)); + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(PORT); + + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); //当服务器非正常断开的时候重启服务器,不会进入TIME_WAIT状态 + + if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + printf("can not bind\n"); + exit(1); + } + + while (1) { + printf("========wait for client's request========\n"); + n = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&server_addr, &len); + buf[n] = '\0'; + printf("receive client's data: %s\n", buf); + sendto(sockfd, buf, n, 0, (struct sockaddr *)&server_addr, len); + printf("send data to client: %s\n", buf); + } + + close(sockfd); +} + +int main(int argc, char **argv) +{ + int sockfd; + + if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { + printf("create socket false\n"); + exit(1); + } + + udp_server(sockfd); + + exit(0); +} + +