Ric*_*ard 0 python raspberry-pi
我正在尝试保存并加载传感器的校准数据.我能够读取校准数据并将其保存到文本文件中.查看文本文件时,在一组方括号中有22个由逗号分隔的数字.
我需要以set_calibration函数读取它的方式导入该文本文件.
这是设定校准功能.
def set_calibration(self, data):
"""Set the sensor's calibration data using a list of 22 bytes that
represent the sensor offsets and calibration data. This data should be
a value that was previously retrieved with get_calibration (and then
perhaps persisted to disk or other location until needed again).
"""
# Check that 22 bytes were passed in with calibration data.
if data is None or len(data) != 22:
raise ValueError('Expected a list of 22 bytes for calibration data.')
# Switch to configuration mode, as mentioned in section 3.10.4 of datasheet.
self._config_mode()
# Set the 22 bytes of calibration data.
self._write_bytes(ACCEL_OFFSET_X_LSB_ADDR, data)
# Go back to normal operation mode.
self._operation_mode()
Run Code Online (Sandbox Code Playgroud)
问题是,这是我第一次使用python,我的编码经验非常少.我试过这个:
calfile=open("calibrate.txt","r")
caldata=calfile.readlines()
calfile.close()
Run Code Online (Sandbox Code Playgroud)
之后,caldata将打印为"['[242,255,0,0,27,0,65,2,139,3,174,255,255,255,255,255,0,0,232,3, 102,3]']",但set_calibration函数返回"预期的校准数据的22字节列表".错误.当运行时
bno.set_calibration(caldata)
Run Code Online (Sandbox Code Playgroud)
我不确定这会有多大帮助,但传感器是Adafruit BNO055,而我正在使用Raspberry Pi运行它.我有一种强烈的感觉,我滥用和/或滥用阅读功能,但是,就像我说的,我对此很新.
calfile.readlines()这不是你需要在这里使用的.如果你的数字全部在文件的不同行上,没有逗号或方括号,那就是这样:
23
51
32
Run Code Online (Sandbox Code Playgroud)
因为你的数字已经是列表式的格式(即用逗号和方括号),我们需要将它转换为真实的列表而不是字符串表示形式.要做到这一点,更换之间的代码calfile=open...,并calfile.close()使用此:
caldata=[int(x.strip()) for x in calfile.read()[1:-1].split(",")]
Run Code Online (Sandbox Code Playgroud)
这是一个列表解析,它将文件的内容转换为实际的Python列表.