Hi Karol,
Thanks for sharing.
Your code looks weird...
- First, you didn't assign the enumeration values. For the MODE & FSR, it works, but not for the ODR
enum gyroODR {
// DR = CTRL_REG1[4:2]
GODR_800HZ = (0x00<<2),
GODR_400HZ = (0x01<<2),
GODR_200HZ = (0x02<<2),
GODR_100HZ = (0x03<<2),
GODR_50HZ = (0x04<<2),
GODR_12_5HZ = (0x05<<2),
GODR_6_25HZ = (0x06<<2),
GODR_1_56HZ = (0x07<<2),
};
enum gyroFSR {
// FR = CTRL_REG0[1:0]
GFS_2000DPS = 0x00,
GFS_1000DPS = 0x01,
GFS_500DPS = 0x02,
GFS_250DPS = 0x03,
};
enum gyroMODE {
// STANDBY/READY =CTRL_REG1[0] ; STANDBY/ACTIVE = CTRL_REG1[1]
MODE_READY = 0x01,
MODE_STANDBY = 0x00,
MODE_ACTIVE = 0x02,
};
- As I mentioned in an old comment, you have to use a timer to know how many time elapsed between two data collection
int main(void){
float sum = 0;
float timeElapsed = 0;
GyroInit();
GyroCalibrate()
while(1)
{
GyroReadData();
Timer_Stop();
timeElapsed = Timer_GetTime();
sum += GyroGetAngle(timeElapsed, raw.z, prev_z); //args: Ts, current, prev
Timer_Reset();
Timer_Start();
printf("%f\r\n", sum);
HAL_Delay(10);
}
}
- I suggest to remove the printf function in your loop because it take a lot a time and that's why your integration does not work... I have two examples to improve your solution:
uint8_t counter =0;
while(1)
{
GyroReadData();
Timer_Stop();
timeElapsed = Timer_GetTime();
sum += GyroGetAngle(timeElapsed, raw.z, prev_z); //args: Ts, current, prev
Timer_Reset();
Timer_Start();
if(counter%100 == 0)
printf("%f\r\n", sum);
HAL_Delay(10);
counter++
}
- The more efficient (but it takes a RTOS):
Mutex mux;
float sum;
void xTaskDataCollection()
{
while(1)
{
GyroReadData();
Timer_Stop();
timeElapsed = Timer_GetTime();
Mutex_Lock(mux);
sum += GyroGetAngle(timeElapsed, raw.z, prev_z); //args: Ts, current, prev
Mutex_Unlock(mux);
Timer_Reset();
Timer_Start();
if(counter%100 == 0)
HAL_Delay(10);
counter++
}
}
void xTaskDisplay()
{
float angle;
while(1)
{
Mutex_Lock(mux);
angle = sum;
Mutex_Unlock(mux);
printf("%f\r\n", angle);
HAL_Delay(1000);
}
}
As i mentioned before I read 100 samples in self-test mode and they all have the same value. Yours are changing so I think I should try another gyro
Try with the new code. But that's strange it does not work...
I need only Z axis data but since it is recommended to read all 3 axis I do it.
Z axis in enough.
It's a very good gryo but there is a one little catch. You can't read from both gyro and acc using SPI I was wondering why everyone use I2C rather than SPI
Yes you can use the same SPI bus for serveral sensors. That's why there is a chip select pin (/CS).
Anthony