Hi liebe Community,
Ich stelle mich grad ziemlich blöd an, ich versuche ein Programm zu erstellen das die Werte eines Analog Joysticks und die Werte eines Encoders ausliest.
Jeweils einzeln habe ich diese Programme schon und sie Funktionieren. Doch sobald ich diese zusammenfüge kriege ich bei mir immer das reinste chaos raus. Entweder es gibt mir immer nur die werte von einem Bauteil raus oder der Raspi hängt sich auf.
ich bin auch für Tipps dankbar.
Gruss Dani
Das Encoder Programm ist von: https://www.sunfounder.com/learn/Super_Ki…aspberrypi.html
Das Joystick Programm ist von: https://www.raspberrypi-spy.co.uk/2014/04/using-…ing-an-mcp3008/
Code vom Encoder:
Code
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
RoAPin = 11 # pin11
RoBPin = 12 # pin12
RoSPin = 15 # pin13
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN) # input mode
GPIO.setup(RoBPin, GPIO.IN)
GPIO.setup(RoSPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
rotaryClear()
def rotaryDeal():
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
while(not GPIO.input(RoAPin)):
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter + 1
print (globalCounter)
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter - 1
print(globalCounter)
def rotaryClear():
GPIO.add_event_detect(RoSPin, GPIO.FALLING, callback=clear) # wait for falling
def clear(ev=None):
globalCounter = 0
print(globalCounter)
time.sleep(1)
def loop():
global globalCounter
while True:
rotaryDeal()
# print 'globalCounter = %d' % globalCounter
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
Alles anzeigen
Code vom Joystick:
Python
#!/usr/bin/python
#--------------------------------------
# This script reads data from a
# MCP3008 ADC device using the SPI bus.
#
# Analogue joystick version!
#
#--------------------------------------
import spidev
import time
import os
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
swt_channel = 0
vrx_channel = 1
vry_channel = 2
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
print (channel)
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
#print (channel)
return data
# Define sensor channels
# (channels 3 to 7 unused)
#swt_channel = 0
#vrx_channel = 1
#vry_channel = 2
# Define delay between readings (s)
delay = 0.25
while True:
# Read the joystick position data
vrx_pos = ReadChannel(vrx_channel)
vry_pos = ReadChannel(vry_channel)
# Read switch state
swt_val = ReadChannel(swt_channel)
# Print out results
print ("--------------------------------------------")
print("X : {} Y : {} Taster : {}".format(vrx_pos,vry_pos,swt_val))
# Wait before repeating loop
time.sleep(delay)
Alles anzeigen