Ejemplo n.º 1
0
    def test_float_signed(self):
        from QGIS_FMV.klvdata.common import float_to_bytes

        with self.subTest("Signed -20.0"):
            self.assertEqual(
                float_to_bytes(-20.0,
                               _domain=(-(2**15 - 1), 2**15 - 1),
                               _range=(-20, 20)), b'\x80\x01')

        with self.subTest("Signed 0.0"):
            self.assertEqual(
                float_to_bytes(0.0,
                               _domain=(-(2**15 - 1), 2**15 - 1),
                               _range=(-20, 20)), b'\x00\x00')

        with self.subTest("Signed -0.4315"):
            self.assertEqual(
                float_to_bytes(-0.4315,
                               _domain=(-(2**15 - 1), 2**15 - 1),
                               _range=(-20, 20)), b'\xFD\x3D')

        with self.subTest("Signed 20.0"):
            self.assertEqual(
                float_to_bytes(20.0,
                               _domain=(-(2**15 - 1), 2**15 - 1),
                               _range=(-20, 20)), b'\x7F\xFF')
Ejemplo n.º 2
0
    def test_float_unsigned(self):
        from QGIS_FMV.klvdata.common import float_to_bytes

        with self.subTest("Unsigned 0.0"):
            self.assertEqual(
                float_to_bytes(0.0, _domain=(0, 2**16 - 1), _range=(0, 360)),
                b'\x00\x00')

        with self.subTest("Unsigned 159.974"):
            self.assertEqual(
                float_to_bytes(159.974,
                               _domain=(0, 2**16 - 1),
                               _range=(0, 360)), b'\x71\xC2')

        with self.subTest("Unsigned 360.0"):
            self.assertEqual(
                float_to_bytes(360.0, _domain=(0, 2**16 - 1), _range=(0, 360)),
                b'\xFF\xFF')
Ejemplo n.º 3
0
    def CreateMISB(self):
        ''' Create MISB Video '''
        ''' Only tested using DJI Data '''
        # Create ProgressBar
        self.iface.messageBar().clearWidgets()
        progressMessageBar = self.iface.messageBar().createMessage("Creating video packets...")
        progress = QProgressBar()
        progress.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        progressMessageBar.layout().addWidget(progress)
        self.iface.messageBar().pushWidget(progressMessageBar, QGis.Info)
        
        QApplication.setOverrideCursor(Qt.WaitCursor)
        QApplication.processEvents()
                
        HFOV = self.sp_hfov.value()
        #VFOV = self.sp_vfov.value()
        
        index = self.cmb_telemetry.currentIndex()
        out_record = self.cmb_telemetry.itemData(index)
        rowCount = self.GetRows(out_record)
        progress.setMaximum(rowCount)
        
        d = {}
        with open(out_record) as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                date_start = datetime.strptime(row["CUSTOM.updateTime"], '%Y/%m/%d %H:%M:%S.%f')
                break
        
        with open(out_record) as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                for k in row:
                    stripK = k.strip()
                    stripV = row[k].strip()
                    d[stripK] = stripV    
            
                # We create the klv file for every moment
                sizeTotal = 0
                bufferData = b''
                cnt = 0

                for k, v in d.items():
                    try:
                        if k == "CUSTOM.updateTime":
                            # We prevent it from failing in the exact times that don't have milliseconds
                            try:
                                date_end = datetime.strptime(v, '%Y/%m/%d %H:%M:%S.%f')
                            except:
                                date_end = datetime.strptime(v, '%Y/%m/%d %H:%M:%S')
    
                            _bytes = datetime_to_bytes(date_end)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key2 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes
                        
                        # Platform Heading Angle
                        if k == "OSD.yaw":
                            OSD_yaw = float(v)
                            _bytes = float_to_bytes(round(OSD_yaw, 4), _domain5, _range5)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key5 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes   
                        
                        # Platform Pitch Angle
                        if k == "OSD.pitch":
                            OSD_pitch = 0.0
                            _bytes = float_to_bytes(round(OSD_pitch, 4), _domain6, _range6)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key6 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes
                        
                        # Platform Roll Angle
                        if k == "OSD.roll":
                            OSD_roll = 0.0
                            _bytes = float_to_bytes(round(OSD_roll, 4), _domain7, _range7)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key7 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes
                        
                        # Sensor Latitude
                        if k == "OSD.latitude":
                            OSD_latitude = float(v)
                            _bytes = float_to_bytes(round(OSD_latitude, 4), _domain13, _range13)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key13 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes
                        
                        # Sensor Longitude
                        if k == "OSD.longitude":
                            OSD_longitude = float(v)
                            _bytes = float_to_bytes(OSD_longitude, _domain14, _range14)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key14 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes

                        # Sensor True Altitude
                        if k == "OSD.altitude [m]":
                            OSD_altitude = float(v)
                            _bytes = float_to_bytes(round(OSD_altitude, 4), _domain15, _range15)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key15 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes
                        
                        # Sensor Ellipsoid Height
                        if k == "OSD.height [m]":
                            OSD_height = float(v)
                            _bytes = float_to_bytes(round(OSD_height, 4), _domain75, _range75)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key75 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes
                        
                        # Sensor Relative Azimuth Angle
                        if k == "GIMBAL.yaw":
                            #GIMBAL_yaw = float(v)
                            GIMBAL_yaw = 0.0
                            _bytes = float_to_bytes(round(GIMBAL_yaw, 4), _domain18, _range18)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key18 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes   
                        
                        # Sensor Relative Elevation Angle
                        if k == "GIMBAL.pitch":
                            GIMBAL_pitch = float(v)
                            _bytes = float_to_bytes(round(GIMBAL_pitch, 4), _domain19, _range19)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key19 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes   
                        
                        # Sensor Relative Roll Angle
                        if k == "GIMBAL.roll":
                            GIMBAL_roll = 0.0
                            _bytes = float_to_bytes(round(GIMBAL_roll, 4), _domain20, _range20)
                            _len = int_to_bytes(len(_bytes))
                            _bytes = _key20 + _len + _bytes
                            sizeTotal += len(_bytes)
                            bufferData += _bytes   
                            
                    except Exception:
                        print("Multiplexer error")
                        continue    
                
                try:
                    # Diference time
                    td = date_end - date_start
                    end_path = self.klv_folder + "/%.1f.klv" % (round(td.total_seconds(), 1))
                    
                    # CheckSum
                    v = abs(hash(end_path)) % (10 ** 4)
                    _bytes = int_to_bytes(v, 4)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key1 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                    
                    # Sensor Horizontal Field of View
                    v = self.sp_hfov.value()
                    _bytes = float_to_bytes(float(v), _domain16, _range16)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key16 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                    
                    # Sensor Vertical Field of View
                    v = self.sp_vfov.value()
                    _bytes = float_to_bytes(float(v), _domain17, _range17)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key17 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes

                    # TODO : Check these calculations
                    # Slant Range                    
                    anlge = 180 + (OSD_pitch + GIMBAL_pitch)
                    slantRange = abs(OSD_altitude / (cos(radians(anlge))))
                    
                    _bytes = float_to_bytes(round(slantRange, 4), _domain21, _range21)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key21 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                    
                    # Target Width
                    # targetWidth = 0.0
                    targetWidth = 2.0 * slantRange * tan(radians(HFOV / 2.0)) 
                    
                    _bytes = float_to_bytes(round(targetWidth, 4), _domain22, _range22)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key22 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                    
                    # Frame Center Latitude
                    angle = 90 + (OSD_pitch + GIMBAL_pitch)
                    tgHzDist = OSD_altitude * tan(radians(angle))
                    r_earth = 6371008.8
                    
                    dy =  tgHzDist * cos(radians(OSD_yaw))
                    framecenterlatitude = OSD_latitude  + (dy / r_earth) * (180 / pi)
                    
                    _bytes = float_to_bytes(round(framecenterlatitude, 4), _domain23, _range23)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key23 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                       
                    # Frame Center Longitude
                    dx = tgHzDist * sin(radians(OSD_yaw))
                    framecenterlongitude = OSD_longitude + (dx / r_earth) * (180 / pi) / cos(OSD_latitude * pi/180)
                    
                    _bytes = float_to_bytes(round(framecenterlongitude, 4), _domain24, _range24)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key24 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                    
                    # Frame Center Elevation 
                    frameCenterElevation = 0.0
                    
                    _bytes = float_to_bytes(frameCenterElevation, _domain25, _range25)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key25 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes

                    # TODO : If we save the corners in the klv have a overflow
#                     # CALCULATE CORNERS COORDINATES
#                     sensor = (OSD_longitude, OSD_latitude, OSD_altitude)
#                     frameCenter = (destPoint[0], destPoint[1], frameCenterElevation)
#                     FOV = (VFOV, HFOV)
#                     others = (OSD_yaw, GIMBAL_yaw, targetWidth, slantRange)
#                     cornerPointUL, cornerPointUR, cornerPointLR, cornerPointLL = CornerEstimationWithoutOffsets(sensor=sensor, frameCenter=frameCenter, FOV=FOV, others=others)
#                      
#                     # Corner Latitude Point 1 (Full)
#                     _bytes = float_to_bytes(round(cornerPointUL[0],4), _domain82, _range82)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key82 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                       
#                     # Corner Longitude Point 1 (Full)
#                     _bytes = float_to_bytes(round(cornerPointUL[1],4), _domain83, _range83)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key83 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                       
#                     # Corner Latitude Point 2 (Full) 
#                     _bytes = float_to_bytes(round(cornerPointUR[0],4), _domain84, _range84)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key84 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                       
#                     # Corner Longitude Point 2 (Full) 
#                     _bytes = float_to_bytes(round(cornerPointUR[1],4), _domain85, _range85)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key85 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                       
#                     # Corner Latitude Point 3 (Full)
#                     _bytes = float_to_bytes(round(cornerPointLR[0],4), _domain86, _range86)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key86 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                        
#                     # Corner Longitude Point 3 (Full)
#                     _bytes = float_to_bytes(round(cornerPointLR[1],4), _domain87, _range87)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key87 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                       
#                     # Corner Latitude Point 4 (Full)
#                     _bytes = float_to_bytes(round(cornerPointLL[0],4), _domain88, _range88)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key88 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
#                        
#                     # Corner Longitude Point 4 (Full)
#                     _bytes = float_to_bytes(round(cornerPointLL[1],4), _domain89, _range89)
#                     _len = int_to_bytes(len(_bytes))
#                     _bytes = _key89 + _len + _bytes
#                     sizeTotal += len(_bytes)
#                     bufferData += _bytes
                     
                    # Platform Pitch Angle (Full)
                    _bytes = float_to_bytes(round(OSD_pitch, 4), _domain90, _range90)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key90 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                     
                    # Platform Roll Angle (Full)
                    _bytes = float_to_bytes(round(OSD_roll, 4), _domain91, _range91)
                    _len = int_to_bytes(len(_bytes))
                    _bytes = _key91 + _len + _bytes
                    sizeTotal += len(_bytes)
                    bufferData += _bytes
                            
                    # set packet header
                    writeData = cle
                    writeData += int_to_bytes(sizeTotal)
                    writeData += bufferData
                    
                    # Write packet
                    f_write = open(end_path, "wb+")
                    f_write.write(writeData)
                    f_write.close()
                    
                    cnt += 1
                    
                    progress.setValue(cnt)
                    
                except Exception as e:
                    print("Multiplexer error : " + str(e))   
 
        QApplication.restoreOverrideCursor() 
        QApplication.processEvents()    
        progress.setValue(rowCount)
        self.iface.messageBar().clearWidgets()
        # We add it to the manager
        _, name = os.path.split(self.video_file)
        self.parent.AddFileRowToManager(name, self.video_file, islocal=True, klv_folder=self.klv_folder)
        # Close dialog
        self.close()
        return
Ejemplo n.º 4
0
 def __bytes__(self):
     return float_to_bytes(self.value, self._domain, self._range,
                           self._error)