2016-12-07 8 views
0

私は2つのpythonファイル(1つはkivyファイル)を結合しようとしています。2つのpythonファイルとkivyの結合方法

それでは、どのように私はこの(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() 

(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() 

を結合し、それはこれで(piMote.kv)を動作させるん

#: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) 

答えて

0

このようにしてください。

私はマイクロコントローラとジョイスティックを持っていないので、コードをテストできません。しかし、これはおそらくあなたにそれがどのようにできるかについてのアイデアを与えるでしょう。

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 
from kivy.clock import Clock 

import time 
import Adafruit_GPIO.SPI as SPI 
import Adafruit_MCP3008 
import paho.mqtt.client as mqtt 

currentRobot = "kvakke" 

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 

SPI_PORT = 0 
SPI_DEVICE = 0 
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) 

deadBand = 100 



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) 



class piMoteGridLayout(GridLayout): 

    def __init__(self,**kwargs): 
     super(piMoteGridLayout,self).__init__(**kwargs) 
     Clock.schedule_interval(self.update_joystik, 0.5) 


    def callback(self, instance): 
     mqttc.publish("piMote/" + currentRobot +"/button", instance) 
     time.sleep(0.1) 


    def update_joystik(self,dt): 
     values = [0]*8 
     for i in range(8): 
      values[i] = mcp.read_adc(i) 

     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)) 



class piMoteApp(App): 
    def build(self): 
     return piMoteGridLayout() 


if __name__ == '__main__': 
    piMoteApp().run() 
+0

私はコードに小さな自己を追加したとき、それは働いた: デフ__init __(自己、** kvargs): – PunyViking

+0

OFC :)ことを忘れてしまったそう@PunyViking。前述のように、私はコードを実行することができませんでした希望はあなたの質問に答えるtho :)私は答えを編集する – EL3PHANTEN

関連する問題