'How to store continuous data(serial data from a sensor) in an array and later(after 100ms) delete it for for other set of data?
I am working on a project in which I have use lidar sensors and Arduino Board to get continuous inputs(400Hz) of distance. But the catch is that, how can I store the values of; lets say the Distance1 and Distance2 with a difference of 50ms(Data input is around 400Hz) from the sensor and then later delete it for the other set of data?
Here is a piece of code I am working with-
int16_t tfDist = 0; // Distance to object in centimeters
int16_t tfFlux = 0; // Signal strength or quality of return signal
int16_t tfTemp = 0; // Internal temperature of Lidar sensor chip
int16_t tfaddr = 18;
int16_t tfDist2 = 0; // Distance to object in centimeters
int16_t tfFlux2 = 0; // Signal strength or quality of return signal
int16_t tfTemp2 = 0; // Internal temperature of Lidar sensor chip
int16_t tfaddr2 = 17;
int16_t tfDist3 = 0; // Distance to object in centimeters
int16_t tfFlux3 = 0; // Signal strength or quality of return signal
int16_t tfTemp3 = 0; // Internal temperature of Lidar sensor chip
int16_t tfaddr3 = 16;
// = = = = = = = = = = MAIN LOOP = = = = = = = = = =
void loop()
{
tfmP.getData( tfDist, tfFlux, tfTemp, tfaddr); // Get a frame of data
if( tfmP.status == TFMP_READY) // If no error...
{
printf( "Dist 1:%05icm ", tfDist); // display distance,
printf( "Flux 1:%05i ", tfFlux); // display signal strength/quality,
printf( "Temp 1:%2i%s", tfTemp, "°C" ); // display temperature,
printf( "Speed 1:%03i",tspeed);
printf( " "); // end-of-line.
}
else
{
tfmP.printFrame(); // Display error and data frame
if( tfmP.status == TFMP_I2CWRITE) // If I2C error...
{
tfmP.recoverI2CBus(); // recover hung bus.
}
}
delay(0); // Run loop at approximately 20Hz.
if(tfDist<100)
{
printf("Brakes!!!!");
}
And some codes of the header file to help understand better -
//
bool TFMPI2C::getData( int16_t &dist, int16_t &flux, int16_t &temp, uint8_t addr)
{
// `frame` data array is declared in TFMPI2C.h
status = TFMP_READY; // clear status of any error condition
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Step 0 - Command device to ready distance data in centimeters
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// The device can also return data in millimeters, but its
// resolution is only 5mm (o.5cm) and its accuracy is ±5cm.
if( sendCommand( I2C_FORMAT_CM, 0, addr) != true) return false;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Step 1 - Get data from the device.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Request one data-frame from the slave device address
// and close the I2C interface.
Wire.requestFrom( (int)addr, TFMP_FRAME_SIZE, 1);
memset( frame, 0, sizeof( frame)); // Clear the data-frame buffer.
for( uint8_t i = 0; i < TFMP_FRAME_SIZE; i++)
{
if( Wire.peek() == -1) // If there is no next byte...
{
status = TFMP_I2CREAD; // then set error...
return false; // and return "false."
}
else frame[ i] = uint8_t( Wire.read());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Step 2 - Perform a checksum test.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Clear the `chkSum` variable declared in `TFMPI2C.h`
chkSum = 0;
// Add together all bytes but the last.
for( uint8_t i = 0; i < ( TFMP_FRAME_SIZE - 1); i++) chkSum += frame[ i];
// If the low order byte does not equal the last byte...
if( ( uint8_t)chkSum != frame[ TFMP_FRAME_SIZE - 1])
{
status = TFMP_CHECKSUM; // then set error...
return false; // and return "false."
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Step 3 - Interpret the frame data
// and if okay, then go home
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dist = frame[ 2] + ( frame[ 3] << 8);
flux = frame[ 4] + ( frame[ 5] << 8);
temp = frame[ 6] + ( frame[ 7] << 8);
// Convert temp code to degrees Celsius.
temp = ( temp >> 3) - 256;
// Convert Celsius to degrees Fahrenheit
// temp = uint8_t( temp * 9 / 5) + 32;
// - - Evaluate Abnormal Data Values - -
// Values are from the TFMini-S Product Manual
// Signal strength <= 100
if( dist == -1) status = TFMP_WEAK;
// Signal Strength saturation
else if( flux == -1) status = TFMP_STRONG;
// Ambient Light saturation
else if( dist == -4) status = TFMP_FLOOD;
// Data is apparently okay
else status = TFMP_READY;
if( status != TFMP_READY) return false;
else return true;
}
// Pass back data using default I2C address.
bool TFMPI2C::getData( int16_t &dist, int16_t &flux, int16_t &temp)
{
return getData( dist, flux, temp, TFMP_DEFAULT_ADDRESS);
}
// Pass back only distance data using given I2C address.
bool TFMPI2C::getData( int16_t &dist, uint8_t addr)
{
static int16_t flux, temp;
return getData( dist, flux, temp, addr);
}
// Pass back only distance data using default I2C address.
bool TFMPI2C::getData( int16_t &dist)
{
static int16_t flux, temp;
return getData( dist, flux, temp, TFMP_DEFAULT_ADDRESS);
}
//
// - - - - - - End of Get a Frame of Data - - - - - - - - - -```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
