Exemple #1
0
def wait_until_next_us_multiple(us_multiple):
    """
    Waits until the next microsecond multiple of the number you specify in `us_multiple`.

    Args:
        us_multiple(:any:`I64Value`): the microsecond multiple to wait until.

    Returns:
        int: actual microseconds waited.

    This wait is non-blocking, so other tasks will run while this wait executes.

    """
    ticks = I64Value(0)
    if us_multiple.value > 0:
        last_q = I64Value(0)
        q = I64Value(0)
        ticks.value = tickcountus()
        q.value = quotient(ticks.value, us_multiple.value)
        last_q.value = q.value
        while q.value == last_q.value:
            last_q.value = q.value
            ticks.value = tickcountus()
            q.value = quotient(ticks.value, us_multiple.value)
            nivs_yield()
    else:
        nivs_yield()
        ticks.value = tickcountus()
    return ticks.value
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
Exemple #3
0
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
Exemple #4
0
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 array_operations():
    """
    Shows operations you can perform with array data types in a real-time sequence.

    An array can hold multiple values of the same data type. You cannot have arrays of arrays.
    Use arrays to pass buffers of data for playback or storage.

    Returns:
        float: sum of all values in the array.

    """
    var = DoubleValue(0)
    arr_size = I64Value(0)
    array = DoubleValueArray([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])

    # Indexes a value out of an array.
    var.value = array[4].value + 100
    # Updates a value in an array.
    array[2].value = 6.0
    # Gets the size of an array.
    arr_size.value = arraysize(array)
    # Loops over each element of an array. Each time the loop iterates a value from the array is copied into x.
    var.value = 0.0
    for x in array:
        var.value += x
    return var.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 ramp(ramp_out, init_value, final_value, duration):
    """
    Ramps a variable from an initial value to an ending value over the duration you specify.

    Args:
        ramp_out(:any:`DoubleValue`): variable you want to ramp.
        init_value(:any:`DoubleValue`): starting value.
        final_value(:any:`DoubleValue`): ending value.
        duration(:any:`DoubleValue`): time, in seconds, you want the ramp to take.
    """
    step_count = I64Value(0)
    increment = DoubleValue(0)

    step_count.value = ceil(duration.value / deltat())
    if step_count.value <= 0:
        ramp_out.value = final_value.value
    else:
        increment.value = ((final_value.value - init_value.value) /
                           step_count.value)
        for i in range(step_count.value + 1):
            ramp_out.value = (i * increment.value) + init_value.value
            localhost_wait(deltat())
            nivs_yield()
Exemple #8
0
def arithmetic_shift_right_variables1():
    a = I64Value(5)
    b = I64Value(0)
    b.value = 128 >> a.value
    return b.value
Exemple #9
0
def arithmetic_shift_right_variable_variable1():
    a = I32Value(3)
    b = I64Value(1)
    c = DoubleValue(0)
    c.value = a.value >> b.value
    return c.value
def bitwise_and_variable_variable1():
    a = I32Value(1)
    b = I64Value(3)
    c = DoubleValue(0)
    c.value = a.value & b.value
    return c.value
Exemple #11
0
def wait_wrong_param_type():
    duration = I64Value(1)
    wait(duration)
    return duration.value
def arithmetic_shift_left_multiple_types1():
    a = I64Value(1)
    a.value = 1 << I64Value(5) << 3 << I32Value(7)
    return a.value
def bitwise_and_multiple_types1():
    a = I64Value(1)
    a.value = 1 & I64Value(5) & 3 & I32Value(7)
    return a.value
Exemple #14
0
    Returns:
        float: actual seconds waited.

    This wait is non-blocking, so other tasks will run while this wait executes.

    """
    init_time = DoubleValue(0)
    init_time.value = seqtime()
    while seqtime() - init_time.value < duration.value:
        nivs_yield()

    init_time.value = seqtime()
    return init_time.value


@NivsParam('ms_multiple', I64Value(0), NivsParam.BY_REF)
@nivs_rt_sequence
def wait_until_next_ms_multiple(ms_multiple):
    """
    Waits until the next millisecond multiple of the number you specify in `ms_multiple`.

    Args:
        ms_multiple(:any:`I64Value`): the millisecond multiple to wait until.

    Returns:
        int: actual milliseconds waited.

    This wait is non-blocking, so other tasks will run while this wait executes.

    """
    ticks = I64Value(0)
def bitwise_and_variables1():
    a = I64Value(5)
    b = I64Value(0)
    b.value = 1 & a.value
    return b.value
Exemple #16
0
def arithmetic_shift_left_augassign_parentheses():
    a = I32Value(1024)
    a.value >>= (2 + I32Value(1)) + I64Value(1)
    return a.value
Exemple #17
0
def abs_nivsdatatype2():
    a = DoubleValue(0)
    a.value = abs(I64Value(-5))
    return a.value
Exemple #18
0
def arithmetic_shift_right_augassign_nivsdatatype3():
    a = I32Value(2)
    a.value >>= I64Value(1)
    return a.value
def call_iteration():
    a = I64Value(0)
    a.value = iteration()
    return a.value
def call_tickcountus():
    a = I64Value(0)
    a.value = tickcountus()
    a.value = tickcountus() - a.value
    return a.value
def call_rem():
    a = I64Value(1439)
    b = DoubleValue(12)
    b.value = rem(a, b)
    return b.value
def call_quotient():
    a = I64Value(1440)
    b = DoubleValue(12)
    b.value = quotient(a, b)
    return b.value
Exemple #23
0
def arithmetic_shift_right_augassign_variable3():
    a = I64Value(2)
    b = I32Value(1)
    a.value >>= b.value
    return a.value
def arithmetic_shift_left_variables1():
    a = I64Value(5)
    b = I64Value(0)
    b.value = 1 << a.value
    return b.value
Exemple #25
0
def arithmetic_shift_right_multiple_types1():
    a = I64Value(0)
    a.value = 127 >> I64Value(2) >> 3 >> I32Value(1)
    return a.value
def arithmetic_shift_left_variable_variable1():
    a = I32Value(1)
    b = I64Value(3)
    c = DoubleValue(0)
    c.value = a.value << b.value
    return c.value
Exemple #27
0
def abs_variable_i64():
    a = I64Value(0)
    b = I64Value(-5)
    a.value = abs(b.value)
    return a.value
def arithmetic_shift_left_augassign_nivsdatatype3():
    a = I32Value(1)
    a.value <<= I64Value(2)
    return a.value
def arithmetic_shift_left_augassign_variable3():
    a = I64Value(1)
    b = I32Value(2)
    a.value <<= b.value
    return a.value
Exemple #30
0
def modulo_with_parentheses7():
    a = DoubleValue(0)
    a.value = I32Value(11) % (I64Value(11) % DoubleValue(7)) % DoubleValue(2)
    return a.value