PSoC calculation returns nan(0x8000000000000). What means nan? pleas Help me

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

#include <project.h>
#include <ADXL345.h>
#include "math.h"

   

int main()
{
    int  st_speed=30; // m/s
     int x_axis = 10; // in  meter
    
    float g = 9.807; // in  m/s^2
   

   

void ADXL_Init();
    CyGlobalIntEnable; /* Enable global interrupts. */
  I2C_ADXL_Start(); 

   

 

   

calc_Angle_x = (1/2*asin(x_axis*g/st_speed*st_speed))*((180.0)/3.14);

   

for(;;)
    {   

   

why does calc_Angle_x return nan(0x8000000000000) and what nan means?

   

It supposed to return 3.129462438 degree

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

NAN means "Not a Number" and indicates the result of an invalid operation as division by zero. asin acceps an argument between 1 and -1. In your case the argument is 10 which is not allowed. Try to use parentheses.

   

 

   

Bob

View solution in original post

0 Likes
16 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

NAN means "Not a Number" and indicates the result of an invalid operation as division by zero. asin acceps an argument between 1 and -1. In your case the argument is 10 which is not allowed. Try to use parentheses.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

where do you think I have to put parentheses

   

calc_Angle_x = (1/2*asin((x_axis*g)/(st_speed*st_speed))*((180.0)/3.14);

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

This differs from your previous post. Now you have used parentheses.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

In your project you have

   

calc_Angle_x = (1/2*asin(x_axis*g/st_speed*st_speed)*((180.0)/3.141592));

   

and you should have

   

calc_Angle_x = (1/2*asin(x_axis*g/(st_speed*st_speed))*((180.0)/3.141592));

   

or

   

calc_Angle_x = (1/2*asin(x_axis*g/st_speed/st_speed)*((180.0)/3.141592));

   

What makes things easier would be to calculate the argument as

   

double Argument;

   

Argument = x_axis*g/(st_speed*st_speed);

   

calc_Angle_x = (1/2*asin(Argument)*((180.0)/3.141592));

   

 

   

By the way: What is the goal of your project?

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thank you Bob

   

Goal of my project is to compare the measure of calc_Angle_x with angle x from accelerometer.

   

if those to angle are equal. green led has to turn on if there are not, green led has to turn off and red led turn on.

0 Likes
Anonymous
Not applicable

by using calculator I get the right sulution. the problems comes in psoc. it doesn't help to put parentheses in. 

   

NB: the values has to be the one you see in project. 

0 Likes
moro_1580446
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

Hi MR Robertniyo.could u runing adxl345 with i2c?because i cant solved this problem!!!

   

can u give me your project for learning how i can to do?

   

tanks...

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Please stop flooding the forum!

   

 

   

Bob

0 Likes
moro_1580446
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

dear bob i dont want to flooding the forum.tankyou for your explanation but it is not helpful for me.so i want to check my code project with a correct project code to find mistake.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Post your actual schematics (sketch, no photos) and project bundle, tell us where you got stuck. Did you follow all hints I gave you when I explained I2C handling?

   

 

   

Bob

0 Likes
moro_1580446
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

Tanks MR Bob Marlowe.I attach my avr project and pcos project interfacing on I2C.so you can see what is diffrent between them.so tell me where is wrong please?

   

I observance all hints that you explain.but it is not worked.really i dont know what happening?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

OK:

   

The ADXL address is 0x53, 0xa6 is definitively wrong.

   

When reading and writing you use I2C_1_Start() This has to be used only once at initializatzion.

   

The I2C_1_Stop() will break the current conversation instantly.

   

Use I2C_1_MasterSendStart() as I showed you in the explanation of I2C

   

I2C_1_MasterSendStart() returns a status byte indicating success (zero) or faillure. When debugging you should control this result.

   

I repeat the I2C handling:

   

Byte I2C interface is quite simple: After setting up the component and starting it you use

   

    I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE);    // Initialize a transaction for writing
    I2C_MasterWriteByte(Register);                // Indicate which register you want to write to
    I2C_MasterWriteByte(Value);                // Write to register
    I2C_MasterSendStop();                    // End of transaction

   

When you want to read from a device you use (example for reading two bytes)

   

    I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE);    // Initialize a transaction for writing
    I2C_MasterWrite(Register);                // Indicate which register you want to write to
    I2C_MasterSendRestart(DeviceAddress,I2C_READ_XFER_MODE);
    I2C_MasterReadByte(I2C_ACK_DATA);            // Read from register
    I2C_MasterReadByte(I2C_NAK_DATA);            // Read from register, last byte is NAKed
    I2C_MasterSendStop();                    // End of transaction

   

Not too difficult. Keep in mind that most of the APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition.

   

 

   

Bob

moro_1580446
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

I try to writing code with your explanation.but it is not working again.no data recieved.and i checked returns a status byte of I2C_1_MasterSendStart() command,it is zero,so is it correct?see my code and tell me mistakes please?

   

tanks for attention dear Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Made a few corrections.

   

read_i2c(ADXL345_Addr,RA_DATAX1)<<8

   

This will go el wracko!!! read_i2c() is an unsigned char, shifting that 8 positions left will always result in zero!

   

You did not check each result from the I2C functions

   

You did not NAK the last (only) byte red from I2C

   

As a tip: You are using many global variables. This is wasting sram resources. Local variables are allocated on the stack and live as long as the function runs and freed when the function finishes.

   

 

   

Bob

0 Likes
moro_1580446
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

Its worked very well.Bob Marlowe tank you so much.you are very good.now i want to calibrate and set sensor on best operation.

   

this picture is result of program.

   

Can I have your telegram number or whatsapp?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Sorry, but I do not give away phone numbers, skype etc. too much traffic, too much time.

   

Fine that you got it running!

   

 

   

Bob

0 Likes