Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
time_lock implemented need to be tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguez committed Oct 19, 2023
1 parent 00db8ba commit fb38eae
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 57 deletions.
9 changes: 9 additions & 0 deletions local/run-test-manual.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# ===================================================================
# PARAMETERS
PYTHON_VENV_PATH=/usr/local/bin/panduza/venv

# ===================================================================

${PYTHON_VENV_PATH}/bin/python3 $1

2 changes: 1 addition & 1 deletion platform/panduza_platform/connectors/serial_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async def read_data(self, n_bytes = None):
pass

@abc.abstractmethod
async def write_data(self, message):
async def write_data(self, message, time_lock_s=None):
"""
"""
pass
23 changes: 13 additions & 10 deletions platform/panduza_platform/connectors/serial_tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, loop,**kwargs):
self._mutex = asyncio.Lock()

# Init time lock
self._time_lock = None
self._time_lock_s = None


key = kwargs["serial_port_name"]
Expand Down Expand Up @@ -144,18 +144,21 @@ async def read_data(self, n_bytes = None):

# ---

async def write_data(self, message, time_lock=None):
async def write_data(self, message, time_lock_s=None):
"""write to UART using asynchronous mode
"""
async with self._mutex:

try:
# Manage time lock by waiting for the remaining duration
if self._time_lock:
elapsed = time.time() - self._time_lock["t0"]
if elapsed < self._time_lock["duration"]:
await asyncio.sleep(elapsed)
self._time_lock = None
if self._time_lock_s:
elapsed = time.time() - self._time_lock_s["t0"]
if elapsed < self._time_lock_s["duration"]:

wait_time = self._time_lock_s["duration"] - elapsed
SerialTty.log.debug(f"wait lock {wait_time}")
await asyncio.sleep(wait_time)
self._time_lock_s = None

# Start sending the message
self.writer.write(message.encode())
Expand All @@ -164,9 +167,9 @@ async def write_data(self, message, time_lock=None):
await self.writer.drain()

# Set the time lock if requested by the user
if time_lock != None:
self._time_lock = {
"duration": time_lock,
if time_lock_s != None:
self._time_lock_s = {
"duration": time_lock_s,
"t0": time.time()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from meta_drivers.ammeter import MetaDriverAmmeter
from connectors.serial_tty import SerialTty

COMMAND_TIME_LOCK=0.1

class DrvKoradKa3005pAmmeter(MetaDriverAmmeter):
# =============================================================================
Expand Down Expand Up @@ -30,7 +31,7 @@ async def _PZA_DRV_loop_init(self, loop, tree):
# Checks
assert_that(settings, has_key("serial_baudrate"))

self.uart_connector = await SerialTty.Get(loop,**settings)
self.serial_connector = await SerialTty.Get(loop,**settings)

# Call meta class BPC ini
await super()._PZA_DRV_loop_init(loop, tree)
Expand All @@ -39,8 +40,8 @@ async def _PZA_DRV_loop_init(self, loop, tree):

async def _PZA_DRV_AMMETER_read_measure_value(self):
cmd = "IOUT1?"
await self.uart_connector.write_data(cmd)
current = await self.uart_connector.read_data(n_bytes=5)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)
current = await self.serial_connector.read_data(n_bytes=5)
return float(current)

# ---
30 changes: 18 additions & 12 deletions platform/panduza_platform/drivers/bpc/drv_korad_ka3005p_bpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def int_to_state_string(v_int):
position = val_list.index(v_int)
return key_list[position]


COMMAND_TIME_LOCK=0.1


class DrvKoradKa3005pBPC(MetaDriverBpc):
# =============================================================================
# FROM MetaDriverBpc
Expand Down Expand Up @@ -40,7 +44,7 @@ async def _PZA_DRV_loop_init(self, loop, tree):
# Checks
assert_that(settings, has_key("serial_baudrate"))

self.uart_connector = await SerialTty.Get(loop,**settings)
self.serial_connector = await SerialTty.Get(loop,**settings)

# Call meta class BPC ini
await super()._PZA_DRV_loop_init(loop, tree)
Expand All @@ -51,33 +55,33 @@ async def _PZA_DRV_loop_init(self, loop, tree):
async def _PZA_DRV_BPC_read_enable_value(self):
await asyncio.sleep(1)
print("Sending cmd: {}".format("STATUS?"))
await self.uart_connector.write_data("STATUS?")
status = await self.uart_connector.read_data(n_bytes=1)
await self.serial_connector.write_data("STATUS?", time_lock_s=COMMAND_TIME_LOCK)
status = await self.serial_connector.read_data(n_bytes=1)
print("LOL", status)
return bool(status[0] & (1 << 6))

async def _PZA_DRV_BPC_write_enable_value(self, v):
await asyncio.sleep(1)
cmd = "OUT{}".format(int(v))
print("Sending cmd: {}".format(cmd))
await self.uart_connector.write_data(cmd)
status = await self.uart_connector.read_data(n_bytes=1)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)
status = await self.serial_connector.read_data(n_bytes=1)
await asyncio.sleep(1)

# ---

async def _PZA_DRV_BPC_read_voltage_value(self):
await asyncio.sleep(0.2)
cmd = "VSET1?"
await self.uart_connector.write_data(cmd)
voltage = await self.uart_connector.read_data(n_bytes=5)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)
voltage = await self.serial_connector.read_data(n_bytes=5)
return float(voltage)

async def _PZA_DRV_BPC_write_voltage_value(self, v):
await asyncio.sleep(0.2)
v = "{:05.2f}".format(v)
cmd = "VSET1:{}".format(v)
await self.uart_connector.write_data(cmd)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)

async def _PZA_DRV_BPC_voltage_value_min_max(self):
return VOLTAGE_BOUNDS
Expand All @@ -90,18 +94,20 @@ async def _PZA_DRV_BPC_read_voltage_decimals(self):
async def _PZA_DRV_BPC_read_current_value(self):
await asyncio.sleep(0.2)
cmd = "ISET1?"
await self.uart_connector.write_data(cmd)
current = await self.uart_connector.read_data(n_bytes=5)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)
current = await self.serial_connector.read_data(n_bytes=5)
return float(current)

async def _PZA_DRV_BPC_write_current_value(self, v):
await asyncio.sleep(0.2)
v = "{:05.3f}".format(v)
cmd = "ISET1:{}".format(v)
await self.uart_connector.write_data(cmd)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)

async def _PZA_DRV_BPC_current_value_min_max(self):
return CURRENT_BOUNDS

async def _PZA_DRV_BPC_read_current_decimals(self):
return 3
return 3


24 changes: 14 additions & 10 deletions platform/panduza_platform/drivers/bpc/drv_tenma_722710_bpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
VOLTAGE_BOUNDS = { "min": 0, "max": 30 }
CURRENT_BOUNDS = { "min": 0, "max": 5 }


COMMAND_TIME_LOCK=0.1


def int_to_state_string(v_int):
key_list = list(STATE_VALUE_ENUM.keys())
val_list = list(STATE_VALUE_ENUM.values())
Expand Down Expand Up @@ -46,7 +50,7 @@ async def _PZA_DRV_loop_init(self, loop, tree):
assert_that(settings, has_key("serial_baudrate"))

# Get the Serial Connector
self.uart_connector = await SerialTty.Get(loop,**settings)
self.serial_connector = await SerialTty.Get(loop,**settings)

#
#self.modbus_unit = 1
Expand All @@ -64,8 +68,8 @@ async def _PZA_DRV_BPC_read_enable_value(self):
# Send "STATUS?" to get back the output state


await self.uart_connector.write_data(f"STATUS?\n")
statusBytes = await self.uart_connector.read_data()
await self.serial_connector.write_data(f"STATUS?\n", time_lock_s=COMMAND_TIME_LOCK)
statusBytes = await self.serial_connector.read_data()

self.log.debug(f"{statusBytes.strip()}")
status = ord(statusBytes.strip())
Expand All @@ -82,21 +86,21 @@ async def _PZA_DRV_BPC_read_enable_value(self):
async def _PZA_DRV_BPC_write_enable_value(self, v):
# Send "OUT{v}" to enable output
int16_value = STATE_VALUE_ENUM[v]
await self.uart_connector.write_data(f"OUT{int16_value}\n")
await self.serial_connector.write_data(f"OUT{int16_value}\n", time_lock_s=COMMAND_TIME_LOCK)

# VOLTAGE #

async def _PZA_DRV_BPC_read_voltage_value(self):
# Send "VSET1?" to get the voltage value
await self.uart_connector.write_data(f"VSET{self.channel}?\n")
voltage = await self.uart_connector.read_data()
await self.serial_connector.write_data(f"VSET{self.channel}?\n", time_lock_s=COMMAND_TIME_LOCK)
voltage = await self.serial_connector.read_data()
return float(voltage)

# ---

async def _PZA_DRV_BPC_write_voltage_value(self, v):
# Send "VSET1:{v}" to set the voltage value
await self.uart_connector.write_data(f"VSET{self.channel}:{v}\n")
await self.serial_connector.write_data(f"VSET{self.channel}:{v}\n", time_lock_s=COMMAND_TIME_LOCK)

# ---

Expand All @@ -112,15 +116,15 @@ async def _PZA_DRV_BPC_read_voltage_decimals(self):

async def _PZA_DRV_BPC_read_current_value(self):
# Send "ISET1?" to get the Current value
await self.uart_connector.write_data(f"ISET{self.channel}?\n")
current = await self.uart_connector.read_data()
await self.serial_connector.write_data(f"ISET{self.channel}?\n", time_lock_s=COMMAND_TIME_LOCK)
current = await self.serial_connector.read_data()
return float(current)

# ---

async def _PZA_DRV_BPC_write_current_value(self, v):
# Send "ISET1:{v}" to set the Current value
await self.uart_connector.write_data(f"ISET{self.channel}:{v}\n")
await self.serial_connector.write_data(f"ISET{self.channel}:{v}\n", time_lock_s=COMMAND_TIME_LOCK)

# ---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from meta_drivers.voltmeter import MetaDriverVoltmeter
from connectors.serial_tty import SerialTty

COMMAND_TIME_LOCK=0.1

class DrvKoradKa3005pVoltmeter(MetaDriverVoltmeter):
# =============================================================================
Expand Down Expand Up @@ -30,7 +31,7 @@ async def _PZA_DRV_loop_init(self, loop, tree):
# Checks
assert_that(settings, has_key("serial_baudrate"))

self.uart_connector = await SerialTty.Get(loop,**settings)
self.serial_connector = await SerialTty.Get(loop,**settings)

# Call meta class BPC ini
await super()._PZA_DRV_loop_init(loop, tree)
Expand All @@ -39,8 +40,8 @@ async def _PZA_DRV_loop_init(self, loop, tree):

async def _PZA_DRV_VOLTMETER_read_measure_value(self):
cmd = "VOUT1?"
await self.uart_connector.write_data(cmd)
voltage = await self.uart_connector.read_data(n_bytes=5)
await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK)
voltage = await self.serial_connector.read_data(n_bytes=5)
return float(voltage)

# ---
45 changes: 45 additions & 0 deletions platform/tests/manual/bpc_hell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import time
import numpy as np
from panduza import Bpc

ADDR="localhost"
PORT=1883

# power_channel = Bpc(addr=ADDR, port=PORT, topic="pza/default/Hanmatek_Hm310t/bpc")
# power_channel = Bpc(addr=ADDR, port=PORT, topic="pza/default/Panduza_FakeBps/channel_1")
# power_channel = Bpc(addr=ADDR, port=PORT, topic="pza/default/Tenma_722710/bpc")
# power_channel = Bpc(addr=ADDR, port=PORT, topic="pza/default/fake/:channel_0:_ctrl")
power_channel = Bpc(addr=ADDR, port=PORT, topic="pza/default/xavier/:channel_0:_ctrl")


# Read from interface
MIN_VOLTAGE=0
MAX_VOLTAGE=10
STP_VOLTAGE=0.1
DEC_VOLTAGE=1



for i in range(0, 20):
state_value = bool(i%2)
print(f"set enable {state_value}")
power_channel.enable.value.set(state_value)


for voltage in np.arange(MIN_VOLTAGE, MAX_VOLTAGE, STP_VOLTAGE):
voltage = round(voltage, DEC_VOLTAGE)
print(f"set voltage {voltage}V")
power_channel.voltage.value.set(voltage, ensure=True)


for voltage in np.arange(MIN_VOLTAGE, MAX_VOLTAGE, STP_VOLTAGE):
voltage = round(voltage, DEC_VOLTAGE)
print(f"set voltage {voltage}V")
power_channel.voltage.value.set(voltage, ensure=False)







Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

print("===")
for topic, info in interfaces.items():
print(topic, " - ", info)
print(topic, " - ", info)

17 changes: 0 additions & 17 deletions tests/py_tests/test2.py

This file was deleted.

0 comments on commit fb38eae

Please sign in to comment.