Tandin Wangchuk / Mbed 2 deprecated Lab7-01_template_2019

Dependencies:   mbed HeptaBattery SDFileSystem Hepta9axis HeptaTemp

Committer:
tandin
Date:
Sun Aug 25 03:19:04 2019 +0000
Revision:
22:228f5d9aed7b
Parent:
21:9592659dd31f
Child:
23:3dad6c03c214
Version 1 working version of group B mission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:bdbd3d6fc5d5 1 #include "mbed.h"
mbed_official 0:bdbd3d6fc5d5 2 #include "SDFileSystem.h"
umeume 2:1c5cdb2c3e0f 3 #include "HeptaXbee.h"
umeume 2:1c5cdb2c3e0f 4 #include "HeptaCamera_GPS.h"
umeume 2:1c5cdb2c3e0f 5 #include "Hepta9axis.h"
umeume 2:1c5cdb2c3e0f 6 #include "HeptaTemp.h"
umeume 2:1c5cdb2c3e0f 7 #include "HeptaBattery.h"
tandin 21:9592659dd31f 8 #include"Air_Quality.h"
tandin 21:9592659dd31f 9
tandin 21:9592659dd31f 10 AnalogIn sensorUV(p18);
tandin 21:9592659dd31f 11 AirQuality airqualitysensor;
tandin 21:9592659dd31f 12 int current_quality = -1;
tandin 21:9592659dd31f 13 PinName analogPin = p15;
umeume 2:1c5cdb2c3e0f 14
umeume 2:1c5cdb2c3e0f 15 Serial pc(USBTX,USBRX);
HEPTA 9:942eb6e8c117 16 SDFileSystem sd(p5, p6, p7, p8, "sd");
umeume 2:1c5cdb2c3e0f 17 HeptaXbee xbee(p9,p10);
umeume 2:1c5cdb2c3e0f 18 HeptaCamera_GPS cam_gps(p13, p14,p25,p24);
HEPTA 10:4d6870d0caa0 19 Hepta9axis n_axis(p28,p27,0xD0,0x18);
HEPTA 7:fab080b8be64 20 HeptaTemp temp(p17);
HEPTA 13:a1fa75a002f6 21 HeptaBattery battery(p16,p26);
umeume 2:1c5cdb2c3e0f 22
tandin 21:9592659dd31f 23 // Interrupt Handler
tandin 21:9592659dd31f 24 void AirQualityInterrupt()
tandin 21:9592659dd31f 25 {
tandin 21:9592659dd31f 26 AnalogIn sensor(analogPin);
tandin 21:9592659dd31f 27 airqualitysensor.last_vol = airqualitysensor.first_vol;
tandin 21:9592659dd31f 28 airqualitysensor.first_vol = sensor.read()*1000;
tandin 21:9592659dd31f 29 airqualitysensor.timer_index = 1;
tandin 21:9592659dd31f 30 }
umeume 5:c5ccb1b07e8f 31
umeume 2:1c5cdb2c3e0f 32 int main()
umeume 2:1c5cdb2c3e0f 33 {
tandin 21:9592659dd31f 34 pc.baud(9600);
tandin 21:9592659dd31f 35 xbee.baud(9600);
tandin 21:9592659dd31f 36 pc.printf("Xbee Uplink Ok Mode\r\n");
tandin 21:9592659dd31f 37 float bt;
tandin 21:9592659dd31f 38 float temper;
tandin 21:9592659dd31f 39 //char str[100];
tandin 21:9592659dd31f 40 float ax,ay,az;
tandin 21:9592659dd31f 41 float gx,gy,gz;
tandin 21:9592659dd31f 42 float mx,my,mz;
tandin 21:9592659dd31f 43 int rcmd=0,cmdflag=0;
tandin 21:9592659dd31f 44 int flag = 0;
tandin 22:228f5d9aed7b 45 FILE *dummy = fopen("/sd/dummy.txt","w");
tandin 22:228f5d9aed7b 46 fclose(dummy);
tandin 21:9592659dd31f 47 while(1) {
tandin 21:9592659dd31f 48 battery.power_saving_mode(&flag,&bt);
tandin 21:9592659dd31f 49 temp.temp_sense(&temper);
tandin 21:9592659dd31f 50 wait(0.5);
tandin 22:228f5d9aed7b 51 mkdir("/sd/mydir", 0777);
tandin 22:228f5d9aed7b 52 FILE *fp = fopen("/sd/mydir/data.txt","w");
tandin 22:228f5d9aed7b 53 if(fp == NULL) {
tandin 22:228f5d9aed7b 54 error("Could not open file for write\r\n");
tandin 22:228f5d9aed7b 55 }
tandin 21:9592659dd31f 56 pc.printf("flag = %d, bt = %.2f [V], temp = %.1f [C]\r\n",flag,bt,temper);
tandin 21:9592659dd31f 57 xbee.printf("flag = %d, bt = %.2f [V], temp = %.1f [C]\r\n",flag,bt,temper);
tandin 22:228f5d9aed7b 58 fprintf(fp,"flag = %d, bt = %.2f [V], temp = %.1f [C]\r\n",flag,bt,temper);
tandin 21:9592659dd31f 59 xbee.xbee_receive(&rcmd,&cmdflag);
tandin 22:228f5d9aed7b 60
tandin 21:9592659dd31f 61 if(flag == 1) {
tandin 21:9592659dd31f 62 pc.printf("Low Battery\r\n");
tandin 21:9592659dd31f 63 xbee.printf("Low Battery\r\n");
tandin 21:9592659dd31f 64 } else if(temper > 35.0) {
tandin 22:228f5d9aed7b 65 pc.printf("High Temperature\n\r");
tandin 22:228f5d9aed7b 66 xbee.printf("High Temperature\n\r");
tandin 21:9592659dd31f 67 } else if((flag == 1) & (temper > 35.0)) {
tandin 22:228f5d9aed7b 68 pc.printf("Low Battery and High Temperature\n\r");
tandin 22:228f5d9aed7b 69 xbee.printf("Low Battery and High Temperature\n\r");
tandin 21:9592659dd31f 70 }
tandin 21:9592659dd31f 71 if (cmdflag == 1) {
tandin 21:9592659dd31f 72 pc.printf("Command Get = %d\r\n",rcmd);
tandin 21:9592659dd31f 73 xbee.printf("Command Get = %d\r\n",rcmd);
tandin 21:9592659dd31f 74 switch (rcmd) {
tandin 22:228f5d9aed7b 75 case 'a':
tandin 21:9592659dd31f 76 pc.printf("Command Get %d\r\n",rcmd);
tandin 21:9592659dd31f 77 xbee.printf("HEPTA Uplink OK\r\n");
tandin 21:9592659dd31f 78 break;
tandin 21:9592659dd31f 79 case 'b':
tandin 21:9592659dd31f 80 airqualitysensor.init(analogPin, AirQualityInterrupt);
tandin 21:9592659dd31f 81 for(int i = 0; i < 10; i++){
tandin 21:9592659dd31f 82 current_quality=airqualitysensor.slope();
tandin 21:9592659dd31f 83 if (current_quality >= 0) { // if a valid data returned.
tandin 21:9592659dd31f 84 if (current_quality == 0){
tandin 21:9592659dd31f 85 printf("High pollution! Force signal active\n\r");
tandin 21:9592659dd31f 86 xbee.printf("High pollution! Force signal active\n\r");
tandin 21:9592659dd31f 87 }
tandin 21:9592659dd31f 88 else if (current_quality == 1){
tandin 21:9592659dd31f 89 printf("High pollution!\n\r");
tandin 21:9592659dd31f 90 xbee.printf("High pollution!\n\r");
tandin 21:9592659dd31f 91 }
tandin 21:9592659dd31f 92 else if (current_quality == 2){
tandin 21:9592659dd31f 93 printf("Low pollution!\n\r");
tandin 21:9592659dd31f 94 xbee.printf("Low pollution!\n\r");
tandin 21:9592659dd31f 95 }
tandin 21:9592659dd31f 96 else if (current_quality == 3){
tandin 21:9592659dd31f 97 printf("Fresh air\n\r");
tandin 21:9592659dd31f 98 xbee.printf("Fresh air\n\r");
tandin 21:9592659dd31f 99 }
tandin 21:9592659dd31f 100 }
tandin 21:9592659dd31f 101 wait(1.0);
tandin 21:9592659dd31f 102 }
tandin 21:9592659dd31f 103 break;
tandin 21:9592659dd31f 104 case 'c':
tandin 21:9592659dd31f 105 float value;
tandin 21:9592659dd31f 106 for(int i = 0; i < 10; i++){
tandin 21:9592659dd31f 107 value = sensorUV;
tandin 22:228f5d9aed7b 108 pc.printf("\rUV Value = %3.2f%%\r\n",value*100);
tandin 22:228f5d9aed7b 109 xbee.printf("\rUV Value = %3.2f%%\r\n",value*100);
tandin 21:9592659dd31f 110 wait(1.0);
tandin 21:9592659dd31f 111 }
tandin 21:9592659dd31f 112 break;
tandin 21:9592659dd31f 113 case 'd':
tandin 21:9592659dd31f 114
tandin 21:9592659dd31f 115 pc.printf("Camera Snapshot Mode\r\n");
tandin 21:9592659dd31f 116 pc.printf("Hit Any Key To Take Picture\r\n");
tandin 21:9592659dd31f 117 xbee.printf("Camera Snapshot Mode\r\n");
tandin 21:9592659dd31f 118 xbee.printf("Hit Any Key To Take Picture\r\n");
tandin 22:228f5d9aed7b 119 while(!xbee.readable()) {}
tandin 21:9592659dd31f 120 cam_gps.Sync();
tandin 21:9592659dd31f 121 cam_gps.initialize(HeptaCamera_GPS::Baud115200, HeptaCamera_GPS::JpegResolution320x240);
tandin 21:9592659dd31f 122 cam_gps.test_jpeg_snapshot_picture("/sd/test.jpg");
tandin 21:9592659dd31f 123 break;
tandin 21:9592659dd31f 124 case 'e':
tandin 21:9592659dd31f 125 float temper;
tandin 21:9592659dd31f 126 for (int i=0;i<10;i++) {
tandin 21:9592659dd31f 127 temp.temp_sense(&temper);
tandin 21:9592659dd31f 128 pc.printf("Air Temperature: %f\n\r", temper);
tandin 21:9592659dd31f 129 xbee.printf("Air Temperature: %f\n\r", temper);
tandin 21:9592659dd31f 130 wait(1.0);
tandin 21:9592659dd31f 131 }
tandin 21:9592659dd31f 132 break;
tandin 21:9592659dd31f 133 case 'f':
tandin 21:9592659dd31f 134 //int quality=0,stnum=0,gps_check=0;
tandin 21:9592659dd31f 135 // char ns='A',ew='B',aunit='m';
tandin 21:9592659dd31f 136 //float time=0.0,latitude=0.0,longitude=0.0,hacu=0.0,altitude=0.0;
tandin 21:9592659dd31f 137 for (int i=0;i<10;i++) {
tandin 21:9592659dd31f 138 n_axis.sen_acc(&ax,&ay,&az);
tandin 21:9592659dd31f 139 n_axis.sen_mag(&mx,&my,&mz);
tandin 21:9592659dd31f 140 n_axis.sen_gyro(&gx,&gy,&gz);
tandin 21:9592659dd31f 141 pc.printf("Acceleration: %f,%f,%f\t",ax,ay,az);
tandin 21:9592659dd31f 142 pc.printf("Magnetism: %f,%f,%f\t",mx,my,mz);
tandin 21:9592659dd31f 143 pc.printf("Velocity: %f,%f,%f\r\n",gx,gy,gz);
tandin 21:9592659dd31f 144 xbee.printf("Acceleration: %f,%f,%f\t",ax,ay,az);
tandin 21:9592659dd31f 145 xbee.printf("Magnetism: %f,%f,%f\t",mx,my,mz);
tandin 21:9592659dd31f 146 xbee.printf("Velocity: %f,%f,%f\r\n",gx,gy,gz);
tandin 21:9592659dd31f 147 wait(1.0);
tandin 21:9592659dd31f 148 }
tandin 21:9592659dd31f 149
tandin 21:9592659dd31f 150 break;
tandin 21:9592659dd31f 151 case 'g':
tandin 21:9592659dd31f 152
tandin 21:9592659dd31f 153 cam_gps.gps_setting();
tandin 21:9592659dd31f 154 int quality=0,stnum=0,gps_check=0;
tandin 21:9592659dd31f 155 char ns='A',ew='B',aunit='m';
tandin 21:9592659dd31f 156 float time=0.0,latitude=0.0,longitude=0.0,hacu=0.0,altitude=0.0;
tandin 21:9592659dd31f 157 for(int i=1; i<10; i++) {
tandin 21:9592659dd31f 158 cam_gps.gga_sensing(&time, &latitude, &ns, &longitude, &ew, &quality, &stnum, &hacu, &altitude, &aunit, &gps_check);
tandin 21:9592659dd31f 159 if((gps_check==0)|(gps_check==1)) {
tandin 21:9592659dd31f 160 pc.printf("GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c\r\n",time,latitude,ns,longitude,ew,quality,stnum,hacu,altitude,aunit);
tandin 21:9592659dd31f 161 xbee.printf("GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c\r\n",time,latitude,ns,longitude,ew,quality,stnum,hacu,altitude,aunit);
tandin 21:9592659dd31f 162 }
tandin 21:9592659dd31f 163 }
tandin 21:9592659dd31f 164 break;
tandin 21:9592659dd31f 165 default:
tandin 22:228f5d9aed7b 166 pc.printf("Unknown command.\r\n");
tandin 22:228f5d9aed7b 167 xbee.printf("Unknown command.\r\n");
tandin 21:9592659dd31f 168 }
tandin 21:9592659dd31f 169
tandin 21:9592659dd31f 170 xbee.initialize();
tandin 21:9592659dd31f 171 }
tandin 21:9592659dd31f 172 wait(1.0);
tandin 21:9592659dd31f 173 }
umeume 2:1c5cdb2c3e0f 174 }