AnsweredAssumed Answered

Problems encounterd when trying to convert double to string

Question asked by anton.bogdan on Jun 24, 2014
Latest reply on Jun 25, 2014 by anton.bogdan
i recently started a school project regarding orientation using MEMS sensors.
I own a MPU6050, and a fusion alrogithm already known developed by Sebastian Madgwick.

Here is my code, and i will explain .

i tryied  do a translation from here  to IAR 6.30, and tryied to show the angles on a plain 20x4 LCD

Setup 2 timers interupts, one at 200hz, and the other at 10hz
Sensors are sampled at 200hz, and the filter is sampled at 10hz.
The problem apears when i try to convert the values to strings,
for example
 sprint(tyaw,"%f", dyaw);
it returns me -nan, all 3 strings returns -nan on display.
I cant understand why?
Tryied even to declare dyaw as float, but the same -nan apears.
Also try do integrate a "Display function" wich do all the string conversions, and include`it in the imufilter clas, and the problems is stil there

 Is there a problem in my timer routines? i cant understand why.
The comunication with MPU is ok, saw`it with a logic analizyer, and is there...
Why is it showing -nan ?

int main(){
SystemInit(); ///// Initialize System Clocks and PLLs 
lcd_init(); // init display
calibrateAccelerometer(); // take samples of acc then average
calibrateGyroscope();  // take samples of gyro, then avarage
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
 // timer 2 setup to generate every 10ms ( 10hz) interrupt
   TIM_InitStruct.TIM_Prescaler = 2000 - 1; 
   TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_InitStruct.TIM_Period = 4200 - 1; //105          
   TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;    
   TIM_InitStruct.TIM_RepetitionCounter = 0;             
   TIM_TimeBaseInit(TIM2, &TIM_InitStruct);
   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
   TIM_Cmd(TIM2, ENABLE);    
// timer 3 setup to generate every 2.5ms ( 200hz) interrupts
   TIM_InitStruct.TIM_Prescaler = 2000 - 1;
   TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_InitStruct.TIM_Period = 210 - 1;                 
   TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;    
   TIM_InitStruct.TIM_RepetitionCounter = 0;             
   TIM_TimeBaseInit(TIM3, &TIM_InitStruct);
   TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
   TIM_Cmd(TIM3, ENABLE);    
   // interrupt priority for timer 2
   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   // interrupt priority for timer 3
   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
char troll[10];
char tpitch[10];
char tyaw[10];
 double droll  = toDegrees(imuFilter.getRoll());   // read roll after euler computation
 double dpitch = toDegrees(imuFilter.getPitch()); // read  pitch after euler computation
 double dyaw   = toDegrees(imuFilter.getYaw());  // read yaw after euler computation
 sprintf(troll,"%f", droll);  // convert to string
 sprintf(tpitch,"%f", dpitch); // convert to  string
 sprintf(tyaw,"%f", dyaw);  // convert to string
 lcd_char(1,1,troll);  // display troll angle
 lcd_char(2,1,tpitch); // display pitch angle
 lcd_char(3,1,tyaw);  // display yaw angle
#ifdef __cplusplus
extern "C" {
  void SysTick_Handler(void) {
void TIM3_IRQHandler(void){ // 200hz
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  sampleAccelerometer(); // sample accelerometor sensors (x,y,z)
  sampleGyroscope(); // sample gyroscopes sensors (x,y,z)
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  } }
void TIM2_IRQHandler(void){ // 10hz
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  //Update the filter variables.
    imuFilter.updateFilter(w_y, w_x, w_z, a_y, a_x, a_z); // updateing filter with values from gyro & acc sampled @ timer 3 rate
    //Calculate the new Euler angles. from updatefilter equations
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);  }   }
#ifdef __cplusplus