Advertisement
Guest User

PX4 visual update sample

a guest
Aug 21st, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <time.h>
  11. #if (defined __QNX__) | (defined __QNXNTO__)
  12. /* QNX specific headers */
  13. #include <unix.h>
  14. #else
  15. /* Linux / MacOS POSIX timer headers */
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #include <arpa/inet.h>
  19. #include <stdbool.h> /* required for the definition of bool in C99 */
  20. #endif
  21.  
  22. /* This assumes you have the mavlink headers on your include path
  23.  or in the same folder as this source file */
  24. #include <mavlink.h>
  25.  
  26.  
  27. #define BUFFER_LENGTH 2041 // minimum buffer size that can be used with qnx (I don't know why)
  28.  
  29. uint64_t microsSinceEpoch();
  30.  
  31. int main(int argc, char* argv[])
  32. {
  33.    
  34.     char help[] = "--help";
  35.    
  36.     char target_ip[100];
  37.    
  38.     float position[6] = {};
  39.     int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  40.     struct sockaddr_in gcAddr;
  41.     struct sockaddr_in locAddr;
  42.     //struct sockaddr_in fromAddr;
  43.     uint8_t buf[BUFFER_LENGTH];
  44.     ssize_t recsize;
  45.     socklen_t fromlen;
  46.     int bytes_sent;
  47.     mavlink_message_t msg;
  48.     uint16_t len;
  49.     int i = 0;
  50.     //int success = 0;
  51.     unsigned int temp = 0;
  52.     bool airborne = false;
  53.  
  54.     // Check if --help flag was used
  55.     if ((argc == 2) && (strcmp(argv[1], help) == 0))
  56.     {
  57.         printf("\n");
  58.         printf("\tUsage:\n\n");
  59.         printf("\t");
  60.         printf("%s", argv[0]);
  61.         printf(" <ip address of QGroundControl>\n");
  62.         printf("\tDefault for localhost: udp-server 127.0.0.1\n\n");
  63.         exit(EXIT_FAILURE);
  64.     }
  65.  
  66.     // Change the target ip if parameter was given
  67.     strcpy(target_ip, "127.0.0.1");
  68.     if (argc == 2)
  69.     {
  70.         strcpy(target_ip, argv[1]);
  71.     }
  72.    
  73.    
  74.     memset(&locAddr, 0, sizeof(locAddr));
  75.     locAddr.sin_family = AF_INET;
  76.     locAddr.sin_addr.s_addr = INADDR_ANY;
  77.     locAddr.sin_port = htons(14540);
  78.    
  79.     /* Bind the socket to port 14551 - necessary to receive packets from qgroundcontrol */
  80.     if (-1 == bind(sock,(struct sockaddr *)&locAddr, sizeof(struct sockaddr)))
  81.     {
  82.         perror("error bind failed");
  83.         close(sock);
  84.         exit(EXIT_FAILURE);
  85.     }
  86.    
  87.     /* Attempt to make it non blocking */
  88. #if (defined __QNX__) | (defined __QNXNTO__)
  89.     if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
  90. #else
  91.     if (fcntl(sock, F_SETFL, O_NONBLOCK | O_ASYNC) < 0)
  92. #endif
  93.  
  94.     {
  95.         fprintf(stderr, "error setting nonblocking: %s\n", strerror(errno));
  96.         close(sock);
  97.         exit(EXIT_FAILURE);
  98.     }
  99.    
  100.    
  101.     memset(&gcAddr, 0, sizeof(gcAddr));
  102.     gcAddr.sin_family = AF_INET;
  103.     gcAddr.sin_addr.s_addr = inet_addr(target_ip);
  104.     gcAddr.sin_port = htons(14557);
  105.    
  106.     int counter = 0;
  107.    
  108.     float reported_ned_x = 0;
  109.     float reported_ned_y = 0;
  110.     float reported_ned_z = 0;
  111.     float reported_roll = 0;
  112.     float reported_pitch = 0;
  113.     float reported_yaw = 0;
  114.  
  115.     for (;;)
  116.     {
  117.         /*Send Heartbeat */
  118.         mavlink_msg_heartbeat_pack(1, 200, &msg, MAV_TYPE_GENERIC, MAV_AUTOPILOT_INVALID, 0, 0, MAV_STATE_ACTIVE);
  119.         len = mavlink_msg_to_send_buffer(buf, &msg);
  120.         bytes_sent = sendto(sock, buf, len, 0, (struct sockaddr*)&gcAddr, sizeof(struct sockaddr_in));
  121.  
  122.         /*Send visual position opdate*/
  123.         float covariance[] = { 1, 0, 0, 0, 0, 0,
  124.                                   1, 0, 0, 0, 0,
  125.                                      1, 0, 0, 0,
  126.                                         1, 0, 0,
  127.                                            1, 0,
  128.                                               1 };
  129.  
  130.         float lat;
  131.         float lon;
  132.         float elev;
  133.         float pitch;
  134.         float roll;
  135.         float yaw;
  136.  
  137.         lat = reported_ned_x;
  138.         lon = reported_ned_y;
  139.         elev = reported_ned_z;
  140.         pitch = reported_pitch;
  141.         roll = reported_roll;
  142.         yaw = reported_yaw;
  143.  
  144.         printf("sending lat: %f lon: %f elev: %f yaw: %f pitch: %f roll: %f\n", lat, lon, elev, yaw, pitch, roll);
  145.  
  146.         mavlink_msg_vision_position_estimate_pack(1, MAV_COMP_ID_PERIPHERAL, &msg, microsSinceEpoch(),
  147.                                                   lat, lon, elev, roll, pitch, yaw,
  148.                                                   covariance);
  149.         len = mavlink_msg_to_send_buffer(buf, &msg);
  150.         bytes_sent = sendto(sock, buf, len, 0, (struct sockaddr*)&gcAddr, sizeof(struct sockaddr_in));
  151.    
  152.  
  153.  
  154.         memset(buf, 0, BUFFER_LENGTH);
  155.         recsize = recvfrom(sock, (void *)buf, BUFFER_LENGTH, 0, (struct sockaddr *)&gcAddr, &fromlen);
  156.         if (recsize > 0)
  157.         {
  158.             // Something received - print out all bytes and parse packet
  159.             mavlink_message_t msg;
  160.             mavlink_status_t status;
  161.            
  162.             //printf("Bytes Received: %d\nDatagram: ", (int)recsize);
  163.             for (i = 0; i < recsize; ++i)
  164.             {
  165.                 if (mavlink_parse_char(MAVLINK_COMM_0, buf[i], &msg, &status))
  166.                 {
  167.                     bool old_airborne = airborne;
  168.  
  169.                     switch (msg.msgid) {
  170.                         case MAVLINK_MSG_ID_ATTITUDE:
  171.                             reported_roll = mavlink_msg_attitude_get_roll(&msg);
  172.                             reported_pitch = mavlink_msg_attitude_get_pitch(&msg);
  173.                             reported_yaw = mavlink_msg_attitude_get_yaw(&msg);
  174.                             printf("attitude, roll: %f, pitch: %f, yaw: %f, rollspeed: %f, pitchspeed: %f, yawspeed: %f\n",
  175.                                                                             mavlink_msg_attitude_get_roll(&msg),
  176.                                                                             mavlink_msg_attitude_get_pitch(&msg),
  177.                                                                             mavlink_msg_attitude_get_yaw(&msg),
  178.                                                                             mavlink_msg_attitude_get_rollspeed(&msg),
  179.                                                                             mavlink_msg_attitude_get_pitchspeed(&msg),
  180.                                                                             mavlink_msg_attitude_get_yawspeed(&msg));
  181.                             break;
  182.                         case MAVLINK_MSG_ID_LOCAL_POSITION_NED:
  183.                             reported_ned_x = mavlink_msg_local_position_ned_get_x(&msg);
  184.                             reported_ned_y = mavlink_msg_local_position_ned_get_y(&msg);
  185.                             reported_ned_z = mavlink_msg_local_position_ned_get_z(&msg);
  186.                             printf("local position NED, x: %f, y: %f, z: %f, vx: %f, vy: %f, vz: %f\n",
  187.                                                                             mavlink_msg_local_position_ned_get_x(&msg),
  188.                                                                             mavlink_msg_local_position_ned_get_y(&msg),
  189.                                                                             mavlink_msg_local_position_ned_get_z(&msg),
  190.                                                                             mavlink_msg_local_position_ned_get_vx(&msg),
  191.                                                                             mavlink_msg_local_position_ned_get_vy(&msg),
  192.                                                                             mavlink_msg_local_position_ned_get_vz(&msg));
  193.                             break;
  194.                         case MAVLINK_MSG_ID_GPS_RAW_INT:
  195.                             printf("GPS raw int lat: %d lon: %d alt: %d\n", mavlink_msg_gps_raw_int_get_lat(&msg),
  196.                                                                             mavlink_msg_gps_raw_int_get_lon(&msg),
  197.                                                                             mavlink_msg_gps_raw_int_get_alt(&msg));
  198.                             break;
  199.                         default:
  200.                             printf("message id: %d\n", msg.msgid);
  201.                             break;
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.         memset(buf, 0, BUFFER_LENGTH);
  207.     }
  208. }
  209.  
  210.  
  211. /* QNX timer version */
  212. #if (defined __QNX__) | (defined __QNXNTO__)
  213. uint64_t microsSinceEpoch()
  214. {
  215.    
  216.     struct timespec time;
  217.    
  218.     uint64_t micros = 0;
  219.    
  220.     clock_gettime(CLOCK_REALTIME, &time);  
  221.     micros = (uint64_t)time.tv_sec * 1000000 + time.tv_nsec/1000;
  222.    
  223.     return micros;
  224. }
  225. #else
  226. uint64_t microsSinceEpoch()
  227. {
  228.    
  229.     struct timeval tv;
  230.    
  231.     uint64_t micros = 0;
  232.    
  233.     gettimeofday(&tv, NULL);  
  234.     micros =  ((uint64_t)tv.tv_sec) * 1000000u + tv.tv_usec;
  235.    
  236.     return micros;
  237. }
  238. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement