def encoder_run(self): GPIO.add_event_detect(self.encoder_pinA, GPIO.BOTH, callback=self.encoder_A) GPIO.add_event_detect(self.encodef_pinB, GPIO.BOTH, callback=self.encoder_B)
def main(): # Set our GPIO numbering to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin that we have our digital output from our sensor connected to channel = 17 # Set the GPIO pin to an input GPIO.setup(channel, GPIO.IN) # This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function GPIO.add_event_callback(channel, callback) while True: time.sleep(600)
def initialize(self): # Turn Everything off for key in self.rf_transmit_keys.keys(): self.switch_off(key) # Initialize Buttons for key in self.rf_transmit_keys.keys(): # Setup Button GPIO.setup(self.rf_transmit_keys[key]['ButtonGPIO'], GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup LED GPIO.setup(self.rf_transmit_keys[key]['LEDGPIO'], GPIO.OUT) GPIO.output(self.rf_transmit_keys[key]['LEDGPIO'], GPIO.HIGH) GPIO.output(self.rf_transmit_keys[key]['LEDGPIO'], GPIO.LOW) # Set Interrupts for Button Presses GPIO.add_event_detect(self.rf_transmit_keys['VentFan']['ButtonGPIO'], GPIO.FALLING, callback=self.vent_fan_callback, bouncetime=300) GPIO.add_event_detect(self.rf_transmit_keys['Heater']['ButtonGPIO'], GPIO.FALLING, callback=self.heater_callback, bouncetime=300) GPIO.add_event_detect(self.rf_transmit_keys['GrowLight']['ButtonGPIO'], GPIO.FALLING, callback=self.grow_light_callback, bouncetime=300) GPIO.add_event_detect(self.rf_transmit_keys['StandFan']['ButtonGPIO'], GPIO.FALLING, callback=self.stand_fan_callback, bouncetime=300)
class WallStop(): def __init__(self): self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=1) self.goahead_param = 500 self.back_param = 1000 self.sensor_values = LightSensorValues() rospy.Subscriber('/lightsensors', LightSensorValues, self.callback) self.M = 0.10 self.M1 = 0.00 self.e = 0.00 self.e1 = 0.00 self.e2 = 0.00 self.goal = 600 self.Kp = 0.0002 self.Ki = 0.000025 self.Kd = 0.00010 def callback(self, messages): self.sensor_values = messages def callBackTest(channel): print("callback") GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) GPIO.add_event_detect(pin, GPIO.FALLING, callBack=callBackTest, bouncetime=300) try: while (Trure): time.sleep(1) except KeyboardInterrpt: print("break") GPIO.cleanup() def run(self): rate = rospy.Rate(10) data = Twist() data.linear.x = 0.00 while not rospy.is_shutdown(): key = ord(getch()) if key == 65: data.linear.x = 0.1 elif key == 66: data.linear.x = -0.1 elif key == 67: data.linear.y = 0.1 elif key == 68: data.linear.y = -0.1 elif key == 13: print("Enter") break else: pause() """self.M1 = self.M self.e2 = self.e1 self.e1 = self.e self.e = self.goal - self.sensor_values.sum_all #self.M = self.M1 + self.Kp * (self.e-self.e1) #self.M = self.M1 + self.Kp * (self.e-self.e1) + self.Kd * ((self.e-self.e1) - (self.e1-self.e2)) self.M = self.M1 + self.Kp * (self.e-self.e1) + self.Ki * self.e + self.Kd * ((self.e-self.e1) - (self.e1-self.e2)) if self.sensor_values.sum_all < self.goahead_param: data.linear.x = 0.2 elif self.sensor_values.sum_all > self.back_param: data.linear.x = -0.2 else: data.linear.x = 0.0 if self.M < 0.25 and self.M > -0.25: data.linear.x = self.M #else: data.linear.x = 0.0 #print('move param_____________' + str(self.M))""" self.cmd_vel.publish(data) rate.sleep()
# *********************************************************************************************************** # Timer Interrupt Setup End # *********************************************************************************************************** # External Interrupt Setup for left and right encoders # Each interrupt (L and R) will execute at the rising edge of the incoming signal # Define our motor pins as outputs GPIO.setup(pin1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(pin2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(pin3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(pin4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(pin1, GPIO.RISING, callback=ei_r) GPIO.add_event_Detect(pin2, GPIO.RISING, callback=ei_l) def ei_l(): if GPIO.INPUT(pin3): counter_L+=1 elif GPIO.INPUT(pin3)==GPIO.LOW: counter_L-=1 def ei_r(): if GPIO.INPUT(pin4): counter_R+=1 elif GPIO.INPUT(pin4)==GPIO.LOW: counter_R-=1
# TC4_IRQn where 4 is the timer number * timer channels (3) + the channel number (=(1*3)+1) for timer1 channel1 NVIC_EnableIRQ(TC1_IRQn) # *********************************************************************************************************** # Timer Interrupt Setup End # *********************************************************************************************************** # External Interrupt Setup for left and right encoders # Each interrupt (L and R) will execute at the rising edge of the incoming signal # Define our motor pins as outputs GPIO.setup(pin1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(pin2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(pin3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(pin4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(pin1, GPIO.RISING, callback=EI_R) GPIO.add_event_Detect(pin2, GPIO.RISING, callback=ei_l) def ei_l(): if GPIO.INPUT(pin3): counter_L += 1 elif GPIO.INPUT(pin3) == GPIO.LOW: counter_L -= 1 def ei_r(): if GPIO.INPUT(pin4): counter_R += 1 elif GPIO.INPUT(pin4) == GPIO.LOW: counter_R -= 1
# -*- coding: utf-8 -*- """ Created on Mon Mar 19 08:30:36 2018 @author: Paulo Augusto """ """ USE SU TO RUN THIS SCRIPT """ import RPI.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback, bouncetime=300) def my_callback(channel): print "Got something" RPIO.wait_for_interrupts(threaded=True) while True: GPIO.clean_up()