Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HEPTA_SENSOR mbed HEPTA_EPS HEPTA_COM HEPTA_CDH
main.cpp@33:8a992b69fcd3, 2022-08-30 (annotated)
- Committer:
- Yanagihara
- Date:
- Tue Aug 30 02:39:39 2022 +0000
- Revision:
- 33:8a992b69fcd3
- Parent:
- 32:c22cbcfb4635
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
HeptaSatTraining2019 | 25:ccc5ff675e0c | 2 | #include "HEPTA_CDH.h" |
HeptaSatTraining2019 | 25:ccc5ff675e0c | 3 | #include "HEPTA_EPS.h" |
MEXT1 | 27:b4689aa48bf5 | 4 | #include "HEPTA_SENSOR.h" |
MEXT1 | 27:b4689aa48bf5 | 5 | #include "HEPTA_COM.h" |
Yanagihara | 33:8a992b69fcd3 | 6 | |
HeptaSatTraining2019 | 26:220e5f95168a | 7 | HEPTA_CDH cdh(p5, p6, p7, p8, "sd"); |
HeptaSatTraining2019 | 26:220e5f95168a | 8 | HEPTA_EPS eps(p16,p26); |
MEXT1 | 27:b4689aa48bf5 | 9 | HEPTA_SENSOR sensor(p17, |
csmk18112 | 32:c22cbcfb4635 | 10 | p28,p27,0x19,0x69,0x13, |
csmk18112 | 32:c22cbcfb4635 | 11 | p13, p14,p25,p24); |
heptasat2021 | 29:eb84063fe5c9 | 12 | HEPTA_COM com(p9,p10); |
Yanagihara | 33:8a992b69fcd3 | 13 | |
heptasat2021 | 29:eb84063fe5c9 | 14 | DigitalOut condition(LED1); |
Yanagihara | 33:8a992b69fcd3 | 15 | DigitalOut LandLED1(p29); |
Yanagihara | 33:8a992b69fcd3 | 16 | DigitalOut LandLED2(p30); |
Yanagihara | 33:8a992b69fcd3 | 17 | DigitalOut LandLED3(p11); |
Yanagihara | 33:8a992b69fcd3 | 18 | DigitalOut LandLED4(p12); |
Yanagihara | 33:8a992b69fcd3 | 19 | |
heptasat2021 | 29:eb84063fe5c9 | 20 | Serial sat(USBTX,USBRX,9600); |
heptasat2021 | 29:eb84063fe5c9 | 21 | Timer sattime; |
MEXT1 | 27:b4689aa48bf5 | 22 | int rcmd = 0,cmdflag = 0; //command variable |
heptasat2021 | 29:eb84063fe5c9 | 23 | |
Yanagihara | 33:8a992b69fcd3 | 24 | DigitalOut debug2(LED2); |
Yanagihara | 33:8a992b69fcd3 | 25 | DigitalOut debug3(LED3); |
Yanagihara | 33:8a992b69fcd3 | 26 | DigitalOut debug4(LED4); |
Yanagihara | 33:8a992b69fcd3 | 27 | |
Yanagihara | 33:8a992b69fcd3 | 28 | int co2ppm = 0; |
Yanagihara | 33:8a992b69fcd3 | 29 | |
MEXT1 | 27:b4689aa48bf5 | 30 | int main() { |
Yanagihara | 33:8a992b69fcd3 | 31 | |
Yanagihara | 33:8a992b69fcd3 | 32 | debug2 = 1; |
Yanagihara | 33:8a992b69fcd3 | 33 | debug3 = 1; |
Yanagihara | 33:8a992b69fcd3 | 34 | debug4 = 1; |
Yanagihara | 33:8a992b69fcd3 | 35 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 36 | debug2 = 0; |
Yanagihara | 33:8a992b69fcd3 | 37 | debug3 = 0; |
Yanagihara | 33:8a992b69fcd3 | 38 | debug4 = 0; |
Yanagihara | 33:8a992b69fcd3 | 39 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 40 | debug2 = 1; |
Yanagihara | 33:8a992b69fcd3 | 41 | debug3 = 1; |
Yanagihara | 33:8a992b69fcd3 | 42 | debug4 = 1; |
Yanagihara | 33:8a992b69fcd3 | 43 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 44 | debug2 = 0; |
Yanagihara | 33:8a992b69fcd3 | 45 | debug3 = 0; |
Yanagihara | 33:8a992b69fcd3 | 46 | debug4 = 0; |
Yanagihara | 33:8a992b69fcd3 | 47 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 48 | debug2 = 1; |
Yanagihara | 33:8a992b69fcd3 | 49 | debug3 = 1; |
Yanagihara | 33:8a992b69fcd3 | 50 | debug4 = 1; |
Yanagihara | 33:8a992b69fcd3 | 51 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 52 | debug2 = 0; |
Yanagihara | 33:8a992b69fcd3 | 53 | debug3 = 0; |
Yanagihara | 33:8a992b69fcd3 | 54 | debug4 = 0; |
Yanagihara | 33:8a992b69fcd3 | 55 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 56 | |
Yanagihara | 33:8a992b69fcd3 | 57 | LandLED1 = 1; |
Yanagihara | 33:8a992b69fcd3 | 58 | LandLED2 = 1; |
Yanagihara | 33:8a992b69fcd3 | 59 | LandLED3 = 1; |
Yanagihara | 33:8a992b69fcd3 | 60 | LandLED4 = 1; |
heptasat2021 | 29:eb84063fe5c9 | 61 | sat.printf("From Sat : Nominal Operation\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 62 | com.printf("From Sat : Nominal Operation\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 63 | com.baud(9600); |
heptasat2021 | 29:eb84063fe5c9 | 64 | int flag = 0; //condition flag |
heptasat2021 | 29:eb84063fe5c9 | 65 | float batvol, temp; //voltage, temperature |
heptasat2021 | 29:eb84063fe5c9 | 66 | int rcmd=0,cmdflag=0; //command variable |
Yanagihara | 33:8a992b69fcd3 | 67 | float ax,ay,az; |
Yanagihara | 33:8a992b69fcd3 | 68 | float gx,gy,gz; |
Yanagihara | 33:8a992b69fcd3 | 69 | |
heptasat2021 | 29:eb84063fe5c9 | 70 | sattime.start(); |
heptasat2021 | 29:eb84063fe5c9 | 71 | eps.turn_on_regulator();//turn on 3.3V conveter |
Yanagihara | 33:8a992b69fcd3 | 72 | while(1) { |
heptasat2021 | 30:f300d1e88f4c | 73 | com.xbee_receive(&rcmd,&cmdflag);//interupting by ground station command |
heptasat2021 | 30:f300d1e88f4c | 74 | |
heptasat2021 | 29:eb84063fe5c9 | 75 | //satellite condition led |
heptasat2021 | 29:eb84063fe5c9 | 76 | condition = !condition; |
heptasat2021 | 29:eb84063fe5c9 | 77 | |
heptasat2021 | 29:eb84063fe5c9 | 78 | //senssing HK data(dummy data) |
heptasat2021 | 29:eb84063fe5c9 | 79 | eps.vol(&batvol); |
MEXT1 | 27:b4689aa48bf5 | 80 | sensor.temp_sense(&temp); |
heptasat2021 | 29:eb84063fe5c9 | 81 | |
heptasat2021 | 29:eb84063fe5c9 | 82 | //Transmitting HK data to Ground Station(GS) |
heptasat2021 | 29:eb84063fe5c9 | 83 | com.printf("HEPTASAT::Condition = %d, Time = %f [s], batVol = %.2f [V],Temp = %.2f [C]\r\n",flag,sattime.read(),batvol,temp); |
heptasat2021 | 29:eb84063fe5c9 | 84 | wait_ms(1000); |
heptasat2021 | 29:eb84063fe5c9 | 85 | //Power Saving Mode |
heptasat2021 | 29:eb84063fe5c9 | 86 | if((batvol <= 3.5) | (temp > 35.0)){ |
heptasat2021 | 29:eb84063fe5c9 | 87 | eps.shut_down_regulator(); |
heptasat2021 | 29:eb84063fe5c9 | 88 | com.printf("Power saving mode ON\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 89 | flag = 1; |
heptasat2021 | 29:eb84063fe5c9 | 90 | } else if((flag == 1) & (batvol > 3.7) & (temp <= 25.0)) { |
heptasat2021 | 29:eb84063fe5c9 | 91 | eps.turn_on_regulator(); |
heptasat2021 | 29:eb84063fe5c9 | 92 | com.printf("Power saving mode OFF\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 93 | flag = 0; |
HeptaSatTraining2019 | 26:220e5f95168a | 94 | } |
MEXT1 | 27:b4689aa48bf5 | 95 | //Contents of command |
HeptaSatTraining2019 | 26:220e5f95168a | 96 | if (cmdflag == 1) { |
HeptaSatTraining2019 | 26:220e5f95168a | 97 | if (rcmd == 'a') { |
heptasat2021 | 29:eb84063fe5c9 | 98 | sat.printf("rcmd=%c,cmdflag=%d\r\n",rcmd,cmdflag); |
Yanagihara | 33:8a992b69fcd3 | 99 | com.printf("Hepta-Sat Uplink Ok\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 100 | for(int j=0;j<5;j++){ |
heptasat2021 | 29:eb84063fe5c9 | 101 | com.printf("Hello World!\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 102 | condition = 1; |
heptasat2021 | 29:eb84063fe5c9 | 103 | wait_ms(1000); |
heptasat2021 | 29:eb84063fe5c9 | 104 | } |
heptasat2021 | 29:eb84063fe5c9 | 105 | }else if (rcmd == 'b') { |
heptasat2021 | 29:eb84063fe5c9 | 106 | sat.printf("rcmd=%c,cmdflag=%d\r\n",rcmd,cmdflag); |
Yanagihara | 33:8a992b69fcd3 | 107 | com.printf("Hepta-Sat Uplink Ok\r\n"); |
MEXT1 | 27:b4689aa48bf5 | 108 | char str[100]; |
MEXT1 | 27:b4689aa48bf5 | 109 | mkdir("/sd/mydir", 0777); |
heptasat2021 | 29:eb84063fe5c9 | 110 | FILE *fp = fopen("/sd/mydir/satdata.txt","w"); |
MEXT1 | 27:b4689aa48bf5 | 111 | if(fp == NULL) { |
MEXT1 | 27:b4689aa48bf5 | 112 | error("Could not open file for write\r\n"); |
MEXT1 | 27:b4689aa48bf5 | 113 | } |
heptasat2021 | 29:eb84063fe5c9 | 114 | for(int i = 0; i < 10; i++) { |
heptasat2021 | 29:eb84063fe5c9 | 115 | eps.vol(&batvol); |
heptasat2021 | 29:eb84063fe5c9 | 116 | fprintf(fp,"%f\r\n",batvol); |
heptasat2021 | 29:eb84063fe5c9 | 117 | condition = 1; |
heptasat2021 | 29:eb84063fe5c9 | 118 | wait_ms(1000); |
MEXT1 | 27:b4689aa48bf5 | 119 | } |
heptasat2021 | 29:eb84063fe5c9 | 120 | fclose(fp); |
heptasat2021 | 29:eb84063fe5c9 | 121 | fp = fopen("/sd/mydir/satdata.txt","r"); |
heptasat2021 | 29:eb84063fe5c9 | 122 | for(int i = 0; i < 10; i++) { |
heptasat2021 | 29:eb84063fe5c9 | 123 | fgets(str,100,fp); |
heptasat2021 | 29:eb84063fe5c9 | 124 | com.puts(str); |
heptasat2021 | 29:eb84063fe5c9 | 125 | } |
heptasat2021 | 29:eb84063fe5c9 | 126 | fclose(fp); |
heptasat2021 | 29:eb84063fe5c9 | 127 | }else if (rcmd == 'c') { |
heptasat2021 | 29:eb84063fe5c9 | 128 | //Please insert your answer |
Yanagihara | 33:8a992b69fcd3 | 129 | sat.printf("Command Get %d\r\n",rcmd); |
Yanagihara | 33:8a992b69fcd3 | 130 | com.printf("HEPTA Uplink OK\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 131 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 132 | sat.printf("Accel sensing Mode\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 133 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 134 | for(int ii = 0; ii < 10; ii++) { |
Yanagihara | 33:8a992b69fcd3 | 135 | sensor.sen_acc(&ax,&ay,&az); |
Yanagihara | 33:8a992b69fcd3 | 136 | com.printf("AX = %f\r\n",ax); |
Yanagihara | 33:8a992b69fcd3 | 137 | com.printf("AY = %f\r\n",ay); |
Yanagihara | 33:8a992b69fcd3 | 138 | com.printf("AZ = %f\r\n",az); |
Yanagihara | 33:8a992b69fcd3 | 139 | wait(0.5); |
Yanagihara | 33:8a992b69fcd3 | 140 | } |
heptasat2021 | 29:eb84063fe5c9 | 141 | }else if (rcmd == 'd') { |
Yanagihara | 33:8a992b69fcd3 | 142 | sat.printf("Command Get %d\r\n",rcmd); |
Yanagihara | 33:8a992b69fcd3 | 143 | com.printf("HEPTA Uplink OK\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 144 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 145 | sat.printf("Gyro sensing Mode\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 146 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 147 | for(int ii = 0; ii < 10; ii++) { |
Yanagihara | 33:8a992b69fcd3 | 148 | sensor.sen_gyro(&gx,&gy,&gz); |
Yanagihara | 33:8a992b69fcd3 | 149 | com.printf("GX = %f\r\n",gx); |
Yanagihara | 33:8a992b69fcd3 | 150 | com.printf("GY = %f\r\n",gy); |
Yanagihara | 33:8a992b69fcd3 | 151 | com.printf("GZ = %f\r\n",gz); |
Yanagihara | 33:8a992b69fcd3 | 152 | wait(0.5); |
Yanagihara | 33:8a992b69fcd3 | 153 | } |
heptasat2021 | 29:eb84063fe5c9 | 154 | |
heptasat2021 | 29:eb84063fe5c9 | 155 | }else if (rcmd == 'e') { |
Yanagihara | 33:8a992b69fcd3 | 156 | sat.printf("Command Get %d\r\n",rcmd); |
Yanagihara | 33:8a992b69fcd3 | 157 | com.printf("HEPTA Uplink OK\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 158 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 159 | sat.printf("CO2 sensing Mode\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 160 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 161 | |
Yanagihara | 33:8a992b69fcd3 | 162 | co2ppm = 42; |
Yanagihara | 33:8a992b69fcd3 | 163 | for(int ii = 0; ii < 10; ii++) { |
Yanagihara | 33:8a992b69fcd3 | 164 | com.printf("CO2 = %d [ppm]\r\n",co2ppm); |
Yanagihara | 33:8a992b69fcd3 | 165 | wait(0.5); |
Yanagihara | 33:8a992b69fcd3 | 166 | } |
heptasat2021 | 29:eb84063fe5c9 | 167 | |
Yanagihara | 33:8a992b69fcd3 | 168 | }else if (rcmd == 'f') { |
Yanagihara | 33:8a992b69fcd3 | 169 | sat.printf("Command Get %d\r\n",rcmd); |
Yanagihara | 33:8a992b69fcd3 | 170 | com.printf("HEPTA Uplink OK\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 171 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 172 | sat.printf("Landing sense Mode\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 173 | sat.printf("===================\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 174 | //Try do detect landing for 10 seconds |
Yanagihara | 33:8a992b69fcd3 | 175 | for(int j = 0; j< 20; j++){ |
Yanagihara | 33:8a992b69fcd3 | 176 | com.printf("Checking for landing!\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 177 | sensor.sen_acc(&ax,&ay,&az); |
Yanagihara | 33:8a992b69fcd3 | 178 | sensor.sen_gyro(&gx,&gy,&gz); |
Yanagihara | 33:8a992b69fcd3 | 179 | //com.printf("Gravity^2 = %4f\r\n",(ax*ax)+(ay*ay)+(az*az)); |
Yanagihara | 33:8a992b69fcd3 | 180 | //com.printf("Gyros = %4f,%4f,%4f\r\n",gx,gy,gz); |
Yanagihara | 33:8a992b69fcd3 | 181 | |
Yanagihara | 33:8a992b69fcd3 | 182 | // Gyros < 0,1? |
Yanagihara | 33:8a992b69fcd3 | 183 | if((gx<0.5)&&(gy<0.5)&&(gz<0.5)){ |
Yanagihara | 33:8a992b69fcd3 | 184 | // Gravity > 9.6 and < 10.0? |
Yanagihara | 33:8a992b69fcd3 | 185 | if(( (ax*ax)+(ay*ay)+(az*az) ) > 92.16){ |
Yanagihara | 33:8a992b69fcd3 | 186 | if(((ax*ax)+(ay*ay)+(az*az) ) < 100.0){ |
Yanagihara | 33:8a992b69fcd3 | 187 | com.printf("Landing detected!\r\n"); |
Yanagihara | 33:8a992b69fcd3 | 188 | for(int i = 0; i < 10; i++){ |
Yanagihara | 33:8a992b69fcd3 | 189 | //flash LEDS |
Yanagihara | 33:8a992b69fcd3 | 190 | LandLED1 = ! LandLED1; |
Yanagihara | 33:8a992b69fcd3 | 191 | LandLED2 = ! LandLED2; |
Yanagihara | 33:8a992b69fcd3 | 192 | LandLED3 = ! LandLED3; |
Yanagihara | 33:8a992b69fcd3 | 193 | LandLED4 = ! LandLED4; |
Yanagihara | 33:8a992b69fcd3 | 194 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 195 | //put on sound |
Yanagihara | 33:8a992b69fcd3 | 196 | //TODO |
Yanagihara | 33:8a992b69fcd3 | 197 | } |
Yanagihara | 33:8a992b69fcd3 | 198 | } |
Yanagihara | 33:8a992b69fcd3 | 199 | } |
Yanagihara | 33:8a992b69fcd3 | 200 | } |
Yanagihara | 33:8a992b69fcd3 | 201 | wait_ms(500); |
Yanagihara | 33:8a992b69fcd3 | 202 | } |
HeptaSatTraining2019 | 26:220e5f95168a | 203 | } |
heptasat2021 | 30:f300d1e88f4c | 204 | com.initialize(); |
HeptaSatTraining2019 | 26:220e5f95168a | 205 | } |
HeptaSatTraining2019 | 24:3659e0c223c8 | 206 | } |
heptasat2021 | 29:eb84063fe5c9 | 207 | sattime.stop(); |
heptasat2021 | 29:eb84063fe5c9 | 208 | sat.printf("From Sat : End of operation\r\n"); |
heptasat2021 | 29:eb84063fe5c9 | 209 | com.printf("From Sat : End of operation\r\n"); |
MEXT1 | 27:b4689aa48bf5 | 210 | } |
Yanagihara | 33:8a992b69fcd3 | 211 |