h1

Problem with Serial Interfacing to Simulink

July 19, 2008

I’m trying to do a HIL(hardware-in-the-loop) simulation with ATmega8535, as a digital controller and a virtual plant that implemented in Matlab Simulink. The AVR and Simulink are communicating using serial port. I used the Instrument Control Toolbox in Matlab to send and receive binary data via serial port to the AVR. I also include the direct simulation, without using the Instrument Control Toolbox, to compare both the results.

First, I tried to send a constant number (double type) from computer to AVR. Inside the microcontroller (uC), there is a program that will multiply the input with a gain (in this case the gain is 5). After being multiplied, the result is being send back to the computer. It worked, both of the results were same.

Then I added a transfer function block (in this case, 1/(s² + 2s + 1) ) and a step signal for the reference input. This experiment also worked, the direct simulation and HIL simulation showed same result. The problem occured when I added a negative feedback to the system. The results between HIL simulation and direct simulation were not same. Picture below is the result. The output signal using HIL simulation (yellow) doesn’t match with using direct simulation (red).

I tried to post this problem on Avrfreaks Forum and someone named ‘buffi‘ gives me advices about his problem. Below is some of his advices:

The following happens: During the short period when e > 0.5, simulink sends 1 to the controller. This shows up in the terminal as 1’s being sent from the beginning of the step input to about 0.6 seconds after the rise. After that, the error is < 0.5 and zeros are sent to the controller. Negative errors are sent as zero. The plant will (in your case) now receive zeros from the controller: 0 * 5 = 0. The plant output goes down to zero, but when it crosses the 0.5 line, the error is greater than 0.5 again, so the controller receives a 1 and outputs 5 to the plant again. This causes your plant output to oscillate. You can do several things now. First, rescale the output from simulink to your controller by k and from the controller to simulink by 1/k, k > 0 to get a higher resolution than just 0 and 1. Second, use 32-bit float in “To Instrument” and figure out how to store it for your C code.

Thanks for the advices. Trying to implemented them now.

Advertisements

2 comments

  1. Hello,

    I have got nearly the same task with an Atmega168. But ich have some problems interfacing the serialport with Matlab simulink. I am wether able to send data or receive them.
    I am very sorry for asking, but is it possible to have a look on your simulink file and perhabs also the source code (if you are programming in C) for the Atmega?

    Thx a lot
    Thorsten


  2. @ Thorsten:
    What’s the simulink block you used to connect to serial?
    For my case, I used the “Query Instrument” and “To Instrument” block. It’s in the “Instrument Control Toolbox” section.

    You can find my source code and pictures of the simulink model in:
    http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=66499

    Hope that will help you.
    Danke.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: