Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 1 month ago.
Getc() and putc() 24 bit data
Hi everyone. My problem is: I have to receive data from a sensor on a Nucleo L152RE and to send that data to the PC. My sensor gives in output a datagram of 38 byte. The first byte is an identification-byte (0x93). Then I have 3 3-byte data; a 1-byte data; another 3 3-byte data; a 1-byte data; another 3 3-bytet data; a 1-byte data and so on. I've written the code below in which data are received and send 1 byte at time. When i make the acquisition with MatLab, i have a vector of uint8 data. In the sensor datasheet there are some formulas by which combining, for exemple, the 3 byte of one 3-byte data, it gives me back the proper value of the data in a specified measurement unit (see the figure below the code part). But, all that formulas in the datasheet are with plus (not minus) and the uint8 are positive, so i cannot detect sign variations. My question is: the sign problem is a parsing-part problem (i.e. MatLab problem)? Or i can resolve this problem with a proper software logic in mbed?
include the mbed library with this snippet
#include "mbed.h" RawSerial STIM300(PA_9, PA_10, 460800); RawSerial portaUSB(USBTX, USBRX, 460800); DigitalOut led(LED1); Timer t; uint8_t buffer[38]; uint8_t app; int main() { STIM300.format(8,SerialBase::None,1); portaUSB.format(8,SerialBase::None,1); while(1) { led = 1; do { app=STIM300.getc(); } while(app!=0x93); portaUSB.putc(app); for(int i=0; i<37; i++) { portaUSB.putc(STIM300.getc()); } led=0; }//end while 1 }//end main
1 Answer
6 years, 1 month ago.
Hello Nikolaos,
It can be done on either side. But if you concern about speed then I think a PC will do a floating point computation much faster than a NUCLEO-L152RE. Unfortunately, I do not have any experience with a STIM300 sensor but according to this driver the three bytes carying gyro's data actually represent a signed integer
:
struct sensor_value { signed int value:24; } __attribute__ ((__packed__));
Then using your formula and the code available for the driver above, gyro's data can be converted to a (signed) double
:
double convertGyro2AngularRate(const uint8_t* gyro_data) { double gyro_double = 0; uint8_t buffer[3]; double twopower14 = 16384; //2^14 struct sensor_value *gyro_value; if (gyro_data != NULL) { buffer[2] = gyro_data[0]; buffer[1] = gyro_data[1]; buffer[0] = gyro_data[2]; gyro_value = reinterpret_cast<struct sensor_value *>(buffer); gyro_double = (double)(gyro_value->value) / twopower14; } return gyro_double * (M_PI/180.00); //in rad/second }