01-13-2022 12:41 PM
Hello everybody,
We are students from Portugal having some problems executing a project with both LabView and arduino.
We have to send periodically a set of values from Labview to Arduino, make a calculation with the values in arduino and send multiple values again from Arduino to Labview but we seem to enter a problem with the function Scan From String. And because we have to write and read a set of values using the same VI we don't know where we are failing at.
If anyone could help us we would be most thankfull.
Arduino Code:
float KP=0;
float KI=0;
float KD=0;
float processValue=0;
float controlAction=0;
float setPoint=0;
float previousSetPoint=0;
float error;
float integralError = 0;
float derivadaError= 0;
float lastError = 0;
String outPut="";
unsigned long previousTime;
unsigned long currentTime;
unsigned long elapsedTime;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600); //velocidade de comunicação série 9600bit/s
previousTime = millis();
currentTime = millis()+500;
}
float readIntoFloat(){
String strVar = Serial.readStringUntil(' ');
outPut=strVar+'\t';
int firstCommaIndex = strVar.indexOf(',');
strVar[firstCommaIndex]='.';
float numVar = strVar.toFloat();
return numVar;
}
void loop() {
// put your main code here, to run repeatedly:
outPut="";
elapsedTime = (currentTime - previousTime)*0.001;
// Determinação do Process Value tendo em conta que na primeira iteração este não vai ser introduzido
processValue=readIntoFloat();
// Determinação da constante Kp
KP=readIntoFloat();
// Determinação da constante Ki
KI=readIntoFloat();
// Determinação da constante Kd
KD=readIntoFloat();
// Determinação do Set Point
setPoint=readIntoFloat();
if(setPoint!=previousSetPoint)
integralError=0;
// Determinação das diferentes porções do erro
error = setPoint-processValue;
integralError += error;//*elapsedTime;
derivadaError = (error-lastError);//elapsedTime;
lastError = error; // erro desta iteração torna-se o lastError da próxima
// Cálculo da Control Action
controlAction=(KP*error)+(KI*integralError)+(KD*derivadaError);
// Junção e formatação dos diferentes strings como output
String strCA=String(controlAction, 2);
int firstCommaIndex = strCA.indexOf('.');
strCA[firstCommaIndex]=',';
outPut+=strCA;
Serial.println(outPut);
previousTime=currentTime; // currentTime desta iteração torna-se o previousTime da próxima
delay(500);//Atraso de 500 milissegundos
currentTime = millis();
}
01-30-2022 11:03 PM
I guess that the answer might be "because we're supposed to do it this way", but is there a reason you can't do the calculations directly in LabVIEW?
The Arduino seems to be being used only for calculations based on string parsing (including, if I understand correctly, some code to convert commas to periods for decimal places - you could probably adjust what is sent from LabVIEW and then remove this code if you wanted), and then sending a string with the "control action" back to LabVIEW.
Maybe in the future you're going to use the Arduino to interact with some other hardware?
(I can't currently open your VI because it is newer than my installed LabVIEW version, but I'm planning on installing 2021 shortly. If you'd like a faster response, feel free to upload the VI "backsaved" - see Saving for a Previous Version .)