def measure_elapsed_time():
    """
    Shows different ways to measure elapsed time in a sequence.

    You can measure time in milliseconds, microseconds, or seconds.

    Returns:
        int: time, in milliseconds, it took to run this sequence.

    """
    seqtime_timer = DoubleValue(0)
    seqtime_us_timer = I64Value(0)
    tick_ms_timer = I64Value(0)
    tick_us_timer = I64Value(0)

    # The following steps demonstrate different ways you can capture an initial timestamp:
    seqtime_timer.value = seqtime()
    seqtime_us_timer.value = seqtimeus()
    tick_ms_timer.value = tickcountms()
    tick_us_timer.value = tickcountus()

    # Simulates work to time.
    while iteration() < 1000:
        nivs_yield()

    # Measures the elapsed time by subtracting the initial timestamp from the current time.
    seqtime_timer.value = seqtime() - seqtime_timer.value
    seqtime_us_timer.value = seqtimeus() - seqtime_us_timer.value
    tick_ms_timer.value = tickcountms() - tick_ms_timer.value
    tick_us_timer.value = tickcountus() - tick_us_timer.value

    return tick_ms_timer.value
def sawtooth_wave(wave_out, amplitude, freq, phase, bias, duration):
    """
    Plays a sawtooth wave with the parameters you specify.

    Args:
        wave_out(:any:`DoubleValue`): variable onto which the sawtooth wave plays.
        amplitude(:any:`DoubleValue`): amplitude of the sawtooth wave.
        freq(:any:`DoubleValue`): frequency, in Hz, of the sawtooth wave.
        phase(:any:`DoubleValue`): phase, in degrees, of the sawtooth wave.
        bias(:any:`DoubleValue`): offset to add to the sawtooth wave.
        duration(:any:`DoubleValue`): duration, in seconds, to play the sawtooth wave.
    """
    init_time = DoubleValue(0)
    curr_phase = DoubleValue(0)
    init_time.value = seqtime()
    while seqtime() - init_time.value < duration.value:
        curr_phase.value = rem(
            (freq.value * 360.0 * (seqtime() - init_time.value)) + phase.value,
            360.0)
        if curr_phase.value < 180.0:
            wave_out.value = (
                (curr_phase.value / 180.0) * amplitude.value) + bias.value
        else:
            wave_out.value = (((curr_phase.value / 180.0) - 2.0) *
                              amplitude.value) + bias.value
        localhost_wait(deltat())
        nivs_yield()
示例#3
0
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 uniform_white_noise_wave(wave_out, amplitude, seed, duration):
    """
    Plays a uniform white noise wave with the parameters you specify.

    Args:
        wave_out(:any:`DoubleValue`): variable onto which the white noise wave plays.
        amplitude(:any:`DoubleValue`): amplitude of the white noise wave.
        seed(:any:`I32Value`): seed for random number generator.
        duration(:any:`DoubleValue`): duration, in seconds, to play the white noise wave.
    """
    x_seed = I32Value(0)
    y_seed = I32Value(0)
    z_seed = I32Value(0)
    init_time = DoubleValue(0)
    seed_sum = DoubleValue(0)

    x_seed.value = seed.value
    y_seed.value = (seed.value * 8191) & 16383
    z_seed.value = (y_seed.value * 8191) & 16383
    init_time.value = seqtime()

    while seqtime() - init_time.value < duration.value:
        x_seed.value = rem(x_seed.value * 171.0, 30269.0)
        y_seed.value = rem(x_seed.value * 172.0, 30307.0)
        z_seed.value = rem(x_seed.value * 170.0, 30323.0)
        seed_sum.value = (x_seed.value / 30269.0) + (
            y_seed.value / 30307.0) + (z_seed.value / 30323.0)
        wave_out.value = amplitude.value * (
            (seed_sum - floor(seed_sum.value)) - 0.5) * 2.0
        localhost_wait(deltat())
        nivs_yield()
def square_wave(wave_out, amplitude, freq, phase, bias, duty_cycle, duration):
    """
    Plays a square wave with the parameters you specify.

    Args:
        wave_out(:any:`DoubleValue`): variable onto which the square wave plays.
        amplitude(:any:`DoubleValue`): amplitude of the square wave.
        freq(:any:`DoubleValue`): frequency, in Hz, of the square wave.
        phase(:any:`DoubleValue`): phase, in degrees, of the square wave.
        bias(:any:`DoubleValue`): offset to add to the square wave.
        duty_cycle(:any:`DoubleValue`): percentage of time the square wave remains high versus low over one period.
        duration(:any:`DoubleValue`): time, in seconds, to play the square wave.
    """
    init_time = DoubleValue(0)
    curr_phase = DoubleValue(0)

    init_time.value = seqtime()
    while seqtime() - init_time.value < duration.value:
        curr_phase.value = rem(((freq.value * 360.0 *
                                 (seqtime() - init_time.value)) + phase.value),
                               360.0)
        if curr_phase.value < (duty_cycle.value * 3.6):
            wave_out.value = amplitude.value + bias.value
        else:
            wave_out.value = -amplitude.value + bias.value
        localhost_wait(deltat())
        nivs_yield()
示例#6
0
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
示例#7
0
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
示例#8
0
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
示例#9
0
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
示例#10
0
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 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.
    """
    actual_rpm = ChannelReference('Aliases/ActualRPM')
    desired_rpm = ChannelReference('Aliases/DesiredRPM')
    start_time = DoubleValue(0)
    settle_time = DoubleValue(0)

    desired_rpm.value = setpoint.value
    # Waits .5 seconds, so the gateway has time to update.
    localhost_wait(0.5)

    start_time.value = seqtime()
    wait_until_settled(actual_rpm, desired_rpm.value + tolerance.value,
                       desired_rpm.value - tolerance.value, DoubleValue(2.0),
                       timeout.value)
    settle_time.value = seqtime() - start_time.value
    return settle_time.value
示例#12
0
def sine_wave(wave_out, amplitude, freq, phase, bias, duration):
    """
    Plays a sine wave with the parameters you specify.

    Args:
        wave_out(:any:`DoubleValue`): variable onto which the sine wave plays.
        amplitude(:any:`DoubleValue`): amplitude of the sine wave.
        freq(:any:`DoubleValue`): frequency, in Hz, of the sine wave.
        phase(:any:`DoubleValue`): phase, in degrees, of the sine wave.
        bias(:any:`DoubleValue`): offset to add to the sine wave.
        duration(:any:`DoubleValue`): duration, in seconds, to play the sine wave.
    """
    init_time = DoubleValue(0)
    phase_rad = DoubleValue(0)

    init_time.value = seqtime()
    phase_rad.value = (phase.value * pi) / 180.0
    while seqtime() - init_time.value < duration.value:
        wave_out.value = amplitude.value * \
            sin(((2 * pi * freq.value) * (seqtime() - init_time.value)) + phase_rad.value) + bias.value
        localhost_wait(deltat())
        nivs_yield()
示例#13
0
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
示例#14
0
 def f2():
     init2.value = seqtime()
     end2.value = wait(DoubleValue(3)) - init2.value
示例#15
0
 def f1():
     init1.value = seqtime()
     nivs_yield()
     end1.value = wait(DoubleValue(1)) - init1.value