1. 某宝上的四自由度机械臂拼装板
2. 9克舵机 x 3
3. Arduino UNO
4. 步进电机 x 1
5. 外电源 及 导线
拼装后发现原来的底盘旋转舵机出现头重脚轻,摇摇欲坠的感觉. 决定改进,换装一个步进电机作为机械臂的底盘,果然效果外观都很好.步进电机最好接外部电源,5-9V 都可以.
//四自由度机械臂程序(三个9克舵机,一个步进电机. Arduino程序控制.)
#include <Stepper.h>
#include <Servo.h>
//抓手,主臂,次臂三舵机
Servo myservo1; //抓手
Servo myservo2; //主臂
Servo myservo3; //次臂
int pos =0;
//底盘步进电机
const int stepsPerRevolution=200;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); //步进电机 Pin
int step=0;
void setup() {
//三舵机Pin
myservo1.attach(2); //Pin 2
myservo2.attach(3); //Pin 3
myservo3.attach(4); //Pin 4
//三舵机复位
myservo1.write(0);
delay(150);
myservo2.write(0);
delay(150);
myservo3.write(0);
delay(150);
myStepper.setSpeed(30);
}
void loop() {
//第一次顺环动作
s1open(20); //抓手开启
delay(1000);
s2Forward(45); //主臂向前 45 度
delay(1000);
s1close(20); //抓手关闭
delay(1000);
s2Backward(45); //主臂向后 45 度
delay(1000);
s3Forward(45); //次臂抬升 45 度
Stepper(200); //步进电机正转 200 步
delay(500);
delay(1000);
//第二次顺环动作
s2Forward(45); //主臂向前 45 度
delay(1000);
s3Backward(45); //次臂下降 45 度
delay(1000);
s1open(20); //抓手开启
delay(1000);
s1close(20); //抓手关闭
delay(1000);
s2Backward(45); //主臂向后 45 度
//s3Forward(45); //次臂抬升 45 度
Stepper(-200); //步进电机反转 200 步
delay(500);
delay(1000);
}
//抓手开启
void s1open(int anlge) {
for (pos = 0; pos <= anlge; pos += 1) {
myservo1.write(pos);
delay(30);
}
}
//抓手关闭
void s1close(int anlge) {
for (pos = anlge; pos >= 0; pos -= 1) {
myservo1.write(pos);
delay(30);
}
}
//主臂向前
void s2Forward(int anlge) {
for (pos = 0; pos <= anlge; pos += 1) {
myservo2.write(pos);
delay(30);
}
}
//主臂向后
void s2Backward(int anlge) {
for (pos = anlge; pos >= 0; pos -= 1) {
myservo2.write(pos);
delay(30);
}
}
//次臂向前
void s3Forward(int anlge) {
for (pos = 0; pos <= anlge; pos += 1) {
myservo3.write(pos);
delay(30);
}
}
//次臂向后
void s3Backward(int anlge) {
for (pos = anlge; pos >= 0; pos -= 1) {
myservo3.write(pos);
delay(30);
}
}
//步进电机
void Stepper(int stepsPerRevolution) {
myStepper.step(stepsPerRevolution);
delay(500);
}
效果视频
链接:
https://pan.baidu.com/s/12Ih93Nk2UeFAssc5kYogpA 提取码:vfqo 复制这段内容后打开百度网盘手机App,操作更方便哦
很好玩的东东.各位朋友如果有更好的玩法请留言.