如何加入2个python文件& kivy

时间:2016-12-07 19:37:41

标签: python 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
<CustButton@Button>:
    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)

1 个答案:

答案 0 :(得分:0)

试试这样。

我无法测试你的代码,因为我没有微控制器和joystik。但这可能会让你知道如何做到这一点。

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