机械臂控制2

1.电机步进模式

1.1啥意思

我们知道1个脉冲对应1.8°,但是这个1.8°对于精细化的操作来说度数太大

所有我们要让1个脉冲对应电机旋转角度更小才行

这就是电机的步进模式

1.2有几种?

一共5中

-全步(就是默认的1.8°)

-半步(0.9°)

-1/4步(0.45°)

-1/8步(0.215°)

-1/16步(0.1125°)

1.3怎样设置?

步进模式的设置是通过插针来进行修改的

通过插针安装跳线帽,我们可以改变电机的步进模式

2. 调速

from machine import Pin
import time
import machine
import utime
import socket
import network

# 使能
en_pin = Pin(12, Pin.OUT)
en_pin.value(1)  # 关闭

# 第1轴
x_dir = 1
x_pin = Pin(16, Pin.OUT)
x_pin.value(x_dir)
# Pin对象
axis_x_pin = Pin(26, Pin.OUT)

# 第2轴
y_dir = 1
y_pin = Pin(27, Pin.OUT)
y_pin.value(y_dir)
# Pin对象
axis_y_pin = Pin(25, Pin.OUT)

# 第3轴
z_dir = 1
z_pin = Pin(14, Pin.OUT)
z_pin.value(z_dir)
# Pin对象
axis_z_pin = Pin(17, Pin.OUT)

# 定义变量
time_value = 1000
lock_status = False  # False未锁定, True锁定
relative_position = [0, 0, 0]  # 第1/2/3轴相对于 锁定的位置转动的脉冲次数


def do_connect():
    """链接WIFI"""
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('dongfeiqiu', 'wangmingdong1225')
        i = 1
        while not wlan.isconnected():
            print("正在链接...{}".format(i))
            i += 1
            time.sleep(1)
    print('network config:', wlan.ifconfig())


def main():
    global lock_status, time_value
    
    # 链接wifi
    do_connect()
    # 创建UDP
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # 绑定本地信息
    udp_socket.bind(("0.0.0.0", 7788))

    # 接收网络数据
    while True:
        recv_data, sender_info = udp_socket.recvfrom(1024)
        print("{}发送{}".format(sender_info, recv_data))
        recv_data_str = recv_data.decode("utf-8")
        cmd = int(recv_data_str[0])
        data = int(recv_data_str[2:])
        if cmd == 0:
            data = int(recv_data_str[2:])
            if data == 0:
                print("锁定...")
                relative_position[0] = 0
                relative_position[1] = 0
                relative_position[2] = 0
                lock_status = True
                en_pin.value(0)  # 打开使能
            else:
                print("解锁...")
                lock_status = False
                en_pin.value(1)  # 关闭使能
        elif lock_status and cmd == 1:
            print("第1轴...", data)
            if data == relative_position[0]:
                continue
            # 方向
            x_dir = 0 if data > relative_position[0] else 1
            x_pin.value(x_dir)
            pwm_times = abs(data - relative_position[0])
            relative_position[0] = data
            for i in range(pwm_times):
                # 模拟发送pwm
                axis_x_pin.value(1)
                utime.sleep_us(time_value)
                axis_x_pin.value(0)
                utime.sleep_us(time_value)
        elif lock_status and cmd == 2:
            print("第2轴...", data)
            if data == relative_position[1]:
                continue
            # 方向
            y_dir = 0 if data > relative_position[1] else 1
            y_pin.value(y_dir)
            pwm_times = abs(data - relative_position[1])
            relative_position[1] = data
            for i in range(pwm_times):
                # 模拟发送pwm
                axis_y_pin.value(1)
                utime.sleep_us(time_value)
                axis_y_pin.value(0)
                utime.sleep_us(time_value)
        elif lock_status and cmd == 3:
            print("第3轴...", data)
            if data == relative_position[2]:
                continue
            # 方向
            z_dir = 0 if data > relative_position[2] else 1
            z_pin.value(z_dir)
            pwm_times = abs(data - relative_position[2])
            relative_position[2] = data
            for i in range(pwm_times):
                # 模拟发送pwm
                axis_z_pin.value(1)
                utime.sleep_us(time_value)
                axis_z_pin.value(0)
                utime.sleep_us(time_value)
        elif lock_status and cmd == 4:  # 调速
            if data > 0:
                print("调速...", data)
                time_value = data
        else:
            if lock_status is False:
                print("未锁定...运动命令无效")
            else:
                print("未知数据类型...")


if __name__ == "__main__":
    main()

3.逆向运动学-水平计算

 

大臂角 

math.degrees(math.acos(125**2/(2*125*120)))  # 结果为58.61183353565145

小臂角

math.degrees(math.acos((120**2+120**2-125**2)/(2*120*120)))  # 结果为62.776332928697094

4.逆向运动学2

import math

math.degrees(math.atan(20/125))  # 9.090276920822323

 5.逆运动学3-底座比末端高

import math

math.degrees(math.atan(125/20))  # 80.90972307917767

 

length = (125**2 + 20**2)**0.5

math.degrees(math.acos((120**2 + length**2 - 120**2)/(2*120*length)))  # 58.16614622904558

 综上大摆臂的角度:80.90°+58.16°=139.06°

利用余弦定理,计算小摆臂夹角

math.degrees(math.acos((120**2 + 120**2 - length**2)/(2*120*120)))  # 63.66770754190885

6.夹手

from machine import Pin
import time


a = Pin(23, Pin.OUT)
b = Pin(5, Pin.OUT)
c = Pin(13, Pin.OUT)
d = Pin(19, Pin.OUT)

a.value(0)
b.value(0)
c.value(0)
d.value(0)

delay_time_ms = 2


def open_lock():
    """用来实现朝1个方向转动"""
    for i in range(120):
        a.value(1)
        b.value(0)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(1)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(0)
        c.value(1)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(0)
        c.value(0)
        d.value(1)
        time.sleep_ms(delay_time_ms)


def close_lock():
    """用来实现朝另1个方向转动"""
    for i in range(120):
        a.value(0)
        b.value(0)
        c.value(0)
        d.value(1)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(0)
        c.value(1)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(1)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(1)
        b.value(0)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)


# 夹手来回10次
for i in range(10):
    open_lock()
    time.sleep(0.5)
    close_lock()
    time.sleep(0.5)

7.机械臂控制夹手

from machine import Pin
import time
import machine
import utime
import socket
import network

# 使能
en_pin = Pin(12, Pin.OUT)
en_pin.value(1)  # 关闭

# 第1轴
x_dir = 1
x_pin = Pin(16, Pin.OUT)
x_pin.value(x_dir)
# Pin对象
axis_x_pin = Pin(26, Pin.OUT)

# 第2轴
y_dir = 1
y_pin = Pin(27, Pin.OUT)
y_pin.value(y_dir)
# Pin对象
axis_y_pin = Pin(25, Pin.OUT)

# 第3轴
z_dir = 1
z_pin = Pin(14, Pin.OUT)
z_pin.value(z_dir)
# Pin对象
axis_z_pin = Pin(17, Pin.OUT)

# 定义变量
time_value = 1000
lock_status = False  # False未锁定, True锁定
relative_position = [0, 0, 0]  # 第1/2/3轴相对于 锁定的位置转动的脉冲次数

delay_time_ms = 2

# ----------------------------- 添加 start ----------------------------
# 夹手相关
a = Pin(23, Pin.OUT)
b = Pin(5, Pin.OUT)
c = Pin(13, Pin.OUT)
d = Pin(19, Pin.OUT)

a.value(0)
b.value(0)
c.value(0)
d.value(0)
# ----------------------------- 添加 stop -----------------------------


def do_connect():
    """链接WIFI"""
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('dongfeiqiu', 'wangmingdong1225')
        i = 1
        while not wlan.isconnected():
            print("正在链接...{}".format(i))
            i += 1
            time.sleep(1)
    print('network config:', wlan.ifconfig())
    

# ----------------------------- 添加 start ----------------------------
def open_lock():
    """用来实现朝1个方向转动"""
    for i in range(120):
        a.value(1)
        b.value(0)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(1)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(0)
        c.value(1)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(0)
        c.value(0)
        d.value(1)
        time.sleep_ms(delay_time_ms)


def close_lock():
    """用来实现朝另1个方向转动"""
    for i in range(120):
        a.value(0)
        b.value(0)
        c.value(0)
        d.value(1)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(0)
        c.value(1)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(0)
        b.value(1)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
        
        a.value(1)
        b.value(0)
        c.value(0)
        d.value(0)
        time.sleep_ms(delay_time_ms)
# ----------------------------- 添加 stop -----------------------------


def main():
    global lock_status, time_value
    
    # 链接wifi
    do_connect()
    # 创建UDP
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # 绑定本地信息
    udp_socket.bind(("0.0.0.0", 7788))

    # 接收网络数据
    while True:
        recv_data, sender_info = udp_socket.recvfrom(1024)
        print("{}发送{}".format(sender_info, recv_data))
        recv_data_str = recv_data.decode("utf-8")
        cmd = int(recv_data_str[0])
        data = int(recv_data_str[2:])
        if cmd == 0:
            data = int(recv_data_str[2:])
            if data == 0:
                print("锁定...")
                relative_position[0] = 0
                relative_position[1] = 0
                relative_position[2] = 0
                lock_status = True
                en_pin.value(0)  # 打开使能
            else:
                print("解锁...")
                lock_status = False
                en_pin.value(1)  # 关闭使能
        elif lock_status and cmd == 1:
            print("第1轴...", data)
            if data == relative_position[0]:
                continue
            # 方向
            x_dir = 0 if data > relative_position[0] else 1
            x_pin.value(x_dir)
            pwm_times = abs(data - relative_position[0])
            relative_position[0] = data
            for i in range(pwm_times):
                # 模拟发送pwm
                axis_x_pin.value(1)
                utime.sleep_us(time_value)
                axis_x_pin.value(0)
                utime.sleep_us(time_value)
        elif lock_status and cmd == 2:
            print("第2轴...", data)
            if data == relative_position[1]:
                continue
            # 方向
            y_dir = 0 if data > relative_position[1] else 1
            y_pin.value(y_dir)
            pwm_times = abs(data - relative_position[1])
            relative_position[1] = data
            for i in range(pwm_times):
                # 模拟发送pwm
                axis_y_pin.value(1)
                utime.sleep_us(time_value)
                axis_y_pin.value(0)
                utime.sleep_us(time_value)
        elif lock_status and cmd == 3:
            print("第3轴...", data)
            if data == relative_position[2]:
                continue
            # 方向
            z_dir = 0 if data > relative_position[2] else 1
            z_pin.value(z_dir)
            pwm_times = abs(data - relative_position[2])
            relative_position[2] = data
            for i in range(pwm_times):
                # 模拟发送pwm
                axis_z_pin.value(1)
                utime.sleep_us(time_value)
                axis_z_pin.value(0)
                utime.sleep_us(time_value)
        elif lock_status and cmd == 4:  # 调速
            if data > 0:
                print("调速...", data)
                time_value = data
        # ----------------------------- 添加 start ----------------------------
        elif lock_status and cmd == 5:  # 夹手
            if data > 0:
                if data == 1:
                    close_lock()
                    print("夹手...已加紧")
                elif data == 2:
                    open_lock()
                    print("夹手...已松开")
                else:
                    print("夹手命令,data=%d,无效..." % data)
        # ----------------------------- 添加 stop -----------------------------
        else:
            if lock_status is False:
                print("未锁定...运动命令无效")
            else:
                print("未知数据类型...")


if __name__ == "__main__":
    main()

8.姿态跟随

8.1说明

上位机的程序模拟的机械臂的姿态,与真正的机械臂一样

  • 17
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值