ArmPi機械手臂寫字機器人企劃書

Posted on Thu, May 19, 2022 python

三乙陳季磊、梁世叡、劉承洋、鄭又寧

三丙黃皓群、潘耑彣

使用機器人手臂書寫文字

wakuwaku 版本

程式碼:

#!/usr/bin/python3
# coding=utf8
import sys
sys.path.append('/home/pi/ArmPi/')
import time
import threading
from ArmIK.Transform import *
from ArmIK.ArmMoveIK import *
import HiwonderSDK.Board as Board

if sys.version_info.major == 2:
    print('Please run this program with python3!')
    sys.exit(0)

AK = ArmIK()

h = 2.5
x = -5
y = 20
#z = 5
pen_up = 0

data = [
    [(0,0,1),(1,0.5,0),(1,1,1),(1,1.5,0),(1,2,1)],
    [(0,1,1),(1,1,0),(1,0,0),(1,0,1),(1,1,1),(1,1.5,0)],
    [(0,0,2),(1,0,0),(0,1,1),(1,0,0.5),(1,1,0)],
    [(0,0,1),(1,0,0),(1,1,0),(1,1,1),(1,1.5,0)]
]





def initMove():
    Board.setBusServoPulse(1, 500 - 50, 300)
    Board.setBusServoPulse(2, 500, 500)
    AK.setPitchRangeMoving((0, 10, 10), -30, -30, -90, 1500)
    
def move(a,b):
    global pen_up,x,y
    if(pen_up == 0):
        AK.setPitchRangeMoving((x, y, h+4), -30, -30, -90, 500)
        time.sleep(0.5)
        pen_up = 1
    x=a
    y=b
    data=AK.setPitchRangeMoving((x, y, h+4), -30, -30, -90)
    print(data[2])
    time.sleep(data[2]/1000*1.7)

def draw(a,b):
    global pen_up,x,y
    if(pen_up==1):
        AK.setPitchRangeMoving((x, y, h), -30, -30, -90, 500)
        time.sleep(0.5)
        pen_up=0
    x=a
    y=b
    data=AK.setPitchRangeMoving((x, y, h), -30, -30, -90)
    time.sleep(data[2]/1000*1.7)
    
    
initMove()
time.sleep(2)
AK.setPitchRangeMoving((x, y, 5), -30, -30, -90)
a = input("pen")
setBusServoPulse(1,700,100)
b = input("go")
time.sleep(0.1)

a = "0123"
cursor_x = -5
cursor_y = 28
dis = 2 #字母間距為1.5倍的字母寬
font_size = 2 #1為原始大小
for char in a:
    num = ord(char) - ord('0')
    instruct = data[num]
    print(instruct)
    
    for step in instruct:
        if(step[0]==0):
            print("move")
            print(str(step[1]*font_size+cursor_x)+','+str(step[2]*font_size+cursor_y))
            move(step[1]*font_size+cursor_x,step[2]*font_size+cursor_y)
        else:
            print("draw")
            print(str(step[1]*font_size+cursor_x)+','+str(step[2]*font_size+cursor_y))
            draw(step[1]*font_size+cursor_x,step[2]*font_size+cursor_y)
    
    cursor_x += dis*font_size

move(-5,24)

for char in a:
    num = ord(char) - ord('0')
    instruct = data[num]
    print(instruct)
    
    for step in instruct:
        if(step[0]==0):
            print("move")
            print(str(step[1]*font_size+cursor_x)+','+str(step[2]*font_size+cursor_y))
            move(step[1]*font_size+cursor_x,step[2]*font_size+cursor_y)
        else:
            print("draw")
            print(str(step[1]*font_size+cursor_x)+','+str(step[2]*font_size+cursor_y))
            draw(step[1]*font_size+cursor_x,step[2]*font_size+cursor_y)
    
    cursor_x += dis*font_size

move(0,10)

範例影片:

結果:

數字版本

程式碼:

#!/usr/bin/python3
# coding=utf8
import sys
sys.path.append('/home/pi/ArmPi/')
import time
import threading
from ArmIK.Transform import *
from ArmIK.ArmMoveIK import *
import HiwonderSDK.Board as Board

if sys.version_info.major == 2:
    print('Please run this program with python3!')
    sys.exit(0)

AK = ArmIK()

h = 2.5
x = -5
y = 20
#z = 5
pen_up = 0

data = [
    [(0,0,2),(1,1,2),(1,1,0),(1,0,0),(1,0,2)],
    [(0,1,2),(1,1,0)],
    [(0,0,2),(1,1,2),(1,1,1),(1,0,1),(1,0,0),(1,1,0)],
    [(0,0,2),(1,1,2),(1,1,0),(1,0,0),(0,0,1),(1,1,1)],
    [(0,0,2),(1,0,1),(1,1,1),(1,1,2),(1,1,0)],
    [(0,1,2),(1,0,2),(1,0,1),(1,1,1),(1,1,0),(1,0,0)],
    [(0,1,2),(1,0,2),(1,0,0),(1,1,0),(1,1,1),(1,0,1)],
    [(0,0,1),(1,0,2),(1,1,2),(1,1,0)],
    [(0,0,2),(1,1,2),(1,1,0),(1,0,0),(1,0,2),(0,0,1),(1,1,1)],
    [(0,1,1),(1,0,1),(1,0,2),(1,1,2),(1,1,0)]
    ]



def initMove():
    Board.setBusServoPulse(1, 500 - 50, 300)
    Board.setBusServoPulse(2, 500, 500)
    AK.setPitchRangeMoving((0, 10, 10), -30, -30, -90, 1500)
    
def move(a,b):
    global pen_up,x,y
    if(pen_up == 0):
        AK.setPitchRangeMoving((x, y, h+4), -30, -30, -90, 500)
        time.sleep(0.5)
        pen_up = 1
    x=a
    y=b
    data=AK.setPitchRangeMoving((x, y, h+4), -30, -30, -90)
    print(data[2])
    time.sleep(data[2]/1000*1.7)

def draw(a,b):
    global pen_up,x,y
    if(pen_up==1):
        AK.setPitchRangeMoving((x, y, h), -30, -30, -90, 500)
        time.sleep(0.5)
        pen_up=0
    x=a
    y=b
    data=AK.setPitchRangeMoving((x, y, h), -30, -30, -90)
    time.sleep(data[2]/1000*1.7)
    
    
initMove()
time.sleep(2)
AK.setPitchRangeMoving((x, y, 5), -30, -30, -90)
a = input("pen")
setBusServoPulse(1,700,100)
#b = input("go")
#time.sleep(0.1)

a = input("please type some number: ")
cursor_x = 0
cursor_y = 28
dis = 1.5 #字母間距為1.5倍的字母寬
font_size = 1 #1為原始大小
for char in a:
    num = ord(char) - ord('0')
    instruct = data[num]
    print(instruct)
    
    for step in instruct:
        if(step[0]==0):
            print("move")
            print(str(step[1]*font_size+cursor_x)+','+str(step[2]*font_size+cursor_y))
            move(step[1]*font_size+cursor_x,step[2]*font_size+cursor_y)
        else:
            print("draw")
            print(str(step[1]*font_size+cursor_x)+','+str(step[2]*font_size+cursor_y))
            draw(step[1]*font_size+cursor_x,step[2]*font_size+cursor_y)
    
    cursor_x += dis*font_size
    
move(0,10)

範例影片: