current_sink.lua

-- Battery profiling

otii.clear()

-- Example load profile
local example_profile = {{
  current = 0.008,  -- 8mA
  duration = 0.320, -- 320ms
}, {
  current = 0.003,  -- 3mA
  duration = 0.968, -- 968ms
}, {
  resistance = 4.7, -- 4.7 Ohm
  duration = 10.0,  -- 10s
}, {
  power = 0.5,      -- 0.5W
  duration = 10.0,  -- 10s
}}

-- Exit criterias
local max_iterations = 100
local min_voltage = 2.0

-- Open active project if exists,
-- otherwise create a new project.
local project = otii.get_active_project()
if not project then
    project = otii.create_project()
    assert(project, "cannot create project")
end

-- Open first available Otii device.
local devices = otii.get_devices("Arc")
assert(#devices > 0, "No available devices")
local arc = otii.open_device(devices[1].id)
assert(arc, "Cannot open device")

-- Clean up function
function cleanup()
    project:enable_main_power(false)
    arc:enable_battery_profiling(false)
    project:stop()
    arc:set_power_regulation("voltage")

    arc:close()
end

otii.on_stop(cleanup)

-- Enable profiling
arc:set_power_regulation("current")
arc:set_range("high")
arc:set_battery_profile(example_profile)

project:start()
arc:enable_battery_profiling(true)
project:enable_main_power(true)

-- Iterate until exit condition is met
local iteration = -1
while true do
    local battery_data = arc:wait_for_battery_data(60000)
    if battery_data.iteration ~= iteration then
        if battery_data.iteration >= max_iterations then
            break
        end
        iteration = battery_data.iteration
        otii.writeln(string.format("Iteration %d", iteration + 1))
    end

    otii.writeln(battery_data.voltage)

    if battery_data.voltage < min_voltage then
        otii.writeln("Voltage (" .. tostring(battery_data.voltage) .. ") below cut off voltage (" .. tostring(min_voltage) .. "), stop measurement")
        break
    end
end

cleanup()
generated by LDoc 1.4.6 Last updated 2021-10-13 21:46:39