Laser Communication Protocol developed during HEPTA-Sat training in Bulgaria, October 2017.

Dependencies:   Hepta9axis HeptaBattery HeptaCamera_GPS HeptaTemp HeptaXbee SDFileSystem mbed

Revision:
0:95bf84c286f5
Child:
1:f7206e74c970
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/3daf572bcae1/main.cpp	Sat Nov 11 14:34:04 2017 +0000
@@ -0,0 +1,459 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "HeptaXbee.h"
+#include "HeptaCamera_GPS.h"
+#include "Hepta9axis.h"
+#include "HeptaTemp.h"
+#include "HeptaBattery.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+Serial pc(USBTX,USBRX);
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+HeptaXbee xbee(p9,p10);
+HeptaCamera_GPS cam_gps(p13, p14,p25,p24);
+Hepta9axis n_axis(p28,p27,0xD0,0x18);
+HeptaTemp temp(p17);
+AnalogIn light(p18);
+PwmOut led(p21);
+HeptaBattery battery(p16,p26);
+DigitalOut myled(LED1);
+
+typedef struct {
+
+char* morse;
+
+char* ascii;
+
+} morse_table_t;
+
+
+int main()
+
+{
+
+morse_table_t table[] = { {".-", "A"},
+
+{"-...", "B"},
+
+{"-.-.", "C"},
+
+{"-.." , "D"},
+
+{"." , "E"},
+
+{"..-." , "F"},
+
+{"--." , "G"},
+
+{"...." , "H"},
+
+{".." , "I"},
+
+{".---" , "J"},
+
+{"-.-" , "K"},
+
+{".-.." , "L"},
+
+{"--" , "M"},
+
+{"-." , "N"},
+
+{"---" , "O"},
+
+{".--." , "P"},
+
+{"--.-" , "Q"},
+
+{".-." , "R"},
+
+{"..." , "S"},
+
+{"-" , "T"},
+
+{"..-" , "U"},
+
+{"...-" , "V"},
+
+{".--" , "W"},
+
+{"-..-" , "X"},
+
+{"-.--" , "Y"},
+
+{"--.." "Z"},
+
+{"-----" ,"0"},
+
+{".----" ,"1"},
+
+{"..---" , "2"},
+
+{"...--" , "3"},
+
+{"....-" , "4"},
+
+{"....." , "5"},
+
+{"-...." ,"6"},
+
+{"--..." , "7"},
+
+{"---.." , "8"},
+
+{"----." , "9"},
+
+{"/" , " "}
+
+};
+
+pc.baud(9600);
+
+int st=0;
+
+char symbol,space;
+
+char mes[100];
+
+char sysp[2];
+
+float count=0.0,count_on=0.0,count_off=0.0, len = 0.0;
+
+float current,vol,sig,lux;
+
+led = 1;
+
+mkdir("/sd/LaserComms", 0777);
+
+FILE *fp = fopen("/sd/LaserComms/message.txt", "w");
+
+if(fp == NULL) {
+
+error("Could not open file for writing\r\n");
+
+}
+
+while (1) {
+
+do {
+
+vol = light.read();
+
+sig = 3.3*1.947*vol;
+
+current = sig * 100;
+
+lux = (current)*2;
+
+if ( lux > 300 ) {
+
+count = count + 0.1;
+
+} else {
+
+count = 0.0;
+
+}
+
+xbee.printf("lux= %f , current= %f ,vol= %f , count= %f \r\n",lux,current,vol,count);
+
+wait (0.1);
+
+} while ( count <= 3.0 );
+
+xbee.printf("LASER COMMUNICATION ESTABLISHED! RECEIVING MODE ON!\r\n");
+
+
+
+lux=0;
+
+while(1) {
+
+count_on = 0.0;
+
+count_off = 0.0;
+
+/*vol = light.read();
+
+sig = 3.3*1.947*vol;
+
+current = sig * 100;
+
+lux = (current)*2;*/
+
+
+
+while(lux<300) {
+
+xbee.printf("lux= %f, count_off= %f \r\n",lux,count_off);
+
+count_off = count_off + 0.05;
+
+wait(0.05);
+
+vol = light.read();
+
+sig = 3.3*1.947*vol;
+
+current = sig * 100;
+
+lux = (current)*2;
+
+}
+
+if (count_off >= 0.5) {
+
+space = '/';
+
+} else {
+
+space = '\0';
+
+}
+
+if (count_off > 3.0) {
+
+break;
+
+}
+
+
+
+while(lux>=300) {
+
+xbee.printf("lux= %f, count_on= %f \r\n",lux,count_on);
+
+count_on = count_on + 0.05;
+
+wait(0.05);
+
+vol = light.read();
+
+sig = 3.3*1.947*vol;
+
+current = sig * 100;
+
+lux = (current)*2;
+
+}
+
+if (count_on >= 0.5) {
+
+symbol = '-';
+
+} else {
+
+symbol = '.';
+
+}
+
+if (count_off > 3.0) {
+
+break;
+
+}
+
+pc.printf("%c%c", space,symbol);
+
+fprintf(fp,"%c",symbol);
+
+
+
+}
+
+fclose(fp);
+
+fp = fopen("/sd/LaserComms/message.txt","r");
+
+fgets(mes,100,fp);
+
+pc.printf("%s\n",mes);
+
+fclose(fp);
+
+
+
+if (mes != "\0") {
+
+xbee.printf("Message accepted successfully! \r\n");
+
+xbee.printf("Morse: %s\r\n Translating... \r\n",mes);
+
+}
+
+
+
+char* segment,final_mes;
+
+int i;
+
+segment = strtok(mes, " ");
+
+xbee.printf("Translated: ");
+
+FILE *fh = fopen("/sd/LaserComms/final_message.txt", "w");
+
+if(fh == NULL) {
+
+error("Could not open file for writing\r\n");
+
+}
+
+while(segment) {
+
+for(i=0; i<37; ++i) {
+
+if (!strcmp(segment, table[i].morse)) {
+
+xbee.printf("%s",(table[i].ascii));
+
+fprintf(fh,"%s",(table[i].ascii));
+
+}
+
+}
+
+segment = strtok(NULL, " ");
+
+}
+
+
+
+fclose(fh);
+
+fh = fopen("/sd/LaserComms/final_message.txt","r");
+
+fgets(&final_mes,100,fh);
+
+fclose(fh);
+
+
+
+
+
+if ( "final_mes" == "TELEMETRY" ) {
+
+xbee.printf("Telemetry requested!");
+
+cam_gps.gps_setting();
+
+float gx,gy,gz,ax,ay,az,mx,my,mz,bt,temper;
+
+char ns='A',ew='B',aunit='m';
+
+float time=0.0,latitude=0.0,longitude=0.0,hacu=0.0,altitude=0.0;
+
+int quality=0,stnum=0,gps_check=0;
+
+for(int i = 0; i < 10; i++) {
+
+n_axis.sen_gyro(&gx,&gy,&gz);
+
+n_axis.sen_acc(&ax,&ay,&az);
+
+n_axis.sen_mag(&mx,&my,&mz);
+
+battery.vol(&bt);
+
+temp.temp_sense(&temper);
+
+cam_gps.gga_sensing(&time, &latitude, &ns, &longitude, &ew, &quality, &stnum, &hacu, &altitude, &aunit, &gps_check);
+
+
+
+xbee.printf("gx = %f gy = %f gz = %f\r\n",gx,gy,gz);
+
+xbee.printf("ax = %f ay = %f az = %f\r\n",ax,ay,az);
+
+xbee.printf("mx = %f my = %f mz = %f\r\n",mx,my,mz);
+
+xbee.printf("Temperature = %f\r\n",temper);
+
+xbee.printf("Battery voltage: = %f[V]\r\n",bt);
+
+xbee.printf("%f,%f,%c,%f,%c,%d,%d,%f,%f,%c\r\n\n",time,latitude,ns,longitude,ew,quality,stnum,hacu,altitude,aunit);
+
+led = (i%2);
+
+wait(1.0);
+
+}
+
+xbee.initialize();
+
+}
+
+if ( "final_mes" == "PHOTO") {
+
+led = 0;
+
+wait(0.2);
+
+led = 1;
+
+wait(0.2);
+
+led = 0;
+
+wait(0.2);
+
+led = 1;
+
+wait(0.2);
+
+led = 0;
+
+wait(0.2);
+
+led = 1;
+
+wait(0.2);
+
+led = 0;
+
+xbee.printf("Photo requested! \r\n Camera Snapshot Mode\r\n\n Please Smile! :) \r\n");
+
+cam_gps.Sync();
+
+cam_gps.initialize(HeptaCamera_GPS::Baud115200, HeptaCamera_GPS::JpegResolution640x480);
+
+cam_gps.test_jpeg_snapshot_picture(1);
+
+led = 1;
+
+}
+
+if ( "final_mes" == "SOS") {
+
+// LED blinks SOS message
+
+}
+
+xbee.putc(xbee.getc());
+
+led=0;
+
+wait(0.2);
+
+led = 1;
+
+wait(0.2);
+
+led = 0;
+
+wait(0.2);
+
+led = 1;
+
+wait(0.2);
+
+led = 0;
+
+xbee.printf("Communication terminated! \r\n");
+
+}
+
+}
\ No newline at end of file