0

I am using my arduino as a hardware-in-the-loop (HIL) simulation along with simulink. Right now what I am trying to is to serially transmit a matrix with decimal elements to my arduino, have the arduino perform some simple computations and return the decimal value.

This is the code that I am using for my arduino to receive the data:

#include <MatrixMath.h>

float incomingbyte;

void setup() 
{

Serial.begin(9600);

}

void loop() 
{

 if(Serial.available()>0){
 incomingbyte = Serial.read();
}

Serial.write(incomingbyte*2);
delay(1000);

}

Which always gives me the following error:

exit status 1
call of overloaded 'write(float)' is ambiguous

The simulink diagram that I have done up is as shown:

Simulink Diagram

I have noticed that only sending uint8 through the serial port works, I have tried not converting the numbers to uint8 before sending and basically nothing happens.

Are there any ways to transmit a matrix with decimal elements?

Thanks.

John
  • 155
  • 8

1 Answers1

1

If you want to send float in text format, you have to use Serial.print(float_var); and some delimiter like space or new line (by using println).

If you want to send float value in it's binary representation, you have to use Serial.write((byte*)&float_var, sizeof(float_var)); as write purpose is to send raw bytes.

I'm not using your float incomingbyte as it's quite misleading name.

KIIV
  • 3,534
  • 2
  • 18
  • 23
  • What does that line of code mean? To send the float value in it's binary form. My simulink converts the numbers to uint8 data types before sending it to my arduino so using the binrary representation would be better. Also, would that code work for matrices? – John Oct 11 '16 at 15:56
  • @John `&float_var` is address of float_var in memory (pointer to float), `(byte*)` means pointer to float is now casted to pointer to byte. And if you want to send whole float, you have to send more than one byte. That's why `sizeof(float_var)` is there. It gets size of float variable in Bytes. – KIIV Oct 11 '16 at 16:12
  • ah I see. I went to google up the Serial.write function. I guess basically I am supposed to use the format Serial.write(buf,len). Supposing I had the following matrix defined: float x[4][1] = { {1.233} , {2.334} , {3.445} , {5.446} }; If I wanted to send the matrix through Serial.write to simulink, would I just use `code` Serial.write((byte*)&x,sizeof(x)); `code` ? – John Oct 11 '16 at 16:18
  • I have tried your code using `Serial.write((byte*)&float_var, sizeof(float_var));` , but I am getting rubbish values on my simulink. Eg I sent a 1.22 and got back a 64, I am using a byte unpacking block that unpacks the bytes. But somehow it isn't working. – John Oct 12 '16 at 04:37
  • Well, it might be time to read the [manual](https://www.mathworks.com/help/instrument/serialreceive.html). By default it expects Bytes only! – KIIV Oct 12 '16 at 05:31
  • Yeah. Which is why I used the byte unpacker to unpack the bytes back to a double value. Am I missing something out? – John Oct 12 '16 at 05:35
  • The `float` is more like `single` in Matlab. Anyway, I don't have a Matlab, so I can't test it. But you have an answer to that error in Arduino sketch. – KIIV Oct 12 '16 at 05:45
  • Would it help to convert the received data to a single data type in simulink then? The arduino code error may have been solved but if I can't convert the data to anything useful in simulink then it's pointless. – John Oct 12 '16 at 09:28