I am using a Raspberry Pi to create a program for a rudder feedback indicator on a boat.
Pretty much a potentiometer is connected to the rudder and a variable voltage is sent to an Analog to digital converter connected to my Pi.
I can use the input to display my current rudder angle (in text form) however I want it to be able to rotate an image I have to act as the dial (or guage)
I want the angle to be constantly updated and appear on the GUI. How do I get a function to refer back to the rotation angle in my .KV file?
This will work when the program is first run but will not update throughout the program
class RudderAngle(Screen):
def __init__(self, **kwargs):
super(RudderAngle, self).__init__(**kwargs)
Clock.schedule_interval(self.Print_Rudder,0.02)
def Print_Rudder(self, *args):
angle = mcp.read_adc(7)
deg = angle/17.0
dega = angle/17.0
if deg<=30:
ri = (30-deg)
colour = [0, 1, 0, 1]
angle = 90-dega*3
if deg==30:
ri = (0)
colour = [0,0,0,1]
angle = 0
if deg>30:
ri = (deg-30)
colour = [1, 0, 0, 1]
angle =-(dega*3)+90
ri = math.floor(ri)
ri = int(ri)
ri = str(ri)
rai = self.ids.rudderposition
ind1 = self.ids.ind
rai.text = ri
rai.color = colour
ind1.angle = angle
return angle
----------KV File----------
FloatLayout:
id: rudder_angle
padding: 10
canvas.before:
Color:
rgba: .95, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
font_size: 100
id: rudderposition
text: '0'
pos_hint: {"center_x": .5, "center_y": .6}
background_color: (1, 1, 1, 1)
Image:
id: ind
source: 'rai-pics/dial.png'
size_hint: 1,1
pos_hint: {"center_x": .5, "center_y": .5}
Image:
source: 'rai-pics/ri.png'
size_hint: 0.7,0.5
pos_hint: {"center_x": .5, "center_y": .5}
canvas.before:
PushMatrix
Rotate:
#angle: root.Print_Rudder()
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
For these cases you must create a property and perform the binding with the angle, for this case NumericProperty
is used:
.py
...
from kivy.properties import NumericProperty
class RudderAngle(Screen):
angle_of_rudder = NumericProperty()
def __init__(self, **kwargs):
super(RudderAngle, self).__init__(**kwargs)
Clock.schedule_interval(self.Print_Rudder,0.02)
def Print_Rudder(self, *args):
...
ind1.angle = angle
self.angle_of_rudder = angle
root = Builder.load_file("main.kv")
class TestApp(App):
def build(self):
return root
if __name__ == '__main__':
TestApp().run()
main.kv
RudderAngle:
name : 'screen'
FloatLayout:
id: rudder_angle
padding: 10
canvas.before:
Color:
rgba: 0.95, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
font_size: 100
id: rudderposition
text: '0'
pos_hint: {"center_x": .5, "center_y": .6}
background_color: (1, 1, 1, 1)
Image:
id: ind
source: 'rai-pics/dial.png'
size_hint: 1,1
pos_hint: {"center_x": .5, "center_y": .5}
Image:
source: 'rai-pics/ri.png'
size_hint: 0.7,0.5
pos_hint: {"center_x": .5, "center_y": .5}
canvas.before:
PushMatrix
Rotate:
angle: root.angle_of_rudder
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.