def less_to_channel_ref(): a = BooleanValue(False) b = ChannelReference("Aliases/DesiredRPM") b.value = 5.0 localhost_wait() a.value = 1 < b.value return a.value
def wait_multitask(): ret = BooleanValue(False) init1 = DoubleValue(0) end1 = DoubleValue(0) init2 = DoubleValue(0) end2 = DoubleValue(0) tot_init = DoubleValue(0) tot_end = DoubleValue(0) tot_init.value = seqtime() with multitask() as mt: @task(mt) def f1(): init1.value = seqtime() nivs_yield() end1.value = wait(DoubleValue(1)) - init1.value @task(mt) def f2(): init2.value = seqtime() end2.value = wait(DoubleValue(3)) - init2.value tot_end.value = seqtime() - tot_init.value ret.value = tot_end.value >= 3 and tot_end.value <= 4 and \ end1.value >= 1 and end1.value <= 2 and \ end2.value >= 3 and end2.value <= 4 return ret.value
def if_condition_variable(): var = BooleanValue(0) if var.value: var.value = True else: var.value = False return var.value
def while_condition_identity_operator(): var = BooleanValue(True) ret = I32Value(0) while var.value is True: ret.value = 1 var.value = False return ret.value
def gt_equal_to_channel_ref(): a = BooleanValue(False) b = ChannelReference("Aliases/DesiredRPM") b.value = 1.0 localhost_wait() a.value = 1 >= b.value return a.value
def state_machine_example(): state = I32Value(0) iters = I32Value(0) amplitude = DoubleValue(1000) stop = BooleanValue(False) output = ChannelReference('Aliases/DesiredRPM') while stop.value != True and iters.value < 10: # noqa: E712 NI recommends you use comparison instead of identity. state.value = rand(7) if state.value == 0: wait(2) elif state.value == 1: sine_wave(output, amplitude, 1, 0, 0, 2) elif state.value == 2: square_wave(output, amplitude, 5, 0, 0, 50, 2) elif state.value == 3: triangle_wave(output, amplitude, 1, 0, 0, 2) elif state.value == 4: uniform_white_noise_wave(output, amplitude, tickcountus(), 2) elif state.value == 5: ramp(output, -amplitude.value, amplitude, 2) elif state.value == 6: sawtooth_wave(output, amplitude, 1, 0, 0, 2) else: stop.value = True iters.value += 1 state.value = rand(7)
def finite_recursion(x): res = BooleanValue(False) if x < 0: res.value = True else: finite_recursion(x - 1) return res.value
def equal_to_channel_ref(): a = BooleanValue(True) b = ChannelReference("Aliases/DesiredRPM") b.value = 5.0 localhost_wait() a.value = 1 == b.value return a.value
def call_getlasterror(): generate_error(1, "Continue1", ErrorAction.ContinueSequenceExecution) generate_error(2, "Continue2", ErrorAction.ContinueSequenceExecution) a = BooleanValue(False) b = I64Value(0) b.value = getlasterror() if b.value == 2: a.value = True return a.value
def while_multiple_statements(): cond = BooleanValue(False) ret_var = I32Value(0) while cond.value is False: cond.value = True ret_var.value = 1 ret_var.value = 2 ret_var.value = 3 return ret_var.value
def wait_const(): init = DoubleValue(0) end = DoubleValue(0) ret = BooleanValue(False) init.value = seqtime() end.value = wait(DoubleValue(1)) - init.value if end.value >= 1 and end.value <= 1.1: ret.value = True return ret.value
def wait_const_negative(): init = DoubleValue(0) end = DoubleValue(0) ret = BooleanValue(True) init.value = seqtime() end.value = wait(DoubleValue(-1)) - init.value if end.value >= 0.1 or end.value < 0: ret.value = False return ret.value
def wait_subseq_call(): init = DoubleValue(0) end = DoubleValue(0) ret = BooleanValue(False) init.value = seqtime() end.value = wait(_return_one()) - init.value if end.value >= 1 and end.value <= 1.1: ret.value = True return ret.value
def wait_until_next_ms(): init = I64Value(0) end = I64Value(0) ret = BooleanValue(False) init.value = tickcountms() end.value = wait_until_next_ms_multiple(DoubleValue(231)) - init.value if end.value <= 231 and end.value >= 0: ret.value = True return ret.value
def inbounds_check(test_value, upper, lower): """Returns True if lower <= value <= upper. Performs an inbounds check. """ result = BooleanValue(False) # Typically, you could write this instruction as lower.value <= test_value.value <= upper.value # because Python supports cascading operators. However, for real-time sequences, # you must write all comparisons using only two operands. result.value = test_value.value >= lower.value and test_value.value <= upper.value return result.value
def wait_nivstype(): init = DoubleValue(0) duration = DoubleValue(1) end = DoubleValue(0) ret = BooleanValue(False) init.value = seqtime() end.value = wait(duration) - init.value if end.value >= duration.value and end.value <= duration.value + 0.1: ret.value = True return ret.value
def wait_until_next_us(): init = I64Value(0) end = I64Value(0) ret = BooleanValue(False) init.value = tickcountus() end.value = wait_until_next_us_multiple(DoubleValue(17000)) - init.value # give this one a few us buffer because come on, no way python can do it all that fast if end.value <= 22000 and end.value >= 0: ret.value = True return ret.value
def wait_until_settled_timeout(): pass_test = BooleanValue(False) time = DoubleValue(0) time.value = seqtime() pass_test.value = wait_until_settled(DoubleValue(100), DoubleValue(90), DoubleValue(0), DoubleValue(2), DoubleValue(1)) time.value = seqtime() - time.value pass_test.value &= time.value > 1 and time.value < 1.1 return pass_test.value
def call_clearfault(): a = ChannelReference("Aliases/DesiredRPM") b = DoubleValue(0) c = BooleanValue(False) # Store initial channel value b.value = a.value fault(a, 1001) clearfault(a) # Try to assign back to the channel the initial value a.value = b.value # If everything went well the initial value and the current value should now be equal c.value = b.value == a.value return c.value
def wait_until_settled(signal, upper_limit, lower_limit, settle_time, timeout): """ Waits until `signal` settles for the amount of time you specify in `settle_time`. Args: signal(:any:`DoubleValue`): value to monitor. upper_limit(:any:`DoubleValue`): maximum value of the settle range. lower_limit(:any:`DoubleValue`): minimum value of the settle range. settle_time(:any:`DoubleValue`): time, in seconds, `signal` must stay inside the settle range. timeout(:any:`DoubleValue`): seconds to wait before the function times out. Returns: bool: True: The signal failed to settle before the operation timed out. False: The signal settled before the operation timed out. This wait is non-blocking, so other tasks will run while this wait executes. """ init_time = DoubleValue(0) curr_time = DoubleValue(0) in_limits_duration = DoubleValue(0) in_limits_start = DoubleValue(0) timed_out = BooleanValue(False) in_limits = BooleanValue(False) first = BooleanValue(True) init_time.value = seqtime() # first is just used to emulate a do-while loop while first.value \ or ((not in_limits.value or (in_limits_duration.value < settle_time.value)) and not timed_out.value): first.value = False curr_time.value = seqtime() if signal.value <= upper_limit.value and signal.value >= lower_limit.value: if not in_limits.value: in_limits.value = True in_limits_start.value = curr_time.value in_limits_duration.value = 0 else: in_limits_duration.value = curr_time.value - in_limits_start.value else: in_limits.value = False if timeout.value >= 0: timed_out.value = (curr_time.value - init_time.value) > timeout.value nivs_yield() return timed_out.value
def enter_autonomous(): error_status = BooleanValue(False) enter_autonomous_complete = BooleanValue(False) enter_autonomous_succeeded = BooleanValue(False) ReAX_GearA_Disable_chan = ChannelReference( 'Targets/TruckSim_TTC580/User Channels/NI-XNET/VCAN-B/ReAX_GearA (486535187)/ReAX_GearA (486535187) Disable') ReAX_GearB_Disable_chan = ChannelReference( 'Targets/TruckSim_TTC580/User Channels/NI-XNET/VCAN-B/ReAX_GearB (486535443)/ReAX_GearB (486535443) Disable') Power_On_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/DAQ/PXI1Slot2/Digital Output/port0/Power on') SystemStatus_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/Server CAN/Incoming/Single-Point' '/SystemStatus(69785)/SystemStatus') Autonomous_Drive_chan = ChannelReference('Targets/TruckSim_TTC580/Hardware/Chassis/DAQ' '/PXI1Slot2/Digital Output/port0/Autonomous Drive') MODE_TRANS_chan = ChannelReference( 'Targets/TruckSim_TTC580/Simulation Models/Models/trucksim_LVRT/Inports/MODE_TRANS') with multitask() as mt: @task(mt) def enter_30(): ReAX_GearA_Disable_chan.value = 0 ReAX_GearB_Disable_chan.value = 0 # Power on button Power_On_chan.value = 1 # Check System Status if SystemStatus_chan.value != 25: error_status.value = True # Push autonomous driving button Autonomous_Drive_chan.value = 1 wait(DoubleValue(1)) Autonomous_Drive_chan.value = 0 # Check System Status if SystemStatus_chan.value != 30: error_status.value = True MODE_TRANS_chan.value = 18 enter_autonomous_succeeded.value = True @task(mt) def monitor_error_status(): while enter_autonomous_complete.value is False: if error_status.value: stop_task(enter_30) enter_autonomous_complete.value = True enter_autonomous_succeeded.value = False nivs_yield() return enter_autonomous_succeeded.value
def ifexp_nivsboolvar_test_int_assign1(): a = I32Value(0) b = BooleanValue(False) c = I32Value(1) d = I32Value(0) a.value = c.value if not b.value else d.value return a.value
def if_elif_return_fails(): a = BooleanValue(True) if False: pass elif True: return a.value else: pass
def enter_30(): error_status = BooleanValue(True) # channel references Power_On_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/DAQ/PXI1Slot2/Digital Output/port0/Power on') ReAX_GearA_Disable_chan = ChannelReference( 'Targets/TruckSim_TTC580/User Channels/NI-XNET/VCAN-B/ReAX_GearA (486535187)/ReAX_GearA (486535187) Disable') ReAX_GearB_Disable_chan = ChannelReference( 'Targets/TruckSim_TTC580/User Channels/NI-XNET/VCAN-B/ReAX_GearB (486535443)/ReAX_GearB (486535443) Disable') RemoteAccelPedalPos_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/Vehicle CAN/Incoming/Single-Point/EEC2_CMD (418382648)/RemoteAccelPedalPos') XBRBrakeDemand_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/Vehicle CAN/Incoming/Single-Point/XBR (201591804)/XBRBrakeDemand') XPRPReq_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/VCAN-B/Incoming/Single-Point/XPRCmd (218060344)/XPRPReq') Autonomous_Drive_chan = ChannelReference('Targets/TruckSim_TTC580/Hardware/Chassis/DAQ' '/PXI1Slot2/Digital Output/port0/Autonomous Drive') MODE_TRANS_chan = ChannelReference( 'Targets/TruckSim_TTC580/Simulation Models/Models/trucksim_LVRT/Inports/MODE_TRANS') SystemStatus_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/Server CAN/Incoming/Single-Point/SystemStatus (69785)/SystemStatus') # *** end initialization # *** steps # start sending lateral drive ReAX_GearA_Disable_chan.value = 0 ReAX_GearB_Disable_chan.value = 0 # power on VCU Power_On_chan.value = 1 wait(DoubleValue(3)) if SystemStatus_chan.value != 25: error_status.value = False Autonomous_Drive_chan.value = 1 wait(DoubleValue(1)) Autonomous_Drive_chan.value = 0 if SystemStatus_chan.value != 30: error_status.value = False MODE_TRANS_chan.value = 18 return error_status.value
def run_engine_demo_advanced(): """Run the engine_demo_advanced example. To handle a condition that stops a task (such as, the engine temperature rising above a safe value), use a try/finally block. Regardless of the result of the execution, the finally block can be used to safely shut down the engine. """ try: warmup_succeeded = BooleanValue(False) engine_power = ChannelReference('Aliases/EnginePower') desired_rpm = ChannelReference('Aliases/DesiredRPM') actual_rpm = ChannelReference('Aliases/ActualRPM') engine_temp = ChannelReference('Aliases/EngineTemp') engine_power.value = True warmup_succeeded.value = engine_demo_advanced(desired_rpm, actual_rpm, engine_temp) finally: engine_power.value = False desired_rpm.value = 0 return warmup_succeeded.value
def engine_demo_advanced(desired_rpm, actual_rpm, engine_temp): """Turns on the engine, sets it to the desired rpm, and monitors the engine temperature.""" # Use the following local variable declarations to keep track of the test's status: warmup_complete = BooleanValue(False) warmup_succeeded = BooleanValue(False) test = BooleanValue(False) # Create a multitask with two tasks: one for setting rpm values and another for monitoring. # In general, a multitask can contain as many tasks as desired. The tasks will all execute asynchronously, # but not in parallel. For more information on multitask behavior, refer to the VeriStand help. with multitask() as mt: # You must decorate tasks using the following notation. # The following code shows example of a task. @task(mt) def engine_warmup(): """Spawns a task to wait for the actual rpm signal to settle.""" desired_rpm.value = 2500 # Waits for up to 120 seconds for the actual RPM to be between 999999 and 2450 for 25 seconds. wait_until_settled(actual_rpm, 9999999, 2450, 25, 120) desired_rpm.value = 8000 wait_until_settled(actual_rpm, 9999999, 7800, 25, 120) warmup_complete.value = True test.value = check_val(desired_rpm, 8000) @task(mt) def monitor_temp(): """Spawns a task to monitor engine temperature. If the temperature rises above 110 degrees (C), the previous task will stop. """ while warmup_complete.value is False: if engine_temp.value > 110: stop_task(engine_warmup) warmup_complete.value = True warmup_succeeded.value = False nivs_yield() # You can use a return value, but some restrictions will apply. # For example, the function may only return previously declared variables. return warmup_succeeded.value
def self_check(): # *** initialization # variables TestPass = BooleanValue(True) # channel references Power_On_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/DAQ/PXI1Slot2/Digital Output/port0/Power on') ReAX_GearA_Disable_chan = ChannelReference( 'Targets/TruckSim_TTC580/User Channels/NI-XNET/VCAN-B/ReAX_GearA (486535187)/ReAX_GearA (486535187) Disable') ReAX_GearB_Disable_chan = ChannelReference( 'Targets/TruckSim_TTC580/User Channels/NI-XNET/VCAN-B/ReAX_GearB (486535443)/ReAX_GearB (486535443) Disable') RemoteAccelPedalPos_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/Vehicle CAN/Incoming/Single-Point/EEC2_CMD (418382648)/RemoteAccelPedalPos') XBRBrakeDemand_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/Vehicle CAN/Incoming/Single-Point/XBR (201591804)/XBRBrakeDemand') XPRPReq_chan = ChannelReference( 'Targets/TruckSim_TTC580/Hardware/Chassis/NI-XNET/CAN/VCAN-B/Incoming/Single-Point/XPRCmd (218060344)/XPRPReq') # *** end initialization # *** steps # start sending lateral drive ReAX_GearA_Disable_chan.value = 0 ReAX_GearB_Disable_chan.value = 0 # power on VCU Power_On_chan.value = 1 wait(DoubleValue(3)) # check RemoteAccelPedalPos, XBRBrakeDemand and XPRPReq has been zeroed if RemoteAccelPedalPos_chan.value != 0 or \ -0.02 > XBRBrakeDemand_chan.value or XBRBrakeDemand_chan.value > 0.02 or \ XPRPReq_chan.value != 0: TestPass.value = False # wait for relays to switch on wait(DoubleValue(5)) # *** end steps return TestPass.value
def wait_until_settled_multitask(): a = DoubleValue(15000) timer = DoubleValue(0) ret = BooleanValue(False) timer.value = seqtime() with multitask() as mt: @task(mt) def monitor(): ret.value = wait_until_settled(a, DoubleValue(1000), DoubleValue(500), DoubleValue(2), DoubleValue(-1)) @task(mt) def signal(): a.value = 600 wait(DoubleValue(1)) a.value = 12000 wait(DoubleValue(1)) a.value = 300 wait(DoubleValue(1)) a.value = 750 timer.value = seqtime() - timer.value ret.value = a.value == 750 and timer.value >= 4 and timer.value <= 6 and not ret.value return ret.value
def enter_autonomous(): error_status = BooleanValue(False) enter_autonomous_complete = BooleanValue(False) enter_autonomous_succeeded = BooleanValue(False) with multitask() as mt: @task(mt) def enter_30(): ReAX_GearA_Disable_chan.value = 0 ReAX_GearB_Disable_chan.value = 0 # Set power supply set_power_supply_voltage(13, 8) # Power on button Power_On_chan.value = 1 # Check System Status if SystemStatus_chan.value != 25: error_status.value = True # Push autonomous driving button Autonomous_Drive_chan.value = 1 wait(DoubleValue(1)) Autonomous_Drive_chan.value = 0 # Check System Status if SystemStatus_chan.value != 30: error_status.value = True MODE_TRANS_chan.value = 18 enter_autonomous_succeeded.value = True @task(mt) def monitor_error_status(): while enter_autonomous_complete.value is False: if error_status.value: stop_task(enter_30) enter_autonomous_complete.value = True enter_autonomous_succeeded.value = False nivs_yield() return enter_autonomous_succeeded.value
from niveristand import nivs_rt_sequence, NivsParam, run_py_as_rtseq from niveristand.clientapi import BooleanValue, ChannelReference, DoubleValue, DoubleValueArray from niveristand.library import localhost_wait, seqtime, wait_until_settled """ This module contains a complex example for running multiple tests in sequence. This example mirrors the 'Test Engine Setpoints' stimulus profile found in the examples that install with VeriStand. Instead of using a stimulus profile to report results, this example uses the py.test unit-testing framework that is commonly used for running Python tests. """ @nivs_rt_sequence @NivsParam('on_off', BooleanValue(False), NivsParam.BY_VALUE) def set_engine_power(on_off): """Turns the engine on or off.""" engine_power = ChannelReference('Aliases/EnginePower') engine_power.value = on_off.value # If you do not specify a parameter decorator, the parameter defaults to the following: # Type=DoubleValue # Default Value = 0 # Passed by reference. # In this case, the default is adequate, so you do not need to specify the decorator. @nivs_rt_sequence def measure_set_point_response(setpoint, timeout, tolerance): """Sets the desired rpm to the specified setpoint and wait until the signal settles. The tolerance is used to create upper and lower boundaries for the signal. Returns the amount of time it takes the signal to settle or timeout.