2016-12-21 01:00 PM
i have question about accelerometer of stm32f4 LIS302dl version
why i get wr
ong data of pitch and roll angle ?
mySPI_SendData(0x20,0xC7); //LIS302D Config
acc_pitch = atan2(acc_x, -acc_z)*180/M_PI;
acc_roll = -atan2(acc_y, -acc_z)*180/M_PI;uint8_t mySPI_GetData(uint8_t adress)
{
GPIO_ResetBits(GPIOE, GPIO_Pin_3);
adress = 0x80 | adress;
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //transmit buffer empty?
SPI_I2S_SendData(SPI1, adress);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
SPI_I2S_ReceiveData(SPI1); //Clear RXNE bit
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //transmit buffer empty?
SPI_I2S_SendData(SPI1, 0x00); //Dummy byte to generate clock
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
GPIO_SetBits(GPIOE, GPIO_Pin_3);
return SPI_I2S_ReceiveData(SPI1); //return reveiced data
}
void mySPI_SendData(uint8_t adress, uint8_t data){
GPIO_ResetBits(GPIOE, GPIO_Pin_3);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //transmit buffer empty?
SPI_I2S_SendData(SPI1, adress);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
SPI_I2S_ReceiveData(SPI1);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //transmit buffer empty?
SPI_I2S_SendData(SPI1, data);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
SPI_I2S_ReceiveData(SPI1);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
}
void mySPI_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitTypeDef SPI_InitTypeDefStruct;
SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // SPI frequency is APB2 frequency / 2
SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft; // set the NSS management to internal and pull internal NSS high
SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB; // data is transmitted MSB first
SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High; // clock is low when idle
SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_2Edge; // data sampled at first edge
//SPI_InitTypeDefStruct.SPI_CRCPolynomial
SPI_Init(SPI1, &SPI_InitTypeDefStruct);//Initializes function according to above
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE , ENABLE);
GPIO_InitTypeDef GPIO_InitTypeDefStruct;
/* configure pins used by SPI1
* PA5 = SCK
* PA6 = MISO
* PA7 = MOSI
*/
GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_6;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);//Initializes function according to above
// Configure the chip select pin
GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOE, &GPIO_InitTypeDefStruct);//Initializes function according to above
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
SPI_Cmd(SPI1, ENABLE);
}
void InitializeLEDs()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef GPIO_InitTypeDefStruct;
/*Configure GPIO pins : LED1_Pin LED2_Pin LED3_Pin LED4_Pin */
GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_12 |GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_OUT;
//GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitTypeDefStruct);
}
2016-12-21 10:47 PM
why i get wr
ong data of pitch and roll angle ?
mySPI_SendData(0x20,0xC7); //LIS302D Config
acc_pitch = atan2(acc_x, -acc_z)*180/M_PI;
acc_roll = -atan2(acc_y, -acc_z)*180/M_PI;I might guess it has to do with parameters and data types.
The
atan2
function, like most other trigonometriclibm
functions, take double parameters which are expected to be radians. But theacc_x, acc_y and acc_z
are
unsigned short
, aren't they ?BTW, you can verify such calculations and algorithms easily on a PC, using a generic toolchain + debugger. I would even recommend it.