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:
17:75815e312312
Parent:
16:e423f891cfbc
Child:
18:1ef20c13693c
--- a/main.cpp	Thu Jun 15 04:23:01 2017 +0000
+++ b/main.cpp	Thu Jun 15 17:20:59 2017 +0000
@@ -49,6 +49,7 @@
 
 //Timer to be used for the speed computation function
 Timer timer1;
+Timer timer3;
 
 //The input for the encoder's index channel
 InterruptIn event(PA_8);
@@ -190,7 +191,7 @@
     if (sens1 == sens2) {
         pos = pos2 - pos1;
     } else {
-        printf("E:Speed computation error, change of direction between readings!\n");
+        printf("\nE:Speed computation error, change of direction between readings!\n");
     }
 
     //For debugging
@@ -199,7 +200,7 @@
         //speed computation in rot/s
         speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
     } else {
-        printf("E:Error, time interval not greater than zero, speed not calculated!\n");
+        printf("\nE:Error, time interval not greater than zero, speed not calculated!\n");
     }
     return speed;
 }
@@ -208,29 +209,35 @@
 //the serial messages and invoques the corresponding commands
 void readData(void)
 {
-
-    char message[20];
-    if(raspi.readable()) {
+    led1 = 1;
+    char message[15];
+    if (raspi.readable()) {
         // Signalling the beginning of serial read
-        led1 = 1;
-        int p1 = 0;
-
-        raspi.scanf("%s", message);
+        int i = 0;
+        bool rx = true;
+        while(rx && i<14) {
+            message[i] = raspi.getc();
+            if (message[i] == '\r') {
+                message[i] = NULL;
+                rx = false;
+            }
+            i++;
+        }
         //Message received printed for debugging
         //printf("M:%d %s\n", strlen(message), message);
-
+        int p1 = 0;
         if (strcmp(message, "adcOn") == 0) {
             adc_en = true;
-            printf("M:ADC true\n");
+            printf("\nM:ADC true\n");
         } else if (strcmp(message, "adcOff") == 0) {
             adc_en = false;
-            printf("M:ADC false\n");
+            printf("\nM:ADC false\n");
         } else if (p1=strstr(message, "dac=") != NULL) {
             //Writing the dac1 value read from serial
             //The DCPS has 1V offset, so we have to remove it
             dac_val = (atof(message+p1+3)-1.0f)/15.51;
             dac1.write(dac_val);
-            printf("M:Value to write to DAC: %f\n", dac_val*3.3f);
+            printf("\nM:Value to write to DAC: %f\n", dac_val*3.3f);
         } else if (strcmp(message, "reset") == 0) {
             //encoder related counters reset command
             TIM2->CNT = 0x0000;
@@ -239,11 +246,11 @@
             count3 = 0;
             count4 = 0;
             adjustOffset = false;
-            printf("M:Encoder counters reset!\n");
+            printf("\nM:Encoder counters reset!\n");
         } else if (strcmp(message, "powerOn") == 0) {
             //command to power on the DCPS
             relay1.write(1);
-            printf("M:DCPS on\n");
+            printf("\nM:DCPS on\n");
         } else if (strcmp(message, "powerOff") == 0) {
             //command to power off the DCPS
             relay1.write(0);
@@ -251,30 +258,30 @@
         } else if (strcmp(message, "posOn") == 0) {
             //command to enable the encoder position notification
             pos_en = true;
-            printf("M:Position notification enabled\n");
+            printf("\nM:Position notification enabled\n");
         } else if (strcmp(message, "posOff") == 0) {
             //command to disable the encoder position notification
             pos_en = false;
-            printf("M:Position notification disabled\n");
+            printf("\nM:Position notification disabled\n");
         } else if (strcmp(message, "posIndexOn") == 0) {
             //command to enable the index related encoder position notification
             posIndex_en = true;
-            printf("M:Index related position notification enabled\n");
+            printf("\nM:Index related position notification enabled\n");
         } else if (strcmp(message, "posIndexOff") == 0) {
             //command to disable the index related encoder position notification
             posIndex_en = false;
-            printf("M:Index related position notification disabled\n");
+            printf("\nM:Index related position notification disabled\n");
         } else if (strcmp(message, "speedOn") == 0) {
             //command to enable speed computation and notification
             speed_en = true;
-            printf("M:Speed enabled\n");
+            printf("\nM:Speed enabled\n");
         } else if (strcmp(message, "speedOff") == 0) {
             //command to disable speed computation and notification
             speed_en = false;
-            printf("M:Speed disabled\n");
+            printf("\nM:Speed disabled\n");
         }
-
     }
+    
     //Signalling the end of searial read
     led1 = 0;
 }
@@ -320,7 +327,7 @@
 
     //Attach functin to call for serial interrupt event
     wait(1);
-    raspi.attach(&readData);
+    raspi.attach(&readData, Serial::RxIrq);
 
     //Message to mark the initialisation of the program
     printf("M:STM HAL encoder with ADC and DAC\n");