Skip to content

Commit

Permalink
fix: new improved version
Browse files Browse the repository at this point in the history
  • Loading branch information
bartei81 committed Dec 28, 2023
1 parent 5ce73c9 commit 7ce7d6c
Show file tree
Hide file tree
Showing 20 changed files with 485 additions and 454 deletions.
24 changes: 12 additions & 12 deletions rotary_controller_python/components/coordbar.kv
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#: import Factory kivy.factory.Factory
#: import Keypad components.keypad
#: import appsettings components


<CoordBar>:
orientation: "horizontal"
height: 80
size_hint_y: None
Label:
text: root.data.axis_name
text: root.axis_name
width: self.height
size_hint_x: None
font_size: 64
Expand All @@ -25,18 +25,18 @@
font_size: 48
font_style: "bold"
color: app.display_color
text: app.pos_format.format(root.data.position / app.unit_factor)
text: app.formats.position_format.format(root.position / app.formats.factor)
text_size: self.size
halign: 'right'
valign: 'center'
on_release: Factory.Keypad().show(root.data, 'set_position')
on_release: root.update_position()
Label:
size_hint_y: 0.3
font_name: "fonts/iosevka-regular.ttf"
font_size: 16
font_style: "bold"
color: app.display_color
text: app.speed_format.format(root.formatted_axis_speed)
text: app.formats.speed_format.format(root.formatted_axis_speed / app.formats.factor)
text_size: self.size
halign: 'right'
valign: 'top'
Expand All @@ -59,11 +59,11 @@
font_style: "bold"
background_color: [0.2, 0.2, 0.2, 1]
color: app.display_color
text: str(int(root.data.sync_num))
text: str(int(root.sync_ratio_num))
text_size: self.size
halign: 'center'
valign: 'middle'
on_release: Factory.Keypad().show(root.data, 'sync_num')
on_release: Factory.Keypad().show(root, 'sync_ratio_num')

BoxLayout:
size_hint_x: 0.20
Expand All @@ -83,11 +83,11 @@
font_style: "bold"
background_color: [0.2, 0.2, 0.2, 1]
color: app.display_color
text: str(int(root.data.sync_den))
text: str(int(root.sync_ratio_den))
text_size: self.size
halign: 'center'
valign: 'middle'
on_release: Factory.Keypad().show(root.data, 'sync_den')
on_release: Factory.Keypad().show(root, 'sync_ratio_den')

BoxLayout:
size_hint_x: 0.10
Expand All @@ -102,9 +102,9 @@
valign: 'top'
ToggleButton:
text: "ON" if self.state == "down" else "OFF"
state: root.data.sync_enable
state: root.sync_enable
background_color: [0.2, 1, 0.2, 1] if self.state == "down" else [1, 0.2, 0.2, 1]
on_release: root.data.sync_enable = self.state
on_release: root.sync_enable = self.state

Button:
text: "Zero"
Expand All @@ -113,4 +113,4 @@
background_color: [0.5, 0.5, 0.5, 1]
width: self.height
size_hint_x: None
on_release: root.data.set_position = 0
on_release: root.new_position = 0
72 changes: 63 additions & 9 deletions rotary_controller_python/components/coordbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,103 @@

from decimal import Decimal

from kivy.factory import Factory
from loguru import logger as log
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import NumericProperty, ObjectProperty
from kivy.properties import NumericProperty, StringProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App

app = App.get_running_app()
from rotary_controller_python.dispatchers import SavingDispatcher

kv_file = os.path.join(os.path.dirname(__file__), __file__.replace(".py", ".kv"))
if os.path.exists(kv_file):
log.info(f"Loading KV file: {kv_file}")
Builder.load_file(kv_file)


class CoordBar(BoxLayout):
class CoordBar(BoxLayout, SavingDispatcher):
device = ObjectProperty()
input_index = NumericProperty(0)
axis_name = StringProperty("NN")
ratio_num = NumericProperty(1)
ratio_den = NumericProperty(200)
sync_ratio_num = NumericProperty(100)
sync_ratio_den = NumericProperty(360)
sync_enable = StringProperty("normal")
position = NumericProperty(0)

formatted_axis_speed = NumericProperty(0.000)

data = ObjectProperty(rebind=True, defaultvalue=app.data[0])
_skip_save = ["position", "formatted_axis_speed"]

def __init__(self, input_index, **kv):
super().__init__(**kv)
self.input_index = input_index

def __init__(self, *args, **kv):
super(CoordBar, self).__init__(**kv)
self.speed_history = collections.deque(maxlen=5)
self.previous_axis_time: float = 0
self.previous_axis_pos: Decimal = Decimal(0)
self.upload()
Clock.schedule_interval(self.update_speed, 1.0 / 10)

# def on_device(self, instance, value):
# self.upload()

def upload(self):
props = self.get_our_properties()
prop_names = [item.name for item in props]
device_props = self.get_writeable_properties(type(self.device.scales[self.input_index]))
matches = [item for item in prop_names if item in device_props]
for item in matches:
log.info(f"Writing scale settings for scale {self.input_index}: {item}={self.__getattribute__(item)}")
self.device.scales[self.input_index].__setattr__(item, self.__getattribute__(item))

def on_sync_enable(self, instance, value):
if value == "down":
self.device.scales[instance.input_index].sync_motion = True
else:
self.device.scales[instance.input_index].sync_motion = False

def on_sync_ratio_num(self, instance, value):
self.device.scales[instance.input_index].sync_ratio_num = int(value)

def on_sync_ratio_den(self, instance, value):
self.device.scales[instance.input_index].sync_ratio_den = int(value)

def update_position(self):
Factory.Keypad().show(self, 'new_position')

@property
def new_position(self):
return None

@new_position.setter
def new_position(self, value):
self.device.scales[self.input_index].position = int(float(value) * 1000)

def update_speed(self, *args, **kv):
current_time = time.time()

# Calculate axis speed
self.speed_history.append(
(Decimal(self.data.position) - self.previous_axis_pos) /
(Decimal(self.position) - self.previous_axis_pos) /
Decimal(current_time - self.previous_axis_time)
)

average = (sum(self.speed_history) / Decimal(len(self.speed_history)))

if app.current_units == "in":
app = App.get_running_app()
if app is None:
return

if app.formats.current_format == "IN":
# Speed in feet per minute
self.formatted_axis_speed = float(average * 60 / Decimal("25.4") / 12)
else:
# Speed in mt/minute
self.formatted_axis_speed = float(average * 60 / 1000)

self.previous_axis_time = current_time
self.previous_axis_pos = Decimal(self.data.position)
self.previous_axis_pos = Decimal(self.position)
100 changes: 0 additions & 100 deletions rotary_controller_python/components/jog.kv

This file was deleted.

21 changes: 0 additions & 21 deletions rotary_controller_python/components/jog.py

This file was deleted.

Loading

0 comments on commit 7ce7d6c

Please sign in to comment.