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.
Dependencies: MODSERIAL SDFileSystemSeth mbed
Fork of Opstellingbachelor_opdracht by
Diff: main.cpp
- Revision:
- 0:f7cd934498e4
- Child:
- 1:8b45b3ad350a
diff -r 000000000000 -r f7cd934498e4 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Sep 04 18:20:08 2016 +0000 @@ -0,0 +1,133 @@ +#include "mbed.h" +#include "MODSERIAL.h" +#include "SDFileSystem.h" + + +////////////////////Declarations//////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +AnalogIn fcs(PTB2); // force sensor output connected to analog 0 + +DigitalOut r_led(LED_RED); +DigitalOut g_led(LED_GREEN); +DigitalOut b_led(LED_BLUE); + + +MODSERIAL pc(USBTX,USBRX, 1024, 512); +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS +FILE *fp; + +Ticker tick; + +const int baudrate = 115200; // baud rate +const float Ts = 0.005; // sample time (sec) + +const int led_on = 0; +const int led_off = 1; + +volatile bool fn1_go = 0; // go-flag starts in disabled state +volatile bool start = 0; + +float fcs_read; + + +int trial_numb = 0; +int cal_numb = 1; + +char filename[64]; + + + +////////////////////Functions/////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +void led_control(bool r, bool g, bool b) // control rgb LEDs +{ + r_led.write(r); + g_led.write(g); + b_led.write(b); + return; +} + +void fn1_activate() // function called by ticker every Ts seconds +{ + fn1_go = 1; // go-flag active +} + + +////////////////////Main//////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +int main() +{ + pc.baud(baudrate); + tick.attach(&fn1_activate,Ts); + led_control(led_off,led_off,led_off); // start with LEDs off + + while (1) { + if(fn1_go) { // statements to execute when go-flag is active + fn1_go = 0; + + } + + if(pc.readable()) { // if character available + switch(pc.getc()) { // read a character from keyboard + case 'c': + led_control(led_on,led_on,led_off); //orange LED during calibration + sprintf(filename, "/sd/cal_torque_%d.txt", cal_numb); //construct new textfile + fp = fopen(filename, "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } else { + while(1) { + for (int i = 0; i<50; i++) { //get 50 samples from torque sensor + fcs_read = fcs.read(); + fprintf(fp, "%.3f\t \n\r", fcs_read); //output values to the screen + } + break; + } + } + fclose(fp); //close file + cal_numb++; //increment calibration number + pc.printf("File saved as %s\n\r", filename); //print name of calibration file to screen + pc.printf("Press 'c' to record new calibration or 't' to start new trial\n\r"); // print message to screen + break; + case 't': + trial_numb++; + pc.printf("Trial number: %d\n\r", trial_numb); + pc.printf("Press 's' to start measurement\n\r"); + break; + case 's': + start = !start; + if (start) { + led_control(led_off,led_on,led_off); //green LED during calibration + sprintf(filename_torque, "/sd/trial%d.txt", trial_numb); //construct new textfile to store data from torque sensor + fp = fopen(filename_torque, "w"); + if(fp == NULL) { + error("Could not open file for write\n\r"); + } else { + pc.printf("Measurement started... \n\r"); + pc.printf("Press 's' to stop measurement\n\r"); + while(1) { + fcs_read = fcs.read(); + fprintf(fp, "%.3f\t \n\r", fcs_read); + } + break; + } + + break; + } else { + led_control(led_on,led_off,led_off); // RED LED when ready to stopped measuring + fclose(fp); + pc.printf("File saved as %s\n\r", filename_torque); // print filename to screen + pc.printf("Press 'c' to perform new calibration or 't' to start new trial\n\r"); // print message to screen + + } + break; + } + } + } +}