如何连接2个python文件&kivy

2024-06-26 07:37:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试合并两个python文件(一个与kivy文件)。在

那么我该怎么组合呢(操纵杆.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()

用这个(主.py)在

^{pr2}$

然后用它来工作(皮莫特.kv)在

^{3}$

Tags: theinspireturnoutminpublishmax
1条回答
网友
1楼 · 发布于 2024-06-26 07:37:07

试试这个。在

我不能测试你的代码,因为我没有微控制器和乔伊斯蒂克。但这也许能让你知道怎么做。在

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

相关问题 更多 >