我正在开发一个代码,该代码假设是要从称重传感器中获取数据的,并且应该基于该数据控制电动机。如果称重传感器读取特定值,则电动机应向一个方向旋转;如果读取不同值,则电动机应沿另一个方向旋转。现在,如果我向传感器施加负载,电动机将朝一个方向运行,而当卸下负载时,电动机将不会朝另一方向运行。相反,它继续朝错误的方向前进。我还将python将来自称重传感器的数据保存到一个csv文件中,因此我注意到,即使从称重传感器上去除了力,称重传感器值仍保持较高的数值,如117牛顿。在arduino和python之间进行通信之前,先确定传感器的输出。我已经阅读了一些有关双面打印的内容,并认为可能是我的问题所在。为什么我的python force值现在是错误的任何帮助都将很棒。
我尝试使用不同的格式制作arduino代码。 Arduino Code 2可以更好地工作,我可以使电动机响应称重传感器上的输入,但是当我打印输入到python中的数据时,它确实有点搞砸了。但是,这不会意外地影响我的运动。我打算保留这些传入的数据以备将来使用,并希望可以对其进行清理。例如,假设我在上面有5牛顿的力,它会在几秒钟内显示5牛顿,然后在几个实例中突增为随机数,然后恢复正常。
Python
import serial
import csv
import time
from time import localtime, strftime
import warnings
import serial.tools.list_ports
__author__ = 'Matt Munn'
arduino_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'Arduino' in p.description
]
if not arduino_ports:
raise IOError("No Arduino found - is it plugged in? If so, restart computer.")
if len(arduino_ports) > 1:
warnings.warn('Multiple Arduinos found - using the first')
Arduino = serial.Serial(arduino_ports[0],9600,timeout=1)
time.sleep(2)
start_time=time.time()
Force = []
Actuator_Signal=[]
outputFileName = "Cycle_Pull_Test_#.csv"
outputFileName = outputFileName.replace("#", strftime("%Y-%m-%d_%H %M %S", localtime()))
with open(outputFileName, 'w',newline='') as outfile:
outfileWrite = csv.writer(outfile)
while True:
while (Arduino.inWaiting()==0):
pass
try:
data = Arduino.readline()
dataarray = data.decode().rstrip().split(',')
Force = round(float(dataarray[0]),3)
print (Force)
if Force < 50:
Arduino.write(b'u')
print ('up')
else:
Arduino.write(b'd')
print('down')
except (KeyboardInterrupt, SystemExit,IndexError,ValueError):
pass
outfileWrite.writerow([Force,"N"])
Arduino
#include <HX711_ADC.h>
#include "CytronMotorDriver.h"
// Configure the motor driver.
CytronMD motor(PWM_DIR, 3, 2); // PWM = Pin 3, DIR = Pin 2.
int up = HIGH;
int down = LOW;
int dstate = up;
int python_direction = 0;
float interval = 12000;
float pretime= 0;
float curtime = 0;
// LOAD CELL
//HX711 constructor (dout pin, sck pin)
HX711_ADC LoadCell(11, 12);
float force;
float calforce;
float newtons;
// The setup routine runs once when you press reset.
void setup() {
Serial.begin(9600);
LoadCell.begin();
LoadCell.start(2000); // tare preciscion can be improved by adding a few seconds of stabilising time
LoadCell.setCalFactor(100); // user set calibration factor (float)
}
// The loop routine runs over and over again forever.
void loop() {
if (Serial.available()>0){
python_direction = Serial.read();
Serial.print (python_direction);
Serial.print (",");
}
LoadCell.update();
force = LoadCell.getData();
force = (force/285); // Force in (N) // 285 is conversion factor
calforce = (-1.0389*force)+0.0181, // This is in lbs
newtons = 4.45*calforce;
receive from serial terminal for tare
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}
unsigned long curtime = millis();
if (python_direction == 'u'){
motor.setSpeed(255); // Run forward at full speed.
}
if (python_direction == 0){
motor.setSpeed(0); // Stop motor.
}
if (python_direction == 'd'){
motor.setSpeed(-255); // Run backward at full speed.
}
Serial.println(newtons);
}
Arduino 2
#include <HX711_ADC.h> // HX711 Library
#include "CytronMotorDriver.h"
// Configure the motor driver.
CytronMD motor(PWM_DIR, 3, 2); // PWM = Pin 3, DIR = Pin 2.
int up = HIGH;
int down = LOW;
int dstate = up;
int python_direction = 0;
float interval = 12000;
float pretime= 0;
float curtime = 0;
HX711_ADC LoadCell(11, 12);
float force;
float calforce;
float newtons;
void setup() {
Serial.begin(9600);
LoadCell.begin();
LoadCell.start(2000); // tare preciscion can be improved by adding a few seconds of stabilising time
LoadCell.setCalFactor(100.0); // user set calibration factor (float)
}
void loop() {
LoadCell.update();
forceRead();
actuatorControl();
}void forceRead()
{
force = LoadCell.getData();
force = (force/285); // Force in (N) // 285 is conversion factor
calforce = (-1.0389*force)+0.0181, // This is in lbs
newtons = 4.45*calforce;
Serial.println(newtons);
//receive from serial terminal for tare
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}
}
void actuatorControl(){
if (Serial.available()>0){
python_direction = Serial.read();
Serial.print (python_direction);
Serial.print (",");
}
if (python_direction == 'd'){
motor.setSpeed(255); // Run forward at full speed.
}
if (python_direction == 0){
motor.setSpeed(0); // Stop motor.
}
if (python_direction == 'u'){
motor.setSpeed(-255); // Run backward at full speed.
}
}