除了左右摆动以外,机械臂还需要跟随头部上下摆动。
一、初始化机械臂
定义了四个点位,到时候可以随着脸部的检测到的区域跟随变化
from pymycobot import MyCobot280
mc = MyCobot280('COM3')
# 初始化机械臂
motor1=90
motor2=0
motor3=0
motor4=0
motor5=90
motor6=55
mc.send_angles([motor1, motor2, motor3, motor4, motor5, motor6], 100)
downAngles=[90,53,-121,67,90,54.4]
upAngles=[motor1, motor2, motor3, motor4, motor5, motor6]
rightAngles=[motor1, 90, -90, motor4, motor5, motor6]
leftAngles=[motor1, -90, 90, motor4, motor5, motor6]
# 从上下左右四个点各走一遍
for angles in [downAngles, upAngles, rightAngles, leftAngles]:
mc.send_angles(angles, 100)
time.sleep(1) # 等待1秒以便观察动作
二、判断脸部检测的区域
修改条件以确保在判断上下和左右偏移时,y的偏移比x大时才认为是上下移动,反之则为左右移动。以下是更新后的代码片段:
# 计算偏移量
y_offset = abs(center_y - frame_center_y