00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _MAV2DJI_MAVSENSORS_H_
00010 #define _MAV2DJI_MAVSENSORS_H_
00011
00012
00013 #include <mavlink/common/mavlink.h>
00014 #include <new>
00015 #include <string>
00016 #include <cstdarg>
00017
00018 #include "dji_sdk_dji2mav/modules/mavModule.h"
00019 #include "sensorLocPosNed.h"
00020 #include "sensorAtt.h"
00021 #include "sensorGloPosInt.h"
00022 #include "dji_sdk_dji2mav/log.h"
00023
00024 namespace dji2mav{
00025
00026 class MavSensors : public MavModule {
00027 public:
00035 MavSensors(MavHandler &handler, std::string name,
00036 uint16_t gcsNum, ...) : MavModule(handler, name, 1024) {
00037
00038 DJI2MAV_DEBUG("Going to construct Sensors module with name "
00039 "%s and gcsNum %u...", name.c_str(), gcsNum);
00040
00041 setSensorsHook(NULL);
00042
00043 va_list arg;
00044 va_start(arg, gcsNum);
00045 if(1 == gcsNum) {
00046 setMasterGcsIdx( (uint16_t)va_arg(arg, int) );
00047 } else {
00048 for(uint16_t i = 0; i < gcsNum; ++i) {
00049 employGcsSender( (uint16_t)va_arg(arg, int) );
00050 }
00051 }
00052 va_end(arg);
00053
00054
00055
00056 DJI2MAV_DEBUG("...finish constructing Sensors module.");
00057
00058 }
00059
00060
00061 ~MavSensors() {
00062 DJI2MAV_DEBUG("Going to destruct Sensors module...");
00063 DJI2MAV_DEBUG("...finish destructing Sensors module.");
00064 }
00065
00066
00074 void setLocalPosition(const int32_t* ts, const float* x,
00075 const float* y, const float* z) {
00076
00077 m_locPos.setTimeBootMs(ts);
00078 m_locPos.setX(x);
00079 m_locPos.setY(y);
00080 m_locPos.setZ(z);
00081
00082 }
00083
00084
00092 void setVelocity(const int32_t* ts, const float* vx,
00093 const float* vy, const float* vz) {
00094
00095 m_gloPos.setTimeBootMs(ts);
00096 m_gloPos.setVx(vx);
00097 m_gloPos.setVy(vy);
00098 m_gloPos.setVz(vz);
00099
00100 }
00101
00102
00110 void setAttitudeQuaternion(const int32_t* ts, const float* q0,
00111 const float* q1, const float* q2, const float* q3,
00112 const float* wx, const float* wy, const float* wz) {
00113
00114 m_att.setTimeBootMs(ts);
00115 m_att.setRoll(q0, q1, q2, q3);
00116 m_att.setPitch(q0, q1, q2, q3);
00117 m_att.setYaw(q0, q1, q2, q3);
00118 m_att.setRollSpeed(wx);
00119 m_att.setPitchSpeed(wy);
00120 m_att.setYawSpeed(wz);
00121
00122 }
00123
00124
00127 void setGlobalPosition(const int32_t* ts, const double* lat,
00128 const double* lon, const float* alt, const float* height) {
00129
00130 m_gloPos.setTimeBootMs(ts);
00131 m_gloPos.setLat(lat);
00132 m_gloPos.setLon(lon);
00133 m_gloPos.setAlt(alt);
00134 m_gloPos.setRelativeAlt(height);
00135
00136 }
00137
00138
00144 bool sendSensorsDataToGcs(uint16_t gcsIdx) {
00145 bool ret = true;
00146 mavlink_msg_local_position_ned_encode( getMySysid(),
00147 MAV_COMP_ID_IMU, &m_sendLocPosMsg,
00148 m_locPos.getDataPtr() );
00149 if( !sendMsgToGcs(gcsIdx, m_sendLocPosMsg) ) {
00150 DJI2MAV_ERROR("Fail to send Sensor data locPosNed to GCS "
00151 "#%u!", gcsIdx);
00152 ret = false;
00153 }
00154
00155 mavlink_msg_attitude_encode( getMySysid(),
00156 MAV_COMP_ID_IMU, &m_sendAttMsg, m_att.getDataPtr() );
00157 if( !sendMsgToGcs(gcsIdx, m_sendAttMsg) ) {
00158 DJI2MAV_ERROR("Fail to send Sensor data attitude to GCS "
00159 "#%u!", gcsIdx);
00160 ret = false;
00161 }
00162
00163 mavlink_msg_global_position_int_encode( getMySysid(),
00164 MAV_COMP_ID_IMU, &m_sendGloPosMsg,
00165 m_gloPos.getDataPtr() );
00166 if( !sendMsgToGcs(gcsIdx, m_sendGloPosMsg) ) {
00167 DJI2MAV_ERROR("Fail to send Sensor data GloPosInt to GCS "
00168 "#%u!", gcsIdx);
00169 ret = false;
00170 }
00171
00172 return ret;
00173 }
00174
00175
00180 bool sendSensorsDataToAll() {
00181 bool ret = true;
00182 mavlink_msg_local_position_ned_encode( getMySysid(),
00183 MAV_COMP_ID_IMU, &m_sendLocPosMsg,
00184 m_locPos.getDataPtr() );
00185 if( !sendMsgToAll(m_sendLocPosMsg) ) {
00186 DJI2MAV_ERROR("Fail to send Sensor data locPosNed to some "
00187 "GCS!");
00188 ret = false;
00189 }
00190
00191 mavlink_msg_attitude_encode( getMySysid(),
00192 MAV_COMP_ID_IMU, &m_sendAttMsg, m_att.getDataPtr() );
00193 if( !sendMsgToAll(m_sendAttMsg) ) {
00194 DJI2MAV_ERROR("Fail to send Sensor data attitude to some "
00195 "GCS!");
00196 ret = false;
00197 }
00198
00199 mavlink_msg_global_position_int_encode( getMySysid(),
00200 MAV_COMP_ID_IMU, &m_sendGloPosMsg,
00201 m_gloPos.getDataPtr() );
00202 if( !sendMsgToAll(m_sendGloPosMsg) ) {
00203 DJI2MAV_ERROR("Fail to send Sensor data GloPosInt to some "
00204 "GCS!");
00205 ret = false;
00206 }
00207
00208 return ret;
00209 }
00210
00211
00217 void reactToSensors(uint16_t gcsIdx, mavlink_message_t &msg) {
00218 if(NULL != m_hook) {
00219 m_hook();
00220 }
00221 }
00222
00223
00228 inline void setSensorsHook( void (*func)() ) {
00229 m_hook = func;
00230 }
00231
00232
00237 void passivelyReceive(mavlink_message_t &msg) {
00238 }
00239
00240
00244 void activelySend() {
00245 sendSensorsDataToAll();
00246 usleep(20000);
00247 }
00248
00249
00250 private:
00251 mavlink_message_t m_sendLocPosMsg;
00252 mavlink_message_t m_sendAttMsg;
00253 mavlink_message_t m_sendGloPosMsg;
00254
00255 SensorLocPosNed m_locPos;
00256 SensorAtt m_att;
00257 SensorGloPosInt m_gloPos;
00258
00259 void (*m_hook)();
00260
00261
00262 };
00263
00264 }
00265
00266
00267 #endif