00001
00011 #ifndef _DJI_LIB_ROS_ADAPTER_H_
00012 #define _DJI_LIB_ROS_ADAPTER_H_
00013
00014 #define HAS_TIME 0x0001
00015 #define HAS_Q 0x0002
00016 #define HAS_A 0x0004
00017 #define HAS_V 0x0008
00018 #define HAS_W 0x0010
00019 #define HAS_POS 0x0020
00020
00021
00022 #define HAS_MAG 0x0040
00023 #define HAS_RC 0x0080
00024 #define HAS_GIMBAL 0x0100
00025 #define HAS_STATUS 0x0200
00026 #define HAS_BATTERY 0x0400
00027 #define HAS_DEVICE 0x0800
00028
00029
00030 #define A3_HAS_GPS 0x0040
00031 #define A3_HAS_RTK 0x0080
00032 #define A3_HAS_MAG 0x0100
00033 #define A3_HAS_RC 0x0200
00034 #define A3_HAS_GIMBAL 0x0400
00035 #define A3_HAS_STATUS 0x0800
00036 #define A3_HAS_BATTERY 0x1000
00037 #define A3_HAS_DEVICE 0x2000
00038
00039
00040 #include "DJI_HardDriver_Manifold.h"
00041 #include <dji_sdk_lib/DJI_API.h>
00042 #include <dji_sdk_lib/DJI_Flight.h>
00043 #include <dji_sdk_lib/DJI_Camera.h>
00044 #include <dji_sdk_lib/DJI_VirtualRC.h>
00045 #include <dji_sdk_lib/DJI_WayPoint.h>
00046 #include <dji_sdk_lib/DJI_HotPoint.h>
00047 #include <dji_sdk_lib/DJI_Follow.h>
00048
00049 #include <ros/ros.h>
00050 #include <stdlib.h>
00051 #include <string>
00052 #include <pthread.h>
00053 #include <functional>
00054
00055 namespace DJI {
00056
00057 namespace onboardSDK {
00058
00059
00060 class ROSAdapter {
00061 public:
00062 ROSAdapter() {
00063 }
00064
00065
00066 ~ROSAdapter() {
00067 delete flight;
00068 delete camera;
00069 delete virtualRC;
00070 delete waypoint;
00071 delete hotpoint;
00072 delete followme;
00073 delete coreAPI;
00074 delete m_hd;
00075 }
00076
00077
00078 static void* APIRecvThread(void* param) {
00079 CoreAPI* p_coreAPI = (CoreAPI*)param;
00080 while(true) {
00081 p_coreAPI->readPoll();
00082 p_coreAPI->sendPoll();
00083 usleep(1000);
00084 }
00085 }
00086
00087
00088 static void broadcastCallback(CoreAPI *coreAPI, Header *header, void *userData) {
00089 ( (ROSAdapter*)userData )->m_broadcastCallback();
00090
00091 }
00092
00093 static void fromMobileCallback(CoreAPI *coreAPI, Header *header, void *userData) {
00094 uint8_t *data = ((unsigned char*) header) + sizeof(Header) + SET_CMD_SIZE;
00095 uint8_t len = header->length - SET_CMD_SIZE - EXC_DATA_SIZE;
00096 ( (ROSAdapter*)userData )->m_fromMobileCallback(data, len);
00097
00098 }
00099
00100 static void missionStatusCallback(CoreAPI *coreAPI, Header *header, void *userData) {
00101 uint8_t *data = ((unsigned char*) header) + sizeof(Header) + SET_CMD_SIZE;
00102 uint8_t len = header->length - SET_CMD_SIZE - EXC_DATA_SIZE;
00103 ( (ROSAdapter*)userData )->m_missionStatusCallback(data, len);
00104 }
00105
00106 static void missionEventCallback(CoreAPI *coreAPI, Header *header, void *userData) {
00107 uint8_t *data = ((unsigned char*) header) + sizeof(Header) + SET_CMD_SIZE;
00108 uint8_t len = header->length - SET_CMD_SIZE - EXC_DATA_SIZE;
00109 ( (ROSAdapter*)userData )->m_missionEventCallback(data, len);
00110 }
00111
00112 void init(std::string device, unsigned int baudrate) {
00113 printf("--- Connection Info ---\n");
00114 printf("Serial port: %s\n", device.c_str());
00115 printf("Baudrate: %u\n", baudrate);
00116 printf("-----\n");
00117
00118 m_hd = new HardDriver_Manifold(device, baudrate);
00119 m_hd->init();
00120
00121 coreAPI = new CoreAPI( (HardDriver*)m_hd );
00122
00123 coreAPI->setHotPointData(false);
00124
00125 flight = new Flight(coreAPI);
00126 camera = new Camera(coreAPI);
00127 virtualRC = new VirtualRC(coreAPI);
00128 waypoint = new WayPoint(coreAPI);
00129 hotpoint = new HotPoint(coreAPI);
00130 followme = new Follow(coreAPI);
00131
00132 int ret;
00133 ret = pthread_create(&m_recvTid, 0, APIRecvThread, (void *)coreAPI);
00134 if(0 != ret)
00135 ROS_FATAL("Cannot create new thread for readPoll!");
00136 else
00137 ROS_INFO("Succeed to create thread for readPoll");
00138
00139 }
00140
00141
00142 void activate(ActivateData *data, CallBack callback) {
00143
00144 coreAPI->setVersion(data->version);
00145 coreAPI->activate(data, callback);
00146
00147 }
00148
00149
00150 template<class T>
00151 void setBroadcastCallback( void (T::*func)(), T *obj ) {
00152 m_broadcastCallback = std::bind(func, obj);
00153 coreAPI->setBroadcastCallback(&ROSAdapter::broadcastCallback, (UserData)this);
00154 printf("Broadcast call back received \n");
00155 }
00156
00157 template<class T>
00158 void setFromMobileCallback( void (T::*func)(uint8_t *, uint8_t), T *obj) {
00159 m_fromMobileCallback = std::bind(func, obj, std::placeholders::_1, std::placeholders::_2);
00160 coreAPI->setFromMobileCallback(&ROSAdapter::fromMobileCallback, (UserData)this);
00161
00162 }
00163
00164 template<class T>
00165 void setMissionStatusCallback( void (T::*func)(uint8_t *, uint8_t), T *obj) {
00166 m_missionStatusCallback= std::bind(func, obj, std::placeholders::_1, std::placeholders::_2);
00167 coreAPI->setWayPointCallback(&ROSAdapter::missionStatusCallback, (UserData)this);
00168 }
00169
00170 template<class T>
00171 void setMissionEventCallback( void (T::*func)(uint8_t *, uint8_t), T *obj) {
00172 m_missionEventCallback = std::bind(func, obj, std::placeholders::_1, std::placeholders::_2);
00173 coreAPI->setWayPointEventCallback(&ROSAdapter::missionEventCallback, (UserData)this);
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 void sendToMobile(uint8_t *data, uint8_t len){
00183 coreAPI->sendToMobile(data, len, NULL, NULL);
00184 }
00185
00186 void usbHandshake(std::string &device) {
00187 m_hd->usbHandshake(device);
00188 }
00189
00190
00191
00192 CoreAPI *coreAPI;
00193 Flight *flight;
00194 Camera *camera;
00195 VirtualRC *virtualRC;
00196 WayPoint *waypoint;
00197 HotPoint *hotpoint;
00198 Follow *followme;
00199
00200
00201 private:
00202 HardDriver_Manifold *m_hd;
00203
00204 pthread_t m_recvTid;
00205
00206 std::function<void()> m_broadcastCallback;
00207
00208 std::function<void(uint8_t*, uint8_t)> m_fromMobileCallback;
00209
00210 std::function<void(uint8_t*, uint8_t)> m_missionStatusCallback;
00211
00212 std::function<void(uint8_t*, uint8_t)> m_missionEventCallback;
00213 };
00214
00215
00216 }
00217
00218 }
00219
00220
00221 #endif