简体   繁体   English

Arduino Uno DC-Motor Controller概念澄清

[英]Arduino Uno DC-Motor Controller concept clarification

I just started learning Arduino. 我刚刚开始学习Arduino。 I thought about going hands on with a Robot which I want to get some autonomy in. I got stuck trying to understand the motor control for the DC motor. 我考虑过尝试使用要获得一定自治的机器人。我在尝试理解直流电动机的电动机控制时陷入了困境。 I've tried to look up resources but can't find one that adequately explains my problem. 我试图查找资源,但找不到能充分说明我的问题的资源。 I can't understand (being a comp sci undergrad) how or what I am doing wrong writing the code for the motor. 我不明白(正在做Comp sci本科生)为电动机编写代码的方式或做错了什么。

My code just for running the motors for a test is : 我仅用于运行电动机进行测试的代码是:

// MotorA
int dir1PinA = 7;
int dir2PinA = 5;
int speedPinA = 6;

//motor B

int dir1PinB = 4;
int dir2PinB = 2;
int speedPinB = 3;


void setup() {
  //Serial.begin(9600);
  pinMode (dir1PinA, OUTPUT);
  pinMode (dir2PinA, OUTPUT);
  pinMode (speedPinA,OUTPUT);
  pinMode (dir1PinB, OUTPUT);
  pinMode (dir2PinB, OUTPUT);
  pinMode(speedPinB, OUTPUT);

  // put your setup code here, to run once:

}

void loop() {
  analogWrite(speedPinA, 500);
  analogWrite(speedPinB, 500);
  digitalWrite (dir1PinA, LOW);
  digitalWrite (dir2PinA, HIGH);
  digitalWrite (dir1PinB,HIGH);
  digitalWrite (dir2PinB, LOW);

  // put your main code here, to run repeatedly:

}

I am just trying to run the motor at a certain speed to no avail. 我只是试图以一定速度运行电动机,但无济于事。 I found a sample code that uses the same method to control the motor but has just an extra loop and variable speed . 我发现了一个示例代码,该示例代码使用相同的方法来控制电动机,但仅有一个额外的循环和可变 speed

The test code which works is : 有效的测试代码是:

//Motor A
int dir1PinA = 7;
int dir2PinA = 5;
int speedPinA = 6;

//motor B
int dir1PinB = 4;
int dir2PinB = 2;
int speedPinB = 3;

unsigned long time;
int speed;
int dir;

void setup ()
{
pinMode (dir1PinA, OUTPUT);
pinMode (dir2PinA, OUTPUT);
pinMode (speedPinA, OUTPUT);
pinMode (dir1PinB, OUTPUT);
pinMode (dir2PinB, OUTPUT);
pinMode (speedPinB, OUTPUT);
time = millis ();
speed = 0;
dir = 1;
}

void loop ()
{
analogWrite (speedPinA, speed);
analogWrite (speedPinB, 555 - speed);

// set direction
if (1 == dir)
{
digitalWrite (dir1PinA , LOW);
digitalWrite (dir2PinA, HIGH);
digitalWrite (dir1PinB, HIGH);
digitalWrite (dir2PinB, LOW);
}
else
{
digitalWrite (dir1PinA, HIGH);
digitalWrite (dir2PinA, LOW);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, HIGH);
}

if (millis () - time> 5000)
{
time = millis ();
speed += 20;
if (speed> 555) {speed = 0;}

if (1 == dir)
{dir = 0;}
else
{dir = 1;}
}
}

Now I understand there are loops and extra variables but theoretically they shouldn't be required to just run a motor at a certain speed. 现在,我了解到存在循环和额外的变量,但是从理论上讲,仅以一定速度运行电动机就不需要它们。 My question is that What am I missing from the top sketch that is in the second one but is absolutely critical to the running of the motor? 我的问题是, 第二个草图中我缺少什么,但对电动机的运行绝对至关重要?

I want to apologize in advance if this is a noob moment but I tried and can't find any logic lol. 如果这是一个菜鸟的时刻,我想提前道歉,但我尝试过,找不到任何逻辑的笑声。

In the "working" Arduino code ( c++ , yes it's compiled by a c++ compiler ) one of your both motors (B) starts immediately, and the other one 5 sec after reset (or even later)? 在“工作中”的Arduino代码( c ++,是的,它是由c ++编译器编译的 )中,两个电动机(B)之一立即启动,而另一个电动机在复位后5秒钟(甚至更晚)启动? And the immediately started motor changes direction after 5 sec? 5秒后立即启动的电动机改变方向了吗?

Else, try small modifications to the working sample, towards your simplification, to get some more understanding. 否则,请对工作样本进行一些小的修改,以简化您的工作,以获取更多的了解。
Eg first remove the "dir" change and "else" part 例如,首先删除“ dir”更改和“ else”部分

Okay first I will debug your code, I will make a some assumptions about your hardware since you didn't post any photos or schematics, finally I will provide a working solution. 好的,首先,我将调试您的代码,由于您没有发布任何照片或示意图,因此将对您的硬件进行一些假设,最后,我将提供一个可行的解决方案。

Your Code: 您的代码:

So my first assumption is that your Motor has 3 pins because you're toggling two between high and low, presumably to reverse current, and then one to set the speed. 因此,我的第一个假设是您的电机有3个引脚,因为您要在高电平和低电平之间切换两个,大概是反向电流,然后再一个来设置速度。

As I am guessing you know, in order to get a motor to reverse you need to flip the direction the current is going. 正如我猜您所知道的,要使电动机反转,您需要反转电流的方向。 Setting these pins high and low won't actually reverse the current. 将这些引脚设置为高电平和低电平实际上不会使电流反向。 Also, the Arduino doesn't really have enough output current to drive a DC motor. 而且,Arduino确实没有足够的输出电流来驱动直流电动机。 You can probably slowly run one. 您可能可以慢慢运行一个。 If you want to do more you'll need an external power source. 如果您想做更多的事情,您将需要一个外部电源。

Drive a DC motor in one direction: 在一个方向上驱动直流电动机:

This is probably the easiest one to do. 这可能是最容易做到的。 You just need a transistor, diode and resistor. 您只需要一个晶体管,二极管和电阻器。 Here is a tutorial 这是一个教程

Drive up to four DC motors in both directions 双向驱动最多四个直流电动机

If you want to drive your DC motor in two directions then you need to flip the direction the current is going. 如果要沿两个方向驱动直流电动机,则需要反转电流方向。 This seems complex at first until you realize they've already built something for you to do this exact thing. 乍一看,这似乎很复杂,直到您意识到他们已经为您完成某些事情来做这件事为止。 It's called a Motor Control. 这就是所谓的电机控制。 It's just a shield with a few H-Bridges on it. 它只是上面有几个H桥的盾牌。 They're pretty cheap. 他们很便宜。

I like AdaFruit's . 我喜欢AdaFruit的 I have a couple and I built a robot with it not to long ago. 我有一对,不久前我用它建造了一个机器人。

Tutorial on how to use the Adafruit MotorShield 有关如何使用Adafruit MotorShield的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM