繁体   English   中英

python + arduino控制直流电动机

[英]python + arduino controlling DC Motor

嗨,这是我的Arduino代码,因为我只想要循环一次,所以我在void loop()中使用了while(1){}构造

int motorPin = 3;
int motorDir = 12;
int motorBr = 9;

void setup() {
 //pinMode(motorPin, OUTPUT);
 pinMode(motorBr, OUTPUT);  
 pinMode(motorDir, OUTPUT);

 if (Serial.available() > 0) {

  if(Serial.read() == '1') {    
    digitalWrite(motorBr, LOW);
    digitalWrite(motorDir, HIGH);
    digitalWrite(motorPin, HIGH);
    delay(500); 
    digitalWrite(motorBr, HIGH);


  } else if(Serial.read() == '0') {
    digitalWrite(motorBr, LOW);
    digitalWrite(motorDir, LOW);
    digitalWrite(motorPin, HIGH);
    delay(500); 
    digitalWrite(motorBr, HIGH);
  }
 }

}

void loop() { while(1) {}
  }

这是我的python代码

import serial
import time

ser = serial.Serial('COM3', 9600, timeout=1)
time.sleep(2)



#I am forcing the script to write 1 to Arduino to make the motor turn

ser.write(b'1')

ser.flush()

time.sleep(2)

ser.close()

通讯没有发生。 任何见识都应该有所帮助。 我正在将Python 3.5和Arduino Uno与更新的驱动程序配合使用。

编辑:

嗨,朱利安,是的,以下代码可以完成工作:

int motorPin = 3;
int motorDir = 12;
int motorBr = 9;

void setup() {
 // put your setup code here, to run once:
 //pinMode(motorPin, OUTPUT);
 pinMode(motorBr, OUTPUT);  
 pinMode(motorDir, OUTPUT);

 digitalWrite(motorBr, LOW);
 digitalWrite(motorDir, HIGH);
 digitalWrite(motorPin, HIGH);
 delay(500); 
 digitalWrite(motorBr, HIGH);

 delay(2000);

 digitalWrite(motorBr, LOW);
 digitalWrite(motorDir, LOW);
 digitalWrite(motorPin, HIGH);
 delay(500); 
 digitalWrite(motorBr, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
}

我还做了以下更改

ser.write('1') --> ser.write(b'1')

Serial.read() == 1 --> Serial.read() == '1' 

Serial.read() == 1 --> Serial.read() == 0x31 

似乎没有任何作用。

我完成此操作的方法是先将Arduino程序上传到内存,然后运行Python脚本。 也没有错误显示。

通过Python中的Subprocess调用执行Ardiono代码:

import subprocess

actionLine = "upload"
projectFile = "C:/Users/Tomography/Desktop/DCM2/DCM2.ino"
portname = "COM3"
boardname = "arduino:avr:uno"

#I added the ardiono.exe to path, the command automatically sources the 
Command = "arduino" + " --" + actionLine +" --board " + boardname + " --port " + portname + " " + projectFile

print(Command)

result = subprocess.call(Command)

if result != 0:
 print("\n Failed - result code = %s --" %(result))
else:
 print("\n-- Success --")

旧帖子,但我认为我会把我的发现发布在这里,以防将来有人看到。

在void setup()下的arduino文件中,确保包括

Serial.begin(9600);

否则将无法建立连接。

这是我用来在python中使用1或0打开和关闭电机的完整工作代码:

Arduino代码:


void setup() {
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
}

void loop() //This will be executed over and over
{ 
    if (Serial.available() > 0) {
        if(Serial.read() == '1') {
          analogWrite(motorPin, 50);
        } 
        else if(Serial.read() == '0') {
          analogWrite(motorPin, 0);
        }
    }
}

Python代码:

import serial
import time

ser = serial.Serial('COM3', 9600) //established connection

time.sleep(2)

ser.write(b'1') ##sends '1' to serial 

time.sleep(5) ##motor runs for this period

ser.write(b'0') ##sends '0' to serial on arduino to turn motor off

ser.close()

尝试这个 :

import serial
import time

ser = serial.Serial('COM3', 9600, timeout=1) #here you may add write_timeout=1 to avoid indefinite blocking on failing flush

time.sleep(2)

ser.write('1')
ser.flush() #force the physical write

#time.sleep(2) #no need to sleep as flush was blocking

ser.close()

对于Arduino代码,对通讯的测试仅在设置功能中进行一次。 loop()与主循环中的while(1)等价,您可以从“正常” C代码中知道。

设置参考手册

循环参考手册

这意味着一旦执行Python,您的arduino代码就已经在loop()中的while(1)中,并且绝不会允许它分析串行数据。

正确的Arduino代码为:

int motorPin = 3;
int motorDir = 12;
int motorBr = 9;

void setup() //this is executed only once at init
{
 //pinMode(motorPin, OUTPUT);
 pinMode(motorBr, OUTPUT);  
 pinMode(motorDir, OUTPUT);
}

void loop() //This will be executed over and over
{ 
    if (Serial.available() > 0) {

        // here '1' (the character) is important as 1 is the number
        // and '1' equals 0x31 (ASCII)
        if(Serial.read() == '1') {   
            digitalWrite(motorBr, LOW);
            digitalWrite(motorDir, HIGH);
            digitalWrite(motorPin, HIGH);
            delay(500); 
            digitalWrite(motorBr, HIGH);
        } else if(Serial.read() == '0') {
            digitalWrite(motorBr, LOW);
            digitalWrite(motorDir, LOW);
            digitalWrite(motorPin, HIGH);
            delay(500); 
            digitalWrite(motorBr, HIGH);
        }
    }
}

您的Python代码正在发送字符串'1',但是您的arduino代码正在寻找数字1。尝试将arduino代码更改为此

Serial.read() == 0x31

Serial.read() == 0x30

这些分别是“ 1”和“ 0”的ASCII码

在您从python脚本发送字符时,setup()函数中的代码很可能已经运行。

将代码放在loop()函数中,然后在循环函数中放置一些逻辑,使其仅运行一次。

暂无
暂无

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

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