L
@lngz 在 OpenMV脱机执行与点击执行Tx、Ty有差异 中说:
# Untitled - By: zgm - 周五 12月 06 2024
import sensor, image, time, pyb, machine, math
from pyb import UART, Pin, LED, ExtInt, RTC
import onewire
from ds18x20 import DS18X20
mydebug = False
file_path = 'data\\'
fram_list = [8, 9, 10, 11, 27, 32, 36, 38]
# 读取配置参数
def read_args():
with open(file_path + "arg_log.txt", 'r') as f:
# 0时间,1分辨率,2.3窗口大小,4.5.6.7.8.9原点坐标ox,oy,oz, oRx,oRy,oRz, 10.11.12k值kx, ky, kz,13.14.15.16.17.18阈值tx,ty,tz,tRx,tRy,tRz, 19n组重复求平均
args = f.readline().split(',')
return args
# 读取温度相关函数
def read_temperture():
# 设置数据引脚
data_pin = Pin('P0') # 根据实际连接情况选择引脚
# data_pin = '0'
# 创建1-Wire总线对象
ow = onewire.OneWire(data_pin)
# 创建DS18B20传感器对象
temp_sensor = DS18X20(ow)
# 扫描并获取传感器地址
roms = temp_sensor.scan()
# 发送温度转换命令
temp_sensor.convert_temp()
time.sleep_ms(750) # 等待温度转换完成
temperture_list = []
# 读取温度值
for rom in roms:
temp = temp_sensor.read_temp(rom)
temperture_list.append(temp)
return temperture_list
# 初始化相机
def sensorinit(args):
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
# WQXGA2(2592x1944),QXGA(2048x1536),UXGA(1600x1200),SVGA(800x600),VGA(640x480),QVGA(320x240),QQVGA(160x120),QQQVGA(80x60)
sensor.set_framesize(fram_list[6])
sensor.set_windowing(544, 408) # (640x480),(320x240),(160x120),(80x60)
sensor.skip_frames(3) # 跳3帧等待图形稳定
sensor.set_auto_gain(False) # 必须关闭自动增益z
sensor.set_auto_whitebal(False) # 必须关闭白平衡
# 相机偏移量计算参数配置
def sensordata(args):
f_x = 2000 # 2.8mm/1.4um=2000,f_x为像素焦距,1.4um是每个像素的尺寸
f_y = 2000
c_x = 0.5 * sensor.width()
c_y = 0.5 * sensor.height()
return f_x, f_y, c_x, c_y
# 弧度转角度
def degrees(radians):
return (180 * radians) / math.pi # 通过弧度求角度
# 剔除坏点
def remove_bad_data(o_data):
# 计算均值
o_data_sum = 0
if len(o_data) == 0:
return False
for j in range(len(o_data)):
o_data_sum = o_data[j] + o_data_sum
u = o_data_sum / len(o_data)
# 计算标准差
o_data_dd = 0
for k in range(len(o_data)):
o_data_dd = o_data_dd + pow(o_data[k] - u, 2)
std = pow(o_data_dd / (len(o_data) - 1), 0.5)
# 剔除坏点 (新加注释,剔除坏点的方法就是,先计算标准差,然后每个值再减去均值得到一个差值,差值与标准差做比较)
data_c = []
for data in range(len(o_data)):
if math.fabs(o_data[data] - u) <= std:
data_c.append(o_data[data])
# 计算剔除坏点后的均值
data_c_sum = 0
for j in range(len(data_c)):
data_c_sum += data_c[j]
data_mean = data_c_sum / len(data_c)
return round(data_mean, 1)
# 计算位移(偏移)信息(六自由度)
def offset(args, f_x, f_y, c_x, c_y):
Tx = []
Ty = []
for i in range(int(30)):
img = sensor.snapshot() # 使用相机拍摄一张照片
tags = img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y)
if len(tags) == 0:
if mydebug:
print("no find apriltags")
else:
for tag in tags:
Tx.append(tag.x_translation)
Ty.append(tag.y_translation)
Tx_mean = remove_bad_data(Tx) * float(15) # x均值(去除坏点)kx是传入的参数
Ty_mean = remove_bad_data(Ty) * float(15) # y均值(去除坏点)
print(tag)
with open(file_path + 'tmp.txt', 'a+', encoding='utf-8') as w:
w.write(str(Tx) + '\n' + str(Ty) + '\n')
w.write(str(tag) + '\n')
print(str(Tx) + '\n' + str(Ty) + '\n')
return Tx_mean, Ty_mean, tag.id
# 数据采集并保存在txt
def get_data(args, f_x, f_y, c_x, c_y):
sensorinit(args) # 相机初始化
Tx_mean, Ty_mean, tag_id = offset(args, f_x, f_y, c_x, c_y) # 计算位移(偏移)信息(六自由度)
Tx_change, Ty_change = round(Tx_mean - float(args[4]), 1), round(Ty_mean - float(args[5]), 1)
temperture_list = read_temperture()
sensor.shutdown(True) # 就测一次,测量关闭相机
localtime = rtc.datetime()
mark = '00'
data = mark + ',' + str(localtime[0]) + '-' + str("%02d" % localtime[1]) + '-' + str(
"%02d" % localtime[2]) + ' ' + str("%02d" % localtime[4]) + ':' + str("%02d" % localtime[5]) + ':' + \
str("%02d" % localtime[6]) + ',' + str(tag_id) + ',' + str(Tx_change) + ',' + str(Ty_change) + ',' + str(temperture_list[0]) + ',' + str(temperture_list[1]) + ',' + str(temperture_list[2])+'\n'
print(data)
with open(file_path + 'tmp.txt', 'a+', encoding='utf-8') as w:
# 记录偏转值
w.write(data)
return Tx_change, Ty_change
# 数据采集
def savedata(args, f_x, f_y, c_x, c_y):
# 数据采集 阈值判断,超阈值立即发送
Tx_change, Ty_change = get_data(args, f_x, f_y, c_x, c_y)
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
red_led.on()
keyflg = 0
rtc = pyb.RTC()
args = read_args() # 读取配置参数
f_x, f_y, c_x, c_y = sensordata(args) # 计算像素焦距和图像中心
localtime = tuple(map(int, args[0].split('-')))
rtc.datetime(localtime) # 配置初始时间
count = 0
while True:
# 采集数据并发送
if keyflg == 0:
if mydebug:
print("start savedata")
blue_led.off()
green_led.on()
red_led.off()
# try:
savedata(args, f_x, f_y, c_x, c_y) # 采集并保存数限制则报据,有超警
# except Exception as e:
# print("Error:", e)
green_led.off()
blue_led.on()
# time.sleep(30)
count += 1
print("count :", count)
if count > 2:
keyflg = 2
# 调试
elif keyflg == 1:
# keyflg = 0
red_led.off()
blue_led.on()
# odmydebug = mydebug # 记录当前调试状态
# mydebug = True # 设置为调试模式
# time.sleep(30)
sensor.shutdown(True) # 关闭摄像头
green_led.off()
blue_led.off()
# mydebug = odmydebug # 恢复调试模式状态
else:
blue_led.off()
red_led.on()
time.sleep(5)
red_led.off()
break
配置文件arg_log.txt内容:
2024-12-06-6-12-59-0-0,6,544,408,-150.0,208.0,-24.1,4.42897,0.608023,2.34857,15,15,15,30,30,30,30,30,30,30
写tmp.txt内容:
[3.31013, 3.31383, 3.31557, 3.32125, 3.31228, 3.31185, 3.31397, 3.30889, 3.31404, 3.31527, 3.31482, 3.31505, 3.31481, 3.31643, 3.31848, 3.31218, 3.31087, 3.31199, 3.3128, 3.31001, 3.31162, 3.30378, 3.30998, 3.31087, 3.31481, 3.31254, 3.31066, 3.31693, 3.31571, 3.3173]
[-2.46145, -2.46579, -2.46668, -2.4695, -2.46326, -2.46407, -2.4643, -2.4623, -2.46512, -2.46665, -2.46687, -2.46588, -2.46729, -2.46692, -2.46867, -2.46418, -2.46378, -2.46588, -2.46577, -2.46346, -2.46363, -2.45978, -2.46279, -2.46316, -2.46713, -2.46653, -2.463, -2.46778, -2.46643, -2.46927]
{"x":190, "y":120, "w":166, "h":167, "id":7, "family":16, "cx":273, "cy":203, "rotation":6.278406, "decision_margin":0.203299, "hamming":0, "goodness":0.000000, "x_translation":3.317296, "y_translation":-2.469271, "z_translation":-24.292740, "x_rotation":3.138917, "y_rotation":6.231083, "z_rotation":6.278406}
00,2024-12-06 12:59:12,7,199.5,-245.5,22.8125,31.4375,22.625
[3.30911, 3.3124, 3.31739, 3.3103, 3.32071, 3.31281, 3.31361, 3.31547, 3.30976, 3.31384, 3.3095, 3.31454, 3.31334, 3.31459, 3.31385, 3.31122, 3.30718, 3.31183, 3.31583, 3.31453, 3.3124, 3.31002, 3.31963, 3.31501, 3.316, 3.3154, 3.31293, 3.31175]
[-2.46144, -2.46356, -2.4676, -2.46257, -2.46867, -2.46507, -2.46516, -2.46665, -2.46276, -2.46582, -2.46155, -2.46681, -2.46636, -2.46478, -2.46578, -2.46375, -2.46034, -2.46353, -2.46692, -2.46482, -2.46346, -2.46348, -2.46967, -2.46585, -2.46673, -2.46758, -2.46597, -2.46567]
{"x":190, "y":120, "w":166, "h":166, "id":7, "family":16, "cx":273, "cy":203, "rotation":6.278444, "decision_margin":0.194185, "hamming":0, "goodness":0.000000, "x_translation":3.311750, "y_translation":-2.465671, "z_translation":-24.254524, "x_rotation":3.168791, "y_rotation":6.252504, "z_rotation":6.278444}
00,2024-12-06 12:59:23,7,199.5,-245.5,22.875,31.4375,22.625
[3.31378, 3.30951, 3.3096, 3.31535, 3.31206, 3.31844, 3.31966, 3.31182, 3.30913, 3.31109, 3.31498, 3.30626, 3.31333, 3.31128, 3.30902, 3.31283, 3.3074, 3.31258, 3.31202, 3.31163, 3.31861, 3.31304, 3.30376, 3.31402, 3.30921, 3.31027, 3.31009, 3.31324, 3.31267]
[-2.46385, -2.46281, -2.46131, -2.46623, -2.46479, -2.46738, -2.46868, -2.46375, -2.46236, -2.46422, -2.46716, -2.46172, -2.4639, -2.46224, -2.46205, -2.46378, -2.45975, -2.46386, -2.46466, -2.46402, -2.46701, -2.46402, -2.45874, -2.46474, -2.46131, -2.46392, -2.46283, -2.46404, -2.46366]
{"x":190, "y":120, "w":166, "h":167, "id":7, "family":16, "cx":273, "cy":203, "rotation":6.275421, "decision_margin":0.209932, "hamming":0, "goodness":0.000000, "x_translation":3.312668, "y_translation":-2.463664, "z_translation":-24.249283, "x_rotation":3.114811, "y_rotation":6.272404, "z_rotation":6.275421}
00,2024-12-06 12:59:35,7,199.5,-245.5,22.875,31.5,22.6875
[3.31431, 3.31462, 3.31126, 3.3124, 3.31433, 3.31936, 3.31287, 3.31344, 3.31205, 3.32782, 3.3115, 3.31164, 3.31686, 3.31647, 3.31211, 3.31701, 3.31928, 3.31123, 3.31516, 3.31696, 3.31874, 3.31187, 3.3137, 3.31443, 3.31215, 3.31839, 3.31341, 3.30985, 3.30897]
[-2.46539, -2.46465, -2.46369, -2.46421, -2.46488, -2.46809, -2.46422, -2.4646, -2.46417, -2.47522, -2.46327, -2.46341, -2.46697, -2.4656, -2.46377, -2.46797, -2.46817, -2.46413, -2.46657, -2.46828, -2.46797, -2.46389, -2.46411, -2.4663, -2.46374, -2.46815, -2.46383, -2.46239, -2.4626]
{"x":190, "y":120, "w":166, "h":166, "id":7, "family":16, "cx":273, "cy":203, "rotation":6.278027, "decision_margin":0.196515, "hamming":0, "goodness":0.000000, "x_translation":3.308969, "y_translation":-2.462605, "z_translation":-24.226555, "x_rotation":3.169530, "y_rotation":6.272390, "z_rotation":6.278027}
00,2024-12-06 12:59:12,7,199.5,-245.5,22.875,31.5,22.6875
直接执行main.py打印内容:
{"x":190, "y":120, "w":166, "h":166, "id":7, "family":16, "cx":272, "cy":203, "rotation":6.265141, "decision_margin":0.168916, "hamming":0, "goodness":0.000000, "x_translation":-9.202141, "y_translation":6.919033, "z_translation":-24.504173, "x_rotation":3.193594, "y_rotation":6.281866, "z_rotation":6.265141}
[-9.1422, -9.17558, -9.20693, -9.16582, -9.22803, -9.15551, -9.20715, -9.17176, -9.20758, -9.2274, -9.215, -9.21051, -9.17986, -9.19855, -9.17892, -9.22544, -9.14652, -9.20039, -9.21184, -9.24758, -9.22094, -9.22238, -9.20255, -9.18648, -9.21952, -9.16781, -9.20214]
[6.87668, 6.90181, 6.92559, 6.89343, 6.94227, 6.88503, 6.92426, 6.89812, 6.92717, 6.93902, 6.93056, 6.92843, 6.90371, 6.91586, 6.90243, 6.93694, 6.87833, 6.91815, 6.92853, 6.95354, 6.93465, 6.93467, 6.92121, 6.90822, 6.93333, 6.89458, 6.91903]
00,2024-12-06 12:59:09,7,12.0,-104.5,22.8125,31.3125,22.625
count : 1
{"x":190, "y":120, "w":166, "h":166, "id":7, "family":16, "cx":272, "cy":203, "rotation":6.265106, "decision_margin":0.166966, "hamming":0, "goodness":0.000000, "x_translation":-9.199024, "y_translation":6.917835, "z_translation":-24.497368, "x_rotation":3.194456, "y_rotation":6.277950, "z_rotation":6.265106}
[-9.20398, -9.14564, -9.18814, -9.23325, -9.17921, -9.16706, -9.24847, -9.18934, -9.19786, -9.18363, -9.23088, -9.19953, -9.07384, -9.19531, -9.19096, -9.15771, -9.11476, -9.18324, -9.22801, -9.18864, -9.21973, -9.20574, -9.11495, -9.19902]
[6.92036, 6.87941, 6.90883, 6.94417, 6.90246, 6.8927, 6.95368, 6.91227, 6.91433, 6.90517, 6.94158, 6.91848, 6.82346, 6.91539, 6.91181, 6.88657, 6.8556, 6.90582, 6.93921, 6.91093, 6.93359, 6.9216, 6.85703, 6.91784]
00,2024-12-06 12:59:18,7,12.0,-104.5,22.8125,31.3125,22.6875
count : 2
{"x":191, "y":120, "w":165, "h":166, "id":7, "family":16, "cx":272, "cy":203, "rotation":6.265394, "decision_margin":0.165816, "hamming":0, "goodness":0.000000, "x_translation":-9.235588, "y_translation":6.944891, "z_translation":-24.594772, "x_rotation":3.203044, "y_rotation":0.010264, "z_rotation":6.265394}
[-9.2225, -9.17507, -9.21946, -9.13687, -9.2307, -9.16623, -9.16377, -9.20612, -9.21534, -9.24745, -9.1968, -9.20597, -9.19703, -9.17002, -9.20272, -9.17203, -9.156, -9.21672, -9.14054, -9.173, -9.1252, -9.25339, -9.19066, -9.167, -9.19414, -9.21725, -9.18978, -9.23559]
[6.93541, 6.89774, 6.93367, 6.8726, 6.94172, 6.894, 6.89113, 6.92472, 6.93115, 6.95584, 6.9157, 6.92339, 6.91672, 6.89616, 6.9228, 6.89791, 6.88684, 6.93182, 6.87509, 6.89724, 6.86343, 6.96022, 6.91123, 6.89414, 6.91433, 6.93128, 6.91187, 6.94489]
00,2024-12-06 12:59:27,7,12.0,-104.5,22.8125,31.25,22.6875
错误描述:通过检查tag打印内容,可以看到其它值基本一致,但唯独x_translation,y_translation值存在较大差异,请尽快帮忙解答下是什么问题?