- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#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
Solved! Go to Solution.
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This differs from your previous post. Now you have used parentheses.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please stop flooding the forum!
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, but I do not give away phone numbers, skype etc. too much traffic, too much time.
Fine that you got it running!
Bob