def handleJoyEvent(e): if e.type == pygame.JOYAXISMOTION: axis = "unknown" if (e.dict['axis'] == 0): axis = "X" if (e.dict['axis'] == 1): axis = "Y" if (e.dict['axis'] == 2): axis = "Throttle" if (e.dict['axis'] == 3): axis = "Z" if (axis != "unknown"): str = "Axis: %s; Value: %f" % (axis, e.dict['value']) # uncomment to debug output(str, e.dict['joy']) # Arduino joystick-servo hack if (axis == "X"): pos = e.dict['value'] # convert joystick position to servo increment, 0-180 move = round(pos * 90, 0) if (move < 0): serv = int(90 - abs(move)) else: serv = int(move + 90) # convert position to ASCII character servoPosition = serv # and send to Arduino over serial connection servo.move(1, servoPosition) # Arduino joystick-servo hack if (axis == "Y"): pos = e.dict['value'] # convert joystick position to servo increment, 0-180 move = round(pos * 90, 0) if (move < 0): serv = int(90 - abs(move)) else: serv = int(move + 90) # convert position to ASCII character servoPosition = serv # and send to Arduino over serial connection servo.move(2, servoPosition) elif e.type == pygame.JOYBUTTONDOWN: str = "Button: %d" % (e.dict['button']) # uncomment to debug output(str, e.dict['joy']) # Button 0 (trigger) to quit if (e.dict['button'] == 0): print "Pew Pew You're DEAD!!\n" if (e.dict['button'] == 8): print "Bye!\n" quit() else: pass
def move(self, irc, msg, args, motor, angle): """<motor> <value in degrees> Moves the camera accordingly """ servo.move(motor, angle) irc.reply("Moving Servo %s %s degrees" % (motor, angle))
def run(): while (True): ret, frame = videoFeed.read() if ret == False: print("Failed to retrieve frame") break processed_img = detect.find_face( frame, 40) # try to find face and return processed image if (processed_img[0]): led('green') print('achou') target_pan, target_tilt = calculate_movement( processed_img[2], processed_img[3]) servo.move(target_pan, target_tilt) print('target pan = ' + str(target_pan)) print('target tilt = ' + str(target_tilt)) else: led('red') roam() print(processed_img[0]) cv2.imshow('Feed', frame) if cv2.waitKey(10) & 0xFF == ord("q"): print("cleaning up") GPIO.cleanup() break videoFeed.release() cv2.destroyAllWindows()
def moveto (degrees): #number of servos to dance n = 1 #center all servos servo.move(1, 90) time.sleep(0.5) print 'Moving to', degrees, 'degrees' # perform 9 iterations, 10deg increments i = 9 while i >= 9: wait = 9 * 0.025 # wait time - large deflections need more # move left servoPosition = int(degrees) # command Arduino s=0 while s <= n: servo.move(s, servoPosition) time.sleep(wait) s += 1 time.sleep(wait) i -= 1
def jointAttentionEye(okao): return 0 if okao.gazeLR[0] != 99999: Shared.gazeLR = queue(Shared.gazeLR, okao.gazeLR[0]) Shared.gazeUD = queue(Shared.gazeUD, okao.gazeUD[0] - 6) else: return None tx = 30.0 ty = 20.0 gazelr = max(min(np.average(Shared.gazeLR), tx), -tx) gazeud = max(min(np.average(Shared.gazeUD), ty), -ty) list1 = [-0.5, 0.0, 0.5] list2 = [0.0] lr = getNearestValue(list1, -gazelr / tx) ud = getNearestValue(list2, gazeud / ty) if lr != Shared.preGazeLR: servo.move(1, lr) Shared.preGazeLR = lr if ud != Shared.preGazeUD: servo.move(2, ud) Shared.preGazeUD = ud if lr != 0.0 or ud != 0.0: Shared.isLookingAwayEye = True else: Shared.isLookingAwayEye = False
def jointAttentionFace(okao): return 0 if okao.LR[0] != 99999: Shared.LR = queue(Shared.LR, okao.LR[0]) Shared.UD = queue(Shared.UD, okao.UD[0] - 8) else: return None tx = 30.0 ty = 20.0 lr = max(min(np.average(Shared.LR), tx), -tx) ud = max(min(np.average(Shared.UD), ty), -ty) list3 = [-0.5, -0.25, 0.0, 0.25, 0.5] list4 = [-0.25, 0.0] lr = getNearestValue(list3, -lr / tx) ud = getNearestValue(list4, -ud / ty) if lr != Shared.preLR: servo.move(3, lr) Shared.preLR = lr if ud != Shared.preUD: print ud servo.move(4, ud) Shared.preUD = ud if lr == -0.5 or lr == 0.5 or ud != -0.25: Shared.isLookingAwayFace = True else: Shared.isLookingAwayFace = False
def gazing(okao_x, okao_y): x, y = getPointFromOKAOXY(okao_x, okao_y) print(x, "--", y) #print "eye", x, y #print "face", addressX, addressY servo.move(1, x - addressX) servo.move(2, y - addressY)
def follow_eye(msg_okao): point = getPointFromOKAO(msg_okao) radx = point[0] * 0.5 rady = point[1] * 0.5 Shared.EYE_X = eye_x = radx - Shared.FACE_X Shared.EYE_Y = eye_y = rady + Shared.FACE_Y servo.move(1, eye_x) servo.move(2, -eye_y) return eye_x, eye_y
def gazingRelatively(x, y): print(x, "--", y) a = np.array([0, 0]) b = np.array([x, y]) u = b - a d = np.linalg.norm(u) print 2 * d z = max(min(2 * d, 1.5), 0.1) servo.move(1, z * 1.5) servo.move(2, z)
def addressing(okao_x, okao_y): global addressX global addressY x, y = getPointFromOKAOXY(okao_x, okao_y) addressY = -2.2 * y addressX = x servo.move(3, addressY) servo.move(4, addressX)
def __init__(self): threading.Thread.__init__(self, None) self.MOUTH_SERVO = 1 self.MOUTH_INIT_POS = 5 self.MOV_CONST = 6 self.MAX_ANGLE = 48 self.SERVO_DELAY = .3 self.stmt = ["hello"] self.speak = False self.stop = 0 servo.move(self.MOUTH_SERVO, self.MOUTH_INIT_POS) self.start()
def __init__(self): threading.Thread.__init__(self,None) self.MOUTH_SERVO=1 self.MOUTH_INIT_POS=5 self.MOV_CONST=6 self.MAX_ANGLE=48 self.SERVO_DELAY=.3 self.stmt=["hello"] self.speak=False self.stop=0 servo.move(self.MOUTH_SERVO,self.MOUTH_INIT_POS) self.start()
def action2(self, led): servo.move(1, 1, 3) servo.move(2, 1, 3) servo.move(3, -1, 3) servo.move(4, (random.random() - 0.5) * 2, 1) time.sleep(0.5) pass
def action0(self, led): servo.move(1, 0.8, 1) servo.move(2, 0.8, 1) servo.move(3, 0.7, 1) servo.move(4, 0.5, 1) led.setColor(200, 255, 50, 10000) time.sleep(0.5) pass
def camMove(self, irc, msg, servo, angle): """<value in degrees> Moves the camera accordingly """ motor = 1 #string.atoi(words[0]) angle = string.atoi(words[0]) if (0 <= angle <= 180): #print motor ser = serial.Serial('/dev/tty.usbserial-A9005aX1', 9600, timeout=1) # instanciate serial ser.open() # opens serial port ser.write(chr(255)) ser.write(chr(motor)) ser.write(chr(angle)) response = ser.readline() # reads the serial buffer #print response ser.close() else: #print "Servo angle must be an integer between 0 and 180.\n" camMove(servo.move(1, int(angle))) irc.reply("Moving Servo %i %i degrees" % (motor, angle)) camMove = wrap(camMove, [many('something')])
def main(): controller = Leap.Controller() while not (controller.is_connected): print "Waiting for controller..." time.sleep(1) print "Connected!" while controller.is_connected: # average = getAverage(controller) # print average angle = getAngle(controller) print "Angle of rotation in radians = %s" % angle time.sleep(0.5) servo.move(99, 0) time.sleep(2) servo.move(99, 180) time.sleep(2)
def action3(self, led): print("action1") servo.move(1, 1, 3) servo.move(2, 1, 3) servo.move(3, 0, 3) servo.move(4, 0.5, 3) led.setColor(255, 100, 50, 300) speed = 3 x, y = self.okaomanager.getMaxAttensionFaceXY("size") self.address(x, y, speed) led.setColor(255, 100, 50, 300) time.sleep(3) pass
def callback(self, data): self.skeleton_angles = np.array( [float(x) for x in data.data.split(',')]) print self.skeleton_angles servo.move(1, int(self.skeleton_angles[4] * (180 / np.pi))) servo.move(2, int(self.skeleton_angles[5] * (180 / np.pi))) servo.move(3, int(self.skeleton_angles[2] * (180 / np.pi)))
def follow_face_and_eye(): eye_x = Shared.EYE_X eye_y = Shared.EYE_Y face_x = Shared.FACE_X face_y = Shared.FACE_Y tx = 0.03 ty = 0.03 if eye_x < 0: if eye_x > -tx: tx = -eye_x if face_x < tx - 1: tx = 1 + face_x eye_x = eye_x + tx face_x = face_x - tx else: if eye_x < tx: tx = eye_x if face_x > 1 - tx: tx = 1 - face_x eye_x = eye_x - tx face_x = face_x + tx servo.move(1, eye_x) servo.move(3, face_x) Shared.EYE_X = eye_x Shared.FACE_X = face_x if eye_y < 0: if eye_y > -ty: ty = -eye_y if face_y > 1 - ty: ty = 1 - face_y eye_y = eye_y + ty face_y = face_y + ty else: if eye_y < ty: ty = eye_y if face_y < ty - 1: ty = 1 + face_y eye_y = eye_y - ty face_y = face_y - ty servo.move(2, eye_y) servo.move(4, face_y) Shared.EYE_Y = eye_y Shared.FACE_Y = face_y
def findingLoop(self): """ 動作: 誰もいない時に見渡す """ notice = '[Interaction]: findingLoop start!\n' print(termcolor.colored(notice, 'yellow')) while True: servo.move(1, random.random(), 1) servo.move(2, random.random(), 1) x, y = self.okaomanager.getMaxAttensionFaceXY("size") if x == 0 and y == 0: servo.move(3, random.random(), 0.5) servo.move(4, random.random(), 0.5) pass else: sleep = 0.8 self.address(x, y, sleep) time.sleep(0.1)
def run(self): while True: if self.speak==True: for i in self.stmt.split(): servo.move(self.MOUTH_SERVO,self.MOUTH_INIT_POS) s=len(i) angle=s*self.MOV_CONST if(angle>self.MAX_ANGLE): angle=self.MAX_ANGLE print i servo.move(self.MOUTH_SERVO,angle) time.sleep(self.SERVO_DELAY) servo.move(self.MOUTH_SERVO,self.MOUTH_INIT_POS) self.speak=False if self.stop==1: break
def run(self): while True: if self.speak == True: for i in self.stmt.split(): servo.move(self.MOUTH_SERVO, self.MOUTH_INIT_POS) s = len(i) angle = s * self.MOV_CONST if (angle > self.MAX_ANGLE): angle = self.MAX_ANGLE print i servo.move(self.MOUTH_SERVO, angle) time.sleep(self.SERVO_DELAY) servo.move(self.MOUTH_SERVO, self.MOUTH_INIT_POS) self.speak = False if self.stop == 1: break
def sort_trash(imgpath): camera = Camera() database = Database() classifier = Classifier(os.path.abspath('classifier/trained_graph.pb'), os.path.abspath('classifier/output_labels.txt')) statusThread = ui.start_status_shower_thread() while True: servo.move(NEUTRAL_POS) ui.set_status("ready") # wait for camera to detect motion, then sleep for a bit to # let the object settle down print "waiting for motion..." motiondetector.waitForMotionDetection(camera.getPiCamera()) time.sleep(0.5) # Lets object settle down, TODO maybe remove print "detected motion" ui.set_status("classifying") # take a photo and classify it camera.takePhoto(imgpath) labels = classifier.get_image_labels(imgpath) print labels selectedLabel = brain.getRecyclingLabel(labels) is_trash = selectedLabel == None database.write_result(imgpath, labels, is_trash, selectedLabel) print "Wrote result to database." if is_trash: print("It's trash.") ui.set_status("trash") servo.move(TRASH_POS) else: print("It's recyclable.") ui.set_status("recycling") servo.move(RECYCLE_POS)
def standup(self, rad=0.1, speed=1.0): servo.move(1, rad, speed) servo.move(2, rad, speed)
def mleft1(): time.sleep(.5) servo.move(8,60)
def finding(): print("finding!!") addressY = 0.0 addressX = 0.0 servo.move(3, addressY) servo.move(4, addressX)
def right(): time.sleep(.5) servo.move(3,10)
import servo, time servo.move(2, 90) servo.move(4,90) servo.move(3,0) time.sleep(1) servo.move(3,180) time.sleep(1) servo.move(3, 90)
def handleJoyEvent(e): # Identify joystick axes and assign events if e.type == pygame.JOYAXISMOTION: axis = "unknown" if (e.dict['axis'] == 0): axis = "X" if (e.dict['axis'] == 1): axis = "Y" if (e.dict['axis'] == 2): axis = "Throttle" if (e.dict['axis'] == 3): axis = "Z" # Convert joystick value to servo position for each axis if (axis != "unknown"): str = "Axis: %s; Value: %f" % (axis, e.dict['value']) # Uncomment to display axis values: #output(str, e.dict['joy']) # X Axis if (axis == "X"): pos = e.dict['value'] # convert joystick position to servo increment, 0-180 move = round(pos * 90, 0) serv = int(90 + move) # and send to Arduino over serial connection servo.move(1, serv) # Y Axis if (axis == "Y"): pos = e.dict['value'] move = round(pos * 90, 0) serv = int(90 + move) servo.move(2, serv) # Z Axis if (axis == "Z"): pos = e.dict['value'] move = round(pos * 90, 0) serv = int(90 + move) servo.move(3, serv) # Throttle if (axis == "Throttle"): pos = e.dict['value'] move = round(pos * 90, 0) serv = int(90 + move) servo.move(4, serv) # Assign actions for Button DOWN events elif e.type == pygame.JOYBUTTONDOWN: # Button 1 (trigger) if (e.dict['button'] == 0): print "Trigger Down" # Set pin 13 LED to HIGH for digital on/off demo servo.move(99, 180) # Button 2 if (e.dict['button'] == 1): print "Button 2 Down" # Button 3 if (e.dict['button'] == 2): print "Button 3 Down" # Button 4 if (e.dict['button'] == 3): print "Button 4 Down" # Button 5 if (e.dict['button'] == 4): print "Button 5 Down" # Button 6 if (e.dict['button'] == 5): print "Button 6 Down" quit() # Assign actions for Button UP events elif e.type == pygame.JOYBUTTONUP: # Button 1 (trigger) if (e.dict['button'] == 0): print "Trigger Up" # Set pin 13 LED to LOW for digital on/off demo servo.move(99, 0) # Button 2 if (e.dict['button'] == 1): print "Button 2 Up" # Button 3 if (e.dict['button'] == 2): print "Button 3 Up" # Button 4 if (e.dict['button'] == 3): print "Button 4 Up" # Button 5 if (e.dict['button'] == 4): print "Button 5 Up" # Button 6 if (e.dict['button'] == 5): print "Button 6 Up" # Assign actions for Coolie Hat Switch events elif e.type == pygame.JOYHATMOTION: if (e.dict['value'][0] == -1): print "Hat Left" servo.move(4, 0) if (e.dict['value'][0] == 1): print "Hat Right" servo.move(4, 180) if (e.dict['value'][1] == -1): print "Hat Down" if (e.dict['value'][1] == 1): print "Hat Up" if (e.dict['value'][0] == 0 and e.dict['value'][1] == 0): print "Hat Centered" servo.move(4, 90) else: pass
# http://www.fsf.org/licensing/ ''' Fun test of the servo.py module. ''' ################################################ import servo import time #number of servos to dance n = 4 #center all servos s = 0 while s <= n: servo.move(s, 90) s += 1 time.sleep(0.5) # perform 9 iterations, 10deg increments i = 9 while i >= 1: units = i + 1 # units to pulse servo deg = i * 10 # degrees moved L/R of center wait = i * 0.025 # wait time - large deflections need more # move left servoPosition = 90 - deg # command Arduino s=0
import servo #import servo library class StepperBot(callbacks.Plugin): """translates irc commands like right, left, up, down to artbus stepper comands, which are then sent via serial connection.""" def __init__(self, irc): self.__parent = super(StepperBot, self) self.__parent.__init__(irc) #self.TESTF = False #servo patterns for X and Y axis: west_pattern = servo.move(2,45) east_pattern = servo.move(2,90) deepwest_pattern = servo.move(2,180) deepeast_pattern = servo.move(2,0) artic_pattern = servo.move(2,45) antartic_pattern = servo.move(2,90) deepnorth_pattern = servo.move(2,180) deepsouth_pattern = servo.move(2,0) # map irc commands to patterns self.irc_patterns = {"west" : west_pattern, "east" : east_pattern, "deepwest" : deepwest_pattern, "deepeast" : deepeast_pattern, "artic" : artic_pattern, "antartic" : antartic_pattern, "deepnorth" : deepnorth_pattern, "deepsouth" : deepsouth_pattern} self.reset_pattern = (0,0) # serial config if self.TESTF == False: self.ser = serial.Serial() # instanciate serial class as ser self.ser.port = '/dev/tty.usbserial-A9005aX1' # connection device
def mright3(): time.sleep(.5) servo.move(8,140)
def set_servos(four_angles): servo.move(1, int(four_angles[0])) servo.move(2, int(four_angles[1])) servo.move(3, int(four_angles[2])) servo.move(4, int(four_angles[3]))
def servos1(): time.sleep(1) servo.move(1,40) time.sleep(1) servo.move(2,0) time.sleep(1) servo.move(3,0) time.sleep(1) servo.move(4,66) time.sleep(1) servo.move(5,180) time.sleep(1) servo.move(6,180) time.sleep(1) servo.move(7,50) time.sleep(1) servo.move(8,110) time.sleep(1) servo.move(10,0) time.sleep(1) servo.move(11,0) time.sleep(1) servo.move(12,0)
#Servo pins in use panServo = 7 tiltServo = 18 name = sys.argv[1] pans = int(sys.argv[2]) tilts = int(sys.argv[3]) #Servo configs minPan = 2.5 maxPan = 12.5 minTilt = 5 maxTilt = 12.5 panStep = round((maxPan-minPan)/pans,1) tiltStep = round((maxTilt-minTilt)/pans, 1) for p in functions.step_range(minPan, maxPan, panStep): servo.move(panServo,p) for t in functions.step_range(minTilt, maxTilt, tiltStep): servo.move(tiltServo,t) console.run(["./photo.sh", functions.name(name,p,t)])
# http://www.fsf.org/licensing/ ''' Fun test of the servo.py module. ''' ################################################ import servo import time import random t = 0.23 # sleep time # center all servos s = 1 while s <= 4: servo.move(s, 90) s += 1 time.sleep(1) # move random servos to random angles i = 0 while i < 80: serv = random.randrange(1, 5) ang = random.randrange(0, 181, 30) servo.move(serv, ang) print " servo.move(%d, %d)" % (serv, ang) time.sleep(t) i += 1 # wave goodbye s = 1
def left(): time.sleep(.5) servo.move(8,50)
def mright2(): time.sleep(.5) servo.move(8,130)
def gazingRelatively_arc(arcx, arcy): x = arc_to_rad(arcx) y = arc_to_rad(arcy) #print "rad: ", x, y servo.move(1, x) servo.move(2, y)
def mid(): time.sleep(.5) servo.move(3,5)
def nod(): servo.move(1, 0.8) servo.move(2, 0.8) servo.move(3, -1.5) servo.move(4, 0)
def left(): time.sleep(.5) servo.move(3,0)
def right(): time.sleep(.5) servo.move(5,180)
# I need help with a python script I am trying to adapt for a specific need import servo servo.move(int(argv[1]), int(argv[2]))
def left(): time.sleep(.5) servo.move(5,170)
def mid(): time.sleep(.5) servo.move(5,175)
def sit(rad=0.1): servo.move(1, rad) servo.move(2, rad)
def move(self, servo_id, rad, speed=1.0, update=True): if update: self.pos[servo_id] = rad servo.move(servo_id, rad, speed)
def servos(): servo.move(1,40) servo.move(2,0) servo.move(3,0) servo.move(4,66) servo.move(5,180) servo.move(6,180) servo.move(7,50) servo.move(8,110) servo.move(10,0) servo.move(11,0) servo.move(12,0)
def right(): time.sleep(.5) servo.move(8,150)
DisplayOfAxis.move(1, L_y_bar) DisplayOfAxis.move(3, L_z_bar) DisplayOfAxis.move(2, L_t_bar) #Coordinates of Right Joystick progress bars DisplayOfAxis.move(4, R_x_bar) DisplayOfAxis.move(5, R_y_bar) DisplayOfAxis.move(7, R_z_bar) DisplayOfAxis.move(6, R_t_bar) #Servo Movements servo.move("servo", "angle") #Left joystick buttons L_T_btn_pos = Ljoystick.get_button(0) #Left trigger if L_T_btn_pos == 1: servo.move(99,180) print "Left Trigger Pressed" else: servo.move(0,0) L_2_btn_pos = Ljoystick.get_button(1) #Left button 2 if L_2_btn_pos == 1: servo.move(0,180) print "Left Button 2 Pressed" else: servo.move(0,0) L_3_btn_pos = Ljoystick.get_button(2) #Left button 3 if L_3_btn_pos == 1: servo.move(0,90) print "Left Button 3 Pressed" else: servo.move(0,0)
################################################ import servo import time # set number of servos to march servos = 4 # marching orders t = 0.3 # give servos time to move deflection = 12 #degrees from center # fall in s = 1 while s <= servos: servo.move(s, 90) s += 1 time.sleep(0.01) # march servos, joining one at a time march=1 while march < servos+1: i = 8 while i >0: s = 1 while s <= march: servo.move(s, 90-deflection) s += 1 time.sleep(t) s = 1 while s <= march: