步骤6:利用现有的程序来旋转无刷电机
现在回到我的项目中,本步骤中将会第一次涉及“如何来做”的内容哦。
在上个步骤中提到的文章,作者利用Arduino来驱动了无刷电机让其旋转。他们参考了
一篇早期由eLABZ撰写的文章。并且他们利用程序验证了它。在这面文章里作者eLABZ解释了如何利用旧的DVD光驱的无刷电机来制作一个频闪观测器。
据我所知,这是仅仅唯一的一篇关于利用Arduino来驱动无刷电机旋转的,并包含了全部细节的文章。这篇文章包含了3个部分。
1.Learning fundamental knowledge of (3-phase) Brushless Motor
2.Rotating Brushless Motor by using Arduino
3.Putting materials together to get Stroboscope
:
在第二个部分里提供了驱动无刷电机的程序。我读完之后并做了相关的注释。然后我明白了我应该需要给电机的三项通上三项交流电,并且每一项之间的相位相差120°(项与项之间无特殊顺序)。我模仿这个程序来驱动电机果然轻松地就让我买的这个电机转了起来。 DOCUMENTARY (5) 这个视频展示了效果。
:
[DOCUMENTARY (5)] Rotating 3-Phase Brushless Motor with Arduino and AC Square Wave
视频地址:https://www.youtube.com/embed/rzWfxoOVmjU
:
这里我解释一下如何利用Arduino UNO来旋转无刷电机。你需要一个三项无刷电机,它有三条引出线。
1.把电机的这三根引出线分别与UNO的9,10,11引脚相连,没有连接顺序的要求。
2.下载我这个步骤最后提供的两个PDF文档到你的电脑中
3.利用PDF阅读器打开他们。
4.把任意其中一个文档中的全部文字拷贝
5.把拷贝内容粘贴到Arduino的开发环境中并且更正一些小错误。
6.上载检查无误后的程序到Arduino中
这两程序都是基于eLABz的原始程序修改而来。高转矩的云台电机会在程序上载完成后立刻开始旋转,而有些模型上用高速型的电机仅用Arduino提供的电力可能不会旋转。这时候就需要外部电源和电机驱动芯片了。在DOCUMENTARY (6)中的展示中就利用里外部电源和驱动芯片。在这个视频的前半部分是展示的高扭矩型电机,在后半部分是展示的高速型电机。
:
[DOCUMENTARY (6)] Rotating Two Types of Brushless Motor with Arduino, Outer Batteries and Sine Wave AC
视频地址: https://www.youtube.com/embed/55_e6MnpI88
PDF1:
// Driving 3-phase Brushless DC Motor with Square-wave
// This sketch is based on the code for the stroboscope project by eLABZ.
// (http://elabz.com/bldc-motor-with-arduino-circuit-and-software/)
// Copyright (C) 2015 ArduinoDeXXX All Rights Reserved.
const int motorDelayActual = 150;
const int motorPin1 =9;
const int motorPin2 =10;
const int motorPin3 =11;
const int motorPinState[]={1,1,1,0,0,0};
int currentStepA=0;
int currentStepB=2;
int currentStepC=4;
long lastMotorDelayTime = 0;
void setup () {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
}
void loop () {
if((millis() - lastMotorDelayTime) > motorDelayActual){
currentStepA = currentStepA ++;
if(currentStepA > 5) currentStepA = 0;
if(currentStepA < 0) currentStepA = 5;
currentStepB = currentStepB ++;
if(currentStepB > 5) currentStepB = 0;
if(currentStepB < 0) currentStepB = 5;
currentStepC = currentStepC ++;
if(currentStepC > 5) currentStepC = 0;
if(currentStepC < 0) currentStepC = 5;
lastMotorDelayTime = millis();
analogWrite(motorPin1, 254 * motorPinState[currentStepA]);
analogWrite(motorPin2, 254 * motorPinState[currentStepB]);
analogWrite(motorPin3, 254 * motorPinState[currentStepC]);
}
}
// Copyright (C) 2015 ArduinoDeXXX All Rights Reserved.
PDF2:
// Driving 3-phase Brushless DC Motor with Sinusoidal Wave
// This sketch is based on the code for the stroboscope project by eLABZ.
// (http://elabz.com/bldc-motor-with-arduino-circuit-and-software/)
// Copyright (C) 2015 ArduinoDeXXX All Rights Reserved.
const int motorDelayActual = 20;
const int motorPin1 =9;
const int motorPin2 =10;
const int motorPin3 =11;
const int motorPinState[]={127,111,94,78,64,50,37,26,17,9,4,1,0,1,4,9,17,26,37,50,64,78,
94,111,127,144,160,176,191,205,218,229,238,245,251,254,255,
254,251,245,238,229,218,205,191,176,160,144,127};
int currentStepA = 0;
int currentStepB = 16;
int currentStepC = 32;
long lastMotorDelayTime = 0;
void setup () {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
}
void loop () {
if((millis() - lastMotorDelayTime) > motorDelayActual){
currentStepA = currentStepA ++;
if(currentStepA > 47) currentStepA = 0;
if(currentStepA < 0) currentStepA = 47;
currentStepB = currentStepB ++;
if(currentStepB > 47) currentStepB = 0;
if(currentStepB < 0) currentStepB = 47;
currentStepC = currentStepC ++;
if(currentStepC > 47) currentStepC = 0;
if(currentStepC < 0) currentStepC = 47;
lastMotorDelayTime = millis();
analogWrite(motorPin1, motorPinState[currentStepA]);
analogWrite(motorPin2, motorPinState[currentStepB]);
analogWrite(motorPin3, motorPinState[currentStepC]);
}
}
// Copyright (C) 2015 ArduinoDeXXX All Rights Reserved.