Skip to content

Commit

Permalink
fix: working array accessor mode for variable definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
bartei81 committed Apr 6, 2024
1 parent 4f6fd85 commit 32278c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 50 deletions.
54 changes: 26 additions & 28 deletions rotary_controller_python/utils/base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ class VariableDefinition(BaseModel):
type: TypeDefinition
count: int = 1

# def __getitem__(self, item):
# item -= 1
# if not isinstance(item, int):
# raise Exception("Specify a number to pick an item from an array type variable")
# if item > self.count:
# raise Exception("Index out of range for array access")
#
# return VariableDefinition(
# name=self.name,
# address=self.address + self.type.length * item,
# type=self.type
# )


variable_definitions = [
TypeDefinition(
Expand Down Expand Up @@ -243,23 +230,34 @@ def parse_addresses_from_definition(self):
def set_fast_data(self, values: Tuple):
values = list(values)
self.fast_data = dict()
sorted_keys = sorted(self.variables, key=lambda v: v.address)
sorted_keys: List[VariableDefinition] = sorted(self.variables, key=lambda v: v.address)
for item in sorted_keys:
hasattr(item.type.read_function, "set_fast_data")
if hasattr(item, "set_fast_data"):
self.fast_data[item.name] = item.type.read_function.set_fast_data(values)
continue

self.fast_data[item.name] = values.pop(0)
return values
item
if hasattr(item.type.read_function, "set_fast_data"):
if item.count > 1:
fd_list = list()
for i in range(item.count):
fd_list.append(
item.type.read_function(self.dm, item.address + item.type.length * i).set_fast_data(values)
)
self.fast_data[item.name] = fd_list
else:
self.fast_data[item.name] = item.type.read_function(self.dm, item.address).set_fast_data(values)
else:
if item.count > 1:
fd_list = list()
for i in range(item.count):
fd_list.append(values[0])
values = values[1:]

self.fast_data[item.name] = fd_list
else:
self.fast_data[item.name] = values[0]
values = values[1:]

return self.fast_data

def refresh(self):
# sort the children
# full_struct_unpack_string = "<"
# sorted_keys = sorted(self.variables, key=lambda v: v.address)
#
# for item in sorted_keys:
# full_struct_unpack_string += item.struct_unpack_string
remaining_size = self.size
max_size = 32
raw_data = []
Expand Down Expand Up @@ -289,7 +287,7 @@ def refresh(self):

raw_bytes = struct.pack("<" + "H" * self.size, *raw_data)
values = struct.unpack("<" + self.struct_unpack_string, raw_bytes)
self.set_fast_data(values)
return self.set_fast_data(values)

# raw_data = self.dm.device.read_registers(
# registeraddress=self.addresses.base_address,
Expand Down
58 changes: 36 additions & 22 deletions rotary_controller_python/utils/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import inspect

import sys
import time

from rotary_controller_python.utils.base_device import BaseDevice
from rotary_controller_python.utils.communication import DeviceManager
from rotary_controller_python.utils.base_device import variable_definitions
Expand Down Expand Up @@ -70,7 +72,7 @@ class Scale(BaseDevice):
int32_t speed;
int32_t error;
int32_t syncRatioNum, syncRatioDen;
uint16_t syncMotion;
bool syncMotion;
input_mode_t mode;
} input_t;
"""
Expand Down Expand Up @@ -142,26 +144,38 @@ class FastData(BaseDevice):
iterations_limit -= 1


def test_scale_structure():
dm = DeviceManager()
global_data = Global(device=dm, base_address=0)
global_data["servo"]["ratioNum"] = 12345
result = global_data["servo"]["ratioNum"]
print(result)
assert result == 12345


def test_get_item_from_array_definiton():
dm = DeviceManager()
global_data = Global(device=dm, base_address=0)
global_data['scales'][1]['ratioDen'] = 111
result = global_data['scales'][1]['ratioDen']
assert result == 111


def test_scale_fast_data():
# def test_scale_structure():
# dm = DeviceManager()
# global_data = Global(device=dm, base_address=0)
# global_data["servo"]["ratioNum"] = 12345
# result = global_data["servo"]["ratioNum"]
# print(result)
# assert result == 12345
#
#
# def test_get_item_from_array_definition():
# dm = DeviceManager()
# global_data = Global(device=dm, base_address=0)
# global_data['scales'][1]['ratioDen'] = 111
# result = global_data['scales'][1]['ratioDen']
# assert result == 111
#
#
# def test_scale_fast_data():
# dm = DeviceManager()
# dm.device.read_registers(0, 10)
# global_data = Global(device=dm, base_address=0)
# global_data.refresh()


if __name__ == "__main__":
dm = DeviceManager()
dm.device.read_registers(0, 10)
global_data = Global(device=dm, base_address=0)
global_data.refresh()
assert global_data.fast_data['index']['divisions'] == 123
while True:
time.sleep(0.1)
values = global_data.refresh()
print(global_data['executionInterval'])
print(global_data['fastData']['scaleSpeed'][1])
print(global_data['fastData']['scaleSpeed'][2])
print(global_data['fastData']['scaleSpeed'][3])
print(values)

0 comments on commit 32278c8

Please sign in to comment.