我尝试合并两个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}$
试试这个。在
我不能测试你的代码,因为我没有微控制器和乔伊斯蒂克。但这也许能让你知道怎么做。在
相关问题 更多 >
编程相关推荐