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: mbed
Diff: main.cpp
- Revision:
- 2:23f848b21b09
- Parent:
- 1:7f99e938ae2a
- Child:
- 3:447b0de4295a
diff -r 7f99e938ae2a -r 23f848b21b09 main.cpp
--- a/main.cpp Mon Jul 23 17:34:20 2018 +0000
+++ b/main.cpp Thu Jul 26 13:44:16 2018 +0000
@@ -1,17 +1,8 @@
#include "mbed.h"
-#define ALL_CH 15 //value of convst bus to read all chanels simultaneosly
-#define IO_REF 10.17 //ref channel voltage mV (most accurate between 9-12V high supply voltage)
-
-#define CHA0_REF 6217
-#define CHA1_REF 6196
-#define CHB0_REF 6231
-#define CHC0_REF 6231
-#define CHC1_REF 6223
-#define CHD0_REF 6240
-#define CHD1_REF 6228
-
-
+#define CH_A 1 // value of convst bus to read channel A only
+#define CH_AC 5 // value of convst bus to read channels A and C
+#define CH_ABCD 15 // value of convst bus to read all chanels simultaneously
Serial pc(USBTX, USBRX); // tx, rx
@@ -29,51 +20,31 @@
DigitalIn busy(p9);
DigitalOut reset(p10);
-LocalFileSystem local("local");
-
-FILE *fp = fopen("/local/TEST_LOG.csv", "w"); // Open "test_log" on the local file system for writing
-
-
char buffer16[16];
int val_array[8];
-float voltages[8];
const char dummy = 0;
-int i = 0;
-float cur1 = 0;
-float cur2 = 0;
-float R1 = 0;
-float R2 = 0;
-int scale_factors[8];
int drivetime_ms;
-float scale_factor = 0.0;
char outString[100];
-int readChannels (char buffer[16], int values[8], int OSR){
- //simultaneosly samples all channels and reads into buffer
+int readChannels (char buffer[16], int values[8]){
+ //simultaneously samples and reads into buffer
short int val_array[8];
-
- for (int i=0; i<OSR; i++) {
-
- //send convert signal to all channels
- convt = ALL_CH;
- wait_us(1);
- convt = 0;
-
- //SPI(like) data transfer
- cs = 0;
- spi.write(&dummy, 1, buffer, 16);
- cs=1;
-
- //loop over bytes to add channel voltage values
- for (int x=0; x<8; x++){
- val_array[x] = buffer[2*x]<<8 | buffer16[(2*x) + 1];
- values [x] = values[x] + val_array[x];
- }
- }
-
- for (int y=0; y<8; y++){
- values[y] = values[y]/OSR;
+
+ //send convert signal to channels
+ convt = CH_AC;
+ wait_us(1);
+ convt = 0;
+
+ //SPI(like) data transfer
+ cs = 0;
+ spi.write(&dummy, 1, buffer, 16);
+ cs=1;
+
+ //loop over bytes to add channel voltage values
+ for (int i=0; i<8; i++){
+ val_array[i] = buffer[2*i]<<8 | buffer16[(2*i) + 1];
+ values [i] = val_array[i];
}
return 0;
@@ -81,26 +52,26 @@
int main() {
+ int n_samples = 50;
+ double r;
+ double r_max = 0;
+ double r_min = 1e10;
+ double r_sum = 0;
+ double r_sum2 = 0;
+ double r_mean;
+ double r_mean2;
+ double r_sd;
+ double r_cv;
+
rLED = 0;
yLED = 0;
gLED = 0;
- int OSR = 5;
-
drive = 0;
drivetime_ms = 1;
-
- scale_factors[0] = CHA0_REF;
- scale_factors[1] = CHA1_REF;
- scale_factors[2] = CHB0_REF;
- scale_factors[3] = 0;
- scale_factors[4] = CHC0_REF;
- scale_factors[5] = CHC1_REF;
- scale_factors[6] = CHD0_REF;
- scale_factors[7] = CHD1_REF;
-
+
pc.baud(115200);
- pc.printf("Test start\n\r");
+ pc.printf("Test start\r\n");
//Reset ADC sequence
reset = 1;
@@ -113,76 +84,46 @@
spi.set_default_write_value(0x00);
cs = 1;
- //while(1) {
rLED = 1;
- yLED = drive;
+ yLED = 0;
gLED = 1;
-
- drive = 1;
- wait_ms(drivetime_ms);
- readChannels (buffer16, val_array, 1);
- drive = 0;
-
- rLED = 0;
- pc.printf("Drive for %d ms\n", drivetime_ms);
- for (int x=0; x<8; x++){
- voltages[x] = (float)val_array[x]*scale_factors[x]/10000;
- pc.printf("%f\n", voltages[x]);
- }
- cur1 = 2* (voltages[0] - voltages[1]);
- cur2 = 2* (voltages[2] - voltages[1]);
-
- R1 = (voltages[4]- voltages[5])/cur1;
- R2 = (voltages[6]- voltages[7])/cur2;
+ sprintf(outString, "I1SIG, I1REF, V1POS, V1NEG, R\r\n");
+ pc.printf("%s", outString);
- pc.printf("V0 = %f\n", voltages[0]);
- pc.printf("V1 = %f\n", voltages[1]);
- pc.printf("V2 = %f\n", voltages[2]);
- pc.printf("V4 = %f\n", voltages[4]);
- pc.printf("V5 = %f\n", voltages[5]);
- pc.printf("V6 = %f\n", voltages[6]);
- pc.printf("V7 = %f\n", voltages[7]);
-
-
- sprintf(outString, "R1, R2, Drive for %d ms, OSR %d\n", drivetime_ms, OSR);
- fprintf(fp, outString);
-
- for (int x=0; x<20; x++) {
+ for (int x=0; x<n_samples; x++) {
drive = 1;
+ yLED = 1;
wait_ms(drivetime_ms);
- readChannels (buffer16, val_array, OSR);
+ readChannels (buffer16, val_array);
drive = 0;
+ yLED = 0;
+
+ r = (double)(val_array[5]-val_array[4])/(double)(val_array[1]-val_array[0]);
+ if (r < r_min) { r_min = r; }
+ if (r > r_max) { r_max = r; }
+ r_sum = r_sum + r;
+ r_sum2 = r_sum2 + (r*r);
- for (int x=0; x<8; x++){
- voltages[x] = val_array[x]*scale_factors[x]/10000;
- }
-
- cur1 = 2* (voltages[0] - voltages[1]);
- cur2 = 2* (voltages[2] - voltages[1]);
-
- // R1 = (voltages[4]- voltages[5])/cur1;
- // R2 = (voltages[6]- voltages[7])/cur2;
- R1 = cur1;
- R2 = cur2;
- sprintf(outString, "%f, %f\n", R1, R2);
- fprintf(fp, outString);
- wait_ms(100);
+ sprintf(outString, "%5d, %5d, %5d, %5d, %f\r\n", val_array[0], val_array[1], val_array[4], val_array[5], r);
+ pc.printf("%s", outString);
+ wait_ms(1000);
}
+
+ r_mean = r_sum/n_samples;
+ r_mean2 = r_sum2/n_samples;
+ r_sd = sqrt(r_mean2-(r_mean*r_mean));
+ r_cv = r_sd/r_mean;
+
+ pc.printf("Statistics:\r\n");
+ pc.printf("n_samples : %d\r\n", n_samples);
+ pc.printf("r_mean : %f\r\n", r_mean);
+ pc.printf("r_min : %f\r\n", r_min);
+ pc.printf("r_max : %f\r\n", r_max);
+ pc.printf("r_sd : %f\r\n", r_sd);
+ pc.printf("r_cv : %f\r\n", r_cv);
+
rLED = 0;
- fclose(fp);
-
-
- //
-// pc.printf("Repeat Test?\n\rPress to increment wait time\n\r");
-// while(1){
-// if (pc.getc() != 0) {
-// drivetime_ms++;
-// break;
-// }
-// }
-
- //}
-}
+ }