将 Arduino/RedBoard 振动器连接到 Python
Posted
技术标签:
【中文标题】将 Arduino/RedBoard 振动器连接到 Python【英文标题】:Interface Arduino/RedBoard vibrator to Python 【发布时间】:2021-12-30 15:14:17 【问题描述】:我正在尝试将我的SparkFun Qwiic Haptic Driver - DA7280 与 Python3 接口。我目前的设置如下:
PC -USB 转 micro-USB-> SparkFun RedBoard Qwiic -Qwiic 电缆-> 触觉驱动器
我已经试用了随附的 Arduino sketch 并设法让 C++ 代码正常运行;调节振动器的强度和频率就好了。
那么,我想做的事情就是用一些 Python 代码及时触发振动脉冲。这样,当python打印出一个单词时,就会触发一个振动脉冲。
我尝试使用pySerial 与微控制器接口,触发控制器运行预加载的脚本。使用简单的 C++ 脚本重复上传到微控制器的 LED 闪烁效果很好:
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly for 6 seconds
*/
void setup()
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// Open serial connection.
Serial.begin(9600);
Serial.write('1');
// the loop function waits until it receives an input from the serial port & will stop again when it receives a stop signal.
void loop()
if(Serial.available() > 0) // if data present, blink
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
Serial.write('0');
结合 Python 脚本触发和关闭控制器:
# import necessary modules
import os
import serial
# connect to the arduino
## Boolean variable that will represent whether or not the arduino is connected
connected = False
## open the serial port that your ardiono is connected to.
ser = serial.Serial("COM8", 9600) # you may need to change this pending on how the board connects to the PC
## loop until the arduino tells us it is ready
while not connected:
serin = ser.read()
connected = True
## trigger the arduino to run the uploaded code
ser.write(1)
## Wait until the arduino tells us it is finished
while ser.read() == '1':
ser.read()
print(ser.read())
## trigger the arduino to run the uploaded code
ser.write(0)
## close the port and end the program
ser.close()
但是,当我尝试用启动和停止振动的命令(如下)替换特定于 LED 的线路时,会出现一些问题:
-
尽管控制器按预期等待来自 Python 的 go 信号,但 Python 很快就会通过脚本,而不是等待控制器停止。
控制器开始振动,但振动脉冲之间没有预期的延迟,并且振动器永远不会停止,即使在 python 发送停止触发器之后也是如此。
/*
Python triggered vibration.
Waits for Python to send a go signal at which point the vibration starts for a given duration of time.
*/
#include <Wire.h>
#include "Haptic_Driver.h" # this module is from the aforementioned Arduino sketch
Haptic_Driver hapDrive;
int event = 0;
void setup()
// Open serial connection.
Wire.begin();
Serial.begin(9600);
if( !hapDrive.begin())
Serial.println("Could not communicate with Haptic Driver.");
else
Serial.println("Qwiic Haptic Driver DA7280 found!");
if( !hapDrive.defaultMotor() )
Serial.println("Could not set default settings.");
// Frequency tracking is done by the IC to ensure that the motor is hitting
// its resonant frequency. I found that restricting the PCB (squeezing)
// raises an error which stops operation because it can not reach resonance.
// I disable here to avoid this error.
hapDrive.enableFreqTrack(false);
Serial.println("Setting I2C Operation.");
hapDrive.setOperationMode(DRO_MODE);
Serial.println("Ready.");
Serial.write('1');
void loop()
if(Serial.available() > 0)
hapDrive.setVibrate(25);
delay(1500);
hapDrive.setVibrate(0);
delay(1500);
Serial.write(0);
Serial.flush();
在微控制器和 C++ 方面,我都是新手,所以请原谅我的任何重大误解/错误。另外,如果上述描述中有任何不清楚的地方,请告诉我。
非常感谢, 利亚姆
【问题讨论】:
【参考方案1】:我怀疑至少部分问题是您没有清除读取缓冲区的内容,只是检查是否存在某些内容。 Serial.flush()
我认为从 Arduino 1.00 开始(不要引用我的话)串行刷新对传入的缓冲区没有任何作用。
尝试在hapDrive.setVibrate(25);
之前添加var = Serial.read()
,看看是否会改变功能。
我也强烈推荐串行中断。有一个非常全面的串行事件示例(虽然我似乎记得这实际上并不是经典微控制器意义上的中断驱动,但它已经足够接近了!)
【讨论】:
谢谢,问题已解决!我已删除 Serial.flush 并使用变量将输入保存到串行缓冲区。感谢您为我指明了正确的方向,现在了解更多有关串行 I/O 和函数的信息 - 干杯!以上是关于将 Arduino/RedBoard 振动器连接到 Python的主要内容,如果未能解决你的问题,请参考以下文章