Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 18:8b3efa4d4a36
- Parent:
- 17:c2e21d347ca5
- Child:
- 19:c0d59019de53
diff -r c2e21d347ca5 -r 8b3efa4d4a36 main.cpp --- a/main.cpp Sun Dec 02 15:10:57 2018 +0000 +++ b/main.cpp Mon Dec 03 15:51:51 2018 +0000 @@ -3,6 +3,10 @@ m3pi m3pi; +//File name +#define FILESYS "robot" +#define FILENAME "/robot/data.txt" + // Minimum and maximum motor speeds #define MAX 0.3 #define MIN 0 @@ -12,6 +16,50 @@ #define I_TERM 0 #define D_TERM 8 + +//typedef -> definer som type i C++ +//uden typedef - det er ikke en type, så du skal skrive struct performance_data +typedef struct performance_data { //<- navn på structen + float wh; + int pitstops; +} performance_data; //<- navn på typen + + +void write_to_file(performance_data data) { + + //Define file system + LocalFileSystem local(FILESYS); + + //Make a pointer to the file + FILE *fp = fopen(FILENAME, "w"); //"w" "r" "a" + + fprintf(fp, "%0.2f %d", data.wh, data.pitstops); //write to the file + + fclose(fp); //close when done. + +} + +performance_data read_from_file() { + + performance_data data; + + + //Define file system + LocalFileSystem local(FILESYS); + + //Make a pointer to the file + FILE *fp = fopen(FILENAME, "r"); //"w" "r" "a" + + //Make some error prevention later + fscanf(fp, "%f %d", &(data.wh), &(data.pitstops) ); //write to the file + + fclose(fp); //close when done. + + return data; + +} + + void sensor_all(int arr[]) //lav en funktion? ved ikke hvad jeg skal her { m3pi.putc(0x87); // send noget sensor data @@ -48,6 +96,24 @@ } int main() { + + //test the function to write to a local file + performance_data robot_stats; + robot_stats.wh = 120.5; + robot_stats.pitstops = 200; + + write_to_file(robot_stats); + + performance_data new_data; + new_data = read_from_file(); + //might get an error here due to timing + + + new_data.wh = new_data.wh * 2; + new_data.pitstops = new_data.pitstops * 2; + + write_to_file(new_data); + //should get 247... and 246 m3pi.locate(0,1); m3pi.printf("Line PID");