projects:ukhas_glider_project:code
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projects:ukhas_glider_project:code [2007/12/04 16:26] – Zagi code laurenceb | projects:ukhas_glider_project:code [2008/07/19 23:33] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 5: | Line 5: | ||
{{projects: | {{projects: | ||
+ | ==== Main code (Zagi.c) ==== | ||
+ | <code c> | ||
+ | //main code | ||
+ | // | ||
+ | |||
+ | //////////////////////////////////////////////////////////////// | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | //#include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | #include " | ||
+ | #define altitudelimit 4000 //4km cutdown | ||
+ | #define timelimit 600 //10 minutes | ||
+ | #define Deg2rad M_PI/180 | ||
+ | #define Rad2deg 180/M_PI | ||
+ | #define tweakingfactor 0.002 | ||
+ | #define batteryinput 4 | ||
+ | #define batteryfactor 8/1024 | ||
+ | void wiggleservo(void); | ||
+ | void cutdown(char channel, | ||
+ | void updategpsdata(void); | ||
+ | char *datastring(float k, float Target, attitude_type *ourattitude); | ||
+ | float getbatteryvoltage(void); | ||
+ | u08 cutdownrulecheck(void); | ||
+ | u08 checksum(char *c_buf); | ||
+ | char EEMEM windeast[1020]; | ||
+ | char EEMEM windnorth[1020]; | ||
+ | float targetnorth; | ||
+ | float targeteast; | ||
+ | int time; //time | ||
+ | char *datastring(float k, float Target, attitude_type *ourattitude) | ||
+ | { | ||
+ | static char outputbuffer[80]; | ||
+ | char longitude; | ||
+ | char latitude; | ||
+ | if (gpsGetInfo()-> | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | if (gpsGetInfo()-> | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | sprintf(outputbuffer," | ||
+ | latitude, | ||
+ | ourattitude-> | ||
+ | sprintf(outputbuffer," | ||
+ | return (outputbuffer); | ||
+ | } | ||
+ | float getbatteryvoltage() | ||
+ | { | ||
+ | return (a2dConvert10bit(batteryinput)*batteryfactor); | ||
+ | } | ||
+ | u08 checksum(char *c_buf) | ||
+ | { | ||
+ | u08 i; | ||
+ | u08 sum=0; | ||
+ | u08 size=strlen(c_buf); | ||
+ | for(i=0 ; i<size ; i++, | ||
+ | { | ||
+ | sum += *c_buf; | ||
+ | } | ||
+ | return(sum); | ||
+ | } | ||
+ | |||
+ | void wiggleservos() | ||
+ | { | ||
+ | timer1PWMASet (trimAbias+limitA); | ||
+ | timer1PWMBSet (trimBbias+limitB); | ||
+ | _delay_ms(150); | ||
+ | timer1PWMASet (trimAbias-limitA); | ||
+ | timer1PWMBSet (trimBbias-limitB); | ||
+ | _delay_ms(150); | ||
+ | timer1PWMASet (trimAbias); | ||
+ | timer1PWMBSet (trimBbias); | ||
+ | } | ||
+ | |||
+ | u08 cutdownrulecheck() | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | stdout=& | ||
+ | printf(" | ||
+ | stdout=& | ||
+ | printf(" | ||
+ | return 1; | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | stdout=& | ||
+ | printf(" | ||
+ | stdout=& | ||
+ | printf(" | ||
+ | return 2; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | void cutdown(char channel, | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | PORTD|=(1<< | ||
+ | _delay_ms(1000*time); | ||
+ | PORTD& | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | PORTD|=(1<< | ||
+ | _delay_ms(1000*time); | ||
+ | PORTD& | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void updategpsdata() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | || gpsGetInfo()-> | ||
+ | { | ||
+ | nmeaProcess(Rxbuff0); | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | uint16_t mem; | ||
+ | int Heightupto, | ||
+ | float K, | ||
+ | init(); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | disablegroundcontrol(); | ||
+ | timerDetach(TIMER0OVERFLOW_INT); | ||
+ | printf(" | ||
+ | timer1PWMASet (trimAbias); | ||
+ | timer1PWMBSet (trimBbias); | ||
+ | for (mem=0; | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | time=0; | ||
+ | while(!cutdownrulecheck()) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | K = gpsGetInfo()-> | ||
+ | if ((int)K > Heightupto) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | L = 0; | ||
+ | } | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | cutdown(1, | ||
+ | reinitlowlevel(); | ||
+ | while(1) | ||
+ | { | ||
+ | | ||
+ | East = targeteast - gpsGetInfo()-> | ||
+ | | ||
+ | K = cos(Deg2rad*North); | ||
+ | East = East * K; //distance to target in equatorial degree units | ||
+ | North = targetnorth - North; | ||
+ | | ||
+ | | ||
+ | { | ||
+ | Target = Target - 180; | ||
+ | } | ||
+ | if( Target < -180) | ||
+ | { | ||
+ | Target = Target + 360; // | ||
+ | } | ||
+ | X = gpsGetInfo()-> | ||
+ | Y = gpsGetInfo()-> | ||
+ | if(Y == 0) | ||
+ | | ||
+ | X = 0; // | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | X = X / Y; | ||
+ | | ||
+ | K = Rad2deg*atan(X); | ||
+ | if (Y < 0) | ||
+ | { | ||
+ | K = K - 180; | ||
+ | | ||
+ | if(K < -180) | ||
+ | { | ||
+ | K = K + 360; //K is now our air vector heading | ||
+ | } | ||
+ | K = K - Target; | ||
+ | if (K >180) | ||
+ | { | ||
+ | K = K - 360; | ||
+ | | ||
+ | if(K < -180) | ||
+ | { | ||
+ | K = K + 360; | ||
+ | | ||
+ | if (K > 0) | ||
+ | { | ||
+ | PORTG& | ||
+ | PORTG|=(1<< | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | PORTG& | ||
+ | PORTG|=(1<< | ||
+ | } | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | { | ||
+ | enablegroundcontrol(); | ||
+ | } | ||
+ | | ||
+ | if (bit_is_set(PIND, | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | return(1); | ||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
==== Initialisation code (Init.c, Init.h) ==== | ==== Initialisation code (Init.c, Init.h) ==== | ||
<code c> | <code c> | ||
- | // this is under construction | + | // initialisation code |
// - Laurenceb | // - Laurenceb | ||
#include " | #include " | ||
Line 40: | Line 303: | ||
} | } | ||
- | static | + | int put_char1(char c, FILE *stream) |
{ | { | ||
| | ||
Line 47: | Line 310: | ||
- | static | + | int put_char0(char c, FILE *stream) |
{ | { | ||
| | ||
Line 58: | Line 321: | ||
{ | { | ||
char teststr[15]; | char teststr[15]; | ||
- | sbi(DDRD, | + | sbi(DDRD, |
sbi(DDRB, | sbi(DDRB, | ||
sbi(DDRB, | sbi(DDRB, | ||
+ | sbi(DDRG, | ||
+ | sbi(DDRG, | ||
+ | sbi(DDRD, | ||
+ | sbi(DDRD, | ||
+ | sbi(PORTD, | ||
uartInit(); | uartInit(); | ||
uartSetBaudRate(0, | uartSetBaudRate(0, | ||
Line 75: | Line 343: | ||
// | // | ||
// | // | ||
+ | FILE mystdio1 = FDEV_SETUP_STREAM(put_char1, | ||
+ | FILE mystdio0 = FDEV_SETUP_STREAM(put_char0, | ||
stdout = & | stdout = & | ||
stdin = & | stdin = & | ||
Line 101: | Line 371: | ||
cbi(TIMSK, TOIE3); | cbi(TIMSK, TOIE3); | ||
timer1PWMInitICR(40000); | timer1PWMInitICR(40000); | ||
+ | a2dInit(); | ||
printf(" | printf(" | ||
enablegroundcontrol(); | enablegroundcontrol(); | ||
Line 241: | Line 512: | ||
==== Low level guidance code (Lowlevel.c, | ==== Low level guidance code (Lowlevel.c, | ||
- | |||
- | |||
- | |||
- | |||
- | |||
<code c> | <code c> | ||
- | // This is the fine guidance code, using the thermopiles, | + | // This is the fine guidance code, using the thermopiles, |
- | // untested, but compiles ok with AVR studio 4.18 using AVR-GCC | + | // untested, but compiles ok with AVR studio 4.18 using AVR-GCC |
- | // obviously it will also need calibrating with the servos and airframe used | + | // obviously it will also need calibrating with the servos and airframe used |
// | // | ||
- | ///////////////////////////////////////////////////////////////// | + | #include " |
- | float tweakfactor; | + | #include " |
- | void highlevelguidance(void); | + | |
- | #define pitchrunningaverageconstant | + | |
- | #define deltapitchrunningaverageconstant | + | |
- | #define PP //PID control constants | + | |
- | #define DP | + | |
- | #define IP | + | |
- | #define PR | + | |
- | #define DR | + | |
- | #define IR | + | |
- | #define pitchintegrallimit | + | |
- | #define rollintegrallimit | + | |
- | #define rollrunningaverageconstant | + | |
- | #define deltarollrunningaverageconstant | + | |
- | #define trimAgain | + | |
- | #define trimBgain | + | |
- | #define trimAbias | + | |
- | #define trimBbias | + | |
- | #define limitA | + | |
- | #define limitB | + | |
- | typedef | + | volatile |
- | typedef unsigned char u08; | + | volatile float rollintegral, |
+ | |||
+ | void reinitlowlevel() | ||
+ | { | ||
+ | timerAttach(TIMER0OVERFLOW_INT, | ||
+ | reset=2; // | ||
+ | rollintegral=0; | ||
+ | pitchintegral=0; | ||
+ | } | ||
Line 284: | Line 539: | ||
attitude_type *theattitude; | attitude_type *theattitude; | ||
static unsigned short servoa, | static unsigned short servoa, | ||
- | static unsigned char reset; | ||
static float oldpitch, | static float oldpitch, | ||
- | static float deltapitchrunningaverage, | + | static float deltapitchrunningaverage, |
float pitchrunningaverageconstant_, | float pitchrunningaverageconstant_, | ||
float deltaroll, | float deltaroll, | ||
Line 351: | Line 605: | ||
} | } | ||
pwmpitch=(pitchrunningaverage*PP)+(deltapitchrunningaverage*DP)+(pitchintegral*IP); | pwmpitch=(pitchrunningaverage*PP)+(deltapitchrunningaverage*DP)+(pitchintegral*IP); | ||
- | pwmroll=(rollrunningaverage*PR)+(rollrunningaverage*DR)+(rollintegral*IR)+tweakfactor; | + | pwmroll=(rollrunningaverage*PR)+(deltarollrunningaverage*DR)+(rollintegral*IR)+tweakfactor; |
servoa=trimAgain*(pwmpitch+pwmroll)+trimAbias; | servoa=trimAgain*(pwmpitch+pwmroll)+trimAbias; | ||
if (servoa> | if (servoa> | ||
Line 404: | Line 658: | ||
| | ||
} // so we reset timer1 to get a pulse out | } // so we reset timer1 to get a pulse out | ||
+ | |||
projects/ukhas_glider_project/code.1196785568.txt.gz · Last modified: 2008/07/19 23:32 (external edit)