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:
10:8862c8779b71
Parent:
9:4d736d29ce19
Child:
11:7a9837acbea0
--- a/main.cpp	Sat May 27 01:26:41 2017 +0000
+++ b/main.cpp	Tue May 30 22:35:08 2017 +0000
@@ -1,21 +1,21 @@
 /*
- * Nucleo STM32F4(or L4) quadrature decoder, ADCs and DAC 
+ * Nucleo STM32F4(or L4) quadrature decoder, ADCs and DACs
  * with serial communication over the USB virtual serial port
- 
+
  * Developed for Elliptec X15 piezoelectric motor control, on a L432KC board
  *
  * Using STM32's counter peripherals to interface rotary encoders.
- * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, 
+ * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count,
  * others 16bit.
- * Take into account that Mbed uses TIM5 for system timer, SPI needs TIM1, 
+ * Take into account that on F4xx Mbed uses TIM5 for system timer, SPI needs TIM1,
  * others are used for PWM.
- * Check your platform's PeripheralPins.c & PeripheralNames.h if you need 
+ * Check your platform's PeripheralPins.c & PeripheralNames.h if you need
  * both PWM & encoders. This project does not use PWM.
  *
- * On L432KC, for example, only TIM2 has 32bit count, others 16bit.
+ * On L432KC, for example, only TIM2 has 32bit count, others have 16bit.
  * However, mbed has TIM2 assigned by default to the system ticker
- * For L432KC to work with TIM2 encoder input one needs to reassign 
- * the system ticker from TIM2 to TIM7, for example, 
+ * For L432KC to work with TIM2 encoder input one needs to reassign
+ * the system ticker from TIM2 to TIM7, for example,
  * in mbed-dev/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/device/hal_tick.h
  *
  * Edit HAL_TIM_Encoder_MspInitFx(Lx).cpp to suit your mcu & board's available
@@ -24,7 +24,7 @@
  * Thanks to:
  * David Lowe (for the quadrature encoder code)
  * https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
- * 
+ *
  * And Frederic Blanc
  * https://developer.mbed.org/users/fblanc/code/AnalogIn_Diff/
  *
@@ -43,11 +43,6 @@
 #include "mbed.h"
 #include "Encoder.h"
 #include "inttypes.h" // for PRIu32 encoding
-//#include <string.h>   // strstr()
-//#include <stdbool.h>  // bool, true, false
-
-#define DAC_RANGE (0xFFF) // 12 bits
-#define SAMPLE_RATE    150000
 
 //Defining the timer and its coresponding encoder
 TIM_HandleTypeDef timer2;
@@ -60,7 +55,7 @@
 DigitalOut led1(LED1);
 
 //LED2 to signal the encoder's index impulses
-//only for debugging, to calibrate the position
+//only for feedback, to calibrate the position
 //of the AVAGO encoder with respect to the shaft
 DigitalOut led2(PB_4);
 
@@ -69,57 +64,40 @@
 
 //Defining the ADCs
 AnalogIn adc1(PA_3); //ADC1_IN8
-AnalogIn adc2(PA_4); //ADC1_IN9
+AnalogIn adc2(PA_7); //ADC1_IN9
 AnalogIn adc3(PA_6); //ADC1_IN11
 
-//Defining the DAC for the Power Source 
+//Defining the DAC for setting the half of the current amplitude
+//and generate a quasi square signal for the PLL current phase input
+AnalogOut dac1(PA_4); // DAC1_OUT1
+
+//Defining the DAC for the Power Source
 //(DCPS - Digitally Controlled Power Source)
 //DAC1_OUT2 on pin PA_5 is at least twices as fast as DAC1_OUT1 on pin PA_4
-AnalogOut dac1(PA_5); // DAC1_OUT2
+AnalogOut dac2(PA_5); // DAC1_OUT2
 
 //Defining the serial object to be used for communicating with Raspi
 Serial raspi(USBTX, USBRX);
-//Serial raspi(SERIAL_TX, SERIAL_RX);
+
 //Serial& raspi = get_stdio_serial();
-
 //Serial debug(PB_6,PA_10);    // Serial1 tx rx
-//Serial configBT(PA_2,PA_3);  // Serial2 tx rx
-//Serial slave(PC_10,PC_11);   // Serial3 tx rx
-//Serial modem(PA_0,PA_1);     // Serial4 tx rx
 
 uint16_t count1=0;
 int16_t count2=0;
 int32_t count3=0;
 int16_t adjust=0;
 
+float speedLast=0;
+
 float dac_val=0;
+float adc3_val=0;
 
 volatile bool adc3_en = false;
 int16_t lines = 4;
 
 //TIM1 to be used with the interrupt for the encoder's index pulses
 Timer timer1;
-
-//Function to convert a string to float
-float stof(const char* s){
-  float rez = 0, fact = 1;
-  if (*s == '-'){
-    s++;
-    fact = -1;
-  };
-  for (int point_seen = 0; *s; s++){
-    if (*s == '.'){
-      point_seen = 1; 
-      continue;
-    };
-    int d = *s - '0';
-    if (d >= 0 && d <= 9){
-      if (point_seen) fact /= 10.0f;
-      rez = rez * 10.0f + (float)d;
-    };
-  };
-  return rez * fact;
-};
+Timer timer3;
 
 //Function invoked by the encoder's index interrupt pulses
 void atint()
@@ -127,7 +105,7 @@
     timer1.start();
     if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) {
         adjust--;
-        if (adjust == -3) {
+        if (adjust == -1) {
             count2--;
             count1 +=__HAL_TIM_GET_COUNTER(&timer2);
             //TIM2->CNT = 0x0000;
@@ -136,7 +114,7 @@
         }
     } else {
         adjust++;
-        if (adjust == 3) {
+        if (adjust == 1) {
             count2++;
             count1 +=__HAL_TIM_GET_COUNTER(&timer2);
             //TIM2->CNT = 0x0000;
@@ -145,6 +123,48 @@
         }
     }
 }
+float speedRead(){
+    uint16_t i = 0;
+    uint32_t deltaT;
+    float speed;
+    uint32_t pos1;
+    uint32_t pos2;
+    int32_t pos;
+    int8_t sens1;
+    int8_t sens2;
+    printf("Encoder speed reading!\r\n");
+    timer3.start();
+    pos1=__HAL_TIM_GET_COUNTER(&timer2);
+    sens1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
+    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
+        wait_ms(5);
+        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){
+        speed = ((float) pos)*12.5f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
+        }
+    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); 
+    return speed;
+    }
 
 //Function attached to the serial RX interrupt event
 void readData(void)
@@ -163,11 +183,15 @@
             adc3_en = false;
             lines = 4;
             printf("false\r\n");
-        } else if (p1=strstr(message, "DAC=") != NULL){
+        } else if (p1=strstr(message, "DAC=") != NULL) {
             dac_val = atof(message+p1+3);
+            dac2.write(dac_val);
             printf("Value to write to DAC: %f\r\n", dac_val*3.3f);
-            }
-        
+        } else if (strcmp(message, "reset") == 0) {
+            TIM2->CNT = 0x0000;
+            printf("Encoder reset!\r\n");
+        }
+
     }
     led1 = 0;
 }
@@ -179,16 +203,15 @@
     //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
-    //from TIM2 to TIM7 in TARGET/../device/hal_tick.h
-    
+    //from TIM2 to TIM7 in TARGET/../device/hal_tick.h\
     //Initialise encoder
     EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
-    
-    //Define function to call for interrupt event rise 
+
+    //Define function to call for interrupt event rise
     //This is triggered by the encoder's index pulses
     //and it resets the encoder counter
     event.rise(&atint);
-    
+
     //Attach functin to call for serial interrupt event
     raspi.attach(&readData);
 
@@ -198,56 +221,65 @@
 
     //printf("%" PRIu32 "\n", UINT32_MAX);
 
-    
+    int i = 0;
     //The main loop
     while(1) {
-        
+
         //Variable for the one loop encoder counter
         uint32_t count3=0;
 
         //Variable for the direction of the counter
         int8_t dir1;
-        
-        
+
         //uint16_t voltage1=0;
 
 
         //OK 401 411 446 NOK 030
         //count1=TIM2->CNT;
         //dir1=TIM2->CR1&TIM_CR1_DIR;
-        
+
         //It reads the ADC1 value converted from 12bit to 16bit resolution
         //voltage1=adc1.read_u16();
-        
+
         //It resets the DAC1
         //dac1.free();
-        
+
         //It writes the DAC1 value as a 16bit number
         //dac1.write_u16(count3*2);
-        
+
         //It writes the DAC1 value as a subunitary float number
         //to be multiplied with the max DAC_RANGE
-        dac1.write(dac_val);
         
+
         //It gets the one loop position and the direction of the encoder
         count3=__HAL_TIM_GET_COUNTER(&timer2);
         dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
-        
-        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, dac1.read()*3.3);
-        //printf("Vref: %f\r\n", vref.read());
-        printf("Voltage ADC2: %3.3f%V\r\n", adc2.read()*3.3f);
-        
-        //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.read()*3.3f);
-            printf("Voltage ADC3: %u\r\n", adc3.read_u16());
-            printf("DAC read: %3.3f%V\r\n", dac1.read()*3.3f);
-        } 
-        //printf("\033[%dA", lines); // Moves cursor up of #lines
+
+        if (i >= 100) {
+            adc3_val = adc3.read();
+            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.read()*3.3f);
+            //printf("Vref: %f\r\n", vref.read());
+            printf("Voltage ADC2: %3.3f%V\r\n", adc2.read()*3.3f);
 
-        wait(3.04);
+            //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("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++;
+
+        wait(0.04);
     }
 
 }
\ No newline at end of file