Go Down

Topic: Interfacing w/ OpenModelica via serial (Read 701 times) previous topic - next topic

SidA

So I am using the Modelica device driver (MDD) library to read and write data from an arduino via the serial port. As a toy example, I am serial writing a sine wave from OpenModelica to the arduino and sending it back to OpenModelica. However, at points, the value read comes to be 0, leading to distorted plots. I have provided the simple firmware I am using for this test, and in the images, the model and the output. As you will see in the code, I have provided a delay of 5 ms. If I lower the delay, the plot distortions get denser. Ideally, I am looking for a way to do away w/ the delays.

Also, when the input is taken from a function generator connected to the arduino, no distortion is seen. This leads me to believe that the problem is w/ the serial writing from OM.

Code:
int Output;
void setup()
{
Serial.begin(115200);
//pinMode(A5, INPUT);
}

void loop()
{
 
      Output=Serial.read();
      Serial.write(Output);
      delay(5);

   
     
//    Serial.write(((int)Output>>8)&0xFF);
//  delay(5);
}

Robin2

Your code for reading the serial data is not at all robust. You don't even check to see if there is data available to read.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SidA

Right, you are and I did modify it to include an availability condition. However, the output is much the same.

Robin2

#3
Nov 28, 2017, 01:50 pm Last Edit: Nov 28, 2017, 01:51 pm by Robin2
Right, you are and I did modify it to include an availability condition. However, the output is much the same.
How about posting your updated code so we can see what you actually did?

A sample of the output together with an example of what it should be would also help us to help you.



And, out of curiosity, what is "OpenModelica"

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SidA

This is my code:


int Output;
void setup()
{
  Serial.begin(115200);

}

void loop()
{
   while (Serial.available()> 0)
  {
   
  Output = Serial.read();
  Serial.write(Output);
  delay(5);
  }
}

The 1st image is the output using this.

The 2nd image is the output on removing the delay.

The 3rd image is how the output should ideally be.

OpenModelica (OM) is a mathematical modelling software, something comparable to matlab but, in layman's terms, more suited to equation based models. What I am trying to do in the long run is to process data on OM  and the arduino in a closed loop.


Robin2

Please don't post picture of text output. Just copy and paste the text.

And you don't seem to have studied my examples.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SidA

It isn't text output. I obviously would not use a screenshot for that. It's a plot, as you will find on opening the images. And I did go through your examples. I used separate functions previously and set the appropriate conditions, but since that gave the same output as the second image, I stuck to this for ease of debugging.

PaulS

Quote
Output = Serial.read();
Output == input? No.
The art of getting good answers lies in asking good questions.

Robin2

Images from Reply #4 so we don't have to download them. See this  Image Guide







...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

If this was my problem I would look at the raw data rather than the graph to figure out what is happening. Just output the data to the Serial Monitor. (That is why I had assumed your images were of text).

My guess is that you are receiving values that you don't allow for - such as a linefeed or carriage return, or a separator character between values.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up