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 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()
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()
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
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()
def f2(): while a.value < 10: a.value += 1 if a.value % 2 != 0: ret.value = a.value * -1 if a.value > 10: ret.value -= 100 nivs_yield()
def f1(): if a.value > 3 and ret.value == 0: ret.value = -1 a.value += 1 nivs_yield() if a.value > 303 and ret.value == 0: ret.value = -100 a.value += 100 nivs_yield()
def yield_many(): a = I32Value(0) nivs_yield() nivs_yield() nivs_yield() nivs_yield() nivs_yield() a.value = iteration() return a.value
def f2(): if a.value > 3 and ret.value == 0: ret.value = -2 a.value += 1 nivs_yield() if a.value > 303 and ret.value == 0: ret.value = -200 a.value += 100 nivs_yield()
def fb(): if a.value > 3 and ret.value == 0: ret.value = -3 a.value += 1 nivs_yield() if a.value > 303 and ret.value == 0: ret.value = -300 a.value += 100 nivs_yield()
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()
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 wait(duration): """ Waits the duration, in seconds, you specify. Args: duration (:any:`DoubleValue`): time, in seconds, this function waits. You may specify fractions of seconds. 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
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()
def f2(): if a.value is 1: a.value = 2 nivs_yield() nivs_yield() nivs_yield() if a.value is 4: a.value = 5
def f2(): if a.value == 1: a.value = 2 nivs_yield() nivs_yield() nivs_yield() if a.value == 4: a.value = 5
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()
def f1(): _subseq_with_multitask(a) nivs_yield() if a.value == 3: a.value = 4
def fa(): nivs_yield() if param.value == 2: param.value = 3
def fb(): if a.value == 2: a.value = 3 nivs_yield()
def fa(): nivs_yield() if a.value == 3: a.value = 4
def f2(): if a.value == 0: a.value = 1 nivs_yield()
def f1(): while a.value < 15: a.value += 1 if a.value % 2 != 1 and a.value < 10: ret.value = a.value * -1 nivs_yield()
def fx(): if a.value != 0: ret.value = -1000 while counter.value > 0: counter.value -= 1 nivs_yield()
def f2(): a.value = 2 nivs_yield()
def f1(): init1.value = seqtime() nivs_yield() end1.value = wait(DoubleValue(1)) - init1.value
def fc(): while counter.value > 0: counter.value -= 1 nivs_yield()
def f2(): nivs_yield() a.value = 1
def f1(): nivs_yield() if a.value == 1: a.value = 2