def sim_pid_lti_comb(Kp, Ki, Kd, Nfilt): plant = tf([1], [1, 10, 20]) config['sim/clk_freq'] = 1000 seq = [0.] * 2 + [1.] * config['sim/clk_freq'] set_point = drv(t=Float, seq=seq) plant_out = set_point \ | pid_lti_comb(Kp=Kp, Ki=Ki, Kd=Kd, Nfilt=Nfilt, plant=plant) find('/pid_lti_comb/lti.x').producer | scope(title="PID Input") plant_out | scope(title="Plant Output") sim(timeout=len(seq))
def sim_hdl_pid(Kp, Ki, Kd): plant = tf([1], [1, 10, 20]) config['sim/clk_freq'] = 1000 seq = [0.] * 2 + [1.] * config['sim/clk_freq'] * 2 set_point = drv(t=Float, seq=seq) plant_out = set_point \ | hdl_pid_sys(Kp=Kp, Ki=Ki, Kd=Kd, plant=plant) find('/hdl_pid_sys/plant.x').producer | scope(title="PID Output") plant_out | scope(title="Plant Output") sim('/tools/home/tmp', timeout=len(seq))
def comp_pid_lti_comb_ref(Kp, Ki, Kd, Nfilt): plant = tf([1], [1, 10, 20]) config['sim/clk_freq'] = 1000 seq = [0.] * 2 + [1.] * config['sim/clk_freq'] set_point = drv(t=Float, seq=seq) plant_out = set_point \ | pid_lti_comb(Kp=Kp, Ki=Ki, Kd=Kd, Nfilt=Nfilt, plant=plant) plant_out | scope(title="Plant Output") ref_out = set_point \ | pid_lti_feedback(Kp=Kp, Ki=Ki, Kd=Kd, Nfilt=Nfilt, plant=plant) ref_out | scope(title="Continuous Reference") report = [] scoreboard(plant_out, ref_out, report=report, tolerance=2e-2) sim(timeout=len(seq))
def comp_pid_pg_comb_lti_comb(Kp, Ki, Kd, Nfilt): plant = tf([1], [1, 10, 20]) config['sim/clk_freq'] = 1000 seq = [0.] * 2 + [1.] * config['sim/clk_freq'] set_point = drv(t=Float, seq=seq) plant_out = set_point \ | pid_pg_comb(Kp=Kp, Ki=Ki, Kd=Kd, Nfilt=Nfilt, plant=plant) find('/pid_pg_comb/pid.x').producer | scope(title="PID Input") find('/pid_pg_comb/pid.dout').consumer | scope(title="PID Output") plant_out | scope(title="Plant Output") ref_out = set_point \ | pid_lti_comb(Kp=Kp, Ki=Ki, Kd=Kd, Nfilt=Nfilt, plant=plant) ref_out | scope(title="Continuous Reference") report = [] scoreboard(plant_out, ref_out, report=report, tolerance=2e-2) sim(timeout=len(seq))
def hdl_pid_sys(set_point, *, Kp, Ki, Kd, plant): plant_out = Intf(Float) pid_in = set_point - plant_out pid_in | scope(title="PID Input") pid_in = pid_in | Fixp[2, 22] pid_out = pid_in | hdl_pid(Kp=Kp, Ki=Ki, Kd=Kd, sim_cls=SimVerilated) pid_out = pid_out | Float plant_out |= pid_out \ | lti(name='plant', sys=plant, init_x=0) return plant_out
from pygears import gear from pygears.typing import Queue from pygears.lib import drv, flatten from pygears.sim.modules.verilator import SimVerilated from pygears_control.lib import scope from pygears.typing import Float from pygears.sim import sim from pygears.conf.registry import bind from pygears.lib import shred bind('hdl/debug_intfs', ['*']) @gear(hdl={'compile': True, 'inline_conditions': True}) async def accum(din: Queue[Fixp[5, 10], 1]) -> Queue[Fixp[12, 17], 1]: acc = Fixp[12, 17](0) async for (data, eot) in din: acc = acc + data yield (acc, eot) seq = [0.] * 2 + [1.125] * 1000 drv(t=Queue[Fixp[5, 10], 1], seq=[seq]) \ | accum(sim_cls=SimVerilated) \ | flatten \ | Float \ | scope(clk_freq=1) sim('/tools/home/tmp')