torque + thermocouple measurement
Dependencies: MODSERIAL SDFileSystem mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MODSERIAL.h" 00003 #include "SDFileSystem.h" 00004 00005 00006 ////////////////////Declarations//////////////////////////////////////////////// 00007 //////////////////////////////////////////////////////////////////////////////// 00008 //////////////////////////////////////////////////////////////////////////////// 00009 00010 // Thermocouple pin declarations 00011 DigitalOut SCK_1(PTD1); // sck pin of thermocouples 00012 DigitalOut CS_1(PTE25); // chip select pin of thermocouples 00013 00014 00015 //DigitalIn pins[12]={D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13}; //thermocouples 1 - 12 select corresponding pin numbers mbed 00016 DigitalIn pins[3]={D2, D3, D4}; //thermocouple select corresponding pin numbers mbed 00017 00018 00019 // Torque sensor 00020 AnalogIn fcs(PTB2); // force sensor output connected to analog 0 00021 00022 00023 // LED 00024 DigitalOut r_led(LED_RED); 00025 DigitalOut g_led(LED_GREEN); 00026 DigitalOut b_led(LED_BLUE); 00027 00028 // Serial connection and SD 00029 MODSERIAL pc(USBTX,USBRX, 1024, 512); 00030 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS 00031 FILE *fp; 00032 00033 // Other declarations 00034 Ticker tick; 00035 00036 const int baudrate = 115200; // baud rate 00037 const float Ts = 0.25; // sample time (sec) 00038 00039 const int led_on = 0; 00040 const int led_off = 1; 00041 00042 volatile bool fn1_go = 0; // go-flag starts in disabled state 00043 volatile bool start = 0; 00044 volatile bool meas = 0; 00045 00046 float fcs_read; 00047 00048 int trial_numb = 0; 00049 int cal_numb = 1; 00050 00051 char filename_torque[64]; 00052 char filename_temp[64]; 00053 char filename_cal[64]; 00054 00055 00056 00057 00058 ////////////////////Functions/////////////////////////////////////////////////// 00059 //////////////////////////////////////////////////////////////////////////////// 00060 //////////////////////////////////////////////////////////////////////////////// 00061 00062 void led_control(bool r, bool g, bool b) // control rgb LEDs 00063 { 00064 r_led.write(r); 00065 g_led.write(g); 00066 b_led.write(b); 00067 return; 00068 } 00069 00070 void fn1_activate() // function called by ticker every Ts seconds 00071 { 00072 fn1_go = 1; // go-flag active 00073 } 00074 00075 00076 void readTC() 00077 { 00078 //uses MAX31855 chip 00079 //see https://datasheets.maximintegrated.com/en/ds/MAX31855.pdf for documentation 00080 00081 00082 float d = 0; 00083 int16_t b[3] = {0,0,0}; 00084 00085 CS_1 = 0; //Active-Low Chip Select. Set CS low to enable the serial interface. 00086 wait_us(0.2); 00087 00088 for (int i = 15; i >= 0; i--) { 00089 SCK_1 = 0; //set clock value to 0 00090 wait_us(0.1); //wait for 100ns 00091 SCK_1 = 1; //then set clock high again 00092 wait_us(0.1); //wait for 100ns 00093 for (int j = 0; j < 3; j++) { //read out bits for each thermocouple (0 to 11) 00094 if (pins[j]) { 00095 b[j] |= (1 << i); //bitshift 00096 } 00097 } 00098 } 00099 00100 CS_1 = 1; 00101 00102 for (int j = 0; j < 3; j++) { //for every thermocouple (starting from 0 to 11, so 12 in total) 00103 if (b[j] & 0x1) { //b[j] contains the bits that are received from every chip. if least significant bit is 1, then open circuit. 00104 b[j] = 20000; 00105 } else { 00106 b[j] &= 0xfffc; //if no fault then select 14 bit thermocouple data from bitstring 00107 d = b[j] / 16.0; 00108 } 00109 //fprintf(fp, "%.3f \n\r", d); 00110 pc.printf("%.3f\t", d); 00111 } 00112 pc.printf("\n\r"); 00113 //fprintf(fp, "\n\r"); 00114 return; 00115 00116 } 00117 00118 void getsample(bool meas) 00119 { 00120 if(meas){ // alleen data uitlezen en opslaan als meas = 1 00121 fcs_read = fcs.read(); // capture torque sensor data 00122 //fprintf(fp, "%.3f\t", fcs_read); // output data to file 00123 pc.printf("%.3f\t", fcs_read); // output data to screen 00124 readTC(); // call function to read thermocouple data 00125 } 00126 return; 00127 } 00128 00129 00130 ////////////////////Main//////////////////////////////////////////////////////// 00131 //////////////////////////////////////////////////////////////////////////////// 00132 //////////////////////////////////////////////////////////////////////////////// 00133 00134 int main() 00135 { 00136 pc.baud(baudrate); 00137 tick.attach(&fn1_activate,Ts); 00138 led_control(led_off,led_off,led_off); // start with LEDs off 00139 00140 while (1) { 00141 if(fn1_go) { // statements to execute when go-flag is active 00142 fn1_go = 0; 00143 getsample(meas); // deze functie wordt elke ts seconde aangeroepen. afhankelijk van waarde meas (0 of 1) wordt de data opgeslagen in textfile 00144 } 00145 00146 if(pc.readable()) { // if character available 00147 switch(pc.getc()) { // read a character from keyboard 00148 case 'c': 00149 led_control(led_on,led_on,led_off); //orange LED during calibration 00150 sprintf(filename_cal, "/sd/cal_torque_%d.txt", cal_numb); //construct new textfile 00151 fp = fopen(filename_cal, "w"); 00152 if(fp == NULL) { 00153 error("Could not open file for write\n"); 00154 } else { 00155 while(1) { 00156 for (int i = 0; i<50; i++) { //get 50 samples from torque sensor 00157 fcs_read = fcs.read(); 00158 fprintf(fp, "%.3f\t \n\r", fcs_read); //output values to the screen 00159 } 00160 break; 00161 } 00162 } 00163 fclose(fp); //close file 00164 cal_numb++; //increment calibration number 00165 pc.printf("File saved as %s\n\r", filename_cal); //print name of calibration file to screen 00166 pc.printf("Press 'c' to record new calibration or 't' to start new trial\n\r"); // print message to screen 00167 break; 00168 case 't': 00169 trial_numb++; 00170 pc.printf("Trial number: %d\n\r", trial_numb); 00171 pc.printf("Press 's' to start measurement\n\r"); 00172 break; 00173 case 's': 00174 start = !start; 00175 if (start) { 00176 led_control(led_off,led_on,led_off); //green LED during measurement 00177 sprintf(filename_torque, "/sd/trial%d.txt", trial_numb); //construct new textfile to store data from torque sensor 00178 fp = fopen(filename_torque, "w"); 00179 if(fp == NULL) { 00180 error("Could not open file for write\n\r"); 00181 } else { 00182 pc.printf("Measurement started... \n\r"); 00183 pc.printf("Press 's' to stop measurement\n\r"); 00184 meas = 1; // hiermee zorg je dat de functie getsample de data wegschrijft naar het .txt file 00185 } 00186 break; 00187 } else { 00188 led_control(led_on,led_off,led_off); // RED LED when ready to stopped measuring 00189 meas = 0; // hiermee stop je de output naar het .txt file 00190 fclose(fp); 00191 pc.printf("File saved as %s\n\r", filename_torque); // print filename to screen 00192 pc.printf("Press 'c' to perform new calibration or 't' to start new trial\n\r"); // print message to screen 00193 00194 } 00195 break; 00196 } 00197 } 00198 } 00199 }
Generated on Mon Aug 15 2022 02:36:00 by
1.7.2