Skip to content

Instantly share code, notes, and snippets.

@tusing
Created December 10, 2016 00:54
Show Gist options
  • Save tusing/d2bbe0a9ac8b7e4a10379a7632fceeb0 to your computer and use it in GitHub Desktop.
Save tusing/d2bbe0a9ac8b7e4a10379a7632fceeb0 to your computer and use it in GitHub Desktop.
project stuff
import freenect
import cv2
import numpy as np
import sys, getopt
import math
import RPi.GPIO as GPIO
import time
import atexit
def main(argv):
#Setup the GPIO
pins = [3, 5, 7, 8, 10, 12, 11, 13, 15]
GPIO.setmode(GPIO.BOARD)
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
test()
#Main loop
while True:
array, _ = freenect.sync_get_depth()
array = array.astype(np.uint8)
#sys.stdout.write('\r' + str(subdivide(array, 9)))
arr2 = subdivide(array, 9)
print(str(arr2))
vibrate(arr2, pins)
#texit.register(GPIO.cleanup)
def subdivide(array, k):
"""Subdivide array into sqrt(k) and provide output with function.
"""
num_seg = int(math.sqrt(k))
w, h = [int(x/num_seg) for x in array.shape]
array = [[int(np.average(array[w*nw:w*(nw+1), h*nh:h*(nh+1)])) for nh in range(num_seg)] for nw in range(num_seg)]
return array
def vibrate(array, pins):
"""Vibrates the motors if they are over a threshold
"""
vibe = False
index = 0
for i in range(len(array)):
for j in range(len(array[i])):
if array[i][j] > 200:
vibe = True
GPIO.output(pins[index], GPIO.HIGH)
else:
GPIO.output(pins[index], GPIO.LOW)
index += 1
#if vibe == True:
# time.sleep(1)
#for pin in pins:
# GPIO.output(pin, GPIO.LOW)
def test():
pins = [3, 5, 7, 8, 10, 12, 11, 13, 15]
for pin in pins:
GPIO.output(pin,GPIO.LOW)
if __name__ == "__main__":
main(sys.argv[1:])
import RPi.GPIO as GPIO
def main(argv):
GPIO.cleanup()
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
p = GPIO.PWM(25, 50)
p.start(0)
while(True):
for dc in xrange(100):
p.ChangeDutyCycle(dc)
time.sleep(0.1)
p.stop()
GPIO.cleanup()
import wiringpi as wp
PIN = 2
wp.wiringPiSetup()
wp.pinMode(PIN, 1)
wp.softPwmCreate(PIN, 0, 100)
#dutyCycle = 20
for time in range(10):
for dutyCycle in range(100):
wp.softPwmWrite(PIN, dutyCycle)
wp.delay(10)
import wiringpi
from time import sleep
pin_base = 65
i2c_addr = 0x20
wiringpi.wiringPiSetup()
wiringpi.mcp23017Setup(pin_base,i2c_addr)
wiringpi.pinMode(65, 1)
#wiringpi.pinMode(27, 1)
wiringpi.digitalWrite(70, 1)
#for i in range(0, 100):
# wiringpi.digitalWrite(27, 1)
# sleep(1)
# wiringpi.digitalWrite(27, 0)
# sleep(1)
#while(True):
# wiringpi.digitalWrite(65, 1)
# sleep(1)
import wiringpi
from time import sleep
wiringpi.wiringPiSetup()
wiringpi.sr595Setup(100, 8, 28, 26, 27)
wiringpi.softPwmCreate(106, 0, 100)
wiringpi.softPwmCreate(103, 0, 100)
while True:
wiringpi.softPwmWrite(106, 70)
wiringpi.softPwmWrite(103, 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment