free-D协议的简单实现


通过python语言来实现一个freeD协议的简单实现,在示例中角度参数都采用的角度制(单位:度),位置坐标位右手坐标系(单位:毫米mm),摄像机的推拉聚焦采用的是采用的百分比(范围:[0~1]),依据实际取样点的个数不同,最好精确到千分位,可以在函数中自己定义实际的取样点数。
def generateZoom(perzvalue,rvalue = 13765):
def generateFocus(perfvalue,rvalue = 18723):
示例中的取样点为13765 和 18723。
示例中的最终传送数据为文本Bytes型,如果实际需要,可以将结果转换为对应的ASCII码,进行二进制传输【 strshexToChrs(strs_hex) 】,直接调用转换函数即可。
示例依据国内的PAL制式,将流模式的30帧/秒,改为了50帧/秒【 Delayms(20) 】。
所有数据的采集来自以Excel表格,数据顺序为'pan','tile','roll','x','y','z','zoom','focus'。后续将会用脚本语言实现一个数据自动生成工具。

数据的接收端实现:

import serial
com_rec = serial.Serial("COM2",38400)
for i in range(1000):
    com_rec.read(58)
    com_rec.flushInput()


暂定接收十秒的数据数据发送端:

def generatePan(panvalue):
    pan = float(panvalue)
    #if the value of pan NOT in range [-180~180]
    while abs(pan) > 180:
        if pan < -180:
            pan += 360
        else:
            pan -= 360
    return angleToByte(pan)
    
def generateTile(tilevalue):
    tile = float(tilevalue)
    #tile in [-90~90],now it's not a perfect solution
    while abs(tile) > 90:
        if tile < -90:
            tile += 180
        else:
            tile -= 180
    return angleToByte(tile)
    
def generateRoll(rollvalue):
    return generatePan(rollvalue)
    
def angleToByte(anglevalue):
    value = float(anglevalue)
    #to binary two's complement
    vbinary = bin(int(value*32768) & 0xffffff)[2:]
    #to hex
    res = hex(int(vbinary,2))[2:]
    res = "{:0>6}".format(res)
    return res
    
def posToByte(posvalue):
    value = float(posvalue)
    #to binary two's complement
    vbinary = bin(int(value*64) & 0xffffff)[2:]
    #to hex
    res = hex(int(vbinary,2))[2:]
    res = "{:0>6}".format(res)
    return res
    
def generatePos(pos_axis):
    axis = float(pos_axis)
    #if the value of position NOT in range (-131072~131072)mm
    while abs(axis) > 131072:
        if axis < -90:
            axis = -131072
        else:
            axis = 131072
    return posToByte(axis)
    
def generateCoordiante(poslist):
    pos_res = ""
    print(poslist)
    if len(poslist) != 3:
        print("position argument error.")
    for i in poslist:
        print(i)
        pos_res += str(generatePos(i))
    print(pos_res)
    return pos_res

def intToByte(intvalue):
    value = int(intvalue)
    #to binary two's complement
    vbinary = bin(value & 0xffffff)[2:]
    #to hex
    res = hex(int(vbinary,2))[2:]
    res = "{:0>6}".format(res)
    return res
    
    # perfvalue is the range of focus [0~1]
    # rvalue is max sample point of focus
def generateFocus(perfvalue,rvalue = 18723):
    fvalue = perfvalue * rvalue
    return intToByte(fvalue)
    
    # perzvalue is the range of zoom [0~1]
    # rvalue is max sample point of zoom
def generateZoom(perzvalue,rvalue = 13765):
    zvalue = perzvalue * rvalue
    return intToByte(zvalue)
    
    # input:the string of all protocal info NOT include checksum
def generateCheckSum(proto_info):
    prstr = proto_info
    res = int('40',16)
    for i in range(0,len(prstr),2):
        subtrack_num = prstr[i:i+2]
        res -= int(subtrack_num,16)
        res %= 256
    res = hex(res)[2:]
    return res
    
def Delayms(delaytime):
    delayT = int(delaytime)
    startT = time.perf_counter() * 1000
    while True:
        offset = int(time.perf_counter() * 1000 - startT)
        if offset >= delayT:
            break
    
def hexascToChr(str_hex):
    numDec = int(str(str_hex),16)
    return chr(numDec)
    
def strshexToChrs(strs_hex):
    res = []
    for i in range(0,len(strs_hex),2):
        str = strs_hex[i:i+2]
        res.append(hexascToChr(str))
    return ''.join(res)
    
    
if __name__ =='__main__':
    import time
    import xlrd
    import serial
    data = xlrd.open_workbook_xls(r'***\axis_data.xls')
    table = data.sheets()[0]
    
    com = serial.Serial("COM1",38400)
    start_time = time.time()
    for  rown in range(table.nrows):
        t1 = time.time()*1000000
        #lcam = ['pan','tile','roll','x','y','z','zoom','focus']
        lcam1 = []
        for i in range(8):
            lcam1.append(table.cell_value(rown,i))

        prlist = ['d1','ff']    
        prlist.append(generatePan(lcam1[0]))
        prlist.append(generateTile(lcam1[1]))
        prlist.append(generateRoll(lcam1[2]))
        prlist.append(generatePos(lcam1[3]))
        prlist.append(generatePos(lcam1[4]))
        prlist.append(generatePos(lcam1[5]))
        prlist.append(generateZoom(lcam1[6]))
        prlist.append(generateFocus(lcam1[7]))
        prlist.append('0000')
        prstr = "".join(prlist)

        CKSum = generateCheckSum(prstr)
        #print(CKSum)
        prres = prstr + CKSum
        t2 = time.time()*1000000

        com.write(prres.encode())
        print("transmit:",prres)
        Delayms(20)


 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
CoAP(Constrained Application Protocol)是一种轻量级的应用层协议,用于在受限的网络环境中传输数据。以下是一个简单的CoAP协议C语言实现的示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <arpa/inet.h> #define COAP_VERSION 1 #define COAP_HEADER_LENGTH 4 typedef struct { uint8_t version; uint8_t type; uint8_t token_length; uint8_t code; uint16_t message_id; } coap_header_t; typedef struct { uint8_t *token; uint16_t token_length; uint8_t *payload; uint16_t payload_length; } coap_message_t; typedef struct { int socket_fd; struct sockaddr_in address; } coap_endpoint_t; typedef struct { uint8_t *data; uint16_t length; } coap_buffer_t; coap_header_t *coap_create_header(uint8_t type, uint8_t code, uint16_t message_id) { coap_header_t *header = calloc(1, sizeof(coap_header_t)); header->version = COAP_VERSION; header->type = type; header->token_length = 0; header->code = code; header->message_id = htons(message_id); return header; } void coap_free_header(coap_header_t *header) { free(header); } coap_message_t *coap_create_message(uint8_t type, uint8_t code, uint16_t message_id) { coap_message_t *message = calloc(1, sizeof(coap_message_t)); message->token = NULL; message->token_length = 0; message->payload = NULL; message->payload_length = 0; coap_header_t *header = coap_create_header(type, code, message_id); message->payload_length += COAP_HEADER_LENGTH; message->payload = calloc(1, message->payload_length); memcpy(message->payload, header, COAP_HEADER_LENGTH); coap_free_header(header); return message; } void coap_free_message(coap_message_t *message) { free(message->token); free(message->payload); free(message); } coap_endpoint_t *coap_create_endpoint(char *ip, int port) { coap_endpoint_t *endpoint = calloc(1, sizeof(coap_endpoint_t)); endpoint->socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); memset(&endpoint->address, 0, sizeof(endpoint->address)); endpoint->address.sin_family = AF_INET; endpoint->address.sin_addr.s_addr = inet_addr(ip); endpoint->address.sin_port = htons(port); return endpoint; } void coap_free_endpoint(coap_endpoint_t *endpoint) { close(endpoint->socket_fd); free(endpoint); } coap_buffer_t *coap_receive(coap_endpoint_t *endpoint) { coap_buffer_t *buffer = calloc(1, sizeof(coap_buffer_t)); socklen_t address_length = sizeof(endpoint->address); int length = recvfrom(endpoint->socket_fd, buffer->data, buffer->length, 0, (struct sockaddr *) &endpoint->address, &address_length); buffer->length = length; return buffer; } void coap_send(coap_endpoint_t *endpoint, coap_message_t *message) { sendto(endpoint->socket_fd, message->payload, message->payload_length, 0, (struct sockaddr *) &endpoint->address, sizeof(endpoint->address)); } int main() { coap_endpoint_t *endpoint = coap_create_endpoint("127.0.0.1", 5683); coap_message_t *message = coap_create_message(0, 1, 1); coap_send(endpoint, message); coap_buffer_t *buffer = coap_receive(endpoint); printf("Received %d bytes: %.*s\n", buffer->length, buffer->length, buffer->data); coap_free_message(message); coap_free_endpoint(endpoint); free(buffer); return 0; } ``` 这个示例实现了CoAP协议的基本功能,包括创建和发送CoAP消息,以及接收和处理CoAP消息。在这个示例中,我们使用了标准的C语言库函数和一些网络相关的函数来实现CoAP协议的功能。如果您想要了解更多关于CoAP协议的信息,请参考CoAP协议的官方网站。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值