A program to monitor some parameters for a motor

Dependencies:   mbed-dev BufferSerial

Thanks to David Lowe for https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/ which I adapted for the use of TIM2 32bit timer as an encoder reader on the Nucleo L432KC board.

Revision:
14:e5f5b345b2fe
Parent:
13:e08e3540c30b
Child:
15:8adff67fe707
--- a/main.cpp	Fri Jun 02 21:30:49 2017 +0000
+++ b/main.cpp	Sat Jun 03 18:59:04 2017 +0000
@@ -85,6 +85,11 @@
 //Serial& raspi = get_stdio_serial();
 //Serial debug(PB_6,PA_10);    // Serial1 tx rx
 
+int ADC_Count = 0;
+float Voltage;
+float Voltage_total;
+float samples[1024];
+
 uint16_t count1=0;
 int16_t count2=0;
 int32_t count3=0;
@@ -96,6 +101,16 @@
 float dac2_val=0;
 float adc3_val=0;
 
+//Array with adc1, adc2 & adc3 correction addition,
+//experimentally determined 
+const float adc_corr[] = { 0, 0.022f, 0.2995, 0.0267};
+ 
+int main()
+{
+    printf("%d\n", lookup("X0"));
+    return 0;
+}
+
 volatile bool adc3_en = true;
 int16_t lines = 4;
 
@@ -127,7 +142,8 @@
         }
     }
 }
-float speedRead(){
+float speedRead()
+{
     uint16_t i = 0;
     uint32_t deltaT;
     float speed;
@@ -143,7 +159,7 @@
     wait_ms(5);
     pos2=__HAL_TIM_GET_COUNTER(&timer2);
     sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
-    
+
     //The speed computation method adapts to slow/fast speeds, in order to
     //optimize the rapport between computation precision and time windows size
     while (pos2 == pos1 && i<99) { // The accepted max delay time is 0.5 seconds
@@ -151,24 +167,23 @@
         i++;
         pos2=__HAL_TIM_GET_COUNTER(&timer2);
         sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
-        }
+    }
     pos2=__HAL_TIM_GET_COUNTER(&timer2);
     sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
     timer3.stop();
     deltaT = timer3.read_us();
     timer3.reset();
     pos = (int32_t) pos2 - (int32_t) pos1;
-    
+
     printf("Time is %lu microseconds, position modified %ld %lu %lu\r\n", deltaT, pos, pos1, pos2);
-    if (deltaT > 0){
+    if (deltaT > 0) {
         speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
-        }
-    else {
+    } else {
         printf("Error, time interval not greater than zero, speed not calculated!\r\n");
-        }
-    printf("The encoder speed is %f rot/s\r\n", speed); 
+    }
+    printf("The encoder speed is %f rot/s\r\n", speed);
     return speed;
-    }
+}
 
 //Function attached to the serial RX interrupt event
 void readData(void)
@@ -188,7 +203,8 @@
             lines = 4;
             printf("false\r\n");
         } else if (p1=strstr(message, "DAC=") != NULL) {
-            dac_val = (atof(message+p1+3)-0.5f)/15.61;
+            //The DCPS has 1V offset, so we have to remove it
+            dac_val = (atof(message+p1+3)-1.0f)/15.61;
             dac2.write(dac_val);
             printf("Value to write to DAC: %f\r\n", dac_val*3.3f);
         } else if (strcmp(message, "reset") == 0) {
@@ -206,12 +222,30 @@
     led1 = 0;
 }
 
+void ADC_read(AnalogIn adc)
+{
+    ADC_Count++;
+    Voltage_total =0;
+    for (int i=0; i<100; i++) {      // do 25 readings
+        Voltage = adc.read();
+
+        Voltage_total = Voltage_total+ Voltage;     //Note Vinput.read can be summed then averaged
+        //wait_us(10);
+    }
+    Voltage=Voltage_total/100.f;
+    samples[ADC_Count] = Voltage;     //Save averaged reading within an array
+    if (ADC_Count == 1023){
+        ADC_Count = 0;
+        }
+
+}
+
 //The main function
 int main()
 {
     //Power onn the DCPS
     relay1.write(1);
-    
+
     //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle,
     //full 32-bit count
     //For L432KC to work with TIM2 one needs to reassign the system ticker
@@ -223,10 +257,10 @@
     //This is triggered by the encoder's index pulses
     //and it resets the encoder counter
     event.rise(&atint);
-    
+
     //Set serial baud rate
     raspi.baud(115200);
-    
+
     //Attach functin to call for serial interrupt event
     raspi.attach(&readData);
 
@@ -265,7 +299,7 @@
 
         //It writes the DAC1 value as a subunitary float number
         //to be multiplied with the max DAC_RANGE
-        
+
 
         //It gets the one loop position and the direction of the encoder
         count3=__HAL_TIM_GET_COUNTER(&timer2);
@@ -273,24 +307,27 @@
 
         if (i >= 100) {
             adc3_val = adc3.read();
-            dac1.write(adc3_val);
-            
+            ADC_read(adc1);
+            ADC_read(adc2);
+            ADC_read(adc3);
+            //dac1.write(adc3_val);
+
             printf("%ld%s passes=%d\r\n", count3, dir1==0 ? "+":"-", count2);
-            printf("Voltage ADC1= %3.3f%V, DAC=%f\r\n", adc1.read()*3.3f, dac2_val*3.3f);
-            printf("VOUT: %f\r\n", dac2_val*15.61+0.5);
-            printf("Voltage ADC2: %3.3f%V\r\n", (adc2.read()*3.3f+0.022)/0.207048458f);
+            printf("Voltage ADC1= %3.3f%V, DAC=%f\r\n", (3.3f*samples[ADC_Count-2]+0.022f), dac2_val*3.3f);
+            printf("VOUT: %f\r\n", dac2_val*15.61f+1.0f);
+            printf("Voltage ADC2: %3.3f%V\r\n", (3.3f*samples[ADC_Count-1]+0.062f)/0.207048458f);
 
             //printf("Vref(f): %f, Vref : %u, Temperature : %u\r\n",
             //         vref.read(), vref.read_u16(), tempint.read_u16());
             if (adc3_en) {
                 printf("Voltage ADC3: %3.3f%V\r\n", adc3_val*3.3f);
-                printf("Voltage ADC3: %u\r\n", adc3.read_u16());
+                printf("Average ADC3: %3.3f%V\r\n", (3.3f*samples[ADC_Count]+0.022f)/0.8245614f);
                 printf("DAC1 read: %3.3f%V\r\n", dac1.read()*3.3f);
                 printf("DAC2 read: %3.3f%V\r\n", dac2.read()*3.3f);
             }
             speedLast = speedRead();
             //printf("\033[%dA", lines); // Moves cursor up of #lines
-            
+
             i=0;
         }
         i++;