Need help C_Math library funtions won't work

General discussion on mikroC PRO for FT90x.
Post Reply
Author
Message
anoble
Posts: 6
Joined: 18 Apr 2015 13:26

Need help C_Math library funtions won't work

#1 Post by anoble » 19 Apr 2015 10:49

Hi

I'm trying to use the C_Math library to calculate angles from accelerometer readings.
I'm using a PIC16F877A. At present i'm trying to debug the maths calculations so I am not using the readings from the acceleromter
instead I'm directly assigning values to the raw X, Y & Z accelerometer variables.

I can seem to get any math functions working. the equation i'm trying to compute is;
X_ANGLE = atan(Y_RAW / sqrt(X_RAW^2 + Z_RAW^2))
but for now reduced to denominator_X_cal = sqrt(X_RAW^2 + Z_RAW^2))

the code has a known working debug function which converts this result into an ascii format and outputs on UART

the function code is below

Code: Select all

void read_accerometers (void)
{
unsigned int Accel_X_raw = 0;
unsigned int Accel_Y_raw = 0;
unsigned int Accel_Z_raw = 0;
double   X_Squared = 0;
double   Y_Squared = 0;
double   Z_Squared = 0;
double   denominator_X_cal = 0;
double a = 0;

//ByteReadI2C(GYRO_DEVICE_ID, ADDR_ACCEL_XOUT_H, &AccXDataH, 1);
//ByteReadI2C(GYRO_DEVICE_ID, ADDR_ACCEL_XOUT_L, &AccXDataL, 1);
//ByteReadI2C(GYRO_DEVICE_ID, ADDR_ACCEL_YOUT_H, &AccYDataH, 1);
//ByteReadI2C(GYRO_DEVICE_ID, ADDR_ACCEL_YOUT_L, &AccYDataL, 1);
//ByteReadI2C(GYRO_DEVICE_ID, ADDR_ACCEL_ZOUT_H, &AccZDataH, 1);
//ByteReadI2C(GYRO_DEVICE_ID, ADDR_ACCEL_ZOUT_L, &AccZDataL, 1);

//Accel_X_raw = ((AccXDataH<<8)|AccXDataL);
//Accel_Y_raw = ((AccYDataH<<8)|AccYDataL);
//Accel_Z_raw = ((AccZDataH<<8)|AccZDataL);

//debug set direct variables so not reading acclerometers, therefore
//math calculations can be proven.
Accel_X_raw = 50;
Accel_Y_raw = 20;
Accel_Z_raw = 80;

X_Squared = ((Accel_X_raw)*(Accel_X_raw));
Y_Squared = ((Accel_Y_raw)*(Accel_Y_raw));
Z_Squared = ((Accel_Z_raw)*(Accel_Z_raw));

a = ((Z_Squared)+(X_Squared));

//below line doesn't execute/ get stuck
//if removed code runs normally
denominator_X_cal = sqrt(a);

RB2_BIT = 1;         //debug turns on LED if executed

UART_VALUE_TO_ASCII_AND_OUTPUT (denominator_X_cal); // function converts value into ascii characters and tx on UART
TX_DATA(0x0D); //line feed and carriage return
TX_DATA(0x0A);
}
At the moment this function is the only thing called in main every 200mS

the code gets stuck at the below code line, if it is removed the code will run continually as expected

Code: Select all

denominator_X_cal = sqrt(a);
I have ticked the C_Math library in library manager, code compiles successfully.
what am I missing?

Any help on this will be very appreciated as I don't think I'll be able to write my own Sqrt and atan functions to a good enough accuracy.

Thanks in advance

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Need help C_Math library funtions won't work

#2 Post by marina.petrovic » 20 Apr 2015 13:13

Hi,

I believe that I answered you via this forum topic:
http://www.mikroe.com/forum/viewtopic.php?f=88&t=64216

Best regards,
Marina

anoble
Posts: 6
Joined: 18 Apr 2015 13:26

Re: Need help C_Math library funtions won't work

#3 Post by anoble » 28 Apr 2015 08:07

Yes you have already answered thanks
(new to register so didn't relise post was delayed so re wrote it)

Post Reply

Return to “mikroC PRO for FT90x General”