新版【openmv】+蓝牙控制机械臂抓取色块放在对应位置 附代码

硬件

1.4自由度机械臂(3自由度也可以,少量改动即可)
2.pca9685舵机扩展(不使用的的话,只需改动所有舵机控制语句)
3.openmv
4.蓝牙hc-06

上一篇【详细讲解 附全部代码】【openmv控制三自由度机械臂抓取物品】硬件+软件

本篇在之前的基础上增加了:

1.蓝牙控制: 开始 结束 选择颜色 自动模式
2.放置位置可以不固定
3.一些优化
在这里插入图片描述
在这里插入图片描述

使用方式:

1.上电
2.运行代码
3.连接蓝牙
4.选择颜色-开始或自动模式
5.选择结束,摆放色块位置,重复步骤4

要更改的内容:

1.颜色阈值


red_threshold  = (18, 60, 29, 127, -3, 124)#颜色阈值2   0010(14, 100, 8, 127, 9, 116)(14, 64, 17, 127, -3, 124)
blue_threshold  = (10, 81, -128, 127, -128, -15)#颜色阈值4   0100
yellow_threshold  = (53, 100, -12, 97, 19, 127)#颜色阈值1    0001
green_threshold  = (25, 100, -128, -29, 6, 127)#颜色阈值

2.你的舵机限制角度和初始值

#以下是我的数据
#0号:0-150度
#1号:0-50度
#2号:0-130度,
#3号:0-90度,0度夹住
s0_now=95
s1_now=35
s2_now=75
s3_now=45

    if s0_move > 140:#这4句限制舵机运动角度,防止卡死
        s0_move = 140
    if s0_move < 10:
        s0_move = 10

3.接线

#以下是我的数据
#接线:一共4跟,正负,scl-p4,sda-p5

#舵机从下往上分别为0,1,2,3号,接在0 3 4 7

4.爪子位置补偿

        cx=int(clap.x()+clap.w()/2+(clap.x()+clap.w()/2-180)*0.15)+5#改0.15和5这两个数字

完整代码




#接线:一共4跟,正负,scl-p4,sda-p5

#舵机从下往上分别为0,1,2,3号,接在0 3 4 7
#0号:0-150度
#1号:0-50度
#2号:0-130度,
#3号:0-90度,0度夹住


import sensor, image, time,pyb
from pyb import UART
from pyb import LED

from servo import Servos
from machine import I2C, Pin

uart = UART(1,38400)
uart.init(9600,bits=8,parity=None,stop=1,timeout_char=1000)#串口初始化

i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
servo = Servos(i2c, address=0x40, freq=50, min_us=650, max_us=2800, degrees=180)

red_threshold  = (18, 60, 29, 127, -3, 124)#颜色阈值2   0010(14, 100, 8, 127, 9, 116)(14, 64, 17, 127, -3, 124)
blue_threshold  = (10, 81, -128, 127, -128, -15)#颜色阈值4   0100
yellow_threshold  = (53, 100, -12, 97, 19, 127)#颜色阈值1    0001
green_threshold  = (25, 100, -128, -29, 6, 127)#颜色阈值

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time
  • 15
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
很抱歉,我是AI语言模型,无法提供您所需的代码。但您可以参考以下步骤: 1. 通过OpenMV摄像头检测块并计算出其中心坐标。 2. 将中心坐标通过串口发送给Arduino。 3. 在Arduino中编写控制六轴机械代码,并使用接收到的中心坐标控制机械抓取动作。 4. 根据具体的机械型号和控制方式,编写相应的代码,实现抓取动作。 以下是可能用到的函数: 在OpenMV中: 1. find_blobs():用于检测块并计算出其中心坐标。 2. uart.write():用于将数据通过串口发送给Arduino。 在Arduino中: 1. Serial.available():用于检测串口是否有数据可读。 2. Serial.read():用于读取串口数据。 3. Servo.write():用于控制舵机角度。 4. digitalWrite():用于控制电机的正反转。 下面是可能用到的框架代码: 在OpenMV中: ```python import sensor, image, time, pyb from pyb import UART uart = UART(3, 9600) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) while(True): img = sensor.snapshot() blobs = img.find_blobs([thresholds], pixels_threshold=200, area_threshold=200) if blobs: for b in blobs: x = b.cx() y = b.cy() uart.write("{},{}".format(x,y)) print("x:{}, y:{}".format(x, y)) ``` 在Arduino中: ```c++ #include <Servo.h> #define SERVO_PIN 9 #define MOTOR_PIN 10 Servo myservo; void setup() { Serial.begin(9600); myservo.attach(SERVO_PIN); pinMode(MOTOR_PIN, OUTPUT); } void loop() { if (Serial.available() > 0) { String data = Serial.readStringUntil(','); int x = data.toInt(); data = Serial.readStringUntil('\n'); int y = data.toInt(); // 根据中心坐标控制机械的动作 // ... } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值