class ShowTimeThread (Thread): def __init__(self): Thread.__init__(self) self.c = 1 self.servo = Servo(12) def run(self): global milltime while True: if milltime == 0: continue dt = datetime.fromtimestamp(milltime) lcd.printString(dt.strftime('%Y/%m/%d'), 0, 0) lcd.printString(dt.strftime('%H:%M'), 0, 1) self.servo.write(90 + (30 * self.c)) self.c *= -1 time.sleep(1)
#!/usr/bin/env python # Author: Andrea Stagi <*****@*****.**> # Description: move a servo motor # Dependencies: None from nanpy import Servo import time servo = Servo(7) for move in [0, 90, 180, 90, 0]: servo.write(move) time.sleep(1)
flKnee = Servo(2) #knee flFoot = Servo(13) #foot brHip = Servo(12) #hip brKnee = Servo(11) #knee brFoot = Servo(10) #foot blHip = Servo(9) #hip blKnee = Servo(8) #knee blFoot = Servo(7) #foot # Zero time.sleep(1) print "Zeroing" frHip.write(100) frKnee.write(100) #lower is forward frFoot.write(90) #lower is forward flHip.write(100) flKnee.write(95) #lower is back flFoot.write(80) #lower is back brHip.write(100) brKnee.write(95) #lower is inward brFoot.write(95) #lower is outward blHip.write(100) blKnee.write(95) #lower is outward blFoot.write(115) #lower is outward
from time import sleep from std_msgs.msg import Bool from std_msgs.msg import String import os from nanpy import ArduinoApi from nanpy import SerialManager from nanpy import Servo import logging logging.basicConfig(level=logging.DEBUG) SERVO_PAN = 7 SERVO_TILT = 8 connection = SerialManager(device='/dev/ttyACM0') panservo=Servo(SERVO_PAN) tiltservo=Servo(SERVO_TILT) panservo.write(40) tiltservo.write(55) sleep(1) panservo.write(40) tiltservo.write(55) sleep(1) panservo.write(40) tiltservo.write(55) r = sr.Recognizer() rospack = rospkg.RosPack() FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 CHUNK = 1024 RECORD_SECONDS = 3
servoPin = 3 buttonPin = 8 buttonState = 0 servoRun = servoIncriment try: connection = SerialManager() a = ArduinoApi(connection=connection) except: print("Failed to connect to Arduino") #Setup arduino pins like in arduino IDE servo = Servo(servoPin) a.pinMode(buttonPin, a.INPUT) servo.write(0) try: while True: buttonState = a.digitalRead(buttonPin) print(" Button State: {}".format(buttonState)) if buttonState: if servoRun > 180: servoRun = 0 servo.write(servoRun) print("Servo Runs to: {}".format(servoRun)) servoRun += servoIncriment sleep(sleepTime) buttonState = False except:
from nanpy import (ArduinoApi, SerialManager, Servo) import urllib.request try: connection = SerialManager() a = ArduinoApi(connection=connection) except: print("Failed to connect to Arduino") counter = 0 servo = Servo(3) link = "http://192.168.1.85:1337/" f = urllib.request.urlopen(link) servo.write(0) while (True): f = urllib.request.urlopen(link) myfile = f.read() full_rot_bool = True if (full_rot_bool): servo.write(180) full_rot_bool = False else: servo.write(0) full_rot_bool = True
Mem1.add(int(Sensor3_feild_sense)) Mem1.add(int(Sensor4_feild_sense)) Mem1.add(int(Sensor5_feild_sense)) Mem1.add(int(Sensor6_feild_sense)) print(Mem1) print( "Number of sensor" + str(int(len(Mem1)) - 1) ) # Len will detected and calculate the joint number to running 1 time for the detection #stop_listening = r.listen_in_background(m,callback) # Sensor1 condition working control application if int(Sensor1_feild_sense) >= 600: #if 4095 in Mem1 == "True": engine.say('I feel touch ') engine.runAndWait() for move in [45, 90]: servo.write(move) servo2.write(move + 20) servo3.write(move + 30) servo4.write(90 - move) servo5.write(move + 10) servo6.write(move) print("finger1 Angle:" + str(move)) time.sleep(0.1) else: print("Sensor1 feild sense waiting status......." + "[" + str(Sensor1_feild_sense) + "]") print("Sensor2 feild sense waiting status......." + "[" + str(Sensor2_feild_sense) + "]") print("Sensor3 feild sense waiting status......." + "[" + str(Sensor3_feild_sense) + "]")
""" An entire file for you to expand. Add methods here, and the client should be able to call them with json-rpc without any editing to the pipeline. """ from nanpy import (Servo, ArduinoApi, SerialManager) from time import sleep lightsPin = 7 ledState = False servo = Servo(9) servo.write(0) tempPin = 6 try: connection = SerialManager() a = ArduinoApi(connection=connection) except: print("Failed to connect to Arduino") a.pinMode(lightsPin, a.OUTPUT) a.pinMode(tempPin, a.OUTPUT) def toggle_led(on): if on: a.digitalWrite(lightsPin, a.HIGH) else: a.digitalWrite(lightsPin, a.LOW)
#!/usr/bin/env python import rospy from time import sleep from std_msgs.msg import String from nanpy import ArduinoApi from nanpy import SerialManager from nanpy import Servo import logging logging.basicConfig(level=logging.DEBUG) SERVO_PAN = 7 SERVO_TILT = 8 connection = SerialManager(device='/dev/ttyACM0') panservo=Servo(SERVO_PAN) tiltservo=Servo(SERVO_TILT) panservo.write(100) tiltservo.write(40) sleep(1) panservo.write(100) tiltservo.write(40) sleep(1) panservo.write(100) tiltservo.write(40) def move_servo(data): global panservo global tiltservo if('move left' in data.data): panservo.write(panservo.read()+10) #tiltservo.write(tiltservo.read()) if('move right' in data.data): panservo.write(panservo.read()-10)
a.digitalWrite(11, a.LOW) a.digitalWrite(2, a.HIGH) for i in range(0, 450, 1): print(i) a.digitalWrite(12, a.HIGH) a.digitalWrite(6, a.HIGH) time.sleep(0.00002) a.digitalWrite(6, a.LOW) a.digitalWrite(12, a.LOW) time.sleep(0.00002) for move in range(20, 180, 1): print(move) #print(ser.readline()) servo3e.write(10 - move) servo3u.write(move) servo1u.write(4 + move) servo1e.write(5 + move) servo2u.write(170 - move) servo2e.write(move) time.sleep(0.01) time.sleep(0.1) StepperFoward() for move in range(180, 19, -1): print(move) # print(ser.readline()) servo3e.write(move) servo3u.write(move) servo1u.write(4 + move)
from mcpi import minecraft from nanpy import Servo, SerialManager import time mc = minecraft.Minecraft.create() def get_angle(pos_player, max_alt=180): angle = pos_player.y * (90.0/max_alt) + 90 return int(angle) connection = SerialManager( device='/dev/ttyACM0', rtscts=True ) servo = Servo( 7, connection=connection ) while True: pos_player = mc.player.getPos() angle = get_angle(pos_player) servo.write(angle)
encoded = r.get(n) h, w = struct.unpack('>II', encoded[:8]) a = np.frombuffer(encoded, dtype=np.uint8, offset=8).reshape(h, w, 3) return a while True: a = 0 b = 0 img = fromRedis(r, 'image') img = cv2.resize(img, (360, 200)) cv2.imwrite('image.jpg', img) image = face_recognition.load_image_file("image.jpg") face_locations = face_recognition.face_locations(image) print(face_locations) try: loc = face_locations[0] print(loc[1]) if prev > loc[1]: a = loc[1] b = prev else: a = prev b = loc[1] for i in range(a, b): servo.write(160 - loc[1] / 2) print(160 - loc[1] / 2) prev = 180 - loc[1] except: print("No body there")
from nanpy import (ArduinoApi, SerialManager, Servo) from time import sleep import sys try: connection = SerialManager() a = ArduinoApi(connection=connection) a.pinMode(13, a.OUTPUT) servo = Servo(9) except: print("Can't connect to arduino") sys.exit(0) try: while True: a.digitalWrite(13, a.HIGH) servo.write(90) sleep(0.1) a.digitalWrite(13, a.LOW) servo.write(80) sleep(0.1) except Exception: a.digitalWrite(13, a.LOW) servo.write(90) sys.exit(0)
return (compensation_angle_x, compensation_angle_y) ### Setup of camera and pan-tilt ### camera = create_video_stream() camera_center = map(lambda x: x/2, camera.resolution) # from camera resolution # Face detection Haar cascade file face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml') # Initial pan-tilt angles angle_x = 90 angle_y = 90 servo_x.write(angle_x) servo_y.write(angle_y) while True: try: # Create a memory stream to avoid photos needing to be saved in a file p_stream = io.BytesIO() camera.capture(p_stream, format='jpeg', use_video_port=True, thumbnail=None) buff = numpy.fromstring(p_stream.getvalue(), dtype=numpy.uint8) image = cv2.imdecode(buff, 1) # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def main(): # GPIO setup GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(FACE_DETECTION_STATUS_PIN, GPIO.OUT) # Serial connection setup (for the servos) connection = SerialManager() ArduinoApi(connection=connection) servo_x = Servo(SERVO_X_ARDUINO_PIN) servo_y = Servo(SERVO_Y_ARDUINO_PIN) # Threaded Video stream setup vs = create_video_stream(CAMERA_RESOLUTION, CAMERA_FRAMERATE, CAMERA_ROTATION) camera_center = map(lambda x: x / 2, CAMERA_RESOLUTION) # from camera resolution # Face detection Haar cascade file face_cascade = cv2.CascadeClassifier( '/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml') # Initial pan-tilt angles angle_x = 90 angle_y = 90 servo_x.write(angle_x) servo_y.write(angle_y) def stop(): vs.stop() GPIO.output(FACE_DETECTION_STATUS_PIN, False) GPIO.cleanup() connection.close() def exit_handler(signum, frame): stop() print "\nBye!" sys.exit(0) # Clean manual stop signal.signal(signal.SIGINT, exit_handler) while True: try: # grab the frame from the threaded video stream frame = vs.read() # Convert to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Look for faces in the image faces = face_cascade.detectMultiScale(gray, 1.5, 2) if faces is not (): # Display face detection on a led GPIO.output(FACE_DETECTION_STATUS_PIN, True) # Look for the biggest face biggest_face = get_biggest_face(faces) # Compute pan-tilt angle to adjust centring ax, ay = get_compensation_angle(biggest_face, camera_center, CAMERA_RESOLUTION, CAMERA_ANGLE_OF_VIEW) # Update angles values angle_x = angle_x + ax angle_y = angle_y + ay servo_x.write(angle_x) servo_y.write(angle_y) else: GPIO.output(FACE_DETECTION_STATUS_PIN, False) except Exception as e: print e stop() sys.exit(1)