Jake Billingham
/
computingproject
JKDVSK`
Fork of mbed_menu by
menu_menu.cpp@3:551061b40ccc, 2017-11-08 (annotated)
- Committer:
- jbillingham
- Date:
- Wed Nov 08 11:37:44 2017 +0000
- Revision:
- 3:551061b40ccc
- Parent:
- 2:2b7eb44d597d
- Child:
- 4:f1fcad386421
JHDVKSLJZ;
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:551061b40ccc | 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 | //??? |
mzpf46 | 0:8d536d5c58c6 | 15 | int matlabdata; |
mzpf46 | 0:8d536d5c58c6 | 16 | char usercmd; |
jbillingham | 2:2b7eb44d597d | 17 | |
jbillingham | 2:2b7eb44d597d | 18 | //Define paramater strings for communication over USB |
mzpf46 | 0:8d536d5c58c6 | 19 | char N[1000]; //Number of samples |
mzpf46 | 0:8d536d5c58c6 | 20 | char T [1000]; //Sample period |
mzpf46 | 0:8d536d5c58c6 | 21 | |
mzpf46 | 0:8d536d5c58c6 | 22 | |
mzpf46 | 0:8d536d5c58c6 | 23 | int main() |
mzpf46 | 0:8d536d5c58c6 | 24 | { |
jbillingham | 2:2b7eb44d597d | 25 | //Create file with pointer |
jbillingham | 2:2b7eb44d597d | 26 | FILE *samplesettings; |
mzpf46 | 1:4938aa677cb3 | 27 | FILE *timesettings; |
mzpf46 | 0:8d536d5c58c6 | 28 | |
jbillingham | 2:2b7eb44d597d | 29 | char res[100];//String to store user command |
mzpf46 | 1:4938aa677cb3 | 30 | |
mzpf46 | 1:4938aa677cb3 | 31 | matlabdata=pc.scanf("%s",res);//Get user command from matlab |
mzpf46 | 1:4938aa677cb3 | 32 | |
jbillingham | 2:2b7eb44d597d | 33 | while(1) |
jbillingham | 2:2b7eb44d597d | 34 | { |
jbillingham | 2:2b7eb44d597d | 35 | if(!strcmp(res,"1")==0)//if user command in matlab is 1 write the sample settings into a file |
mzpf46 | 0:8d536d5c58c6 | 36 | { |
jbillingham | 2:2b7eb44d597d | 37 | char N[1000];//To store the number of samples which is received from the matlab |
jbillingham | 2:2b7eb44d597d | 38 | pc.scanf("%s\n",&N);//to get the number of samples input from the matlab |
jbillingham | 2:2b7eb44d597d | 39 | samplesettings = fopen("/local/samplesettings.txt", "w");//Open file for writing |
jbillingham | 2:2b7eb44d597d | 40 | fprintf(samplesettings,"%s\n",N); //Writes settings values onto file |
jbillingham | 2:2b7eb44d597d | 41 | fclose(samplesettings);//close file |
jbillingham | 2:2b7eb44d597d | 42 | break; |
jbillingham | 2:2b7eb44d597d | 43 | } |
jbillingham | 2:2b7eb44d597d | 44 | else if(!strcmp(res,"2")==0)//If user command is 2 search the file for sample settings |
jbillingham | 2:2b7eb44d597d | 45 | { |
jbillingham | 2:2b7eb44d597d | 46 | //Define variables for output |
jbillingham | 2:2b7eb44d597d | 47 | char value1[1000]; |
jbillingham | 2:2b7eb44d597d | 48 | samplesettings = fopen("/local/samplesettings.txt" , "r"); //Open file for reading |
jbillingham | 2:2b7eb44d597d | 49 | fseek(samplesettings, 0 ,SEEK_SET); //Sets cursor to start of file |
jbillingham | 2:2b7eb44d597d | 50 | fscanf(samplesettings,"%s\n", value1);//Scans fo the value of N |
jbillingham | 2:2b7eb44d597d | 51 | pc.printf("%s\n", value1);//Prints the value of N |
jbillingham | 2:2b7eb44d597d | 52 | fclose(samplesettings); //Close file |
jbillingham | 2:2b7eb44d597d | 53 | break; |
jbillingham | 2:2b7eb44d597d | 54 | } |
jbillingham | 2:2b7eb44d597d | 55 | else if(!strcmp(res,"3")==0)//If user command recieved is 3 then write the time settings into a file |
jbillingham | 2:2b7eb44d597d | 56 | { |
jbillingham | 2:2b7eb44d597d | 57 | char T[1000];//To store value of T recieved from matlab |
jbillingham | 2:2b7eb44d597d | 58 | pc.scanf("%s\n",&T);//To scan value of T from matlab |
jbillingham | 2:2b7eb44d597d | 59 | timesettings = fopen("/local/timesettings.txt", "w");//Open file for writing |
jbillingham | 2:2b7eb44d597d | 60 | fprintf(timesettings,"%s\n",T); //Writes settings values onto file |
jbillingham | 2:2b7eb44d597d | 61 | fclose(timesettings);//Close file |
jbillingham | 2:2b7eb44d597d | 62 | break; |
jbillingham | 2:2b7eb44d597d | 63 | } |
jbillingham | 2:2b7eb44d597d | 64 | else if(!strcmp(res,"4")==0)//If 4 is recieved as a user command then read the time settings |
jbillingham | 2:2b7eb44d597d | 65 | { |
jbillingham | 2:2b7eb44d597d | 66 | //Define variables for output |
jbillingham | 2:2b7eb44d597d | 67 | char value2[100]; |
jbillingham | 2:2b7eb44d597d | 68 | samplesettings = fopen("/local/timesettings.txt" , "r"); //Open file for reading |
jbillingham | 2:2b7eb44d597d | 69 | fseek(timesettings, 0 ,SEEK_SET); //Sets cursor to start of file |
jbillingham | 2:2b7eb44d597d | 70 | fscanf(timesettings,"%s\n", value2);//Reads the time period from the file |
jbillingham | 2:2b7eb44d597d | 71 | pc.printf("%s\n", value2);//Outsputs the time period |
jbillingham | 2:2b7eb44d597d | 72 | fclose(timesettings); //Close file |
jbillingham | 2:2b7eb44d597d | 73 | break; |
jbillingham | 2:2b7eb44d597d | 74 | } |
jbillingham | 2:2b7eb44d597d | 75 | else if(!strcmp(res,"5")==0)//If the user command is 5 then we collect data |
jbillingham | 2:2b7eb44d597d | 76 | { |
jbillingham | 2:2b7eb44d597d | 77 | //Initialise accelerometer |
jbillingham | 2:2b7eb44d597d | 78 | adxl362.init_spi(); |
jbillingham | 2:2b7eb44d597d | 79 | adxl362.init_adxl362(); |
jbillingham | 2:2b7eb44d597d | 80 | wait(0.1); |
jbillingham | 2:2b7eb44d597d | 81 | int8_t xdata, ydata, zdata; |
jbillingham | 2:2b7eb44d597d | 82 | for(int i=0; i<atoi(N); i++) |
mzpf46 | 0:8d536d5c58c6 | 83 | { |
jbillingham | 2:2b7eb44d597d | 84 | adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);//Fetch readings from accelerometer |
jbillingham | 2:2b7eb44d597d | 85 | pc.printf("%+04d\n", xdata); |
jbillingham | 2:2b7eb44d597d | 86 | pc.printf("%+04d\n", ydata); |
jbillingham | 2:2b7eb44d597d | 87 | pc.printf("%+04d\n", zdata); |
jbillingham | 2:2b7eb44d597d | 88 | wait(atoi(T)); |
jbillingham | 2:2b7eb44d597d | 89 | |
jbillingham | 2:2b7eb44d597d | 90 | //Open data file |
jbillingham | 2:2b7eb44d597d | 91 | FILE *data; |
jbillingham | 2:2b7eb44d597d | 92 | data = fopen("/local/data.txt", "w"); |
jbillingham | 2:2b7eb44d597d | 93 | //Data collection loop using sample rate and number of samples |
jbillingham | 2:2b7eb44d597d | 94 | for(int i=0; i<atoi(N); i++) |
jbillingham | 2:2b7eb44d597d | 95 | { |
jbillingham | 2:2b7eb44d597d | 96 | //Get accelerometer data |
jbillingham | 2:2b7eb44d597d | 97 | adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata); |
jbillingham | 2:2b7eb44d597d | 98 | //Print data to data file |
jbillingham | 2:2b7eb44d597d | 99 | fprintf(data, "%+04d", xdata); |
jbillingham | 2:2b7eb44d597d | 100 | fprintf(data, "%+04d", ydata); |
jbillingham | 2:2b7eb44d597d | 101 | fprintf(data, "%+04d", zdata); |
jbillingham | 2:2b7eb44d597d | 102 | //Attempt to print same data to MATLAB if connection is not null |
jbillingham | 2:2b7eb44d597d | 103 | if(pc != NULL) |
jbillingham | 2:2b7eb44d597d | 104 | { |
jbillingham | 2:2b7eb44d597d | 105 | pc.printf("%+04d\n", xdata); |
jbillingham | 2:2b7eb44d597d | 106 | pc.printf("%+04d\n", ydata); |
jbillingham | 2:2b7eb44d597d | 107 | pc.printf("%+04d\n", zdata); |
jbillingham | 2:2b7eb44d597d | 108 | } |
jbillingham | 2:2b7eb44d597d | 109 | //Pause to ensure sample rate |
jbillingham | 2:2b7eb44d597d | 110 | wait(atoi(T)); |
jbillingham | 2:2b7eb44d597d | 111 | } |
jbillingham | 2:2b7eb44d597d | 112 | fclose(data); |
jbillingham | 2:2b7eb44d597d | 113 | break; |
jbillingham | 2:2b7eb44d597d | 114 | } |
jbillingham | 2:2b7eb44d597d | 115 | } |
jbillingham | 2:2b7eb44d597d | 116 | } |
mzpf46 | 0:8d536d5c58c6 | 117 | } |