Example #1
0
 def on_event(self, event, payload):
     # Early abort in case of out ot filament when start printing, as we
     # can't change with a cold nozzle
     if event is Events.PRINT_STARTED and self.no_filament():
         self._logger.info("Printing aborted: no filament detected!")
         self._printer.cancel_print()
     # Enable sensor
     if event in (
         Events.PRINT_STARTED,
         Events.PRINT_RESUMED
     ):
         self._logger.info("%s: Enabling filament sensor." % (event))
         if self.sensor_enabled():
             GPIO.remove_event_detect(self.pin)
             GPIO.add_event_detect(
                 self.pin, GPIO.BOTH,
                 callback=self.sensor_callback,
                 bouncetime=self.bounce
             )
     # Disable sensor
     elif event in (
         Events.PRINT_DONE,
         Events.PRINT_FAILED,
         Events.PRINT_CANCELLED,
         Events.ERROR
     ):
         self._logger.info("%s: Disabling filament sensor." % (event))
         GPIO.remove_event_detect(self.pin)
Example #2
0
 def testInvalidBouncetime(self):
     with self.assertRaises(ValueError):
         GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=-1)
     with self.assertRaises(ValueError):
         GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=-1)
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=123)
     with self.assertRaises(RuntimeError):
         GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=321)
     GPIO.remove_event_detect(LOOP_IN)
Example #3
0
 def testRisingEventDetected(self):
     GPIO.output(LOOP_OUT, GPIO.LOW)
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     time.sleep(0.01)
     self.assertEqual(GPIO.event_detected(LOOP_IN), False)
     GPIO.output(LOOP_OUT, GPIO.HIGH)
     time.sleep(0.01)
     self.assertEqual(GPIO.event_detected(LOOP_IN), True)
     GPIO.output(LOOP_OUT, GPIO.LOW)
     time.sleep(0.01)
     self.assertEqual(GPIO.event_detected(LOOP_IN), False)
     GPIO.remove_event_detect(LOOP_IN)
    def __init__(self,
                 pwm_pin,
                 n1_pin,
                 n2_pin,
                 tick_pin,
                 m_pr_tick,
                 pwm_speed=100,
                 control_loop_hz=100.0):
        # pins
        self.pwm_pin = pwm_pin
        self.n1_pin = n1_pin
        self.n2_pin = n2_pin
        self.tick_pin = tick_pin
        # options
        self.pwm_speed = pwm_speed
        self.control_loop_hz = control_loop_hz
        self.m_pr_tick = m_pr_tick

        self.p_gain = 0
        self.i_gain = 0.01

        # variables
        self.last_tick = rospy.get_rostime()
        self._set_speed = 0.0
        self.speed = 0.0
        self.direction = 0.0
        self.p_reg = 0.0
        self.i_reg = 0.0
        self.last_pwm = 0.0
        self.got_tick = False
        self.direction = 0

        # Pin setup
        self.N1 = GpioTogglePin(self.n1_pin)
        self.N2 = GpioTogglePin(self.n2_pin)
        GPIO.setup(self.pwm_pin, GPIO.OUT)
        GPIO.setup(self.tick_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

        self.pwm = GPIO.PWM(self.pwm_pin, self.pwm_speed)
        self.pwm.start(0.0)

        GPIO.add_event_detect(self.tick_pin,
                              GPIO.FALLING,
                              callback=self.tick_callback,
                              bouncetime=1)

        rospy.Timer(rospy.Duration(1.0 / self.control_loop_hz),
                    self.control_loop)
        rospy.Timer(rospy.Duration(0.1), self.speed_resetter)
Example #5
0
    def testExceptionInCallback(self):
        self.run_cb = False

        def cb(channel):
            with self.assertRaises(ZeroDivisionError):
                self.run_cb = True
                a = 1 / 0

        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb)
        time.sleep(0.01)
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        time.sleep(0.01)
        self.assertEqual(self.run_cb, True)
        GPIO.remove_event_detect(LOOP_IN)
Example #6
0
    def testWaitForEdgeWithCallback(self):
        def cb():
            raise Exception("Callback should not be called")

        def makehigh():
            GPIO.output(LOOP_OUT, GPIO.HIGH)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t = Timer(0.1, makehigh)

        GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
        t.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_callback(LOOP_IN, callback=cb)
        with self.assertRaises(RuntimeError):  # conflicting edge exception
            GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)

        GPIO.remove_event_detect(LOOP_IN)
Example #7
0
 def attach_interrupts(self):
     if self.return_config_bool("pcb") and GPIO.input(
             self.check_pin
     ) == GPIO.LOW:  # check if there is an pcb and if so attach the interrupts
         GPIO.add_event_detect(self.check_pin,
                               GPIO.RISING,
                               callback=self.pcb_interrupt
                               )  # if not the interrupt gets attached
         if GPIO.input(
                 self.power_pin
         ) == GPIO.HIGH:  #when the system gets startet in the on position it gets shutdown
             print('shutting down')
             os.system("sudo shutdown -h now")
         else:
             self.led(1)
             GPIO.add_event_detect(self.reset_pin,
                                   GPIO.FALLING,
                                   callback=self.reset_interrupt)
             GPIO.add_event_detect(self.power_pin,
                                   GPIO.RISING,
                                   callback=self.power_interrupt)
     else:  #no pcb attached so lets exit
         print('no PCB detected')
         GPIO.cleanup()
         exit()
Example #8
0
    def __init__(self, inputs=[[0x48, 0]], rdy_pin=17):
        self.inputs = inputs
        self.rdy_pin = rdy_pin
        self.current_input = 0
        self.last_val = 0

        rospy.init_node('adc_node')
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.ASUS)
        GPIO.setup(self.rdy_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(self.rdy_pin,
                              GPIO.FALLING,
                              callback=self.reading_ready,
                              bouncetime=1)
        self.waiting_for_ready = False

        # Setup adc hi/low tresh to use for conversion ready signal
        for ind in inputs:
            adr = ind[0]
            bus.write_i2c_block_data(adr, 0x02, [0b00000000, 0b00000000])
            bus.write_i2c_block_data(adr, 0x03, [0b10000000, 0b00000000])

        rospy.sleep(0.1)
        self.get_next_reading()
Example #9
0
    def testAddEventCallback(self):
        def cb(channel):
            self.callback_count += 1

        # falling test
        self.callback_count = 0
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        GPIO.add_event_detect(LOOP_IN, GPIO.FALLING)
        GPIO.add_event_callback(LOOP_IN, cb)
        time.sleep(0.01)
        for i in range(2048):
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.001)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.001)
        GPIO.remove_event_detect(LOOP_IN)
        self.assertEqual(self.callback_count, 2048)

        # rising test
        self.callback_count = 0
        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb)
        time.sleep(0.01)
        for i in range(2048):
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.001)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.001)
        GPIO.remove_event_detect(LOOP_IN)
        self.assertEqual(self.callback_count, 2048)

        # both test
        self.callback_count = 0
        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.BOTH, callback=cb)
        time.sleep(0.01)
        for i in range(2048):
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.001)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.001)
        GPIO.remove_event_detect(LOOP_IN)
        self.assertEqual(self.callback_count, 4096)
Example #10
0
def GpioSetup():
    GPIO.setwarnings(False)  #disable warning
    GPIO.setmode(GPIO.ASUS)  #USE ASUS mode

    GPIO.setup(encoder_sw, GPIO.IN, pull_up_down=GPIO.PUD_DOWN
               )  #encoder's switch input and disable pull up resistor
    GPIO.setup(encoder_a, GPIO.IN)  #define encoder channel A and B
    GPIO.setup(encoder_b, GPIO.IN)

    GPIO.add_event_detect(encoder_a, GPIO.RISING, callback=RotaryInterrupt)
    GPIO.add_event_detect(encoder_b, GPIO.RISING, callback=RotaryInterrupt)
    GPIO.add_event_detect(encoder_sw,
                          GPIO.RISING,
                          callback=SwitchInterrupt,
                          bouncetime=250)

    GPIO.setup(usb_mode_sw, GPIO.OUT)
    GPIO.setup(i2s_sw, GPIO.OUT)
    GPIO.setup(ac_sw, GPIO.OUT)
    GPIO.setup(dynalo_sw, GPIO.OUT)
    GPIO.setup(relay_en, GPIO.OUT)
    GPIO.setup(relay_s0, GPIO.OUT)
    GPIO.setup(relay_s1, GPIO.OUT)
    GPIO.setup(relay_s2, GPIO.OUT)
Example #11
0
 def setPinMode(self):
     for pin in self.validPins:
         #print pin
         if (self.pinUse[pin] == self.POUTPUT):
             print 'setting pin' , pin , ' to out' 
             try:
                 GPIO.remove_event_detect(pin)
             except:
                 pass
             try:
                 self.callbackInUse[pin] = False
             except:
                 pass                    
             GPIO.setup(pin,GPIO.OUT)
             if (self.pinInvert[pin] == True):
                 GPIO.output(pin,1)
             else:
                 GPIO.output(pin,0)
             self.pinValue[pin] = 0
         elif (self.pinUse[pin] == self.PINPUT):
             print 'setting pin' , pin , ' to in with pull up' 
             GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
             try:
                 GPIO.add_event_detect(pin, GPIO.BOTH, callback=self.gpioBoth,bouncetime=50)  # add rising edge detection on a channel
             except:
                 pass
         elif (self.pinUse[pin] == self.PINPUTDOWN):
             print 'setting pin' , pin , ' to in with pull down' 
             GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
             try:
                 GPIO.add_event_detect(pin, GPIO.BOTH, callback=self.gpioBoth,bouncetime=50)  # add rising edge detection on a channel
             except:
                 pass             
         elif (self.pinUse[pin] == self.PINPUTNONE):
             print 'setting pin' , pin , ' to in with pull down' 
             GPIO.setup(pin,GPIO.IN)
             try:
                 GPIO.add_event_detect(pin, GPIO.BOTH, callback=self.gpioBoth,bouncetime=50)  # add rising edge detection on a channel
             except:
                 pass             
         elif (self.pinUse[pin] == self.PCOUNT):
             if self.callbackInUse[pin] == False:
                 print 'setting pin' , pin , ' as counting pin' 
                 GPIO.setup(pin,GPIO.IN)#,pull_up_down=GPIO.PUD_DOWN)#,pull_up_down=GPIO.PUD_DOWN)
                 try: # add event callback but use try block just in case its already set
                     if self.encoderCallback == 1:
                         #GPIO.add_event_detect(pin, GPIO.RISING, callback=self.my_callbackB)#,bouncetime=10)  # add rising edge detection on a channel
                         self.callbackInUse[pin] = True
                         self.encoderCallback = 2
                         if self.debug:
                             print "callback B set for pin ", pin
                         
                     if self.encoderCallback == 0:
                         #GPIO.add_event_detect(pin, GPIO.RISING, callback=self.my_callbackA)#,bouncetime=10)  # add rising edge detection on a channel
                         self.callbackInUse[pin] = True
                         self.encoderCallback = 1
                         if self.debug:
                             print "callback A set for pin ", pin
                         
                 except Exception,e: 
                     print "Error on event detection setup on pin" ,pin
                     print str(e)
             else:
                 print ("Callback already in use")
Example #12
0
 def testAlreadyAdded(self):
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     with self.assertRaises(RuntimeError):
         GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     GPIO.remove_event_detect(LOOP_IN)
Example #13
0
 def testEventOnOutput(self):
     with self.assertRaises(RuntimeError):
         GPIO.add_event_detect(LOOP_OUT, GPIO.FALLING)
Example #14
0
 def testHighLowEvent(self):
     with self.assertRaises(ValueError):
         GPIO.add_event_detect(LOOP_IN, GPIO.LOW)
     with self.assertRaises(ValueError):
         GPIO.add_event_detect(LOOP_IN, GPIO.HIGH)
import ASUS.GPIO as GPIO  
from time import sleep     # this lets us have a time delay (see line 12)  
  
GPIO.setmode(GPIO.BOARD)     # set up BCM GPIO numbering  
GPIO.setup(3, GPIO.IN)    # set GPIO25 as input (button)  
  
# Define a threaded callback function to run in another thread when events are detected  
def my_callback(channel):  
    if GPIO.input(3):     # if port 25 == 1  
        print "Rising edge detected on 25"  
    else:                  # if port 25 != 1  
        print "Falling edge detected on 25"  
  
# when a changing edge is detected on port 25, regardless of whatever   
# else is happening in the program, the function my_callback will be run  
GPIO.add_event_detect(3, GPIO.BOTH, callback=my_callback)  
  
print "Program will finish after 30 seconds or if you press CTRL+C\n"  
print "Make sure you have a button connected, pulled down through 10k resistor"  
print "to GND and wired so that when pressed it connects"  
print "GPIO port 25 (pin 22) to GND (pin 6) through a ~1k resistor\n"  
  
print "Also put a 100 nF capacitor across your switch for hardware debouncing"  
print "This is necessary to see the effect we're looking for"  
raw_input("Press Enter when ready\n>")  
  
try:  
    print "When pressed, you'll see: Rising Edge detected on 252"  
    print "When released, you'll see: Falling Edge detected on 252"  
    sleep(30)         # wait 30 seconds  
    print "Time's up. Finished!"