Lab3-05_create_satcode_step4_lite

Dependencies:   mbed HEPTA_CDH_lite HEPTA_EPS_lite

Committer:
heptasat2021
Date:
Fri Aug 20 12:28:06 2021 +0000
Revision:
2:b30e3a46ae2b
Parent:
1:ddac5ec89167
For Hepta-Sat Lite

Who changed what in which revision?

UserRevisionLine numberNew contents of line
heptasat2021 0:da0f6aca15b8 1 #include "mbed.h"
heptasat2021 2:b30e3a46ae2b 2 #include "HEPTA_EPS.h"
heptasat2021 1:ddac5ec89167 3 #include "HEPTA_CDH.h"
heptasat2021 1:ddac5ec89167 4 HEPTA_CDH cdh(PB_5, PB_4, PB_3, PA_8, "sd");
heptasat2021 2:b30e3a46ae2b 5 HEPTA_EPS eps(PA_0,PA_4);
heptasat2021 2:b30e3a46ae2b 6 DigitalOut condition(PB_1);
heptasat2021 2:b30e3a46ae2b 7 Serial gs(USBTX,USBRX,9600);
heptasat2021 2:b30e3a46ae2b 8 Timer sattime;
heptasat2021 2:b30e3a46ae2b 9 int rcmd = 0, cmdflag = 0; //command variable
heptasat2021 2:b30e3a46ae2b 10
heptasat2021 2:b30e3a46ae2b 11 //getting command and command flag
heptasat2021 2:b30e3a46ae2b 12 void commandget()
heptasat2021 2:b30e3a46ae2b 13 {
heptasat2021 2:b30e3a46ae2b 14 rcmd = gs.getc();
heptasat2021 2:b30e3a46ae2b 15 cmdflag = 1;
heptasat2021 2:b30e3a46ae2b 16 }
heptasat2021 2:b30e3a46ae2b 17 //interrupting process by command receive
heptasat2021 2:b30e3a46ae2b 18 void receive(int rcmd, int cmdflag)
heptasat2021 2:b30e3a46ae2b 19 {
heptasat2021 2:b30e3a46ae2b 20 gs.attach(commandget,Serial::RxIrq);
heptasat2021 2:b30e3a46ae2b 21 }
heptasat2021 2:b30e3a46ae2b 22 //initializing
heptasat2021 2:b30e3a46ae2b 23 void initialize()
heptasat2021 2:b30e3a46ae2b 24 {
heptasat2021 2:b30e3a46ae2b 25 rcmd = 0;
heptasat2021 2:b30e3a46ae2b 26 cmdflag = 0;
heptasat2021 2:b30e3a46ae2b 27 condition = 0;
heptasat2021 2:b30e3a46ae2b 28 }
heptasat2021 2:b30e3a46ae2b 29
heptasat2021 0:da0f6aca15b8 30 int main()
heptasat2021 0:da0f6aca15b8 31 {
heptasat2021 2:b30e3a46ae2b 32 gs.printf("From Sat : Nominal Operation\r\n");
heptasat2021 2:b30e3a46ae2b 33 int flag = 0; //condition flag
heptasat2021 2:b30e3a46ae2b 34 float batvol, temp; //voltage, temperature
heptasat2021 2:b30e3a46ae2b 35 sattime.start();
heptasat2021 2:b30e3a46ae2b 36 receive(rcmd,cmdflag); //interrupting
heptasat2021 2:b30e3a46ae2b 37 eps.turn_on_regulator();//turn on 3.3V conveter
heptasat2021 2:b30e3a46ae2b 38 for(int i=0;i<50;i++){
heptasat2021 2:b30e3a46ae2b 39 //satellite condition led
heptasat2021 2:b30e3a46ae2b 40 condition = !condition;
heptasat2021 2:b30e3a46ae2b 41
heptasat2021 2:b30e3a46ae2b 42 //senssing HK data(dummy data)
heptasat2021 2:b30e3a46ae2b 43 eps.vol(&batvol);
heptasat2021 2:b30e3a46ae2b 44 temp = 28.5;
heptasat2021 2:b30e3a46ae2b 45
heptasat2021 2:b30e3a46ae2b 46 //Transmitting HK data to Ground Station(GS)
heptasat2021 2:b30e3a46ae2b 47 gs.printf("HEPTASAT::Condition = %d, Time = %f [s], batvol = %2f [V], temp = %2f [deg C]\r\n",flag,sattime.read(),batvol,temp);
heptasat2021 2:b30e3a46ae2b 48 wait_ms(1000);
heptasat2021 2:b30e3a46ae2b 49
heptasat2021 2:b30e3a46ae2b 50 //Power Saving Mode
heptasat2021 2:b30e3a46ae2b 51 if(batvol <= 3.5){
heptasat2021 2:b30e3a46ae2b 52 eps.shut_down_regulator();
heptasat2021 2:b30e3a46ae2b 53 gs.printf("Power saving mode ON\r\n");
heptasat2021 2:b30e3a46ae2b 54 flag = 1;
heptasat2021 2:b30e3a46ae2b 55 } else if((flag == 1) & (batvol > 3.7)) {
heptasat2021 2:b30e3a46ae2b 56 eps.turn_on_regulator();
heptasat2021 2:b30e3a46ae2b 57 gs.printf("Power saving mode OFF\r\n");
heptasat2021 2:b30e3a46ae2b 58 flag = 0;
heptasat2021 2:b30e3a46ae2b 59 }
heptasat2021 2:b30e3a46ae2b 60
heptasat2021 2:b30e3a46ae2b 61 if(cmdflag == 1){
heptasat2021 2:b30e3a46ae2b 62 if(rcmd == 'a'){
heptasat2021 2:b30e3a46ae2b 63 for(int j=0;j<5;j++){
heptasat2021 2:b30e3a46ae2b 64 gs.printf("Hello World!\r\n");
heptasat2021 2:b30e3a46ae2b 65 condition = 1;
heptasat2021 2:b30e3a46ae2b 66 wait_ms(1000);
heptasat2021 2:b30e3a46ae2b 67 }
heptasat2021 2:b30e3a46ae2b 68 }else if(rcmd == 'b') {
heptasat2021 2:b30e3a46ae2b 69 char str[100];
heptasat2021 2:b30e3a46ae2b 70 mkdir("/sd/mydir", 0777);
heptasat2021 2:b30e3a46ae2b 71 FILE *fp = fopen("/sd/mydir/satdata.txt","w");
heptasat2021 2:b30e3a46ae2b 72 if(fp == NULL) {
heptasat2021 2:b30e3a46ae2b 73 error("Could not open file for write\r\n");
heptasat2021 2:b30e3a46ae2b 74 }
heptasat2021 2:b30e3a46ae2b 75 for(int i = 0; i < 10; i++) {
heptasat2021 2:b30e3a46ae2b 76 eps.vol(&batvol);
heptasat2021 2:b30e3a46ae2b 77 fprintf(fp,"%f\r\n",batvol);
heptasat2021 2:b30e3a46ae2b 78 condition = 1;
heptasat2021 2:b30e3a46ae2b 79 wait_ms(1000);
heptasat2021 2:b30e3a46ae2b 80 }
heptasat2021 2:b30e3a46ae2b 81 fclose(fp);
heptasat2021 2:b30e3a46ae2b 82 fp = fopen("/sd/mydir/satdata.txt","r");
heptasat2021 2:b30e3a46ae2b 83 for(int i = 0; i < 10; i++) {
heptasat2021 2:b30e3a46ae2b 84 fgets(str,100,fp);
heptasat2021 2:b30e3a46ae2b 85 gs.puts(str);
heptasat2021 2:b30e3a46ae2b 86 }
heptasat2021 2:b30e3a46ae2b 87 fclose(fp);
heptasat2021 2:b30e3a46ae2b 88 }
heptasat2021 2:b30e3a46ae2b 89 initialize(); //initializing
heptasat2021 2:b30e3a46ae2b 90 }
heptasat2021 0:da0f6aca15b8 91 }
heptasat2021 2:b30e3a46ae2b 92 sattime.stop();
heptasat2021 2:b30e3a46ae2b 93 gs.printf("From Sat : End of operation\r\n");
heptasat2021 0:da0f6aca15b8 94 }