cancel
Showing results for 
Search instead for 
Did you mean: 

Problems encounterd when trying to convert double to string

Bogdan
Senior
Posted on June 24, 2014 at 17:07

Hi,

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 http://mbed.org/users/aberk/code/IMUfilter_RPYExample/ 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... Thanks Why is it showing -nan ?

int main(){
SystemInit(); ///// Initialize System Clocks and PLLs 
SysTick_Init(); 
lcd_init(); // init display
mpu_I2Cinit(); 
delay_nms(100);
mpu_init();
delay_nms(100);
calibrateAccelerometer(); // take samples of acc then average
delay_nms(30); 
calibrateGyroscope(); // take samples of gyro, then avarage
delay_nms(30);
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;
NVIC_Init(&NVIC_InitStructure); 
// 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;
NVIC_Init(&NVIC_InitStructure); 
char troll[10];
char tpitch[10];
char tyaw[10];
while(1){ 
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'' {
#endif
//delay_nms(1);
void SysTick_Handler(void) {
TimeTick_Decrement();
}
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
imuFilter.computeEuler();
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } }
#ifdef __cplusplus
}
#endif

4 REPLIES 4
Posted on June 24, 2014 at 17:55

I'd perhaps use a larger string buffer, and ''%lf'' format string.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Bogdan
Senior
Posted on June 25, 2014 at 06:33

Hello Clive,

thanks for the tips, i tryied with %lf but the same result. Tryed even to declare the char buffer to 20 ( char droll[20]) and the problem is still there.

I was thinking maybe to read the data via usb on my pc.

Said so, today i tryied to install some usb libs from st cube, but using the ''hal libraries'' means i have to modify the whole project setup wich is a big headacke.

Could you point to some ''non hall'' usb libs?

Thanks

Bogdan
Senior
Posted on June 25, 2014 at 08:02

Found some usb libs/templates, and included them to my project,namely

#include ''usbd_cdc_core.h''<
br
>
#include ''usbd_usr.h''<
br
>
#include ''usbd_desc.h''

but the coplier says '' Fatal Error[Pe035]: #error directive: ''USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined\Desktop\discovery F4\IMU Project\libs\usb\usb_conf.h 164 ''
Bogdan
Senior
Posted on June 25, 2014 at 10:14

Manage to figure that one out, but is stil tricky setting up the library

When i try to complie, it says : Error[Li005]: no definition for ''USBD_Init(USB_OTG_handle *, USB_OTG_CORE_ID_TypeDef, _Device_TypeDef *, _Device_cb *, _USBD_USR_PROP *)'' Error[Li005]: no definition for ''USR_desc'' Error[Li005]: no definition for ''USBD_CDC_cb'' Error[Li005]: no definition for ''USR_cb'' Error while running Linker My code is this

#include ''includes.h''
#include ''stm32f4_discovery.h''
#include ''stm32f4_discovery.c''
#include ''delay.h''
#include ''usbd_core.h''
#include ''usbd_cdc_core.h''
#include ''usbd_usr.h''
#include ''usbd_desc.h''
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma data_alignment = 4 
#endif
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
__ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
__IO uint32_t TimingDelay;
TIM_TimeBaseInitTypeDef TIM_InitStruct;
//NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStruct;
int main(){
SystemInit(); ///// Initialize System Clocks and PLLs 
SysTick_Init(); 
__IO uint32_t i = 0;
/* USB configuration */
USBD_Init(&USB_OTG_dev, 
USB_OTG_FS_CORE_ID, 
&USR_desc, 
&USBD_CDC_cb, 
&USR_cb);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED5);
STM_EVAL_LEDInit(LED6);
while(1)
{
if (i++ == 0x100000)
{
STM_EVAL_LEDToggle(LED3);
STM_EVAL_LEDToggle(LED4);
STM_EVAL_LEDToggle(LED5);
STM_EVAL_LEDToggle(LED6);
i = 0;
}
}
}
#ifdef __cplusplus
extern ''C'' {
#endif
//delay_nms(1);
void SysTick_Handler(void) {
TimeTick_Decrement();
}
#ifdef __cplusplus
}
#endif