spr code

Dependencies:   SDFileSystem mbed

Fork of FTF2014_lab4 by Freescale

Committer:
faithcerebral
Date:
Tue Aug 30 13:37:28 2016 +0000
Revision:
3:b1f5d765d066
Parent:
0:a83db87be46c
to

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:a83db87be46c 1 #include "mbed.h"
Kojto 0:a83db87be46c 2 #include "SDFileSystem.h"
Kojto 0:a83db87be46c 3
Kojto 0:a83db87be46c 4 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
Kojto 0:a83db87be46c 5 Serial pc(USBTX, USBRX);
Kojto 0:a83db87be46c 6 FILE *fp;
faithcerebral 3:b1f5d765d066 7 FILE *dp;
faithcerebral 3:b1f5d765d066 8
Kojto 0:a83db87be46c 9 char buffer[1024];
Kojto 0:a83db87be46c 10
faithcerebral 3:b1f5d765d066 11 AnalogIn sensor(PTB2);
faithcerebral 3:b1f5d765d066 12
faithcerebral 3:b1f5d765d066 13 InterruptIn event(PTB3);
faithcerebral 3:b1f5d765d066 14 AnalogOut led(PTC10);
faithcerebral 3:b1f5d765d066 15
faithcerebral 3:b1f5d765d066 16 int interrupt = 0;
faithcerebral 3:b1f5d765d066 17 Timer clk;
faithcerebral 3:b1f5d765d066 18 Timer clkb;
faithcerebral 3:b1f5d765d066 19
faithcerebral 3:b1f5d765d066 20 /*Configuration_settings*/
faithcerebral 3:b1f5d765d066 21 typedef struct {
faithcerebral 3:b1f5d765d066 22 float min_angle;
faithcerebral 3:b1f5d765d066 23 float max_angle;
faithcerebral 3:b1f5d765d066 24 float step_size;
faithcerebral 3:b1f5d765d066 25 } data;
faithcerebral 3:b1f5d765d066 26 data config;
faithcerebral 3:b1f5d765d066 27
faithcerebral 3:b1f5d765d066 28 void ReadConfigurationSettings()
faithcerebral 3:b1f5d765d066 29 {
faithcerebral 3:b1f5d765d066 30 //http://www.inmoov.fr/wp-content/uploads/2015/02/Introduction-to-Servo-Motors-Arduino.pdf
faithcerebral 3:b1f5d765d066 31 //length of pulse corresponds to angle the servo must turn to
faithcerebral 3:b1f5d765d066 32 //1 ms = 0deg, 1.5ms = 90
faithcerebral 3:b1f5d765d066 33 //2 ms = 180deg
faithcerebral 3:b1f5d765d066 34
faithcerebral 3:b1f5d765d066 35
Kojto 0:a83db87be46c 36 pc.printf("Initializing \n");
faithcerebral 3:b1f5d765d066 37 dp = fopen("/sd/config.txt", "r");
faithcerebral 3:b1f5d765d066 38 if(dp == NULL) {
faithcerebral 3:b1f5d765d066 39 pc.printf("Could not open file for write\r\n");
faithcerebral 3:b1f5d765d066 40 }
faithcerebral 3:b1f5d765d066 41
faithcerebral 3:b1f5d765d066 42 pc.printf("Found the file!");
faithcerebral 3:b1f5d765d066 43 while(!feof (dp)){
faithcerebral 3:b1f5d765d066 44 fscanf(dp, "%f ", &config.min_angle);// in degrees
faithcerebral 3:b1f5d765d066 45 fscanf(dp, "%f ", &config.max_angle);// in degrees
faithcerebral 3:b1f5d765d066 46 fscanf(dp, "%f ", &config.step_size);//in degrees
faithcerebral 3:b1f5d765d066 47
faithcerebral 3:b1f5d765d066 48 }
faithcerebral 3:b1f5d765d066 49 pc.printf("%f, %f, %f", config.min_angle, config.max_angle, config.step_size);
faithcerebral 3:b1f5d765d066 50 fclose(dp);
faithcerebral 3:b1f5d765d066 51
faithcerebral 3:b1f5d765d066 52
faithcerebral 3:b1f5d765d066 53
faithcerebral 3:b1f5d765d066 54 }
faithcerebral 3:b1f5d765d066 55
Kojto 0:a83db87be46c 56
faithcerebral 3:b1f5d765d066 57 void trigger(){
faithcerebral 3:b1f5d765d066 58 interrupt = abs((interrupt - 1) );
faithcerebral 3:b1f5d765d066 59 led = !led;
faithcerebral 3:b1f5d765d066 60 //angle = angle+step_size;
faithcerebral 3:b1f5d765d066 61 // return angle;
faithcerebral 3:b1f5d765d066 62 }
faithcerebral 3:b1f5d765d066 63
faithcerebral 3:b1f5d765d066 64 void BackgroundReading()
faithcerebral 3:b1f5d765d066 65 //this function opens the sd folder and saves the background reading for 10seconds
faithcerebral 3:b1f5d765d066 66 {
faithcerebral 3:b1f5d765d066 67 float time;
faithcerebral 3:b1f5d765d066 68 float reading;
faithcerebral 3:b1f5d765d066 69 FILE *fp = fopen("/sd/mydir/data.csv", "a");
faithcerebral 3:b1f5d765d066 70 if (fp == NULL){
faithcerebral 3:b1f5d765d066 71 pc.printf("Could not opne file for write \r \n");
faithcerebral 3:b1f5d765d066 72 }
faithcerebral 3:b1f5d765d066 73 //start the time and record background for 10seconds
faithcerebral 3:b1f5d765d066 74 clkb.start();
faithcerebral 3:b1f5d765d066 75 time = clkb.read();
faithcerebral 3:b1f5d765d066 76 fprintf(fp, "Background reading\n");
faithcerebral 3:b1f5d765d066 77
faithcerebral 3:b1f5d765d066 78 while(time < 10)
faithcerebral 3:b1f5d765d066 79 {
faithcerebral 3:b1f5d765d066 80 reading = sensor.read();
faithcerebral 3:b1f5d765d066 81 //save in buffer
faithcerebral 3:b1f5d765d066 82 }
faithcerebral 3:b1f5d765d066 83 //at the end of the 10 seconds write in file
faithcerebral 3:b1f5d765d066 84
faithcerebral 3:b1f5d765d066 85
faithcerebral 3:b1f5d765d066 86 }
faithcerebral 3:b1f5d765d066 87
faithcerebral 3:b1f5d765d066 88 void OpenCsv()
faithcerebral 3:b1f5d765d066 89 {
faithcerebral 3:b1f5d765d066 90 mkdir("/sd/mydir", 0777); //make directrory
faithcerebral 3:b1f5d765d066 91 wait(0.2);
faithcerebral 3:b1f5d765d066 92
faithcerebral 3:b1f5d765d066 93 FILE *fp = fopen("/sd/mydir/data.csv", "w");
faithcerebral 3:b1f5d765d066 94 if(fp == NULL) {
faithcerebral 3:b1f5d765d066 95 pc.printf("Could not open file for write\r\n");
faithcerebral 3:b1f5d765d066 96 }
faithcerebral 3:b1f5d765d066 97 fprintf(fp, "value, time, angle \n");
faithcerebral 3:b1f5d765d066 98 fclose(fp);
faithcerebral 3:b1f5d765d066 99 }
faithcerebral 3:b1f5d765d066 100 void SaveCsv(FILE *fp, float value, float time, float angle)
faithcerebral 3:b1f5d765d066 101 {
faithcerebral 3:b1f5d765d066 102 fp = fopen("/sd/mydir/data.csv", "a");
faithcerebral 3:b1f5d765d066 103 fprintf(fp, "%d, %3f ,%f \n", angle, time, value);
faithcerebral 3:b1f5d765d066 104 fclose(fp);
Kojto 0:a83db87be46c 105 }
Kojto 0:a83db87be46c 106
faithcerebral 3:b1f5d765d066 107 int main() {
faithcerebral 3:b1f5d765d066 108
faithcerebral 3:b1f5d765d066 109 event.rise(&trigger);
faithcerebral 3:b1f5d765d066 110 float value = 0.0;
faithcerebral 3:b1f5d765d066 111 float time_ms = 0.0, time_s =0.0, step_time = 0.0;
faithcerebral 3:b1f5d765d066 112 int angle =0 ;
faithcerebral 3:b1f5d765d066 113
faithcerebral 3:b1f5d765d066 114
faithcerebral 3:b1f5d765d066 115
faithcerebral 3:b1f5d765d066 116 ReadConfigurationSettings();
faithcerebral 3:b1f5d765d066 117 wait(2);
faithcerebral 3:b1f5d765d066 118 OpenCsv();
faithcerebral 3:b1f5d765d066 119
faithcerebral 3:b1f5d765d066 120 clk.start();
faithcerebral 3:b1f5d765d066 121 time_s = (clk.read_ms())/1000;
faithcerebral 3:b1f5d765d066 122
faithcerebral 3:b1f5d765d066 123 while (1){ //run for specific time.
faithcerebral 3:b1f5d765d066 124 value = sensor.read();
faithcerebral 3:b1f5d765d066 125 time_ms = clk.read_ms();
faithcerebral 3:b1f5d765d066 126 if (time_ms < 500)
faithcerebral 3:b1f5d765d066 127 {
faithcerebral 3:b1f5d765d066 128 time_s = time_ms/1000;
faithcerebral 3:b1f5d765d066 129 }
faithcerebral 3:b1f5d765d066 130 else {
faithcerebral 3:b1f5d765d066 131 time_s = ((time_ms + 500)/1000 ); //convert milliseconds to seconds
faithcerebral 3:b1f5d765d066 132 }
faithcerebral 3:b1f5d765d066 133 __disable_irq();
faithcerebral 3:b1f5d765d066 134 if(interrupt == 0){
faithcerebral 3:b1f5d765d066 135 SaveCsv(fp, angle, time_s, value);
faithcerebral 3:b1f5d765d066 136 pc.printf("%f, %3f\n", value, time_s);
faithcerebral 3:b1f5d765d066 137 }
faithcerebral 3:b1f5d765d066 138
faithcerebral 3:b1f5d765d066 139 __enable_irq();
faithcerebral 3:b1f5d765d066 140 }
faithcerebral 3:b1f5d765d066 141
Kojto 0:a83db87be46c 142 fclose(fp);
faithcerebral 3:b1f5d765d066 143
faithcerebral 3:b1f5d765d066 144
faithcerebral 3:b1f5d765d066 145 }
Kojto 0:a83db87be46c 146
faithcerebral 3:b1f5d765d066 147