|
|
View previous topic :: View next topic |
Author |
Message |
jcal54321
Joined: 06 Mar 2009 Posts: 10 Location: Salinas, CA
|
Help with typedef struct and quaternions |
Posted: Wed Mar 21, 2012 3:13 pm |
|
|
Hi,
I was hoping I could get some help with setting up a header file to define some functions that work with quaternions (really just arrays with 4 elements).
I have a myheader_r1.h file....
Code: | struct quaternion { // this is based on the example for a struct of a complex number
float w; // on page 307 of Kelly and Pohl "A Book on C."
float x;
float y;
float z;
};
typedef struct quaternion quaternion;
// function to multiply two quaternions
void mult2Quaternions(quaternion *p, quaternion *q, quaternion *r) {
r->w = p->w*q->w - p->x*q->x - p->y*q->y - p->z*q->z;
r->x = p->w*q->x + p->x*q->w + p->y*q->z - p->z*q->y;
r->y = p->w*q->y - p->x*q->z + p->y*q->w + p->z*q->x;
r->z = p->w*q->z + p->x*q->y - p->y*q->x + p->z*q->w;
return;
}
//function to multiply two scalar numbers
double mult2Numbers(double x, double y) {
return x*y;
}
|
And a main file which uses the header file above...
Code: | #include <18F2520.h>
#include <math.h>
#include "myHeader_r1.h"
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10200000)
#use rs232(baud=9600, xmit=PIN_C1, rcv=PIN_C0, parity=N, bits=8)
/* Program to learn how to make my own header files
J.C. 3/20/2012
*/
//~~~~~~~~~~~~~~~~~~~~ DEFINES ~~~~~~~~~~~~~~~~~~~~~~~~
#define LED PIN_B4
//~~~~~~~~~~~~~~~~~~~ VARIABLES ~~~~~~~~~~~~~~~~~~~~
double number1 = 1;
double number2 = 1;
double product = 100;
quaternion quaternion1;// = {0.0, 1.0, 2.0, 3.0};
quaternion quaternion2;// = {4.0, 5.0 6.0, 7.0};
quaternion answer;// = {1.1, 2.2, 3.3, 4.4};
//~~~~~~~~~~~~~ FUNCTION PROTOTYPES ~~~~~~~~~~~~~~
void init_PIC(void);
//~~~~~~~~~~~~~~~~~~~~~~~~ MAIN() ~~~~~~~~~~~~~~~~~~~~~~
void main() {
init_PIC();
number1 = 3;
number2 = 5;
product = mult2Numbers(number1, number2);
quaternion1.w = 0.1826;
quaternion1.x = 0.3651;
quaternion1.y = 0.5477;
quaternion1.z = 0.7303;
quaternion2.w = 0.5324;
quaternion2.x = 0.2662;
quaternion2.y = 0.7986;
quaternion2.z = 0.0887;
mult2Quaternions( quaternion1, quaternion2, answer);
} // end main
//~~~~~~~~~~~~~~~~~~~ FUNCTIONS ~~~~~~~~~~~~~~~~~~~
// initialize the PIC MCU
void init_PIC(void) {
output_low(LED); // LED off
return;
} // end of init_PIC() |
The compiler generates the following error:
Error 51 "C:\JamesStuff\JamesPIC\PIC18F2520\PlayWithC\playWithC.C" Line 50(19,30): A numeric expression must appear here
Does anyone have any insight into what this means? I know the problem is related to the way I am calling the function "mult2Quaternions( quaternion1, quaternion2, answer);" because when I comment out the line, the program compiles.
I have also tried calling the function like this...
Code: | mult2Quaternions( &quaternion1, &quaternion2, &answer); |
with the following compiling error
Error 134 "C:\JamesStuff\JamesPIC\PIC18F2520\PlayWithC\playWithC.C" Line 64(23,24): Unprotected call in a #INT_GLOBAL
Any help would be greatly appreciated. I am testing the code with MPLAM Sim. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 21, 2012 3:32 pm |
|
|
I stripped your program down and made it compile. I changed a few
things to the way I like it. I typedef'ed the struct and got rid of the tag,
you don't need it. I added a _t subscript to show that it's a structure type
declarator. Then I gave the function parameters the address of the
structures. I got rid of most things that are unnecessary to test the
problem. Now it compiles. I don't know if it works. I didn't check that.
Code: |
#include <18F2520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10M)
#use rs232(baud=9600, xmit=PIN_C1, rcv=PIN_C0, parity=N, bits=8)
typedef struct {
float w;
float x;
float y;
float z;
}quaternion_t;
void mult2Quaternions(quaternion_t *p, quaternion_t *q, quaternion_t *r) {
r->w = p->w*q->w - p->x*q->x - p->y*q->y - p->z*q->z;
r->x = p->w*q->x + p->x*q->w + p->y*q->z - p->z*q->y;
r->y = p->w*q->y - p->x*q->z + p->y*q->w + p->z*q->x;
r->z = p->w*q->z + p->x*q->y - p->y*q->x + p->z*q->w;
return;
}
double mult2Numbers(double x, double y) {
return x*y;
}
double number1 = 1;
double number2 = 1;
double product = 100;
quaternion_t quaternion1; // = {0.0, 1.0, 2.0, 3.0};
quaternion_t quaternion2; // = {4.0, 5.0 6.0, 7.0};
quaternion_t answer; // = {1.1, 2.2, 3.3, 4.4};
//==========================================
void main()
{
number1 = 3;
number2 = 5;
product = mult2Numbers(number1, number2);
mult2Quaternions( &quaternion1, &quaternion2, &answer);
while(1);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Mar 21, 2012 4:10 pm |
|
|
As a comment to this, it is vital to realise that the clock headers, _must_ be in front of any code or includes that uses delays. The serial headers must be in front of anything that uses serial I/O etc..
Get into the habit of putting all of these right at the top of the file as PCM_Programmer shows, otherwise you will hit problems at some point.
The main problem though, was trying to make the structure into a 'type', with exactly the same name, didn't work. Using 'markers' in your names, to say 'this is a long/short/type etc.', as PCM_programmer shows, makes for much more reliable code.
Best Wishes |
|
|
jcal54321
Joined: 06 Mar 2009 Posts: 10 Location: Salinas, CA
|
Thank you for your reply |
Posted: Wed Mar 21, 2012 8:23 pm |
|
|
Thank you both for your reply. I appreciate your insight and guidance on proper programming syntax.
I cut and pasted the code from PCM Programmer into a new project, but I have the error:
Quote: | Error 134 "C:\JamesStuff\JamesPIC\PIC18F2520\PlayWithC\playWithC_r3.C" Line 46(1,2): Unprotected call in a #INT_GLOBAL
|
Could I have a problem with my compiler version or a setting in MPLAB Sim? My compiler version is PCH version 3.249. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 21, 2012 9:04 pm |
|
|
Just looking at one line, I was able to make it compile with vs. 3.249 by
breaking up the complex expression into several smaller expressions.
Your version of the compiler apparently needs this to make it work.
There might be other ways to do it. See the changes below in bold:
Quote: |
void mult2Quaternions(quaternion_t *p, quaternion_t *q, quaternion_t *r)
{
float a, b, c, d;
//r->w = p->w*q->w - p->x*q->x - p->y*q->y - p->z*q->z;
//r->x = p->w*q->x + p->x*q->w + p->y*q->z - p->z*q->y;
//r->y = p->w*q->y - p->x*q->z + p->y*q->w + p->z*q->x;
a = p->w*q->z;
b = p->x*q->y;
c = p->y*q->x;
d = p->z*q->w;
r->z = a + b + c + d;
//r->z = (p->w*q->z) + (p->x*q->y) - (p->y*q->x) + (p->z*q->w);
//return;
} |
|
|
|
jcal54321
Joined: 06 Mar 2009 Posts: 10 Location: Salinas, CA
|
Thank you! |
Posted: Wed Mar 21, 2012 9:30 pm |
|
|
Thank you, PCM programmer,
It is working for me, too! |
|
|
|
|
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
|