Touch screen drivers control dashboard for miniature locomotive. Features meters for speed, volts, power. Switches for lights, horns. Drives multiple STM3_ESC brushless motor controllers for complete brushless loco system as used in "The Brute" - www.jons-workshop.com

Dependencies:   TS_DISCO_F746NG mbed Servo LCD_DISCO_F746NG BSP_DISCO_F746NG QSPI_DISCO_F746NG AsyncSerial FastPWM

Revision:
14:6bcec5ac21ca
Parent:
12:a25bdf135348
--- a/main.cpp	Mon Jan 14 16:51:16 2019 +0000
+++ b/main.cpp	Mon Mar 04 17:47:27 2019 +0000
@@ -126,23 +126,30 @@
     int     bigImafptr;
     double  amps_longave,   //  internal use only
             amps_latest,    //  update() called @ 32Hz. Value stored here is average over most recent 3125us
-            amps_filtered;  //  Average of the BIGMAFSIZ most recent samples stored in latest
+            amps_filtered,  //  Average of the BIGMAFSIZ most recent samples stored in latest
+            amps_filtered2;  //  Average of the BIGMAFSIZ most recent samples stored in latest
 public:    
     ammeter_reading ()  {   //  constructor
         bigImafptr = 0;
-        amps_longave = amps_latest = amps_filtered = 0.0;
+        amps_longave = amps_latest = amps_filtered = amps_filtered2 = 0.0;
         for (int i = 0; i < BIGMAFSIZ; i++)
             bigImaf[i] = 0.0;
     }
     void    update  ()  ;   //  Read ammeter core, store most recent 32ms or so worth in amps_latest, 250ms average in amps_filtered
     double  latest  ()  ;
     double  filtered()  ;
+    double  filtered2()  ;
 }   Ammeter ;
 
 double  ammeter_reading::latest     ()  {
     return  amps_latest;
 }
 
+double  ammeter_reading::filtered2   ()  {
+//  could use filter without buffer, weights result more towards more frecent samples
+    return  amps_filtered2;
+}
+
 double  ammeter_reading::filtered   ()  {
     return  amps_filtered;
 }
@@ -156,6 +163,10 @@
     if  (bigImafptr >= BIGMAFSIZ)
         bigImafptr = 0;
     amps_filtered = amps_longave / BIGMAFSIZ;
+const   double  sampweight  = (double)(1) / (double)BIGMAFSIZ;
+const   double  shrinkby    = 1.0 - sampweight;
+    amps_filtered2 *= shrinkby;
+    amps_filtered2 += amps_latest * sampweight;
 }
 
 
@@ -247,9 +258,9 @@
     slider.direction = f_r_switch ? REV : FWD;      //  Only place in the code where direction gets set. Centre-Off power switch REV-OFF-FWD.
 
     My_STM3_ESC_boards.set_I_limit (0.0);
-    My_STM3_ESC_boards.set_V_limit (0.0);
-    My_STM3_ESC_boards.message  ("rb\r");
-    throttle    (0.0);      //  Set revs to idle. Start engine and warm up before powering up control
+    My_STM3_ESC_boards.set_V_limit (0.0);   //  zero power to motors
+    My_STM3_ESC_boards.message  ("rb\r");   //  regen brake mode
+    throttle    (0.0);                      //  Set revs to idle. Start engine and warm up before powering up control
     pc.printf   ("\r\n\n\nJon's Loco_TS_2018 Loco Controller ver %s starting, direction %s\r\n", const_version_string, slider.direction ? "Forward":"Reverse");
 
     uint8_t lcd_status = touch_screen.Init(lcd.GetXSize(), lcd.GetYSize());
@@ -311,25 +322,7 @@
             Speedo.set_value    (My_STM3_ESC_boards.mph);
 
             led_grn = !led_grn;
-/*
-Handbrake more sensibly implemented on STM3_ESC boards ?
-
-            if  (slider.state == PARK)   {
-                if  (My_STM3_ESC_boards.mph > LOCO_HANDBRAKE_ESCAPE_SPEED / 4.0)    {
-                    slider.handbrake_effort *= 1.1;
-                    if  (slider.handbrake_effort > 0.55)    slider.handbrake_effort = 0.55;
-                    set_run_mode    (PARK);
-                    pc.printf   ("Handbrake slipping, effort %.2f\r\n", slider.handbrake_effort);
-                }
-                if  (My_STM3_ESC_boards.mph < 0.02)    {
-                    slider.handbrake_effort *= 0.9;
-                    if  (slider.handbrake_effort < 0.05)    slider.handbrake_effort = 0.05;
-                    set_run_mode    (PARK);
-                    pc.printf   ("Handbrake not slipping, effort %.2f\r\n", slider.handbrake_effort);
-                }
-            }
-*/
-            My_STM3_ESC_boards.request_mph   (); //  issues "'n'rpm\r", takes care of cycling through available boards in sequence
+            My_STM3_ESC_boards.request_mph   (); //  issues "'n'mph\r", takes care of cycling through available boards in sequence
 //            switch  (qtr_sec)   {   //  Can do sequential stuff quarter second apart here
 //            }   //  End of switch qtr_sec
             qtr_sec++;
@@ -341,12 +334,13 @@
                     seconds = 0;
                     minutes++;
                     //  do once per minute stuff here
-                    Controller_Error.report_any ();
+                    Controller_Error.report_any (false);    //  Reset error having reported it once
                 }   //  fall back into once per second
                 if  (seconds & 1)
                     Speedo.LED  (0, LCD_COLOR_DARKGRAY);
                 else
                     Speedo.LED  (0, LCD_COLOR_RED);
+//                pc.printf   ("Filter test %.3f, %.3f\r\n", Ammeter.filtered(), Ammeter.filtered2());
                 My_STM3_ESC_boards.message  ("kd\r");       //  Kick the WatchDog timers in the Twin BLDC drive boards
                 recent_distance += (My_STM3_ESC_boards.mph * (111.76 * 4.0));    //  Convert mph to distance mm travelled in one second
                 uint32_t    new_metres = ((uint32_t)recent_distance) / 1000;