minimal.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <mavlink/common/mavlink.h>
00004 
00005 #include <sys/socket.h>
00006 #include <sys/types.h>
00007 #include <netinet/in.h>
00008 #include <arpa/inet.h>
00009 #include <fcntl.h>
00010 #include <errno.h>
00011 #include <string.h>
00012 
00013 #define BUFFER_LENGTH 2041
00014 
00015 void send_thread(const int &sock);
00016 void recv_thread(const int &sock);
00017 
00018 
00019 int main(int argc, char* argv[]) {
00020 
00021     ros::init(argc, argv, "minimal");
00022     ros::NodeHandle nh("~");
00023 
00024     int sock;
00025     if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
00026         ROS_ERROR("Socket invalid: %s", strerror(errno));
00027         exit(EXIT_FAILURE);
00028     }
00029     /* Set the Communication Nonblocking */
00030     int flag = fcntl(sock, F_GETFL, 0);
00031     if(fcntl(sock, F_SETFL, flag | O_NONBLOCK) < 0) {
00032         ROS_ERROR("Set nonblocking fail: %s", strerror(errno));
00033         close(sock);
00034         exit(EXIT_FAILURE);
00035     }
00036 
00037     //TODO: should be multi-threads
00038     send_thread(sock);
00039     //recv_thread(sock);
00040 
00041     return 0;
00042 }
00043 
00044 
00045 void send_thread(const int &sock){
00046 
00047     struct sockaddr_in locAddr;
00048     memset(&locAddr, 0, sizeof(locAddr));
00049     locAddr.sin_family = AF_INET;
00050     locAddr.sin_addr.s_addr = INADDR_ANY;
00051     locAddr.sin_port = htons(14551);
00052     if(-1 == bind(sock, (struct sockaddr *)&locAddr, sizeof(struct sockaddr_in))) {
00053         ROS_ERROR("Bind fail: %s", strerror(errno));
00054         close(sock);
00055         exit(EXIT_FAILURE);
00056     }
00057 
00058     struct sockaddr_in gcAddr;
00059     memset(&gcAddr, 0, sizeof(gcAddr));
00060     gcAddr.sin_family = AF_INET;
00061     gcAddr.sin_addr.s_addr = inet_addr("10.60.23.122");
00062     gcAddr.sin_port = htons(14550);
00063 
00064     mavlink_system_t mavSys;
00065     mavSys.sysid = 1;
00066     mavSys.compid = MAV_COMP_ID_IMU;
00067 
00068     mavlink_message_t msg;
00069     uint8_t sendBuf[BUFFER_LENGTH], recvBuf[BUFFER_LENGTH]; //TODO: not sure the size
00070     int bytes_sent, bytes_recv;
00071 
00072     while(ros::ok()) {
00073         /* Send Heartbeat */
00074         mavlink_msg_heartbeat_pack(mavSys.sysid, mavSys.compid, &msg, 
00075                 MAV_TYPE_QUADROTOR, //quadrotor type
00076                 MAV_AUTOPILOT_GENERIC, //full support for everything
00077                 MAV_MODE_GUIDED_DISARMED, //autocontrol and disarmed
00078                 0, //uint_32, custom_mode, defined by user/adopter
00079                 MAV_STATE_STANDBY); //vehicle is grounded and on standby
00080         uint16_t len = mavlink_msg_to_send_buffer(sendBuf, &msg);
00081         bytes_sent = sendto(sock, sendBuf, len, MSG_DONTWAIT, (struct sockaddr *)&gcAddr, 
00082                 sizeof(struct sockaddr_in));
00083         //memset(sendBuf, 0, BUFFER_LENGTH);
00084 
00085         /* Receive Data */
00086         socklen_t fromlen;
00087         bytes_recv = recvfrom(sock, (void *)recvBuf, BUFFER_LENGTH, MSG_DONTWAIT, 
00088                 (struct sockaddr *)&gcAddr, &fromlen);
00089         if(bytes_recv > 0) {
00090             mavlink_message_t recvMsg;
00091             mavlink_status_t recvStatus;
00092 
00093             ROS_INFO("Bytes Received: %d bytes.", bytes_recv);
00094             printf("Datagram: ");
00095             for(int i = 0; i < bytes_recv; ++i) {
00096                 unsigned char tmp = recvBuf[i];
00097                 printf("%02x", tmp);
00098                 if(mavlink_parse_char(MAVLINK_COMM_0, recvBuf[i], &recvMsg, &recvStatus)) {
00099                     printf("\nReceived packet: SYS: %d, COMP: %d, LEN: %d, MSG ID: %d\n", recvMsg.sysid, recvMsg.compid, recvMsg.len, recvMsg.msgid);
00100                 }
00101             }
00102             printf("\n");
00103         }
00104         //memset(recvBuf, 0, BUFFER_LENGTH);
00105         ros::Duration(1.0).sleep();
00106     }
00107 }


dji_sdk_dji2mav
Author(s):
autogenerated on Thu Jun 6 2019 17:55:35