diff --git a/src/uas/UAS.cc b/src/uas/UAS.cc index 363b0a4add6f160a5a2b561a9f53df6116c20b0b..6a8266c7db1b61cfade5de88502e5df0fa02ac0c 100644 --- a/src/uas/UAS.cc +++ b/src/uas/UAS.cc @@ -888,7 +888,53 @@ void UAS::receiveMessage(LinkInterface* link, mavlink_message_t message) emit textMessageReceived(uasId, message.compid, severity, text); } break; - case MAVLINK_MSG_ID_DEBUG_VECT: + + case MAVLINK_MSG_ID_DATA_TRANSMISSION_HANDSHAKE: + { + qDebug() << "RECIEVED ACK TO GET IMAGE"; + mavlink_data_transmission_handshake_t p; + mavlink_msg_data_transmission_handshake_decode(&message, &p); + imageSize = p.size; + imagePackets = p.packets; + imagePayload = p.payload; + imageQuality = p.jpg_quality; + imageStart = MG::TIME::getGroundTimeNow(); + } + break; + + case MAVLINK_MSG_ID_ENCAPSULATED_DATA: + { + mavlink_encapsulated_data_t img; + mavlink_msg_encapsulated_data_decode(&message, &img); + int seq = img.seqnr; + int pos = seq * imagePayload; + + for (int i = 0; i < imagePayload; ++i) + { + if (pos <= imageSize) + { + imageRecBuffer[pos] = img.data[i]; + } + ++pos; + } + + ++imagePacketsArrived; + + // emit signal if all packets arrived + if ((imagePacketsArrived == imagePackets)) + { + image.loadFromData(imageRecBuffer); + emit imageReady(this); + // Restart statemachine + imagePacketsArrived = 0; + + //this->requestImage(); + //qDebug() << "SENDING REQUEST TO GET NEW IMAGE FROM SYSTEM" << uasId; + } + } + break; + + case MAVLINK_MSG_ID_DEBUG_VECT: { mavlink_debug_vect_t vect; mavlink_msg_debug_vect_decode(&message, &vect); @@ -1297,6 +1343,32 @@ void UAS::getStatusForCode(int statusCode, QString& uasState, QString& stateDesc } } +QImage UAS::getImage() +{ + return image; +} + +void UAS::requestImage() +{ + qDebug() << "trying to get an image from the uas..."; + + if (imagePacketsArrived == 0) + { + mavlink_message_t msg; + mavlink_msg_data_transmission_handshake_pack(mavlink->getSystemId(), mavlink->getComponentId(), &msg, DATA_TYPE_JPEG_IMAGE, 0, 0, 0, 50); + sendMessage(msg); + } + else if (MG::TIME::getGroundTimeNow() - imageStart >= 1000) + { + // handshake happened more than 1 second ago, packets should have arrived by now + // maybe we missed some packets (dropped along the way) + image.loadFromData(imageRecBuffer); + emit imageReady(this); + // Restart statemachine + imagePacketsArrived = 0; + } + // default else, wait? +} /* MANAGEMENT */ diff --git a/src/uas/UAS.h b/src/uas/UAS.h index 3644256094f84f4a48af253ac56563228656b510..6a1ad5fa010f8d455fb93a41daacac9426c6d92d 100644 --- a/src/uas/UAS.h +++ b/src/uas/UAS.h @@ -162,6 +162,16 @@ protected: //COMMENTS FOR TEST UNIT double yaw; quint64 lastHeartbeat; ///< Time of the last heartbeat message QTimer* statusTimeout; ///< Timer for various status timeouts + + int imageSize; ///< Image size being transmitted (bytes) + int imagePackets; ///< Number of data packets being sent for this image + int imagePacketsArrived; ///< Number of data packets recieved + int imagePayload; ///< Payload size per transmitted packet (bytes). Standard is 254, and decreases when image resolution increases. + int imageQuality; ///< JPEG-Quality of the transmitted image (percentage) + QByteArray imageRecBuffer; ///< Buffer for the incoming bytestream + QImage image; ///< Image data of last completely transmitted image + quint64 imageStart; + QMap* > parameters; ///< All parameters bool paramsOnceRequested; ///< If the parameter list has been read at least once int airframe; ///< The airframe type @@ -181,6 +191,8 @@ public: UASWaypointManager* getWaypointManager() { return &waypointManager; } int getSystemType(); + QImage getImage(); + void requestImage(); // ? int getAutopilotType() {return autopilot;} public slots: @@ -323,6 +335,8 @@ signals: /** @brief Propagate a heartbeat received from the system */ void heartbeat(UASInterface* uas); void imageStarted(quint64 timestamp); + /** @brief A new camera image has arrived */ + void imageReady(UASInterface* uas); protected: /** @brief Get the UNIX timestamp in milliseconds */