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
main.cpp
- Committer:
- c_haarm
- Date:
- 2016-09-04
- Revision:
- 1:8b45b3ad350a
- Parent:
- 0:f7cd934498e4
- Child:
- 2:5c856ceaea2e
File content as of revision 1:8b45b3ad350a:
#include "mbed.h" #include "MODSERIAL.h" #include "SDFileSystem.h" ////////////////////Declarations//////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // Thermocouple pin declarations DigitalOut SCK_1(2); DigitalOut CS_1(3); int pins[12] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16}; for (int i = 0; i <= 11; i++) { DigitalOut(pins[i], INPUT); } double d; int b[12] = { }; // Torque sensor AnalogIn fcs(PTB2); // force sensor output connected to analog 0 // LED DigitalOut r_led(LED_RED); DigitalOut g_led(LED_GREEN); DigitalOut b_led(LED_BLUE); // Serial connection and SD MODSERIAL pc(USBTX,USBRX, 1024, 512); SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS FILE *fp; // Other declarations 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_torque[64]; char filename_temp[64]; char filename_cal[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 } void readTC() { CS_1 = 0; wait_ms(70); for (int i = 15; i >= 0; i--) { SCK_1 = 0; SCK_1 = 1; for (int j = 0; j <= 11; j++) { if (digitalRead(pins[j])) { b[j] |= (1 << i); } } } CS_1 = 1; // capture torque sensor data fcs_read = fcs.read(); // output data to file fprintf(fp, "%.3f\t", fcs_read); for (int j = 0; j <= 11; j++) { if (b[j] & 0x1) { b[j] = 20000; } else { b[j] &= 0xfffc; d = b[j] / 16.0 * 100.0; b[j] = d; } fprintf(fp, "%.3f\t", b[j]); } fprintf(fp, "\n\r"); } ////////////////////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_cal, "/sd/cal_torque_%d.txt", cal_numb); //construct new textfile fp = fopen(filename_cal, "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_cal); //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) { readTC(b); } 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; } } } }