ar.drone控制程序中没有输出

时间:2014-01-28 03:46:52

标签: printf ar.drone

     #include <stdlib.h> // for exit() //
     #include <netdb.h>
     #include <stdio.h>
     #include <string.h>   // memset() //

     #define DRONE_COMMAND_PORT 5556
     #define MAX_MSG 2048

     int main(int argc, char *argv[]) {

          // declare variables
          int    sd, rc, flags, nd;
          char   command[MAX_MSG];  
          struct sockaddr_in clientAddr;
          struct sockaddr_in droneAddr;
          struct hostent     *h;
          struct timespec    wait_command;

          int seq=0;
          char msg[MAX_MSG];

          // initialize variables
          flags = 0;
          wait_command.tv_sec  = 0;
          wait_command.tv_nsec = 5000000;

          // check command line args //
         if(argc<2) {
         printf("usage : %s <server name/IP>\n", argv[0]);
         exit(EXIT_FAILURE);
         }

         // get server IP address (no check if input is IP address or DNS name)
         h = gethostbyname(argv[1]);
         if(h==NULL) {
         printf("%s: unknown host '%s' \n", argv[0], argv[1]);
         exit(EXIT_FAILURE);
         }

         // create structure for ardrone address & port
         droneAddr.sin_family = h->h_addrtype;
         droneAddr.sin_port   = htons(DRONE_COMMAND_PORT);
         memcpy((char *) &droneAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
         // create structure for this client
         clientAddr.sin_family = AF_INET;
         clientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
         clientAddr.sin_port = htons(0);

         // socket creation
         sd = socket(AF_INET,SOCK_DGRAM,0);
         if(sd<0) {
         printf("%s: cannot open socket \n",argv[0]);
         exit(EXIT_FAILURE);
         }
         // bind client's the port and address
         rc = bind(sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr));
         if(rc<0) {
         printf("%s: cannot bind port\n", argv[0]);
         exit(EXIT_FAILURE);
         }
        // send commands
        char cmd[MAX_MSG],cmd2[MAX_MSG],cmd3[MAX_MSG],cmd4[MAX_MSG];
         int i;
        snprintf(cmd,MAX_MSG,"AT*CONFIG= %u,\"general:navdata_demo\",\"TRUE\"",seq);
        printf("%s: %s\n",argv[0],cmd);
        sendto(sd, cmd, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
        seq++;
        snprintf(cmd2,MAX_MSG,"AT*FTRIM=%u",seq);
        printf("%s: %s\n",argv[0],cmd2);
        sendto(sd, cmd2, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
        seq++;
        snprintf(cmd3,MAX_MSG,"AT*PMODE= %u,2",seq);
        printf("%s: %s\n",argv[0],cmd3);
        sendto(sd, cmd3, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
        seq++;
        char cmd1[MAX_MSG];
        printf("%s: sending commands to  %d\n", argv[0], inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
        for(i=0;i<10;i++)
        { 
    cmd1[strlen(cmd1)-1] = 0x0d;  // change last character from new line to carriage return

        snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq);
        printf("%s: %s\n",argv[0],cmd1);
        snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq+1);
        printf("%s: %s\n",argv[0],cmd1);
        snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq+2);
        printf("%s: %s\n",argv[0],cmd1);
        snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq+3);
        printf("%s: %s\n",argv[0],cmd1);
        snprintf(cmd4,MAX_MSG,"AT*COMWDG=%u",seq+4);
        printf("%s :%s\n",argv[0],cmd4);

        rc = sendto(sd, cmd1, MAX_MSG , flags,  (struct sockaddr *) &droneAddr, sizeof(droneAddr));
        if(rc<0) {
        printf("%s: can not send data\n",argv[0]);
        close(sd);
        exit(EXIT_FAILURE);
        }
        seq=seq+5;
        nanosleep(&wait_command , NULL);
        }
        return 1;
        }

您好, 这是我的起飞计划。编译时我没有收到任何错误。 但无人机并没有起飞。 printf语句中的at命令正在打印。那里也没有错误。谁能告诉我程序中是否有任何错误或我的程序无法正常工作?

提前感谢。

关心, shiksha

2 个答案:

答案 0 :(得分:1)

尝试使用

连接()

而不是绑定端口。

答案 1 :(得分:1)

我总是发送以下序列:

&#34; AT * CONFIG = 1,\&#34;一般:navdata_demo \&#34; \&#34; TRUE \&#34; \ R&#34;

&#34; AT * CTRL = 0 \ R&#34;

然后你想要的任何你想要的序列号为1。

我认为你错过了AT * CTRL = 0 / r