CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

structure & pointer error

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kalman
Guest







structure & pointer error
PostPosted: Mon Jan 18, 2010 10:11 am     Reply with quote

I manage to learn structure and come up with this combination of code.
I already check but the compiler still give this error to almost all structure function.
Did I miss something???
Code:

#include<16F877A.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)

#include <math.h>
#include <header.h>

struct Kalmanfilter
{
   /* These variables represent our state matrix x */
   float x_angle,
         x_bias;

   /* Our error covariance matrix */
   float P_00,
         P_01,
         P_10,
         P_11;

   float Q_angle, Q_gyro;
   float R_angle;
}filter_roll;

void main ()
{
   float tmp, update_angle;

   setup_adc_ports( ANALOG_RA3_REF );
   setup_adc( ADC_CLOCK_DIV_32 );
   
   init_Kalmanfilter(&filter_roll, 0.0001, 0.0003, 0.69);    //expecting close paren error

   while (1)
   {
   tmp = Acc_roll(accelero_z(), accelero_y(), accelero_x());  //expecting close paren error... and lot more below

   kalman_predict(&filter_roll, gyro_roll_rad(), dt);
   update_angle = kalman_update(&filter_roll(), tmp);
   }
}

float gyro_roll_rad()
{

  set_ADC_channel(1);
  delay_us(20);
  return ((read_adc()*1.6129)-750);

}

void init_Kalmanfilter(struct Kalmanfilter *filterdata, float Q_angle, float Q_gyro, float R_angle)
{
   filterdata->Q_angle = Q_angle;
   filterdata->Q_gyro  = Q_gyro;
   filterdata->R_angle = R_angle;
}

void kalman_predict(struct Kalmanfilter *filterdata, const float dotAngle, const float dt)
{
   filterdata->x_angle += dt * (dotAngle - filterdata->x_bias);

   filterdata->P_00 +=  - dt * (filterdata->P_10 + filterdata->P_01) + filterdata->Q_angle * dt;
   filterdata->P_01 +=  - dt * filterdata->P_11;
   filterdata->P_10 +=  - dt * filterdata->P_11;
   filterdata->P_11 +=  + filterdata->Q_gyro * dt;
}

float kalman_update(struct Kalmanfilter *filterdata, const float angle_m)
{
   const float y = angle_m - filterdata->x_angle;

   const float S = filterdata->P_00 + filterdata->R_angle;
   const float K_0 = filterdata->P_00 / S;
   const float K_1 = filterdata->P_10 / S;

   filterdata->x_angle +=  K_0 * y;
   filterdata->x_bias  +=  K_1 * y;

   filterdata->P_00 -= K_0 * filterdata->P_00;
   filterdata->P_01 -= K_0 * filterdata->P_01;
   filterdata->P_10 -= K_1 * filterdata->P_00;
   filterdata->P_11 -= K_1 * filterdata->P_01;

   return filterdata->x_angle;
}

float Acc_roll(float a_z, float a_y, float a_x)
{
   return -(atan2(-a_z, a_y)-(3.14159/2.0));
}

float accelero_x()
{
   set_ADC_channel(0);
   delay_us(20);
   return ((((read_adc()*3.3)/1023)-1.5)/0.3);
}

float accelero_y()
{
   set_ADC_channel(5);
   delay_us(20);
   return ((((read_adc()*3.3)/1023)-1.5)/0.3);
}

float accelero_z()
{
   set_ADC_channel(6);
   delay_us(20);
   return ((((read_adc()*3.3)/1023)-1.5)/0.3);
}

compiler PCWHD 4.084
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 18, 2010 1:33 pm     Reply with quote

Your program doesn't compile. It's missing the header.h file.

Also post your compiler version. It's a 4-digit number in this format: x.xxx
kalman
Guest







PostPosted: Mon Jan 18, 2010 4:53 pm     Reply with quote

header file

Code:

float gyro_roll_rad();

float accelero_x();
float accelero_y();
float accelero_z();

void init_Kalmanfilter();
void Kalman_predict();
float Kalman_update();

float Acc_roll();


compiler PCWHD 4.084
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Mon Jan 18, 2010 5:24 pm     Reply with quote

The problem is caused by the header declarations not being complete and matching the actual procedures.

Code:

void init_Kalmanfilter();
void Kalman_predict();
float Kalman_update();


The simple fix is to comment out the header and move main to the
end. After the move you will have a few more issues to address.
Once I fixed the remaining issues it compiled with no problems. I will leave
them you to resolve (and learn :-)
_________________
Google and Forum Search are some of your best tools!!!!
Guest








PostPosted: Mon Jan 18, 2010 7:56 pm     Reply with quote

dyeatman wrote:
After the move you will have a few more issues to address.
Once I fixed the remaining issues it compiled with no problems. I will leave
them you to resolve (and learn :-)


i tried search for any possible solution but its true lost...
any tips?

one more thing, what is this error pointing to my calling function..
a numeric expression must appear here
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 18, 2010 11:04 pm     Reply with quote

Your code is not consistent. You need to clean it up before
presenting it to us.

For example, here you define the function 'Acc_roll()' to accept 3
floating point variables as parameters:
Quote:

float Acc_roll(float a_z, float a_y, float a_x)
{
return -(atan2(-a_z, a_y)-(3.14159/2.0));
}


But when you call Acc_roll() in the code below, you appear to want
to give it 3 function pointers. But that's not how you defined the function.
Quote:

while (1)
{
tmp = Acc_roll(accelero_z(), accelero_y(), accelero_x()); //expecting close paren error... and lot more below

kalman_predict(&filter_roll, gyro_roll_rad(), dt);
update_angle = kalman_update(&filter_roll(), tmp);
}

CCS has an example of using function pointers in this file:
Quote:
c:\program files\picc\examples\ex_qsort.c

The typedef statement for the function pointer for the above file
is contained in this file:
Quote:
c:\program files\picc\drivers\stdlib.h


These threads show how to use function pointers with structures.
I don't know if we have any threads that just show how to pass
them as a parameter to a function.
http://www.ccsinfo.com/forum/viewtopic.php?t=35500
http://www.ccsinfo.com/forum/viewtopic.php?t=33458
http://www.ccsinfo.com/forum/viewtopic.php?t=31637

Be aware that historically, there have been problems with using
function pointers with the 16F compiler. But they have been
made to work with the 18F compiler. You would have better
luck if you switched to an 18F452 or other 18F chip.
kalman
Guest







PostPosted: Tue Jan 19, 2010 7:44 am     Reply with quote

but,, as mention here...

dyeatman wrote:
The problem is caused by the header declarations not being complete and matching the actual procedures.

Code:

void init_Kalmanfilter();
void Kalman_predict();
float Kalman_update();


The simple fix is to comment out the header and move main to the
end. After the move you will have a few more issues to address.
Once I fixed the remaining issues it compiled with no problems. I will leave
them you to resolve (and learn :-)


so it should be no problem right?
Guest








PostPosted: Tue Jan 19, 2010 8:03 am     Reply with quote

PCM programmer wrote:

For example, here you define the function 'Acc_roll()' to accept 3
floating point variables as parameters:
Quote:

float Acc_roll(float a_z, float a_y, float a_x)
{
return -(atan2(-a_z, a_y)-(3.14159/2.0));
}


But when you call Acc_roll() in the code below, you appear to want
to give it 3 function pointers. But that's not how you defined the function.


Shouldn't the compiler be smart enough to know that what the poster wants is to get the return value from each of the three functions (accelero_x, etc) and use those returned float values as the inputs to the function? I'm pretty sure that's what they'd like to do. A reasonably intelligent compiler would do just that. I've seen code like that lots of times in MSVC and GCC code.
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Tue Jan 19, 2010 8:08 am     Reply with quote

Whoops.. I'm the guest... Forgot to login.

Anyway, from the code it's obvious that they hope the compiler will use the return value of each function as the relevant parameter. This should work I'd think. I do it in code in the PCH compiler. For instance:

while (!myfunction()) {
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 12:54 pm     Reply with quote

If you still can't get it to compile, then post your latest program with
all the fixes that you have done so far.
kalman
Guest







PostPosted: Tue Jan 19, 2010 7:30 pm     Reply with quote

working on it Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group