epm/User/test/test_pid.py

83 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import unittest
from ctypes import *
from ctypes import cdll
import matplotlib.pyplot as plt
import threading
case_count = 0
class LimitedArray:
def __init__(self, capacity):
self.capacity = capacity
self.array = []
def append(self, element):
if len(self.array) >= self.capacity:
self.array.pop(0)
self.array.append(element)
class TestPIDCases(unittest.TestCase):
# 给定一个X轴和Y轴的参数列表用作后面承载数据
max_point = 50
obsX = LimitedArray(max_point)
obsY = LimitedArray(max_point)
@classmethod
def setUpClass(cls) -> None:
cls.dll = cdll.LoadLibrary('./epm.dll')
print('[TestPIDCases] PID模块开始测试...')
print('----------------------------------------------------')
pass
@classmethod
def tearDownClass(cls) -> None:
print('[TestPIDCases] 结束测试,测试用例%d' % case_count)
print('----------------------------------------------------')
pass
def setUp(self) -> None:
global case_count
case_count += 1
P= c_float(0.2)
I= c_float(0.4)
D= c_float(0.02)
self.dll.pid_controller_init(P,I,D)
pass
def tearDown(self) -> None:
# print("\r")
pass
# def testPID0(self):
# print('PID 0')
# dst = 500
# src = 200
# setpoint = c_float(dst)
# process_variable = c_float(src)
# self.dll.pid_position_control.restype = c_float
# for i in range(100):
# # 往列表插入展示的点的坐标
# self.obsX.append(i)
# process_variable = self.dll.pid_position_control(setpoint, process_variable)
# print(process_variable)
# if process_variable<0:
# process_variable = 0
# self.obsY.append(process_variable)
# process_variable = c_float(process_variable)
# plt.clf() # 清除之前画的图
# plt.plot(self.obsX.array,self.obsY.array,c="blue",lw=4) # 画出当前 ax 列表和 ay 列表中的值的图形
# plt.ylim(0, 1000) # 设置y轴的范围
# plt.axhline(dst, 0, 100,c="r",ls='--',lw=1)#横线
# plt.pause(1/1000)
# plt.ion() # 开启一个画图的窗口
# plt.pause(3)
# pass
if __name__ == '__main__':
unittest.main()