예제 #1
0
파일: camera.py 프로젝트: wuyou33/MAVProxy
def transmit_thread():
    '''thread for image transmit to GCS'''
    state = mpstate.camera_state

    tx_count = 0
    skip_count = 0
    bsend = block_xmit.BlockSender(0,
                                   bandwidth=state.settings.bandwidth,
                                   debug=False)
    state.bsocket = MavSocket(mpstate.mav_master[0])
    state.bsend2 = block_xmit.BlockSender(mss=96,
                                          sock=state.bsocket,
                                          dest_ip='mavlink',
                                          dest_port=0,
                                          backlog=5,
                                          debug=False)
    state.bsend2.set_bandwidth(state.settings.bandwidth2)

    while not state.unload.wait(0.02):
        bsend.tick(packet_count=1000, max_queue=state.settings.maxqueue1)
        state.bsend2.tick(packet_count=1000,
                          max_queue=state.settings.maxqueue2)
        check_commands()
        if state.transmit_queue.empty():
            continue

        (frame_time, regions, im_full, im_640) = state.transmit_queue.get()
        if state.settings.roll_stabilised:
            roll = 0
        else:
            roll = None
        pos = get_plane_position(frame_time, roll=roll)

        # this adds the latlon field to the regions
        log_joe_position(pos, frame_time, regions)

        # filter out any regions outside the boundary
        if state.boundary_polygon:
            regions = cuav_region.filter_boundary(regions,
                                                  state.boundary_polygon, pos)
            regions = cuav_region.filter_regions(
                im_full, regions, min_score=state.settings.minscore)

        state.xmit_queue = bsend.sendq_size()
        state.xmit_queue2 = state.bsend2.sendq_size()
        state.efficiency = bsend.get_efficiency()
        state.bandwidth_used = bsend.get_bandwidth_used()
        state.rtt_estimate = bsend.get_rtt_estimate()

        jpeg = None

        if len(regions) > 0:
            lowscore = 0
            highscore = 0
            for r in regions:
                lowscore = min(lowscore, r.score)
                highscore = max(highscore, r.score)

            if state.settings.transmit:
                # send a region message with thumbnails to the ground station
                thumb = None
                if state.settings.send1:
                    thumb = cuav_mosaic.CompositeThumbnail(
                        cv.GetImage(cv.fromarray(im_full)),
                        regions,
                        quality=state.settings.quality,
                        thumb_size=state.settings.thumbsize)
                    pkt = ThumbPacket(frame_time, regions, thumb,
                                      state.frame_loss, state.xmit_queue, pos)

                    buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL)
                    bsend.set_bandwidth(state.settings.bandwidth)
                    bsend.set_packet_loss(state.settings.packet_loss)
                    bsend.send(buf,
                               dest=(state.settings.gcs_address,
                                     state.settings.gcs_view_port),
                               priority=1)
                # also send thumbnails via 900MHz telemetry
                if state.settings.send2 and highscore >= state.settings.minscore2:
                    if thumb is None or lowscore < state.settings.minscore2:
                        # remove some of the regions
                        regions = cuav_region.filter_regions(
                            im_full,
                            regions,
                            min_score=state.settings.minscore2)
                        thumb = cuav_mosaic.CompositeThumbnail(
                            cv.GetImage(cv.fromarray(im_full)),
                            regions,
                            quality=state.settings.quality,
                            thumb_size=state.settings.thumbsize)
                        pkt = ThumbPacket(frame_time, regions, thumb,
                                          state.frame_loss, state.xmit_queue,
                                          pos)

                        buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL)
                    state.bsend2.set_bandwidth(state.settings.bandwidth2)
                    state.bsend2.send(buf, priority=highscore)

        # Base how many images we send on the send queue size
        send_frequency = state.xmit_queue // 3
        if send_frequency == 0 or (tx_count +
                                   skip_count) % send_frequency == 0:
            jpeg = scanner.jpeg_compress(im_640, state.settings.quality)

        if jpeg is None:
            skip_count += 1
            continue

        # keep filtered image size
        state.jpeg_size = 0.95 * state.jpeg_size + 0.05 * len(jpeg)

        tx_count += 1

        if state.settings.gcs_address is None:
            continue
        bsend.set_packet_loss(state.settings.packet_loss)
        bsend.set_bandwidth(state.settings.bandwidth)
        pkt = ImagePacket(frame_time, jpeg, state.xmit_queue, pos)
        str = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL)
        bsend.send(str,
                   dest=(state.settings.gcs_address,
                         state.settings.gcs_view_port))
예제 #2
0
파일: camera.py 프로젝트: regaleagle/SQUID
def transmit_thread():
    '''thread for image transmit to GCS'''
    state = mpstate.camera_state

    tx_count = 0
    skip_count = 0
    bsend = block_xmit.BlockSender(0, state.bandwidth)

    while not state.unload.wait(0.02):
        bsend.tick()
        if state.transmit_queue.empty():
            continue

        (frame_time, regions, im_full, im_640) = state.transmit_queue.get()
        pos = get_plane_position(frame_time)
        latlon_list = log_joe_position(pos, frame_time, regions)

        state.xmit_queue = bsend.sendq_size()
        state.efficiency = bsend.get_efficiency()
        state.bandwidth_used = bsend.get_bandwidth_used()
        state.rtt_estimate = bsend.get_rtt_estimate()

        jpeg = None

        if len(regions) > 0 and bsend.sendq_size() < 2000:
            # send a region message with thumbnails to the ground station
            thumb = cuav_mosaic.CompositeThumbnail(im_full,
                                                   regions,
                                                   quality=state.quality,
                                                   thumb_size=100)
            bsend.set_bandwidth(state.bandwidth)
            bsend.set_packet_loss(state.packet_loss)
            pkt = ThumbPacket(frame_time, regions, thumb, latlon_list,
                              state.frame_loss, state.xmit_queue)

            # send matches with a higher priority
            bsend.send(cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL),
                       dest=(state.gcs_address, state.gcs_view_port),
                       priority=1)

        # Base how many images we send on the send queue size
        send_frequency = state.xmit_queue // 3
        if send_frequency == 0 or (tx_count +
                                   skip_count) % send_frequency == 0:
            jpeg = scanner.jpeg_compress(im_640, state.quality)

        if jpeg is None:
            skip_count += 1
            continue

        # keep filtered image size
        state.jpeg_size = 0.95 * state.jpeg_size + 0.05 * len(jpeg)

        tx_count += 1

        if state.gcs_address is None:
            continue
        bsend.set_packet_loss(state.packet_loss)
        bsend.set_bandwidth(state.bandwidth)
        pkt = ImagePacket(frame_time, jpeg)
        bsend.send(cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL),
                   dest=(state.gcs_address, state.gcs_view_port))