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:
10:0bdfd342f393
Parent:
9:644867052318
--- a/main.cpp	Fri Jun 01 07:05:58 2018 +0000
+++ b/main.cpp	Fri Jun 22 14:35:00 2018 +0000
@@ -1,5 +1,5 @@
 /*
-April 2018  -   Jon Freeman
+June 2018  -   Jon Freeman
 
 Touch Screen controller communicates with 1, 2, ... n Twin BLDC Controller boards via opto-isolated serial port.
 
@@ -98,26 +98,30 @@
 bool    trigger_current_read        = false;
 volatile    bool    trigger_32ms    = false;
 
+struct  multi_bogie_options      detected_bogie_options;
+
 class   speed_2018
 {
     static const int    SPEED_AVE_PTS   = 5;    //  AVE_PTS - points in moving average filters
-    uint32_t    speed_maf_mem   [(SPEED_AVE_PTS + 1) * 2][8],   //  Allow for up to 8 axles
-                axle_total  [8],    //  allow up to 8 axles
-                mafptr,
+    double    speed_maf_mem   [(SPEED_AVE_PTS + 1)][4];   //  Allow for up to 4 ESC bogie boards
+//                speed_maf_mem   [(SPEED_AVE_PTS + 1) * 2][8],   //  Allow for up to 8 axles
+//                axle_total  [8],    //  allow up to 8 axles
+    uint32_t    mafptr,
                 board_IDs   [4],    //  allow up to 4 boards
                 board_count,
                 b, reqno;
 public:
     speed_2018  ()  {   //  Constructor
         memset(speed_maf_mem, 0, sizeof(speed_maf_mem));
-        for (int i = 0; i < sizeof(axle_total) / sizeof(uint32_t); i++)
-            axle_total[i] = 0;
+//        for (int i = 0; i < sizeof(axle_total) / sizeof(uint32_t); i++)
+//            axle_total[i] = 0;
         mafptr = 0;
         board_count = 0;
         b = 0;
         reqno = 0;
     }
-    bool    request_rpm ()  ;
+//    bool    request_rpm ()  ;
+    bool    request_mph ()  ;
     void    rpm_update(struct parameters & a)  ;
     void    set_board_IDs (uint32_t *)   ;
     double  mph ()  ;
@@ -138,20 +142,23 @@
 //        pc.printf   ("No boards\r\n");
         return  0.0;
     }
-    int t[8] = {0,0,0,0,0,0,0,0};
+//    int t[8] = {0,0,0,0,0,0,0,0};
+    double t[8] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
     for (int i = 0; i < SPEED_AVE_PTS; i++) {
-        for (int j = 0; j < board_count * 2; j++)
+//        for (int j = 0; j < board_count * 2; j++)
+        for (int j = 0; j < board_count; j++)
             t[j] += speed_maf_mem[i][j];
     }
     int j = 0;
     for (int i = 0; i < board_count * 2; i++) {
         j += t[i];
-        axle_total[i] = t[i];
+//        axle_total[i] = t[i];
     }
-    return  (rpm2mph * ((double) j) / (SPEED_AVE_PTS * board_count * 2));
+//    return  (rpm2mph * ((double) j) / (SPEED_AVE_PTS * board_count * 2));
+    return  (j / (SPEED_AVE_PTS * board_count * 2));
 }
 
-bool    speed_2018::request_rpm ()  {   //  Issue "'n'rpm\r" to BLDC board to request RPM
+/*bool    speed_2018::request_rpm ()  {   //  Issue "'n'rpm\r" to BLDC board to request RPM
     if  (board_IDs[0] == 0)
         return  false;      //  No boards identified
     if  (board_IDs[reqno] == 0)
@@ -159,11 +166,22 @@
     com.putc    (board_IDs[reqno++]);
     com.printf  ("rpm\r");
     return  true;
+}*/
+
+bool    speed_2018::request_mph ()  {   //  Issue "'n'rpm\r" to BLDC board to request RPM
+    if  (board_IDs[0] == 0)
+        return  false;      //  No boards identified
+    if  (board_IDs[reqno] == 0)
+        reqno = 0;
+    com.putc    (board_IDs[reqno++]);
+    com.printf  ("mph\r");
+    return  true;
 }
 
 void    speed_2018::rpm_update(struct parameters & a)  {    //  Puts new readings into mem
-    speed_maf_mem   [mafptr][b++] = (uint32_t)a.dbl[0];
-    speed_maf_mem   [mafptr][b++] = (uint32_t)a.dbl[1];
+//    speed_maf_mem   [mafptr][b++] = (uint32_t)a.dbl[0];
+    speed_maf_mem   [mafptr][b++] = a.dbl[0];
+//    speed_maf_mem   [mafptr][b++] = (uint32_t)a.dbl[1];
     if  ((b + 1) >= (board_count * 2))    {
         b = 0;
         mafptr++;
@@ -432,7 +450,7 @@
 
 #endif
 
-    if  (f_r_switch)    {
+    if  (!f_r_switch)    {
         slider.direction = FWD; //  make decision from key switch position here
         com.printf  ("fw\r");
     }
@@ -489,7 +507,8 @@
     sys_timer_32Hz  = 0;
 
     int     last_digit = 0, board_cnt = 0, ch;
-    while   (sys_timer_32Hz < 12)   {       //  Sniff out system, discover motor controllers connected
+    memset  (&detected_bogie_options, 0, sizeof(struct multi_bogie_options) / sizeof(char));    //  Initialise
+    while   (sys_timer_32Hz < 12)   {       //  Quick Sniff out system, discover motor controllers connected
         while   (!trigger_32ms)
             clicore    (pccom);
         trigger_32ms = false;
@@ -504,19 +523,43 @@
                     last_digit = ch;
             }
             else    {   //  got '\r' at end of line
-                if  (isdigit(last_digit))
-                    boards_fitted[board_cnt++] = last_digit;
+                if  (isdigit(last_digit))   {
+                    boards_fitted[board_cnt] = last_digit;
+                    detected_bogie_options.bogie[board_cnt].p[ID] = last_digit;
+                    board_cnt++;
+                }
                 last_digit = 0;
             }
         }
+    }   //  Done quick sniff, should have identified all bogies connected.
+        //  Next, read complete bogie byte info into detected_bogie_options
+        
+extern  bool    get_bogie_bytes (int bogie_id, char * dest)   ; //  bogie_id could be e.g. 3, or '3' NOTE 22/06/2018 yet to write matching fn in bogie code
+        //  Returns false on timeout
+extern  char *    make_str_from_option_bytes  (char * src, char * dest)   ;       //  Use for easy readback to pc
+    sys_timer_32Hz  = 0;
+//    char txt[164];
+/*    for (int i = 0; i < board_cnt; i++) {
+       // com.printf  ("%cgbb\r", boards_fitted[i]);
+        pc.printf   ("Requesting gbb from %c\r\n", boards_fitted[i]);
+        if  (get_bogie_bytes(boards_fitted[i], detected_bogie_options.bogie[i].p))
+            pc.printf   ("Good\t");
+        else
+            pc.printf   ("Bad \t");
+        pc.printf   ("%d %d %d %d %d %d %d\r\n", detected_bogie_options.bogie[i].p[0], detected_bogie_options.bogie[i].p[1], detected_bogie_options.bogie[i].p[2], detected_bogie_options.bogie[i].p[3], detected_bogie_options.bogie[i].p[4], detected_bogie_options.bogie[i].p[5], detected_bogie_options.bogie[i].p[6]);
+//        pc.printf   ("%s", make_str_from_option_bytes  (detected_bogie_options.bogie[i].p, txt));
     }
-
-//    boards_fitted[0] = '4';
-//    boards_fitted[1] = '5';
-
+*/
+const char stdopts[] = {0,1,1,0,0,2,5,145,17,76,13,0,0,0,0,0};
     while   (boards_fitted[bfptr])    { //  This works, identified BLDC Motor Controller board ID chars '0' to '9' listed in boards_fitted[]
         pc.printf   ("Board %c found\r\n", boards_fitted[bfptr++]);
     }
+    pc.printf   ("Summer Solstice 2018, testing detected_bogie_options\r\n");
+    for (int i = 0; i < MAX_BOGIES; i++)    {
+        //pc.printf   ("\tbogie STM3 board ID %c\r\n", isdigit(detected_bogie_options.bogie[i].id) ? detected_bogie_options.bogie[i].id : '-');
+        pc.printf   ("\tbogie STM3 board %d ID %d\r\n", i, detected_bogie_options.bogie[i].p[ID]);
+    }
+    pc.printf   ("That's what was found\r\n");
     speed.set_board_IDs   (boards_fitted);    //  store number and IDs of Dual BLDC boards identified
 //    bfptr = 0;
     clicore (pccom);
@@ -524,7 +567,7 @@
     //  Done setup, time to roll !
     
     lights  (1);    //  Headlights ON!
-
+    pc.printf   ("Light up and go!\r\n");
     while (1) {
 
         struct ky_bd * present_kybd, * previous_kybd;
@@ -706,7 +749,8 @@
                     pc.printf   ("Handbrake not slipping, effort %.2f\r\n", slider.handbrake_effort);
                 }
             }
-            speed.request_rpm   (); //  issues "'n'rpm\r", takes care of cycling through available boards in sequence
+//            speed.request_rpm   (); //  issues "'n'rpm\r", takes care of cycling through available boards in sequence
+            speed.request_mph   (); //  issues "'n'rpm\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++;
@@ -727,9 +771,11 @@
             if  (!odometer_update (new_metres, (uint16_t)electrical_power_Watt, (uint16_t)(volts * 500.0)))
                 pc.printf   ("Probs with odometer_update");
             char    dist[20];
-            sprintf (dist, "%05d m", odometer_out());
-            displaytext (236, 226, 2, dist);
+            sprintf (dist, "%06dm", odometer_out());   //  12th June 2018 changed 05 to 06 to allow correct display of tot distance > 99999 metres
+//            displaytext (236, 226, 2, dist);
+            displaytext (241, 224, 2, dist);
 #endif
+            com.printf  ("ct\r");
             }   //  endof if(qtr_sec > 3
         }       //  endof if  (qtrsec_trig == true)  {
     }           //  endof while(1) main programme loop