def moveRandom(): SERVO_BOTTOM = 0 SERVO_LEFT = 1 SERVO_RIGHT = 2 SERVO_HAND = 3 position_arr = [] pixel_position = [] angles_arr = [] arm = SwiftAPI() sleep(2.0) rand = np.random.rand(3) angles = rand * 180 print("angles are", angles) arm.flush_cmd() arm.reset() arm.set_servo_angle_speed(SERVO_RIGHT, angles[0], wait=True, timeout=100, speed=5000) arm.set_servo_angle_speed(SERVO_BOTTOM, angles[1], wait=True, timeout=100, speed=5000) arm.set_servo_angle_speed(SERVO_LEFT, angles[2], wait=True, timeout=100, speed=5000) a = arm.get_is_moving() print("the status is", a, "\n") pos = arm.get_position( ) # float array of the format [x, y, z] of the robots current location cap = cv2.VideoCapture(0) cap1 = cv2.VideoCapture(0) red = detect_w_avg(cap, 600, 166, 84, 80, 286, 255, 255, "red") print("red", red) blue = detect_w_avg(cap1, 600, 97, 100, 117, 117, 255, 255, "blue") print("blue", blue) pixel_position.append(red) pixel_position.append(blue) cv2.destroyAllWindows() cap.release() cap1.release() # pixelpostion= get the pixel position here print("the position is ", pos, "\n") position_arr.append(pos) angles = angles.tolist() angles_arr.append(angles) # [right bottom left] sleep(2.0) return angles, pixel_position
def moveRandom(): SERVO_BOTTOM = 0 SERVO_LEFT = 1 SERVO_RIGHT = 2 SERVO_HAND = 3 position_arr=[] angles_arr=[] arm = SwiftAPI() sleep(2.0) rand = np.random.rand(3) angles = rand * 180 print("angles are", angles) arm.flush_cmd() arm.reset() arm.set_servo_angle_speed(SERVO_RIGHT, angles[0], wait=True, timeout=100, speed=5000) arm.set_servo_angle_speed(SERVO_BOTTOM, angles[1], wait=True, timeout=100, speed=5000) arm.set_servo_angle_speed(SERVO_LEFT, angles[2], wait=True, timeout=100, speed=5000) a = arm.get_is_moving() print("the status is", a, "\n") pos = arm.get_position() # float array of the format [x, y, z] of the robots current location # pixelpostion= get the pixel position here print("the position is ", pos, "\n") position_arr.append(pos) angles = angles.tolist() angles_arr.append(angles) # [right bottom left] sleep(2.0)
print('setup swift ...') # swift = SwiftAPI(dev_port='/dev/cu.usbmodem1421') swift = SwiftAPI() print('sleep 2 sec ...') sleep(2) print('device info: ') print(swift.get_device_info()) # swift.set_position(x=120,z=80,y=30) # print('\nset X350 Y0 Z50 F500 ...') # swift.set_position(350, 0, 50, speed=1500) swift.set_position(x=300, wait=True) sleep(3) swift.set_position(x=200,y=100,z=50, wait=True) print('finished') try: while True: sleep(1) except KeyboardInterrupt as e: print('KeyboardInterrupt',e) finally: swift.reset()
swift = SwiftAPI() # default by filters: {'hwid': 'USB VID:PID=2341:0042'} print('sleep 2 sec ...') sleep(2) print('device info: ') print(swift.get_device_info()) # print('开始测试') swift.set_buzzer() swift.set_buzzer() print('解锁电机') swift.set_servo_detach() sleep(10) swift.set_buzzer()#TODO 哔哔2声 swift.set_buzzer() sleep(2) print('解锁电机') swift.set_servo_attach() sleep(3) print('重置机械臂') swift.set_buzzer() swift.reset(x=103, y=0, z=42, speed=800)
wait: if True, will block the thread, until get response or timeout Returns: succeed True or failed False ''' for _ in range(100): rand = np.random.rand(3) angles = rand * 180 print("angles are", angles) arm.flush_cmd() arm.reset() arm.set_servo_angle_speed(SERVO_RIGHT, angles[0], wait=True, timeout=100, speed=5000) arm.set_servo_angle_speed(SERVO_BOTTOM, angles[1], wait=True, timeout=100, speed=5000) arm.set_servo_angle_speed(SERVO_LEFT, angles[2],
sleep(2) print('\nset X350 Y0 Z50 F500 ...') swift.set_position(x=280, y=0, z=140, wait=True) swift.set_buzzer() sleep(2) # for i in range(1):#3 for words in say_list: #3 swift.set_position(x=280, y=0, z=109, wait=True) swift.set_buzzer() # subprocess.call(['say', 'good morning,how are you']) # subprocess.call(['say', 'sing happy birthday']) subprocess.call(['say', words]) # sleep(1) swift.set_position(z=140, wait=True) sleep(15) print('done ...') swift.reset(z=200) try: while True: sleep(1) except KeyboardInterrupt as e: print('KeyboardInterrupt', e) finally: swift.reset() # swift.set_position(x=90, y=0, z=70, speed=1800, wait=True)