Sending information from Simulink to Arduino over Serial
7 次查看(过去 30 天)
显示 更早的评论
I am writing in this group to seek some help with sending PWM signal to Arudino over serial via Simulink
Problem description :
I am working on a personal project which involves building closed loop PID control loop, where I need to send PWM signals to fan, in order to control the position of a ball at a fixed height. The feedback signal is generated via a USB camera, which detects the position of the ball.
Since Arduino can't process the image, I am running my model in Simulink and sending the signal via serial to Arduino. But, the signal somehow doesn't reach or gets processed by the board.
Here is a step by step information on the process I followed to test debug
1. I upload a serial recieve model on the Arduino. It probes the serial port for the data. Once data is found, it is routed to the pin 9 as shown in the image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159843/image.jpeg)
2. In the simulink environment, to debug, I configured a serial send port (from instrument control toolbox) to transfer the desired PWM signal. However, this does not work. I tried debugging the pin 9, but no Voltage signal was received.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159845/image.jpeg)
3. In the model above, I added a serial recieve block from the instrument control toolbox. This somehow slowed down the simulation. Each time step was being executed with some delay, but the PWM signals were getting transferred. The blower speed could be changed in accordance with the PWM signal. However this method is too slow for my control loop to work with
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159846/image.jpeg)
Can someone please help me point out the possible error that I am making. I am very curious to find a workaround
Masoom
2 个评论
Jordan Ross
2017-1-23
Hello Masoom,
One possible issue could be in the enabled subsystem. A good thing to test would be to have a "Repeating Sequence Stair" block send to the ICT "Serial Send" block in your host model. Then on your Arduino, just have "Serial Receive" block send to the LED (Pin 13). This way you can test that you see the LED blinking on the Arduino. If the LED does blink then this gives a good indication that something is not working as expected with your enabled subsystem.
回答(4 个)
Güven Seçkin
2018-4-26
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187930/image.jpeg)
Firts part arduino to simulink
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187932/image.png)
header and terminator means only /n.../n this types values can read by serial recieve Data size[1 2] 2 value can read if you want 3 you adjust [1 3]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187934/image.jpeg)
Serial configuration baund rate must be same arduino's baund rate.
Arduino side
if true
typedef union
{
float number[2];
uint8_t bytes[8];
} myFoo;
myFoo foo;
foo.number[0]=dataToSend;
foo.number[1]=dataToSend;
Serial.print('\n');
for (int i=0; i<8; i++)
{
Serial.write(foo.bytes[i]);
}
Serial.print('\n');
end
Serial print values must same terminator and header.Only datas between /n can read by simulink.
0 个评论
Güven Seçkin
2018-4-26
Second part simulink to arduino
Thanks to serial send you send datas to arduino but you have to send uint type.You can use data type conversion block in simulink.And you have to transform your data discreate.You can translate you data use Zero-order hold block in simulink.For example you want send 3 values to arduno.First translate these data to uint as ı said send put all datas in mux block so you have one line.Than put this line to zero order hold block finaly you can connect end of line to serial send you dont have some adjustment in serial send block.
Arduino codes. Thanks to these code ı get 4 int number from simulink
//Global Side
typedef union {
int num[3];
uint8_t bytes[3];
} foo;
foo myFoo;
typedef union {
int num[3];
uint8_t bytes[3];
} foo1;
foo1 myFoo1;
typedef union {
int num[3];
uint8_t bytes[3];
} foo2;
foo2 myFoo2;
typedef union {
int num[3];
uint8_t bytes[3];
} foo3;
foo3 myFoo3;
uint8_t input[2];
uint8_t inputBufer[2];
uint8_t inputBuf[2];
uint8_t inputBu[2];
//////////////////////////////////////////////////////////////////////
//MAIN FUNCTION
void getDatasFromSimulink() {
if(Serial.available()>0){
Serial.readBytes(inputBufer,1);
myFoo.bytes[0]=inputBufer[0];
Serial.readBytes(input,1);
myFoo1.bytes[0]=input[0];
Serial.readBytes(inputBuf,1);
myFoo2.bytes[0]=inputBuf[0];
Serial.readBytes(inputBu,1);
myFoo3.bytes[0]=inputBu[0];
}
int value1=myFoo.num[0];
int value2=myFoo1.num[0];
int value3=myFoo2.num[0];
int value4=myFoo3.num[0];
}
}
1 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!