前言
随着5G时代到来,更多互联网落地生活的可能性已经可以被看到。机械臂等在车间,工厂内的广泛运用,也证明了在不远的未来,机器代替人的工作将越来越多。机器人技术正在深刻改变着人类的生产和生活方式。针对机器人的开发正是面对时代发展问卷的积极回答。
一、机械臂
多轴机械臂的大部分使用舵机旋转方式实现,比较难实现平移算法,需要大范围计算,比较麻烦。
二、使用步骤
1.引入库
代码如下(示例):
#include <SoftwareSerial.h>
SoftwareSerial zySerial(2, 3);
#include <Servo.h>
2.完整代码
代码如下(示例):
#include <SoftwareSerial.h>
SoftwareSerial zySerial(2, 3); //RX=50,TX=51 需分配Mega 和 Mega 2560 上并非所有引脚都支持更改中断,因此只有以下引脚可用于 RX:10、11、12、13、14、15、50、51、52、53、A8 (62)、A9 (63)、A10 (64)、A11 (65)、A12 (66)、A13 (67)、A14 (68)、A15 (69)。
// #define DEBUG2-3是和esp8266的串口连接
#include <Servo.h>
Servo myservo1;
int cs1 = 1500; //舵机1初始位置
int cs2 = 1000; //舵机2初始位置
int cs3 = 2275; //舵机3初始位置
int cs4 = 1800;
int jw1; //舵机1结束位置
int jw2; //舵机2结束位置
int jw3; //舵机3结束位置
int jw4;
int l1 = 20;
int l2 = 20;
int l11;
int l22;
int h;
double x1;
int jiaodu2;
int jiaodu3;
int jiaodu4;
char x2 = 'g';
char x3;
char x4;
//char x5 = q;
void jc()
{
cs1 = jw1;
cs2 = jw2;
cs3 = jw3;
cs4 = jw4;
delay(1000);
//if(zySerial.available > 0)
// {
// x2=zySrial.read();
// if(x2=='t' && x2=='q')
//{
// x5=x2;
// }
//
// }
// if(x5=='q'){
// }
while (1) {
x2 = Serial.read();
if (x2 == 'a')
{
myServo(3, cs1 - 20);
jw1 = cs1 - 20;
Serial.println('a');
delay(50);
Serial.println('a');
delay(50);
break;
}
if (x2 == 'b')
{
myServo(3, cs1 + 20);
jw1 = cs1 + 20;
Serial.println('b');
delay(50);
Serial.println('b');
delay(50);
break;
}
if (x2 == 'm')
{
myServo(4, cs2 - 20);
jw2 = cs2 - 20;
myServo(8, cs4 + 20);
jw4 = cs4 + 20;
Serial.println('m');
break;
}
if (x2 == 'n')
{
myServo(4, cs2 + 20);
jw2 = cs2 + 20;
myServo(8, cs4 - 20);
jw4 = cs4 - 20;
Serial.println('n');
break;
}
if (x2 == 'p')
{
//向后走
break;
}
if (x2 == 'i')
{
cs1 = jw1;
cs2 = jw2;
cs3 = jw3;
cs4 = jw4;
l11 = l1 * sin((cs2 - 500) * 270 / 2000);
l22 = l2 * sin((cs4 - 1500) * 270 / 2000);
h = l11 - l22;
pingyi();
myServo(4, jw2);
myServo(7, jw3);
myServo(8, jw4);
Serial.println('i');
break;
}
if (x2 == 'e')
{
Serial.println('e');
break;
}
if (x2 == 'q')
{
//前进
break;
}
}
}
void myServo(int dk, int pl)
{
if ((pl <= 2500) && (pl >= 500))
{
digitalWrite(dk, 1);
delayMicroseconds(pl);
digitalWrite(dk, 0);
delayMicroseconds(20000 - pl);
}
}
void pingyi()
{
for (int dx = 1; dx <= 400; dx++)
{
cs2 = cs2 - 1;
myServo(4, cs2);
l11 = l1 * sin((cs2 - 500) * 270 / 2000);
l22 = l11 - h;
cs4 = asin(l22 / l2) * 2000 / 360 + 1500;
myServo(8, cs4);
cs3 = 180 - 360 / 2000 * (cs2 - 500 + cs4 - 1500);
cs3 = 360 - cs3;
cs3 = cs3 / 360 * 2000 + 500;
myServo(7, cs3);
}
}
void setup() {
myservo1.attach(7);
myServo(3, cs1);
myServo(4, cs2);
myServo(8, cs3);
myServo(12, cs4);
delay(2000);
Serial.begin(9600);
Serial.println("INIT_READY");
// put your setup code here, to run once:
zySerial.begin(9600);
}
void loop() {
jc();
delay(100);
Serial.println("WORK_READY");
delay(100);
Serial.println("WORK_READY");
// put your main code here, to run repeatedly:
}
总结
以上就是今天的内容,本文仅仅简单介绍了arduino控制机械臂的使用,仅仅给大家参考。