0
Ich versuche, 2 Python-Dateien (eine mit einer Kivy-Datei) zu kombinieren.Wie zu verbinden 2 Python-Dateien & kivy
So wie kombiniere ich diese (joystick.py)
import time
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Import MQTT
import paho.mqtt.client as mqtt
# Select robot to control
#currentRobot = "quad"
currentRobot = "kvakke"
#currentRobot = "turret"
#currentRobot = "dalek"
# COnfig the mqtt
mqttc = mqtt.Client()
mqttc.connect("piMote.local", 1883) #10.13.37.60
mqttc.publish("piMote/" + currentRobot, "piMote joysticks is online")
mqttc.loop(2) #timeout = 2s
# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
deadBand = 100
# Remapping the joystick value
# return (x_value - in_min) * (out_max - out_min)/(in_max - in_min) + out_min
def remap(mapValue, in_min, in_max, out_min, out_max):
if mapValue < 512 - deadBand:
return((mapValue - in_min) * (out_max - out_min)/(in_max - in_min) + out_min)
elif mapValue > 512 + deadBand:
return((mapValue - in_min) * (out_max - out_min)/(in_max - in_min) + out_min)
else:
return(0)
print('Reading MCP3008 values, press Ctrl-C to quit...')
class joystickControl():
def update(self):
# Read all the ADC channel values in a list.
values = [0]*8
for i in range(8):
# The read_adc function will get the value of the specified channel (0-7).
values[i] = mcp.read_adc(i)
# return (x_value - in_min) * (out_max - out_min)/(in_max - in_min) + out_min
print(remap(values[0], 0, 1024, -255, 255) , remap(values[1], 0, 1024, -255, 255))
# Publish the joystick values
mqttc.publish("piMote/" + currentRobot + "/leftX", remap(values[0], 0, 1024, -255, 255))
mqttc.publish("piMote/" + currentRobot + "/leftY", remap(values[1], 0, 1024, -255, 255))
mqttc.publish("piMote/" + currentRobot + "/leftZ", remap(values[2], 0, 1024, -255, 255))
mqttc.publish("piMote/" + currentRobot + "/rightX", remap(values[3], 0, 1024, -255, 255))
mqttc.publish("piMote/" + currentRobot + "/rightY", remap(values[4], 0, 1024, -255, 255))
mqttc.publish("piMote/" + currentRobot + "/rightZ", remap(values[5], 0, 1024, -255, 255))
# Pause for half a second.
time.sleep(0.5)
joystickControl = joystickControl
while True:
joystickControl().update()
mit diesem (main.py)
import kivy
kivy.require("1.9.0")
import time
import paho.mqtt.client as mqtt
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
# Select robot to control
#currentRobot = "quad"
currentRobot = "kvakke"
#currentRobot = "turret"
#currentRobot = "dalek"
mqttc = mqtt.Client()
mqttc.connect("piMote.local", 1883) #10.13.37.60
mqttc.publish("piMote/"+ currentRobot, "piMote buttons is online")
mqttc.loop(2) #timeout = 2s
class piMoteGridLayout(GridLayout):
def callback(self, instance):
mqttc.publish("piMote/" + currentRobot +"/button", instance)
time.sleep(0.1)
class piMoteApp(App):
def build(self):
return piMoteGridLayout()
piMoteApp = piMoteApp
if __name__ == '__main__':
piMoteApp().run()
und es dann mit diesem (piMote.kv) funktioniert
#:import App kivy.app.App
# Custom button
<[email protected]>:
font_size: 20
<piMoteGridLayout>:
id: piMote
display: entry
rows: 5
padding: 2
spacing: 2
# Where input is displayed
BoxLayout:
TextInput:
id: entry
font_size: 24
Spinner:
text: "Choose Robot"
values: ["Quad", "Kvakke", "Dalek", "Turret"]
# When buttons are pressed update the entry
BoxLayout:
spacing: 2
CustButton:
text: "DPadUp"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "DPadLeft"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnTriangle"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnRound"
on_press:
entry.text = self.text
print"Round"
piMote.callback(entry.text)
BoxLayout:
spacing: 2
CustButton:
text: "DPadRight"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "DPadDown"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnSquare"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnX"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
BoxLayout:
spacing: 2
CustButton:
text: "BtnL1"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnSelect"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnStart"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnR1"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
BoxLayout:
spacing: 2
CustButton:
text: "BtnL2"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnL3"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnR3"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
CustButton:
text: "BtnR1"
on_press:
entry.text = self.text
print entry.text
piMote.callback(entry.text)
Es funktionierte, als ich ein kleines Selbst in den Code hinzugefügt: def __init __ (self, ** kvargs): – PunyViking
@PunyViking ja OFC :) das vergessen. Wie erwähnt, konnte ich den Code Hope, der deine Frage beantwortet, nicht ausführen :) Ich bearbeite die Antwort – EL3PHANTEN