def __init__(self, trigger, echo): self.trigPin = trigger self.echoPin = echo GPIO.output(trigger, GPIO.LOW) # attempt to pre-set it low before enabling output GPIO.setup(trigger, GPIO.OUT) GPIO.output(trigger, GPIO.LOW) GPIO.setup(echo, GPIO.IN)
# On one pair, the main pin "leads", on the other encoder the quad pin leads. # So one encoder counts up, the other down. (16bit unsigned) import time import virtGPIO as GPIO counter2 = GPIO.Intcounter(2, GPIO.Intcounter.QUAD) # Quad encoder oper GPIO.setup(2,GPIO.OUT) GPIO.setup(4,GPIO.OUT) counter3 = GPIO.Intcounter(3, GPIO.Intcounter.QUAD) GPIO.setup(3,GPIO.OUT) GPIO.setup(5,GPIO.OUT) print() print ("Initial counter readings: "), print ( counter2.read()) print ("We will now count one UP and one DOWN.") print ("1000 pulses. (Quad encoder counts at both rising and falling edges) ...") for k in range(1,2001): GPIO.output(4,(k&1)) # quad pin leads on first counter GPIO.output(2,(k&1)) GPIO.output(3,(k&1)) # main pin leads on other GPIO.output(5,(k&1)) print ("... Done") print ("Now read both (16-bit unsigned) counters: "), print (counter2.read())
import time import virtGPIO as GPIO led = 6 # put a LED on pin 6 # Note object GPIO.AVR for MCU register access is pre-defined for us in virtGPIO.py # and pre-defined register names are in virtGPIO.py GPIO.setup(led, GPIO.OUT) c = 0 while True: c += 1 GPIO.output(led, c & 1) # LED blink print("pin 6 input by raw atmega238 access %d " % ((GPIO.AVR.read8(GPIO.AVR.PIND) & 0x40) > 0)) # Guided by 328 MCU manual, we read 8-bit register "PIND" and select bit 6 # See <http://www.atmel.com/Images/doc8161.pdf> page 93 # and <http://arduino.cc/en/Hacking/PinMapping168> time.sleep(2) # As another example (example only), # the lines below show the virtGPIO.py internal implementation of analogHiSpeed(), # which changes the clock prescaler of ADC convertor in the 328. # Refer 328 MCU manual, pages 253 and 264. # If you are out of your depth, you don't need any of the raw GPIO.AVR functions! """ def analogHiSpeed():
print ("Also, reading the 'FLAGS' shows a pin dispute reported") print ("by stepper system at Arduino ...") print ("Flags %s" % bin(GPIO.readFlags())) time.sleep(4) print ("Flag bits are defined near top of VirtGPIO.ino. Most refer to conflict when assigning pins.") print ("\033[32m") # green text print ("b0 resetFlag, b1 SPI, b2 Servo, b3 IR, b4 I2C, b5 INTctr, b6 pwmplus, b7 stepper") print ("b8 actLed, b9 SpiXferWithoutOpen, b10 COMport b15 unexpectedCharsArrived") print ("\033[00m") # normal print ("Observe b0 = arduino reset flag has not been cleared, b7 = stepper pins conflict") print time.sleep(10) print ("10 LED toggles high speed") for k in range(10): GPIO.output(led, k&1) # toggle led. print ("Done") time.sleep(3) print ("5000 LED toggles high speed") for k in range(5000): GPIO.output(led, k&1) # toggle led. print ("Done") time.sleep(4) print ("About to cause a problem: (hard) resetting the Arduino!!") GPIO.hardResetARD() time.sleep(5) print ("Destroy the world. Restart this whole program ...") print
import time import virtGPIO as GPIO led = 9 # we have a LED in pin 9 that we will blink c = 0 # simple loop counter # Next line is the key. If we clear the arduino's reset flag, # then we can later test if arduino was (accidentally) reset later on. GPIO.clearResetFlag() GPIO.setup(led, GPIO.OUT) print( "If you press Arduino reset, this script will detect that, and take action ..." ) while True: print("Loop %d" % c) c = (c + 1) GPIO.output(led, c & 1) # LED blinking if not ( c % 4 ): # every 4th loop we test if arduino was reset (& working again, approximately) # If YOU had pressed reset. then our python will spot it now if (GPIO.resetFlagIsOn()): print("Arduino is reset! This program going down for restart ...") GPIO.restart_program() time.sleep(3)
print( "b0 resetFlag, b1 SPI, b2 Servo, b3 IR, b4 I2C, b5 INTctr, b6 pwmplus, b7 stepper" ) print( "b8 actLed, b9 SpiXferWithoutOpen, b10 COMport b15 unexpectedCharsArrived" ) print("\033[00m") # normal print( "Observe b0 = arduino reset flag has not been cleared, b7 = stepper pins conflict" ) print time.sleep(10) print("10 LED toggles high speed") for k in range(10): GPIO.output(led, k & 1) # toggle led. print("Done") time.sleep(3) print("5000 LED toggles high speed") for k in range(5000): GPIO.output(led, k & 1) # toggle led. print("Done") time.sleep(4) print("About to cause a problem: (hard) resetting the Arduino!!") GPIO.hardResetARD() time.sleep(5) print("Destroy the world. Restart this whole program ...") print
import virtGPIO as GPIO led = 9 # Demonstrate classic digital read/write, & analog read, and pwm # Setup: a LED on pin 9, # plus a jumper from pin 9 to pin A2 GPIO.setup(led,GPIO.OUT) c = 0 while True: c = (c+21) GPIO.output(led,c & 1) # LED blink print ("Written LED as %d" % (c&1)) # read it back print ("pin%d=%x" % (led, GPIO.input(led))) # Analog 2 is tied to LED, so should go from 0000 to 03FF (0 to 1023) print ("anl2 = %04x" % GPIO.analogRead(GPIO.A2)) time.sleep(5) # pin 9 is also a native PWM pin on arduino: print ("Changing pwm brightness to %d" % (c%255)) GPIO.pwmWrite(led, c % 256)
def led_illumination_on(): if led_illumination_string.get() == 'IR': print "IR" GPIO.output(ir, 1) #start infrared GPIO.output(white1, 0) #stop white1 GPIO.output(white2, 0) #stop white2 GPIO.output(white3, 0) #stop white3 elif led_illumination_string.get() == 'WHITE': print "WHITE" GPIO.output(ir, 0) #stop infrared GPIO.output(white1, 1) #start white1 GPIO.output(white2, 1) #start white2 GPIO.output(white3, 1) #start white3 else: print "OFF" GPIO.output(ir, 0) #stop infrared GPIO.output(white1, 0) #stop white GPIO.output(white2, 0) #stop white GPIO.output(white3, 0) #stop white
def camera_start(): GPIO.output(ir, 1) #start infrared GPIO.output(white1, 0) #stop white1 GPIO.output(white2, 0) #stop white2 GPIO.output(white3, 0) #stop white3 # subprocess.call(["rm", "1.png","2.png"]) cam = pexpect.spawn('sudo ./Grab', timeout=400) cam.expect('XXXXXX') GPIO.output(ir, 0) #stop infrared GPIO.output(white1, 1) #start white1 GPIO.output(white2, 1) #start white2 GPIO.output(white3, 1) #start white3 sleep(0.4) GPIO.output(white1, 0) #stop white1 GPIO.output(white2, 0) #stop white2 GPIO.output(white3, 0) #stop white3 sleep(2) #now take the generated images and combine them. process_images()
# setting pin5 as MCU-based counter (no interrupts, no arduino counting code!) # Put a LED on pin 5 to see import time import virtGPIO as GPIO led = 5 GPIO.setup(led, GPIO.OUT) counter5 = GPIO.HWcounter() # set up pin 5 as a hardware counter (rising edge). It can STILL do any other GPIO job! c = 0 while True: print(c) c = (c + 1) GPIO.output(led, c & 1) # LED blink on pin 5 print("Dig read 5 = %d" % GPIO.input(led)) # read pin 5 straight back to confirm # But pin 5 is ALSO doing a counter job. Read the current count print("HW ctr: %x" % counter5.read()) # if not (c % 100): # every 100 passes counter5.zero() time.sleep(0.5)
# setup: put a LED on pin 7. import time import virtGPIO as GPIO led = 7 GPIO.setup(led,GPIO.OUT) print ("VCC = %f Volts" % GPIO.VccRead()) c=0 while True: c += 1 GPIO.output(led,c & 1) # LED print ("Read (as list of int16) all analogs: %s" % GPIO.analogReadAll()) print ("Read (as int16) all digitals 0 to 13 %s" % bin(GPIO.digitalReadAll())) print ("Precision digital read pin 7 (LED?): %d" % GPIO.digitalPreciseRead(7)) print ("Precision digital read pin A0: %d" % GPIO.digitalPreciseRead(GPIO.A0)) print ("(precision read is from analog comparator against internal 1.1V reference.)") if c==10: GPIO.analogHiSpeed() print ("Increasing analog read clockspeed: AnalogReadAll() is a lot faster from now.") print ("") time.sleep(1) print ("pulseIn, a 'blocking' call: in this routine, will timeout by lack of a pulse:") print (GPIO.pulseIn(led, GPIO.HIGH, 500, 1000)) # allow 500 uSEc to start, then 1000 uSec max pulselength. # NOTE: those high value returns correspond to "error codes" set in VirtGPIO.ino. # Examine Pulse_in function there, and the "case 'I':" code. # By testing these codes you can see the precise reason your pulsein failed.
# setting pin5 as MCU-based counter (no interrupts, no arduino counting code!) # Put a LED on pin 5 to see import time import virtGPIO as GPIO led = 5 GPIO.setup(led,GPIO.OUT) counter5 = GPIO.HWcounter() # set up pin 5 as a hardware counter (rising edge). It can STILL do any other GPIO job! c=0 while True: print (c) c = (c+1) GPIO.output(led,c & 1) # LED blink on pin 5 print ("Dig read 5 = %d" % GPIO.input(led)) # read pin 5 straight back to confirm # But pin 5 is ALSO doing a counter job. Read the current count print ("HW ctr: %x" % counter5.read()) # if not (c%100): # every 100 passes counter5.zero() time.sleep(0.5)