Browse Source

default use int16 for reading data from sensors, float is optional

Juraj Ďuďák 2 years ago
parent
commit
688d021501
5 changed files with 127 additions and 82 deletions
  1. 4 0
      .gitlab-ci.yml
  2. 50 21
      readme.md
  3. 49 45
      src/icm20948.cpp
  4. 15 15
      src/icm20948.h
  5. 9 1
      src/icm_datatypes.h

+ 4 - 0
.gitlab-ci.yml

@@ -27,6 +27,10 @@ build_release:
         - (if [ -f changelog.md ]; then scp -r -P 90 ./changelog.md $DEST_IP:$DEST_PATH/$CI_PROJECT_NAME/ ; fi);
         - (if [ -f LICENSE ]; then scp -r -P 90 ./LICENSE $DEST_IP:$DEST_PATH/$CI_PROJECT_NAME/ ; fi);
 
+        # automatic update project
+        - RQ=$(curl --location --request PUT $APOLLO_API/$CI_PROJECT_NAME --header "apiKey:$APOLLO_KEY")
+        - curl --location $APOLLO_API/$CI_PROJECT_NAME/status --header "apiKey:$APOLLO_KEY" --header Content-Type:application/json --data "$RQ"
+
 #doxy_stage:
 #    stage: doxy_stage
 #    image: hrektts/doxygen

+ 50 - 21
readme.md

@@ -1,4 +1,5 @@
 
+
 # ICM 20948 driver for STM32
 
 Basic driver for TDK InvenSense ICM-20948. It support 2 modes:
@@ -6,45 +7,73 @@ Basic driver for TDK InvenSense ICM-20948. It support 2 modes:
 - pooling
 - interrupt
 
-# working modes
+## Public API
+
+Class `Icm20948`
+- `void Stop(void)` - stop the continuous measuring
+- `void Start(void)` - start the automatic/continuous measure
+- `void Sleep() / void Wakeup()`  put to sleep/wakeup sensor
+- `void Reset()` reinitialise sensor
+- `uint8_t GetDataStatus(void)` return 1, when data is ready to read
+- `bool IsReady()` get status information. The state can be changed by Start/Stop methods
+- `uint8_t IsSleep()` return 1, is device is in sleep mode
+- `void Read()` perform data read from device; from all sensors: accelerometer, gyroscope and Magnetometer (if is enabled)
+- `void Calibrate(*config)`  - perform calibration and put configuration (Low pass filter, sample rate, scale) fo sensors, if `config` is not NULL
+- `void SetInterruptSource(source)` Source can be: interrupt_disable, interrupt_DMP_INT1_EN, interrupt_PLL_RDY_EN, interrupt_WOM_INT_EN, interrupt_REG_WOF_EN, interrupt_RAW_DATA_0_RDY_EN
+- public members:
+-- `SensorAccel` - accelerometer
+-- `SensorGyro` - gyroscope
+-- `SensorMag` - magneometer
+
+Public methods in class for sensor representation `Sensor`:
+- `void SetScaleFactor(float sf)`
+- `void SetRange(range) / uint8_t GetRange()`
+- `void SetLowPassFilter(filter) / uint8_t GetLowPassFilter()`
+- `uint8_t SetSampleRate(value) / uint8_t GetSampleRate()`
+- `void Calibrate(void)`
+- `uint8_t GetDataStatus(void)`  return 1 whed data are ready to read
+- `axisesI *GetData(void)` - return pointer to measured data. Data have to be previously read by ICM20948::Read method
+- `bool Read(axisesI *)` peform local read from single sensor
+- `bool ReadUnit(axisesF*)`peform local read from single sensor, make recalculation to real values (acceleration, angular speed) as floats
+
+# Working modes
 
 ## Pooling
 ```c
-
+#include <stdlib.h>
 #include "icm20948.h"
 
-
 int main(void)
 {
   // HAL_Init, etc...
 
   SpiManager manager(&hspi2);
 
-  icm20948_Config config2;
-  config2.pinCS = &pinCS;
-  config2.gyro.low_pass_filter = GYRO_lpf_196_6Hz;
-  config2.gyro.sample_rate = GYRO_samplerate_281_3Hz;
-  config2.accel.low_pass_filter = ACCEL_lpf_246Hz;
-  config2.accel.sample_rate = ACCEL_samplerate_281_3Hz;
+  icm20948_Config config;
+  config.pinCS = &pinCS;
+  config.gyro.low_pass_filter = GYRO_lpf_196_6Hz;
+  config.gyro.sample_rate = GYRO_samplerate_281_3Hz;
+  config.accel.low_pass_filter = ACCEL_lpf_246Hz;
+  config.accel.sample_rate = ACCEL_samplerate_281_3Hz;
 
-  Icm20948 sensor2(&manager, &config2);
+  Icm20948 sensor(&manager, &config2);
 
-  axises *my_gyro2 = NULL;
-  axises *my_accel2 = NULL;
+ axisesF sensor_dataF;  // float values
+ axisesI sensor_dataI;  // int16 values
 
   while (1)
   {
     // read raw values
-    if(sensor2.GetDataStatus() == 1 ){
-      //read data
-      my_accel2 = sensor2.accSensor->Read();
-      my_gyro2 = sensor2.gyroSensor->Read();
-      my_mag2 = sensor2.magSensor->Read();
+    if(sensor.GetDataStatus() == 1 ){
+      //read raw data
+      sensor.accSensor->Read(&sensor_dataI);
+      sensor.gyroSensor->Read(&sensor_dataI);
+      sensor.magSensor->Read(&sensor_dataI);
 
       // or unit conversion
-      my_accel2 = sensor2.accSensor->ReadUnit();
-      my_gyro2 = sensor2.gyroSensor->ReadUnit();
-      my_mag2 = sensor2.magSensor->ReadUnit();
+      sensor.accSensor->ReadUnit(&sensor_dataF);
+      sensor.gyroSensor->ReadUnit(&sensor_dataF);
+      sensor.magSensor->ReadUnit(&sensor_dataF);
     }
 
   }
@@ -105,4 +134,4 @@ int main(void)
   }
 }
 
-```
+```

+ 49 - 45
src/icm20948.cpp

@@ -81,16 +81,19 @@ void Icm20948::Calibrate(icm20948_Config *config)
 	this->accSensor->Calibrate();
 	this->gyroSensor->Calibrate();
 
-	this->gyroSensor->SetLowPassFilter(config->gyro.low_pass_filter);
-	this->accSensor->SetLowPassFilter(config->accel.low_pass_filter);
+	if (config != NULL)
+	{
+		this->gyroSensor->SetLowPassFilter(config->gyro.low_pass_filter);
+		this->accSensor->SetLowPassFilter(config->accel.low_pass_filter);
 
-	this->gyroSensor->SetSampleRate(config->gyro.sample_rate);
-	this->accSensor->SetSampleRate(config->accel.sample_rate);
+		this->gyroSensor->SetSampleRate(config->gyro.sample_rate);
+		this->accSensor->SetSampleRate(config->accel.sample_rate);
 
-	this->accSensor->SetRange(config->accel.full_scale);
-	this->gyroSensor->SetRange(config->gyro.full_scale);
+		this->accSensor->SetRange(config->accel.full_scale);
+		this->gyroSensor->SetRange(config->gyro.full_scale);
 
-	this->ak09916_init(&config->mag);
+		this->ak09916_init(&config->mag);
+	}
 }
 
 bool Icm20948::icm20948_who_am_i()
@@ -331,7 +334,7 @@ uint8_t Sensor::GetDataStatus(void)
 	return new_val & 0x01;
 }
 
-bool Sensor::_read_without_scale_factor(axises* data)
+bool Sensor::_read_without_scale_factor(axisesI* data)
 {
 	return this->Read(data);
 }
@@ -365,26 +368,27 @@ uint8_t Sensor::GetRange(void){
 	return 0;
 }
 
-bool SensorGyro::Read(axises* data)
+//bool SensorGyro::Read(axisesI* data)
+bool SensorGyro::Read(axisesI* localData)
 {
 
 	uint8_t* temp = this->_spi->read_multiple_reg(this->_activeDevice, ub_0, B0_GYRO_XOUT_H, 6);
 
-	data->x = (int16_t)(temp[0] << 8 | temp[1]);
-	data->y = (int16_t)(temp[2] << 8 | temp[3]);
-	data->z = (int16_t)(temp[4] << 8 | temp[5]);
+	localData->x = (int16_t)(temp[0] << 8 | temp[1]);
+	localData->y = (int16_t)(temp[2] << 8 | temp[3]);
+	localData->z = (int16_t)(temp[4] << 8 | temp[5]);
 
 	return true;
 }
 
 
-bool SensorAccel::Read(axises* data)
+bool SensorAccel::Read(axisesI* localData)
 {
 	uint8_t* temp = this->_spi->read_multiple_reg(this->_activeDevice, ub_0, B0_ACCEL_XOUT_H, 6);
 
-	data->x = (int16_t)(temp[0] << 8 | temp[1]);
-	data->y = (int16_t)(temp[2] << 8 | temp[3]);
-	data->z = (int16_t)(temp[4] << 8 | temp[5]) + this->_scaleFactor;
+	localData->x = (int16_t)(temp[0] << 8 | temp[1]);
+	localData->y = (int16_t)(temp[2] << 8 | temp[3]);
+	localData->z = (int16_t)(temp[4] << 8 | temp[5]) + this->_scaleFactor;
 	// Add scale factor because calibraiton function offset gravity acceleration.
 
 	return true;
@@ -393,20 +397,20 @@ bool SensorAccel::Read(axises* data)
 /**
  * Do not use this function to read final data. It is only for calibration of sensor.
  */
-bool SensorAccel::_read_without_scale_factor(axises* data)
+bool SensorAccel::_read_without_scale_factor(axisesI* localData)
 {
 	uint8_t* temp = this->_spi->read_multiple_reg(this->_activeDevice, ub_0, B0_ACCEL_XOUT_H, 6);
 
-	data->x = (int16_t)(temp[0] << 8 | temp[1]);
-	data->y = (int16_t)(temp[2] << 8 | temp[3]);
-	data->z = (int16_t)(temp[4] << 8 | temp[5]);
+	localData->x = (int16_t)(temp[0] << 8 | temp[1]);
+	localData->y = (int16_t)(temp[2] << 8 | temp[3]);
+	localData->z = (int16_t)(temp[4] << 8 | temp[5]);
 	// Add scale factor because calibraiton function offset gravity acceleration.
 
 	return true;
 }
 
 
-bool SensorMag::Read(axises* data)
+bool SensorMag::Read(axisesI* localData)
 {
 	uint8_t* temp;
 	uint8_t drdy;	// data ready
@@ -420,60 +424,60 @@ bool SensorMag::Read(axises* data)
 	hofl = this->_spi->read_single_external_reg(this->_activeDevice, MAG_ST2) & 0x08;
 	if(hofl)	return false;
 
-	data->x = (int16_t)(temp[1] << 8 | temp[0]);
-	data->y = (int16_t)(temp[3] << 8 | temp[2]);
-	data->z = (int16_t)(temp[5] << 8 | temp[4]);
+	localData->x = (int16_t)(temp[1] << 8 | temp[0]);
+	localData->y = (int16_t)(temp[3] << 8 | temp[2]);
+	localData->z = (int16_t)(temp[5] << 8 | temp[4]);
 
 	return true;
 }
 
-bool SensorGyro::ReadUnit(axises* data)
+bool SensorGyro::ReadUnit(axisesF *result)
 {
-	this->Read(data);
+	this->Read(&this->data);
 
-	data->x /= this->_scaleFactor;
-	data->y /= this->_scaleFactor;
-	data->z /= this->_scaleFactor;
+	result->x = this->data.x / this->_scaleFactor;
+	result->y = this->data.y / this->_scaleFactor;
+	result->z = this->data.z / this->_scaleFactor;
 
 	return true;
 }
 
-bool SensorAccel::ReadUnit(axises* data)
+bool SensorAccel::ReadUnit(axisesF *result)
 {
-	this->Read(data);
+	this->Read(&this->data);
 
-	data->x /= this->_scaleFactor;
-	data->y /= this->_scaleFactor;
-	data->z /= this->_scaleFactor;
+	result->x = this->data.x / this->_scaleFactor;
+	result->y = this->data.y / this->_scaleFactor;
+	result->z = this->data.z / this->_scaleFactor;
 
 	return true;
 }
 
-bool SensorMag::ReadUnit(axises* data)
+bool SensorMag::ReadUnit(axisesF *result)
 {
-	axises temp;
-	bool new_data = this->Read(&temp);
+	bool new_data = this->Read(&this->data);
 	if(!new_data)	return false;
 
-	data->x = (float)(temp.x * 0.15);
-	data->y = (float)(temp.y * 0.15);
-	data->z = (float)(temp.z * 0.15);
+	result->x = (float)(data.x * 0.15);
+	result->y = (float)(data.y * 0.15);
+	result->z = (float)(data.z * 0.15);
 
 	return true;
 }
 
 
-axises *SensorAccel::GetData()
+axisesI *SensorAccel::GetData()
 {
 	return &this->data;
 }
 
-axises *SensorGyro::GetData()
+
+axisesI *SensorGyro::GetData()
 {
 	return &this->data;
 }
 
-axises *SensorMag::GetData()
+axisesI *SensorMag::GetData()
 {
 	return &this->data;
 }
@@ -704,7 +708,7 @@ uint8_t SensorGyro::GetRange(void){
 
 void SensorGyro::Calibrate(void)
 {
-	axises temp;
+	axisesI temp;
 	int32_t gyro_bias[3] = {0};
 
 	for(int i = 0; i < 100; i++)
@@ -749,7 +753,7 @@ void SensorGyro::SaveOffset(void)
 
 void SensorAccel::Calibrate()
 {
-	axises temp;
+	axisesI temp;
 	uint8_t* temp2;
 	uint8_t* temp3;
 	uint8_t* temp4;

+ 15 - 15
src/icm20948.h

@@ -23,7 +23,7 @@ protected:
 	SpiManager *_spi;
 public:
 	float _scaleFactor;
-	axises data;
+	axisesI data;
 	int16_t offset_x;
 	int16_t offset_y;
 	int16_t offset_z;
@@ -39,29 +39,29 @@ public:
 	uint8_t CheckLowPassInputValue(uint8_t);
 	void Calibrate(void);
 	uint8_t GetDataStatus(void);
-	bool _read_without_scale_factor(axises* data);
+	bool _read_without_scale_factor(axisesI* data);
 	void SaveOffset(void);
 
 
-	virtual axises *GetData(void) = 0;
-	virtual bool Read(axises *) = 0;
-	virtual bool ReadUnit(axises *) = 0;
+	virtual axisesI *GetData(void) = 0;
+	virtual bool Read(axisesI *) = 0;
+	virtual bool ReadUnit(axisesF *)= 0;
 
 };
 
 class SensorAccel : public Sensor {
 public:
 	SensorAccel(SpiManager *spi, int8_t _activeDevice);
-	axises *GetData(void);
-	bool Read(axises *);
-	bool ReadUnit(axises *);
+	axisesI *GetData(void);
+	bool Read(axisesI *);
+	bool ReadUnit(axisesF*);
 
 	void SetRange(accel_full_scale r);
 	void SetLowPassFilter(accel_dlp_cfg config);
 	uint8_t SetSampleRate(accel_samplerate divider);
 	uint8_t CheckLowPassInputValue(uint8_t);
 	void Calibrate(void);
-	bool _read_without_scale_factor(axises* data);
+	bool _read_without_scale_factor(axisesI* data);
 	void SaveOffset(void);
 
 	uint8_t GetLowPassFilter(void);
@@ -73,9 +73,9 @@ public:
 class SensorGyro: public Sensor {
 public:
 	SensorGyro(SpiManager *spi, int8_t _activeDevice);
-	axises *GetData(void);
-	bool Read(axises *);
-	bool ReadUnit(axises *);
+	axisesI *GetData(void);
+	bool Read(axisesI *);
+	bool ReadUnit(axisesF*);
 
 	void SetRange(gyro_full_scale r);
 	void SetLowPassFilter(gyro_dlp_cfg config);
@@ -92,9 +92,9 @@ public:
 class SensorMag: public Sensor {
 public:
 	SensorMag(SpiManager *spi, int8_t _activeDevice);
-	axises *GetData(void);
-	bool Read(axises *);
-	bool ReadUnit(axises *);
+	axisesI *GetData(void);
+	bool Read(axisesI *);
+	bool ReadUnit(axisesF*);
 };
 
 

+ 9 - 1
src/icm_datatypes.h

@@ -29,7 +29,15 @@ typedef struct
 	float x;
 	float y;
 	float z;
-} axises;
+} axisesF;
+
+typedef struct
+{
+	uint8_t type;
+	int16_t x;
+	int16_t y;
+	int16_t z;
+} axisesI;
 
 
 typedef enum