本课内容:
+ 控制舵机转动速度
+ 控制机械臂运动速度
本课开始示例代码:(无法复制本站示例程序代码?请点击这里获得解决方法。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
/* MeArm机械臂控制程序-1 by 太极创客 (2017-06-02) www.taichi-maker.com 本程序用于太极创客《零基础入门学用Arduino教程 - MeArm篇》。 1 使用串口获得电机数据,设置机械臂动作 将串口指令解读改写为函数,加入状态汇报函数reportStatus 如需要获得具体电路连接细节,请查阅太极创客制作的 《零基础入门学用Arduino教程 - MeArm篇》页面。 */ #include <Servo.h> //使用servo库 Servo base, fArm, rArm, claw ; //创建4个servo对象 // 建立4个int型变量存储当前电机角度值 // 初始角度值为设备启动后初始状态所需要的电机角度数值 int basePos = 90; int rArmPos = 90; int fArmPos = 90; int clawPos = 90; //存储电机极限值(const指定该数值为常量,常量数值在程序运行中不能改变) const int baseMin = 0; const int baseMax = 180; const int rArmMin = 45; const int rArmMax = 180; const int fArmMin = 35; const int fArmMax = 120; const int clawMin = 25; const int clawMax = 100; void setup(){ base.attach(11); // base 伺服舵机连接引脚11 舵机代号'b' delay(200); // 稳定性等待 rArm.attach(10); // rArm 伺服舵机连接引脚10 舵机代号'r' delay(200); // 稳定性等待 fArm.attach(9); // fArm 伺服舵机连接引脚9 舵机代号'f' delay(200); // 稳定性等待 claw.attach(6); // claw 伺服舵机连接引脚6 舵机代号'c' delay(200); // 稳定性等待 Serial.begin(9600); Serial.println("Welcome to Taichi-Maker Robot Arm Tutorial"); } void loop(){ if (Serial.available()>0) { //使用串口监视器输入电机指令控制机械臂电机 char serialCmd = Serial.read();//指令举例: b45,将底盘舵机(舵机代号'b')调整到45度位置 armDataCmd(serialCmd); } base.write(basePos); delay(10); fArm.write(fArmPos); delay(10); rArm.write(rArmPos); delay(10); claw.write(clawPos); delay(10); } void armDataCmd(char serialCmd){ Serial.print("serialCmd = "); Serial.print(serialCmd); int servoData = Serial.parseInt(); switch(serialCmd){ case 'b': basePos = servoData; Serial.print(" Set base servo value: "); Serial.println(servoData); break; case 'c': clawPos = servoData; Serial.print(" Set claw servo value: "); Serial.println(servoData); break; case 'f': fArmPos = servoData; Serial.print(" Set fArm servo value: "); Serial.println(servoData); break; case 'r': rArmPos = servoData; Serial.print(" Set rArm servo value: "); Serial.println(servoData); break; case 'o': reportStatus(); break; default: Serial.println(" Unknown Command."); } } void reportStatus(){ Serial.println(""); Serial.println(""); Serial.println("++++++ Robot-Arm Status Report +++++"); Serial.print("Claw Position: clawPos = "); Serial.println(claw.read()); Serial.print("Base Position: basePos = "); Serial.println(base.read()); Serial.print("Rear Arm Position: rArmPos = "); Serial.println(rArm.read()); Serial.print("Front Arm Position: fArmPos = "); Serial.println(fArm.read()); Serial.println("++++++++++++++++++++++++++++++++++++"); Serial.println(""); } |
本课学习结束示例代码:(无法复制本站示例程序代码?请点击这里获得解决方法。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
/* MeArm机械臂控制程序-1 by 太极创客 (2017-06-02) www.taichi-maker.com 本程序用于太极创客《零基础入门学用Arduino教程 - MeArm篇》。 1 使用串口获得电机数据,设置机械臂动作 将串口指令解读改写为函数,加入状态汇报函数reportStatus 如需要获得具体电路连接细节,请查阅太极创客制作的 《零基础入门学用Arduino教程 - MeArm篇》页面。 */ #include <Servo.h> //使用servo库 Servo base, fArm, rArm, claw ; //创建4个servo对象 // 建立4个int型变量存储当前电机角度值 // 初始角度值为设备启动后初始状态所需要的电机角度数值 int basePos = 90; int rArmPos = 90; int fArmPos = 90; int clawPos = 90; //存储电机极限值(const指定该数值为常量,常量数值在程序运行中不能改变) const int baseMin = 0; const int baseMax = 180; const int rArmMin = 45; const int rArmMax = 180; const int fArmMin = 35; const int fArmMax = 120; const int clawMin = 25; const int clawMax = 100; void setup(){ base.attach(11); // base 伺服舵机连接引脚11 舵机代号'b' delay(200); // 稳定性等待 rArm.attach(10); // rArm 伺服舵机连接引脚10 舵机代号'r' delay(200); // 稳定性等待 fArm.attach(9); // fArm 伺服舵机连接引脚9 舵机代号'f' delay(200); // 稳定性等待 claw.attach(6); // claw 伺服舵机连接引脚6 舵机代号'c' delay(200); // 稳定性等待 Serial.begin(9600); Serial.println("Welcome to Taichi-Maker Robot Arm Tutorial"); } void loop(){ if (Serial.available()>0) { char serialCmd = Serial.read(); armDataCmd(serialCmd); } base.write(basePos); delay(10); fArm.write(fArmPos); delay(10); rArm.write(rArmPos); delay(10); claw.write(clawPos); delay(10); } void armDataCmd(char serialCmd){ Serial.print("serialCmd = "); Serial.print(serialCmd); int servoData = Serial.parseInt(); int fromPos; int toPos; switch(serialCmd){ case 'b': fromPos = base.read(); toPos = servoData; if (fromPos <= toPos){ //如果“起始角度值”小于“目标角度值” for (int i=fromPos; i<=toPos; i++){ base.write(i); delay (15); } } else { //否则“起始角度值”大于“目标角度值” for (int i=fromPos; i>=toPos; i--){ base.write(i); delay (15); } } basePos = servoData; Serial.print(" Set base servo value: "); Serial.println(servoData); break; case 'c': clawPos = servoData; Serial.print(" Set claw servo value: "); Serial.println(servoData); break; case 'f': fArmPos = servoData; Serial.print(" Set fArm servo value: "); Serial.println(servoData); break; case 'r': rArmPos = servoData; Serial.print(" Set rArm servo value: "); Serial.println(servoData); break; case 'o': reportStatus(); break; default: Serial.println(" Unknown Command."); } } void reportStatus(){ Serial.println(""); Serial.println(""); Serial.println("++++++ Robot-Arm Status Report +++++"); Serial.print("Claw Position: clawPos = "); Serial.println(claw.read()); Serial.print("Base Position: basePos = "); Serial.println(base.read()); Serial.print("Rear Arm Position: rArmPos = "); Serial.println(rArm.read()); Serial.print("Front Arm Position: fArmPos = "); Serial.println(fArm.read()); Serial.println("++++++++++++++++++++++++++++++++++++"); Serial.println(""); } |