JKDVSK`

Dependencies:   ADXL362 mbed

Fork of mbed_menu by Rosie Lewis

Committer:
jbillingham
Date:
Wed Nov 08 11:57:56 2017 +0000
Revision:
4:f1fcad386421
Parent:
3:551061b40ccc
Child:
5:ae4520f72498
jbvksv; ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jbillingham 2:2b7eb44d597d 1 //include relevant libraries
mzpf46 0:8d536d5c58c6 2 #include "mbed.h"
mzpf46 0:8d536d5c58c6 3 #include "stdio.h"
mzpf46 0:8d536d5c58c6 4 #include "math.h"
jbillingham 4:f1fcad386421 5 #include "ADXL362.h"
jbillingham 2:2b7eb44d597d 6
jbillingham 2:2b7eb44d597d 7 //Define serial connection
mzpf46 0:8d536d5c58c6 8 Serial pc(USBTX, USBRX);
jbillingham 2:2b7eb44d597d 9 //Define accelerometer
mzpf46 0:8d536d5c58c6 10 ADXL362 adxl362(p11, p12, p13, p10);
jbillingham 2:2b7eb44d597d 11 //Set up local file system to allow writing of txt files
mzpf46 0:8d536d5c58c6 12 LocalFileSystem local("local");
jbillingham 2:2b7eb44d597d 13
jbillingham 2:2b7eb44d597d 14
jbillingham 2:2b7eb44d597d 15 //Define paramater strings for communication over USB
mzpf46 0:8d536d5c58c6 16 char N[1000]; //Number of samples
mzpf46 0:8d536d5c58c6 17 char T [1000]; //Sample period
mzpf46 0:8d536d5c58c6 18
mzpf46 0:8d536d5c58c6 19
mzpf46 0:8d536d5c58c6 20 int main()
mzpf46 0:8d536d5c58c6 21 {
jbillingham 2:2b7eb44d597d 22 //Create file with pointer
jbillingham 2:2b7eb44d597d 23 FILE *samplesettings;
mzpf46 1:4938aa677cb3 24 FILE *timesettings;
mzpf46 0:8d536d5c58c6 25
jbillingham 2:2b7eb44d597d 26 char res[100];//String to store user command
mzpf46 1:4938aa677cb3 27
jbillingham 4:f1fcad386421 28 pc.scanf("%s",res);//Get user command from matlab
mzpf46 1:4938aa677cb3 29
jbillingham 2:2b7eb44d597d 30 while(1)
jbillingham 2:2b7eb44d597d 31 {
jbillingham 2:2b7eb44d597d 32 if(!strcmp(res,"1")==0)//if user command in matlab is 1 write the sample settings into a file
mzpf46 0:8d536d5c58c6 33 {
jbillingham 2:2b7eb44d597d 34 char N[1000];//To store the number of samples which is received from the matlab
jbillingham 2:2b7eb44d597d 35 pc.scanf("%s\n",&N);//to get the number of samples input from the matlab
jbillingham 2:2b7eb44d597d 36 samplesettings = fopen("/local/samplesettings.txt", "w");//Open file for writing
jbillingham 2:2b7eb44d597d 37 fprintf(samplesettings,"%s\n",N); //Writes settings values onto file
jbillingham 2:2b7eb44d597d 38 fclose(samplesettings);//close file
jbillingham 2:2b7eb44d597d 39 break;
jbillingham 2:2b7eb44d597d 40 }
jbillingham 2:2b7eb44d597d 41 else if(!strcmp(res,"2")==0)//If user command is 2 search the file for sample settings
jbillingham 2:2b7eb44d597d 42 {
jbillingham 2:2b7eb44d597d 43 //Define variables for output
jbillingham 2:2b7eb44d597d 44 char value1[1000];
jbillingham 2:2b7eb44d597d 45 samplesettings = fopen("/local/samplesettings.txt" , "r"); //Open file for reading
jbillingham 2:2b7eb44d597d 46 fseek(samplesettings, 0 ,SEEK_SET); //Sets cursor to start of file
jbillingham 2:2b7eb44d597d 47 fscanf(samplesettings,"%s\n", value1);//Scans fo the value of N
jbillingham 2:2b7eb44d597d 48 pc.printf("%s\n", value1);//Prints the value of N
jbillingham 2:2b7eb44d597d 49 fclose(samplesettings); //Close file
jbillingham 2:2b7eb44d597d 50 break;
jbillingham 2:2b7eb44d597d 51 }
jbillingham 2:2b7eb44d597d 52 else if(!strcmp(res,"3")==0)//If user command recieved is 3 then write the time settings into a file
jbillingham 2:2b7eb44d597d 53 {
jbillingham 2:2b7eb44d597d 54 char T[1000];//To store value of T recieved from matlab
jbillingham 2:2b7eb44d597d 55 pc.scanf("%s\n",&T);//To scan value of T from matlab
jbillingham 2:2b7eb44d597d 56 timesettings = fopen("/local/timesettings.txt", "w");//Open file for writing
jbillingham 2:2b7eb44d597d 57 fprintf(timesettings,"%s\n",T); //Writes settings values onto file
jbillingham 2:2b7eb44d597d 58 fclose(timesettings);//Close file
jbillingham 2:2b7eb44d597d 59 break;
jbillingham 2:2b7eb44d597d 60 }
jbillingham 2:2b7eb44d597d 61 else if(!strcmp(res,"4")==0)//If 4 is recieved as a user command then read the time settings
jbillingham 2:2b7eb44d597d 62 {
jbillingham 2:2b7eb44d597d 63 //Define variables for output
jbillingham 2:2b7eb44d597d 64 char value2[100];
jbillingham 2:2b7eb44d597d 65 samplesettings = fopen("/local/timesettings.txt" , "r"); //Open file for reading
jbillingham 2:2b7eb44d597d 66 fseek(timesettings, 0 ,SEEK_SET); //Sets cursor to start of file
jbillingham 2:2b7eb44d597d 67 fscanf(timesettings,"%s\n", value2);//Reads the time period from the file
jbillingham 2:2b7eb44d597d 68 pc.printf("%s\n", value2);//Outsputs the time period
jbillingham 2:2b7eb44d597d 69 fclose(timesettings); //Close file
jbillingham 2:2b7eb44d597d 70 break;
jbillingham 2:2b7eb44d597d 71 }
jbillingham 2:2b7eb44d597d 72 else if(!strcmp(res,"5")==0)//If the user command is 5 then we collect data
jbillingham 2:2b7eb44d597d 73 {
jbillingham 2:2b7eb44d597d 74 //Initialise accelerometer
jbillingham 2:2b7eb44d597d 75 adxl362.init_spi();
jbillingham 2:2b7eb44d597d 76 adxl362.init_adxl362();
jbillingham 2:2b7eb44d597d 77 wait(0.1);
jbillingham 2:2b7eb44d597d 78 int8_t xdata, ydata, zdata;
jbillingham 2:2b7eb44d597d 79 for(int i=0; i<atoi(N); i++)
mzpf46 0:8d536d5c58c6 80 {
jbillingham 2:2b7eb44d597d 81 adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);//Fetch readings from accelerometer
jbillingham 2:2b7eb44d597d 82 pc.printf("%+04d\n", xdata);
jbillingham 2:2b7eb44d597d 83 pc.printf("%+04d\n", ydata);
jbillingham 2:2b7eb44d597d 84 pc.printf("%+04d\n", zdata);
jbillingham 2:2b7eb44d597d 85 wait(atoi(T));
jbillingham 2:2b7eb44d597d 86
jbillingham 2:2b7eb44d597d 87 //Open data file
jbillingham 2:2b7eb44d597d 88 FILE *data;
jbillingham 2:2b7eb44d597d 89 data = fopen("/local/data.txt", "w");
jbillingham 2:2b7eb44d597d 90 //Data collection loop using sample rate and number of samples
jbillingham 2:2b7eb44d597d 91 for(int i=0; i<atoi(N); i++)
jbillingham 2:2b7eb44d597d 92 {
jbillingham 2:2b7eb44d597d 93 //Get accelerometer data
jbillingham 2:2b7eb44d597d 94 adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);
jbillingham 2:2b7eb44d597d 95 //Print data to data file
jbillingham 2:2b7eb44d597d 96 fprintf(data, "%+04d", xdata);
jbillingham 2:2b7eb44d597d 97 fprintf(data, "%+04d", ydata);
jbillingham 2:2b7eb44d597d 98 fprintf(data, "%+04d", zdata);
jbillingham 2:2b7eb44d597d 99 //Attempt to print same data to MATLAB if connection is not null
jbillingham 2:2b7eb44d597d 100 if(pc != NULL)
jbillingham 2:2b7eb44d597d 101 {
jbillingham 2:2b7eb44d597d 102 pc.printf("%+04d\n", xdata);
jbillingham 2:2b7eb44d597d 103 pc.printf("%+04d\n", ydata);
jbillingham 2:2b7eb44d597d 104 pc.printf("%+04d\n", zdata);
jbillingham 2:2b7eb44d597d 105 }
jbillingham 2:2b7eb44d597d 106 //Pause to ensure sample rate
jbillingham 2:2b7eb44d597d 107 wait(atoi(T));
jbillingham 2:2b7eb44d597d 108 }
jbillingham 2:2b7eb44d597d 109 fclose(data);
jbillingham 2:2b7eb44d597d 110 break;
jbillingham 2:2b7eb44d597d 111 }
jbillingham 2:2b7eb44d597d 112 }
jbillingham 2:2b7eb44d597d 113 }
mzpf46 0:8d536d5c58c6 114 }