00001 00013 #include "DJI_WayPoint.h" 00014 #include <string.h> 00015 00016 using namespace DJI; 00017 using namespace DJI::onboardSDK; 00018 00019 #ifndef STATIC_MEMORY 00020 WayPoint::WayPoint(CoreAPI *ControlAPI) 00021 { 00022 api = ControlAPI; 00023 index = 0; 00024 } 00025 #else 00026 WayPoint::WayPoint(WayPointData *list, uint8_t len, CoreAPI *ControlAPI) 00027 { 00028 api = ControlAPI; 00029 index = list; 00030 maxIndex = len; 00031 } 00032 #endif // STATIC_MEMORY 00033 00034 void WayPoint::init(WayPointInitData *Info, CallBack callback, UserData userData) 00035 { 00036 if (Info) 00037 setInfo(*Info); 00038 00039 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_INIT, &info, sizeof(info), 500, 2, 00040 callback ? callback : missionCallback, userData); 00041 } 00042 00043 MissionACK WayPoint::init(WayPointInitData *Info, int timeout) 00044 { 00045 if (Info) 00046 setInfo(*Info); 00047 00048 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_INIT, &info, sizeof(info), 500, 2, 0, 0); 00049 00050 api->serialDevice->lockACK(); 00051 api->serialDevice->wait(timeout); 00052 api->serialDevice->freeACK(); 00053 00054 return api->missionACKUnion.missionACK; 00055 } 00056 00057 void WayPoint::start(CallBack callback, UserData userData) 00058 { 00059 uint8_t start = 0; 00060 00061 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETSTART, &start, sizeof(start), 500, 2, 00062 callback ? callback : missionCallback, userData); 00063 } 00064 00065 MissionACK WayPoint::start(int timeout) 00066 { 00067 uint8_t start = 0; 00068 00069 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETSTART, &start, sizeof(start), 500, 2, 0, 0); 00070 00071 api->serialDevice->lockACK(); 00072 api->serialDevice->wait(timeout); 00073 api->serialDevice->freeACK(); 00074 00075 return api->missionACKUnion.missionACK; 00076 } 00077 00078 void WayPoint::stop(CallBack callback, UserData userData) 00079 { 00080 uint8_t stop = 1; 00081 00082 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETSTART, &stop, sizeof(stop), 500, 2, 00083 callback ? callback : missionCallback, userData); 00084 } 00085 00086 MissionACK WayPoint::stop(int timeout) 00087 { 00088 uint8_t stop = 1; 00089 00090 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETSTART, &stop, sizeof(stop), 500, 2, 0,0); 00091 00092 api->serialDevice->lockACK(); 00093 api->serialDevice->wait(timeout); 00094 api->serialDevice->freeACK(); 00095 00096 return api->missionACKUnion.missionACK; 00097 } 00098 00099 void WayPoint::pause(bool isPause, CallBack callback, UserData userData) 00100 { 00101 uint8_t data = isPause ? 0 : 1; 00102 00103 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETPAUSE, &data, sizeof(data), 500, 2, 00104 callback ? callback : missionCallback, userData); 00105 } 00106 00107 MissionACK WayPoint::pause(bool isPause, int timeout) 00108 { 00109 uint8_t data = isPause ? 0 : 1; 00110 00111 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETPAUSE, &data, sizeof(data), 500, 2, 0, 0); 00112 00113 api->serialDevice->lockACK(); 00114 api->serialDevice->wait(timeout); 00115 api->serialDevice->freeACK(); 00116 00117 return api->missionACKUnion.missionACK; 00118 } 00119 00120 void WayPoint::readIdleVelocity(CallBack callback, UserData userData) 00121 { 00122 uint8_t zero = 0; 00123 00124 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_GETVELOCITY, &zero, sizeof(zero), 500, 2, 00125 callback ? callback : idleVelocityCallback, userData ? userData : this); 00126 } 00127 00128 bool WayPoint::uploadIndexData(WayPointData *data, CallBack callback, UserData userData) 00129 { 00130 setIndex(data, data->index); 00131 return uploadIndexData(data->index, callback ? callback : uploadIndexDataCallback, userData); 00132 } 00133 00134 bool WayPoint::uploadIndexData(uint8_t pos, CallBack callback, UserData userData) 00135 { 00136 WayPointData send; 00137 if (pos < info.indexNumber) 00138 send = index[pos]; 00139 else 00140 return false; 00141 00142 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_ADDPOINT, &send, sizeof(send), 1000, 4, 00143 callback ? callback : uploadIndexDataCallback, userData); 00144 00145 return true; 00146 } 00147 00148 WayPointDataACK WayPoint::uploadIndexData(WayPointData *data, int timeout) 00149 { 00150 WayPointData wpData; 00151 00152 setIndex(data, data->index); 00153 00154 if (data->index < info.indexNumber) 00155 wpData = index[data->index]; 00156 else 00157 throw std::runtime_error("Range error\n"); 00158 00159 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_ADDPOINT, &wpData, sizeof(wpData), 1000, 4, 0, 0); 00160 00161 api->serialDevice->lockACK(); 00162 api->serialDevice->wait(timeout); 00163 api->serialDevice->freeACK(); 00164 00165 return api->missionACKUnion.waypointDataACK; 00166 } 00167 00168 void WayPoint::updateIdleVelocity(float32_t meterPreSecond, CallBack callback, 00169 UserData userData) 00170 { 00171 api->send(2, encrypt, SET_MISSION, CODE_WAYPOINT_SETVELOCITY, &meterPreSecond, 00172 sizeof(meterPreSecond), 500, 2, callback ? callback : idleVelocityCallback, 00173 userData ? userData : this); 00174 } 00175 00176 WayPointInitData WayPoint::getInfo() const 00177 { 00178 return info; 00179 } 00180 00181 void WayPoint::setInfo(const WayPointInitData &value) 00182 { 00184 info = value; 00185 for (int i = 0; i < 16; ++i) info.reserved[i] = 0; 00186 #ifndef STATIC_MEMORY 00187 if (index != 0) 00188 delete index; 00189 index = 0; 00190 #else 00191 if (maxIndex < info.indexNumber) 00192 index = 0; 00193 #endif // STATIC_MEMORY 00194 } 00195 00196 WayPointData *WayPoint::getIndex() const 00197 { 00198 return index; 00199 } 00200 00201 WayPointData *WayPoint::getIndex(size_t pos) const 00202 { 00203 return &(index[pos]); 00204 } 00205 00206 void WayPoint::idleVelocityCallback(CoreAPI *api, Header *protocolHeader, UserData wpapi) 00207 { 00208 WayPoint *wp = (WayPoint *)wpapi; 00209 WayPointVelocityACK ack; 00210 00211 if (protocolHeader->length - EXC_DATA_SIZE <= sizeof(ack)) 00212 memcpy((unsigned char *)&ack, ((unsigned char *)protocolHeader) + sizeof(Header), 00213 (protocolHeader->length - EXC_DATA_SIZE)); 00214 else 00215 { 00216 API_LOG(api->getDriver(), ERROR_LOG, "ACK is exception, session id %d,sequence %d\n", 00217 protocolHeader->sessionID, protocolHeader->sequenceNumber); 00218 return; 00219 } 00220 api->decodeMissionStatus(ack.ack); 00221 wp->info.idleVelocity = ack.idleVelocity; 00222 API_LOG(api->getDriver(), STATUS_LOG, "Current idle velocity: %f", wp->info.idleVelocity); 00223 } 00224 00225 void WayPoint::readInitDataCallback(CoreAPI *api, Header *protocolHeader, UserData wpapi) 00226 { 00227 WayPoint *wp = (WayPoint *)wpapi; 00228 WayPointInitACK ack; 00229 00230 if (protocolHeader->length - EXC_DATA_SIZE <= sizeof(ack)) 00231 memcpy((unsigned char *)&ack, ((unsigned char *)protocolHeader) + sizeof(Header), 00232 (protocolHeader->length - EXC_DATA_SIZE)); 00233 else 00234 { 00235 API_LOG(api->getDriver(), ERROR_LOG, "ACK is exception, session id %d,sequence %d\n", 00236 protocolHeader->sessionID, protocolHeader->sequenceNumber); 00237 return; 00238 } 00239 00240 api->decodeMissionStatus(ack.ack); 00241 wp->info = ack.data; 00242 API_LOG(api->getDriver(), STATUS_LOG, "Index number: %d\n", wp->info.indexNumber); 00243 } 00244 00245 void WayPoint::uploadIndexDataCallback(CoreAPI *api, Header *protocolHeader, UserData wpapi __UNUSED) 00246 { 00247 WayPointDataACK ack; 00248 00249 if (protocolHeader->length - EXC_DATA_SIZE <= sizeof(ack)) 00250 memcpy((unsigned char *)&ack, ((unsigned char *)protocolHeader) + sizeof(Header), 00251 (protocolHeader->length - EXC_DATA_SIZE)); 00252 else 00253 { 00254 API_LOG(api->getDriver(), ERROR_LOG, "ACK is exception, session id %d,sequence %d\n", 00255 protocolHeader->sessionID, protocolHeader->sequenceNumber); 00256 return; 00257 } 00258 api->decodeMissionStatus(ack.ack); 00259 API_LOG(api->getDriver(), STATUS_LOG, "Index number: %d\n", ack.index); 00260 } 00261 00262 void WayPoint::setIndex(WayPointData *value, size_t pos) 00263 { 00264 if (index == 0) 00265 { 00266 index = new WayPointData[info.indexNumber]; 00267 if (index == NULL) 00268 { 00269 API_LOG(api->getDriver(), ERROR_LOG, "Lack of memory\n"); 00270 return; 00271 } 00272 } 00273 index[pos] = *value; 00274 for (int i = 0; i < 8; ++i) index[pos].reserved[i] = 0; 00275 }