2022-11-20 01:47 PM
I am reading data from a magnetic field sensor and storing it in a variable of type float. But what concerns is that I can't see the negative measurements on the debug window. When I store it in a int16_t variable the negative measurements show up. How could I watch the negative float values as well ?
Solved! Go to Solution.
2022-11-20 07:26 PM
Can you see the right sign of a float you directly assign with a constant?
If so perhaps look at how you're converting or casting.
2022-11-20 07:26 PM
Can you see the right sign of a float you directly assign with a constant?
If so perhaps look at how you're converting or casting.
2022-11-21 07:24 AM
Hello BKola.2241,
I would like to thank you for proposing this issue.
Can you please inform me about the exact type of debug window if it was expression view, live expression or something else...?
Also, can you check if you have chosen somewhere casting in your code or the option <Cast To Type> inside your view as shown the figure bellow.
Finally, to know the right solution of this stuff: I would like to see a screenshot of the different values displayed on your debug window! "You can't see the negative measurements on the debug window", so what do you see exactly: a positive value or some other errors?
Wijeden;
2022-11-21 03:11 PM
It is from the live expression window.At this moment they should go under 0. But they get stuck somewhere around(above) 0.
Just to clarify, each cell of mag_fi_meas is a combination of two 8-bits words, build up through (raw_data[1]<<8 | raw_data[0]); (raw_data[2]<<8 | raw_data[3]); and (raw_data[4]<<8 | raw_data[5]); where the MSB of raw_data[1], raw_data[3] and raw_data[5] is the sign bit (data are stored in the registers using 2's complement)
2022-11-21 03:15 PM
I type casted my variable to int16_t before I store it into a float variable and I could see the negative values!
2022-11-21 03:19 PM
But I'm still trying to figure out wether the data I read is correct or not. I'm trying to store a 16-bit data in 2's complement in a float variable. I'm looking for the right type cast that will consider correctly the 2's complement.
2022-11-21 03:34 PM
I thought I posed a rather simple question.
What does this look like when inspected?
float foo = -3.0f;
2022-11-21 03:59 PM
@Community member -3 shows up on debug window. Thank you for the support
2022-11-21 04:06 PM
@Wijeden RHIMI the issue was solved. Thank you for your effort
2022-11-21 05:20 PM
What's the problem of just reading the int16_t data and converting (assigning) it normally to float?
int16_t raw_data[3];
float mag_fi_meas[3];
SensorRead(&raw_data, sizeof(raw_data));
for (size_t i = 0; i < 3; ++i) {
mag_fi_meas[i] = raw_data[i];
}