Files
balance/dm_imu/plot_imu.py

120 lines
3.2 KiB
Python
Raw Normal View History

new file: .vscode/settings.json new file: Legs_controller.py modified: README.md new file: __pycache__/Legs_controller.cpython-310.pyc new file: __pycache__/Legs_controller.cpython-313.pyc new file: __pycache__/balance.cpython-310.pyc new file: __pycache__/gradio.cpython-310.pyc new file: __pycache__/gradio.cpython-313.pyc new file: app_ui.py new file: balance.py new file: build/.cmake/api/v1/query/client-vscode/query.json new file: build/.cmake/api/v1/reply/cache-v2-ae4a9db768b4bbb36baa.json new file: build/.cmake/api/v1/reply/cmakeFiles-v1-389eb8769a8295e7571d.json new file: build/.cmake/api/v1/reply/codemodel-v2-5ea8cfc0b9263cbe5ae5.json new file: build/.cmake/api/v1/reply/directory-.-Debug-f5ebdc15457944623624.json new file: build/.cmake/api/v1/reply/index-2025-09-11T04-53-20-0515.json new file: build/.cmake/api/v1/reply/target-imu_py-Debug-3913d741f2156d7faae9.json new file: build/.cmake/api/v1/reply/toolchains-v1-3105704d088db7adbfb5.json new file: build/CMakeCache.txt new file: build/CMakeFiles/3.22.1/CMakeCXXCompiler.cmake new file: build/CMakeFiles/3.22.1/CMakeDetermineCompilerABI_CXX.bin new file: build/CMakeFiles/3.22.1/CMakeSystem.cmake new file: build/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp new file: build/CMakeFiles/3.22.1/CompilerIdCXX/a.out new file: build/CMakeFiles/CMakeDirectoryInformation.cmake new file: build/CMakeFiles/CMakeOutput.log new file: build/CMakeFiles/Makefile.cmake new file: build/CMakeFiles/Makefile2 new file: build/CMakeFiles/TargetDirectories.txt new file: build/CMakeFiles/cmake.check_cache new file: build/CMakeFiles/imu_py.dir/DependInfo.cmake new file: build/CMakeFiles/imu_py.dir/build.make new file: build/CMakeFiles/imu_py.dir/cmake_clean.cmake new file: build/CMakeFiles/imu_py.dir/compiler_depend.make new file: build/CMakeFiles/imu_py.dir/compiler_depend.ts new file: build/CMakeFiles/imu_py.dir/depend.make new file: build/CMakeFiles/imu_py.dir/flags.make new file: build/CMakeFiles/imu_py.dir/link.txt new file: build/CMakeFiles/imu_py.dir/progress.make new file: build/CMakeFiles/progress.marks new file: build/Makefile new file: build/cmake_install.cmake new file: build/compile_commands.json new file: dm_imu/bsp_crc.cpp new file: dm_imu/bsp_crc.h new file: dm_imu/imu_data.csv new file: dm_imu/imu_driver.cpp new file: dm_imu/imu_driver.h new file: dm_imu/imu_plot.png new file: dm_imu/plot_imu.py new file: dm_imu/test_imu.cpp new file: imu_data.csv new file: pybind_imu/CMakeLists.txt new file: pybind_imu/README.md new file: pybind_imu/build/CMakeCache.txt new file: pybind_imu/build/CMakeFiles/3.22.1/CMakeCXXCompiler.cmake new file: pybind_imu/build/CMakeFiles/3.22.1/CMakeDetermineCompilerABI_CXX.bin new file: pybind_imu/build/CMakeFiles/3.22.1/CMakeSystem.cmake new file: pybind_imu/build/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp new file: pybind_imu/build/CMakeFiles/3.22.1/CompilerIdCXX/a.out new file: pybind_imu/build/CMakeFiles/CMakeDirectoryInformation.cmake new file: pybind_imu/build/CMakeFiles/CMakeOutput.log new file: pybind_imu/build/CMakeFiles/Makefile.cmake new file: pybind_imu/build/CMakeFiles/Makefile2 new file: pybind_imu/build/CMakeFiles/TargetDirectories.txt new file: pybind_imu/build/CMakeFiles/cmake.check_cache new file: pybind_imu/build/CMakeFiles/imu_py.dir/DependInfo.cmake new file: pybind_imu/build/CMakeFiles/imu_py.dir/build.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/cmake_clean.cmake new file: pybind_imu/build/CMakeFiles/imu_py.dir/compiler_depend.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/compiler_depend.ts new file: pybind_imu/build/CMakeFiles/imu_py.dir/depend.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/flags.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/bsp_crc.cpp.o new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/bsp_crc.cpp.o.d new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/imu_driver.cpp.o new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/imu_driver.cpp.o.d new file: pybind_imu/build/CMakeFiles/imu_py.dir/link.txt new file: pybind_imu/build/CMakeFiles/imu_py.dir/progress.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/pybind_imu.cpp.o new file: pybind_imu/build/CMakeFiles/imu_py.dir/pybind_imu.cpp.o.d new file: pybind_imu/build/CMakeFiles/progress.marks new file: pybind_imu/build/Makefile new file: pybind_imu/build/cmake_install.cmake new file: pybind_imu/build/imu_py.cpython-310-aarch64-linux-gnu.so new file: pybind_imu/example.py new file: pybind_imu/pybind_imu.cpp new file: src/example.py new file: test_imu new file: u2can/DM_CAN.py new file: u2can/DM_Motor_Test.py new file: u2can/LICENSE new file: u2can/README.md new file: u2can/__pycache__/DM_CAN.cpython-310.pyc new file: u2can/__pycache__/DM_CAN.cpython-312.pyc new file: u2can/__pycache__/DM_CAN.cpython-313.pyc new file: u2can/__pycache__/gradio.cpython-313.pyc new file: u2can/__pycache__/motor_interface.cpython-313.pyc new file: u2can/motor_interface.py new file: u2can/requirements.txt new file: ui.log
2025-12-06 22:47:57 +08:00
#!/usr/bin/env python3
"""
绘制 dm_imu/test_imu.cpp 生成的 CSV 数据
使用方式
1. 确保已安装 matplotlibpip install matplotlib
2. 运行 C++ 程序生成 imu_data.csv
3. 执行本脚本
python3 dm_imu/plot_imu.py
4. 脚本会在同目录下生成 `imu_plot.png` 并显示图形窗口如果有可视化环境
"""
import csv
import os
import sys
import matplotlib.pyplot as plt
def read_csv(csv_path):
"""读取 CSV返回每列的数据列表。"""
if not os.path.isfile(csv_path):
print(f"错误:未找到 CSV 文件 {csv_path}", file=sys.stderr)
sys.exit(1)
indices = []
roll = []
pitch = []
yaw = []
accx = []
accy = []
accz = []
gyrox = []
gyroy = []
gyroz = []
with open(csv_path, newline='') as f:
reader = csv.DictReader(f)
for row in reader:
indices.append(int(row['index']))
roll.append(float(row['roll']))
pitch.append(float(row['pitch']))
yaw.append(float(row['yaw']))
accx.append(float(row['accx']))
accy.append(float(row['accy']))
accz.append(float(row['accz']))
gyrox.append(float(row['gyrox']))
gyroy.append(float(row['gyroy']))
gyroz.append(float(row['gyroz']))
return {
'index': indices,
'roll': roll,
'pitch': pitch,
'yaw': yaw,
'accx': accx,
'accy': accy,
'accz': accz,
'gyrox': gyrox,
'gyroy': gyroy,
'gyroz': gyroz,
}
def plot_data(data, out_path):
"""绘制三组曲线(姿态、加速度、陀螺仪)并保存为 PNG。"""
idx = data['index']
plt.figure(figsize=(12, 9))
# 1. 姿态Roll, Pitch, Yaw
ax1 = plt.subplot(3, 1, 1)
ax1.plot(idx, data['roll'], label='Roll')
ax1.plot(idx, data['pitch'], label='Pitch')
ax1.plot(idx, data['yaw'], label='Yaw')
ax1.set_ylabel('Angle (°)')
ax1.set_title('IMU 姿态')
ax1.legend()
ax1.grid(True)
# 2. 加速度X, Y, Z
ax2 = plt.subplot(3, 1, 2)
ax2.plot(idx, data['accx'], label='Acc X')
ax2.plot(idx, data['accy'], label='Acc Y')
ax2.plot(idx, data['accz'], label='Acc Z')
ax2.set_ylabel('Acceleration (g)')
ax2.set_title('加速度')
ax2.legend()
ax2.grid(True)
# 3. 陀螺仪X, Y, Z
ax3 = plt.subplot(3, 1, 3)
ax3.plot(idx, data['gyrox'], label='Gyro X')
ax3.plot(idx, data['gyroy'], label='Gyro Y')
ax3.plot(idx, data['gyroz'], label='Gyro Z')
ax3.set_xlabel('Sample Index')
ax3.set_ylabel('Angular Velocity (°/s)')
ax3.set_title('陀螺仪')
ax3.legend()
ax3.grid(True)
plt.tight_layout()
plt.savefig(out_path)
print(f"绘图已保存至 {out_path}")
# 如果当前环境支持图形界面,显示窗口
try:
plt.show()
except Exception:
pass
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(script_dir, 'imu_data.csv')
out_path = os.path.join(script_dir, 'imu_plot.png')
data = read_csv(csv_path)
plot_data(data, out_path)
if __name__ == '__main__':
main()