OpenCV Bot实际上通过图像处理检测或跟踪任何实时对象。此应用程序可以使用其颜色检测任何对象,并在手机屏幕中创建X,Y位置和区域area,使用此应用程序,数据通过蓝牙发送到微控制器。它已经过蓝牙模块测试,适用于各种设备。我们通过手机下载此APP来实现颜色的追踪,通过蓝牙发送数据到Arduino UNO上进行数据分析后执行运动命令。
在手机上安装APP软件(见底部,软件已被下架)安装后图如下:
连接蓝牙(先在设置里匹配好,pin1234,再到APP里面选择连接)
[attach]2302[/attach]
点击需要识别的物体(颜色不要与周围有冲突,影响识别效果)
[attach]2311[/attach]
识别中我们可以看到在图像的上部显示出X,Y,Area值,X表示左右,Y表示上下,Area表示前后,通过得到的数值我们可以很轻松的判断球向哪里运动,通过蓝牙传输数据到Arduino UNO主板中,我们就可以控制机器人怎么运动。(由于抓捕物体的大小不同,参数值也会不同,所以需要根据实际使用的物体大小进行选取判断值)
底盘车架选用灵活的全向轮,行走灵活,无死角的旋转和行走,方便实行我们跟随目标;当然,Mecanum wheels也是不错的选择。我们安装好底盘,每个轮子使用单独的驱动(灵活运动的关键),具体配件如下:
1:4WD全向轮底盘,3WD全向轮底盘,麦克纳姆轮底盘都可以;
2:安卓手机,支持安卓5以上;
3:Arduino UNO R3主板,兼容型号都可以,或者其余单片机等等;
4:2块PM-R3,叠加结构,引脚相同所以需要外部加工修改引脚的位置;
5:18650电池,3S;
6:固定手机的支架;
7:最好准备防滑毯,效果会很好;
如下是安装好的样子:
[attach]2309[/attach]
示例代码如下,检测的X,Y,Area值在串口里面查看,代码驱动部分需要自行修改以适配不同的电机驱动
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
//Initialize global variables
String bluetoothRead;
unsigned short x, y, area;
unsigned short strLength;
//Configure motor pin
int pwmMotorA = 11;
int pwmMotorB = 10;
int leftMotorA = 9;
int leftMotorB = 8;
int rightMotorA = 7;
int rightMotorB = 6;
//Set motor speed
int motorSpeed = 140;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("OpenCV Bot");
// Set the baud rate of (HC-05) Bluetooth module
mySerial.begin(38400);
//Setup pins
pinMode(pwmMotorA, OUTPUT);
pinMode(pwmMotorB, OUTPUT);
pinMode(leftMotorA, OUTPUT);
pinMode(leftMotorB, OUTPUT);
pinMode(rightMotorA, OUTPUT);
pinMode(rightMotorB, OUTPUT);
analogWrite(pwmMotorA, motorSpeed);
analogWrite(pwmMotorB, motorSpeed);
}
void loop() {
register byte ndx = 0;
char commandbuffer[50];
if (mySerial.available() > 0) {
delay(10);
while ( mySerial.available() && ndx < 50) {
commandbuffer[ndx++] = mySerial.read();
}
commandbuffer[ndx] = '\0';
bluetoothRead = (char*)commandbuffer;
strLength = bluetoothRead.length();
//Serial.println(bluetoothRead);
if (bluetoothRead.substring(0, 1).equals("X")) {
uint8_t pos, i = 1;
while (bluetoothRead.substring(i, i + 1) != ("Y")) {
i++;
}
x = bluetoothRead.substring(1, i).toInt();
//Serial.print("X: ");
//Serial.println(x);
pos = i + 1;
while (bluetoothRead.substring(i, i + 1) != ("A")) {
i++;
}
y = bluetoothRead.substring(pos, i).toInt();
//Serial.print("Y: ");
//Serial.println(y);
area = bluetoothRead.substring(i + 1, strLength).toInt();
//Serial.print("Area: ");
//Serial.println(area);
if (x < 750) {
Serial.println("Left");
Left();
}
if (x > 1100) {
Serial.println("Right");
Right();
}
if (x >= 750 && x <= 1100) {
if (area < 150) {
Serial.println("Forward");
Forward();
}
if (area >= 150 && area <= 250) {
Serial.println("Stop");
Stop();
}
if (area > 250) {
Serial.println("Back");
Back();
}
}
}
}
// all data has been sent, and the buffer is empty.
Serial.flush();
}
void Left() {
digitalWrite(leftMotorA, LOW);
digitalWrite(leftMotorB, HIGH);
digitalWrite(rightMotorA, HIGH);
digitalWrite(rightMotorB, LOW);
}
void Right() {
digitalWrite(leftMotorA, HIGH);
digitalWrite(leftMotorB, LOW);
digitalWrite(rightMotorA, LOW);
digitalWrite(rightMotorB, HIGH);
}
void Forward() {
digitalWrite(leftMotorA, HIGH);
digitalWrite(leftMotorB, LOW);
digitalWrite(rightMotorA, HIGH);
digitalWrite(rightMotorB, LOW);
}
void Back() {
digitalWrite(leftMotorA, LOW);
digitalWrite(leftMotorB, HIGH);
digitalWrite(rightMotorA, LOW);
digitalWrite(rightMotorB, HIGH);
}
void Stop() {
digitalWrite(leftMotorA, LOW);
digitalWrite(leftMotorB, LOW);
digitalWrite(rightMotorA, LOW);
digitalWrite(rightMotorB, LOW);
}
串口打印值如下:
[attach]2312[/attach]
anshilike 发表于 2020-5-11 01:57
opencv manager package was not found 科学上网也破不了 怎么办 谷歌市场也搜不到这manager package
anshilike 发表于 2020-5-11 01:57
opencv manager package was not found 科学上网也破不了 怎么办 谷歌市场也搜不到这manager package
laoguang 发表于 2021-6-27 21:12
APK下载有点问题 大小都是0KB
laoguang 发表于 2021-6-27 21:12
APK下载有点问题 大小都是0KB
欢迎光临 YFROBOT创客社区 (http://yfrobot.com.cn/) | Powered by Discuz! X3.1 |