Simulink close loop System control with Arduino and serial

2 views (last 30 days)
I am trying to make a control system using Simulink and Arduino.
I am trying to install a system model in Simulink and a PID controller in Arduino like this pic
For this I made serial communication of Simulink and Arduino.
also Arduino code is this just for you check (I haven't designed a PID controller yet.)
double Setpoint, Input, Output;
float MyValue = 0;
typedef union{
float number;
uint8_t bytes[4];
} FLOATUNION_t;
FLOATUNION_t myValue;
void setup() {
Serial.begin(115200);
}
void loop(){
myValue.number = getFloat();
Input = double(myValue.number);
Send2Matlab();
}
void Send2Matlab(){
Serial.write('A');
for (int i=0; i<4; i++){
Serial.write(myValue.bytes[i]);
}
Serial.print('\n');
delay(100);
}
float getFloat(){
int cont = 0;
FLOATUNION_t f;
while (cont < 4 ){
f.bytes[cont] = Serial.read() ;
cont = cont +1;
}
return f.number;
}
When there is no system or PID controller, the serial communication works fine, although sometimes the communication is lost.
However, a problem arises when the signal from serial communication enters the transfer function or PID block.
To solve the problem, I tried various methods such as using the discrete transfer function or changing the solver by putting a constant such as 0 in the middle of the signal, but
Using the discrete transform outputs nan values, and the other method didn't work either.
Finally, I tried to use the PIL block supported by the embedded coder to solve this problem, but it failed too.
Please can someone tell me what is the problem?
Thank you for reading this long article.

Answers (0)

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!