Concept code which launches two threads, one of which implements a Modbus Tunnel protocol to talk with an ISEM, the other which launches a Modbus RTU protocol to talk to a CPUM (most of the Modbus code has been removed prior to publishing.) A canned AC and DC spectra is provided to display wave forms on start-up however the project normally polls for spectra from the ISEM and then plots is (that functionality has been removed prior to publishing.)

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI

/media/uploads/Damotclese/img_20190530_100915.jpg

Plot of the initial start-up canned AC and DC spectra.

Revision:
0:387684ec9d92
Child:
1:01394e5db95b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NextGen-Exerciser-Main.cpp	Thu May 30 05:16:27 2019 +0000
@@ -0,0 +1,303 @@
+
+// ----------------------------------------------------------------------
+// NextGen-Exerciser-Main.cpp
+//
+// Fredric L. Rice, May 2019
+//
+// o The LCD is cleared and configured
+// o A canned AC and DC 2f water-in-ambient-air spectrum is plotted 
+//   on the LCD after the spectrum is "normalized" by making each of
+//   the sample values positive and then compressed so that it fits 
+//   in the display
+// o A thread is created to perform Modbus against a CPUM device
+// o A thread is created to perform Modbus Tunneling with foreign
+//   frames against an ISEM device
+// o The main() thread wakes up once a second and performs whatever
+//   other functionality might be desired
+//
+// ----------------------------------------------------------------------
+
+#include "mbed.h"                       // The mbed operating system
+#include "NextGen-Exerciser-Modbus.h"   // For the CPUM Modbus thread
+#include "NextGen-Exerciser-Tunnel.h"   // For the Modbus Tunnel thread
+#include "NextGen-Exerciser-Defines.h"  // For defined constants and MACROs
+#include "LCD_DISCO_F429ZI.h"           // For controlling the LCD
+
+// ----------------------------------------------------------------------
+// Defined constants that we will use here
+//
+// ----------------------------------------------------------------------
+
+#define LCD_WIDTH       240
+#define LCD_HEIGHT      320
+
+// ----------------------------------------------------------------------
+// Allocate local data storage
+//
+// ----------------------------------------------------------------------
+
+    // Describe a canned 2f AC spectrum  consisting of 512 floating point samples
+    // Note that the values after the decimal point are not really needed, they
+    // get discarded when the spectrum values are plotted as integer values.
+    // These values were extracted after Spectra Processor decimated a spectrum
+    // of 65536 samples. We "normalize" the values to make them all positive and
+    // later we take the 512 samples and further "decimate" by averaging every 
+    // two samples, resuling in 256 final values. The 256 values get plotted on 
+    // to the LCD along the "width" of the display which is 320 pixels, leaving
+    // 64 pixels to the right of the plot free to be used for other things.
+    static float f_decimatedACSpectrum[] =
+    {
+        -1.502448670,9.237028121,18.872425000,27.111267000,33.651569300,38.191074300,40.437301633,40.118068700,
+        36.992050100,30.858972544,21.569023133,9.031014442,-6.781046860,-25.823335644,-47.979820200,-73.062263400,
+        -100.812744110,-130.908676110,-162.970184330,-196.569778440,-231.243743000,-266.505157440,-301.857879000,-336.811126000,
+        -370.893982000,-403.669342000,-434.746948220,-463.794647220,-490.547790000,-514.816223110,-536.488708440,-555.534729000,
+        -572.003357000,-586.019348110,-597.776916000,-607.531311000,-615.588317000,-622.292602000,-628.014892000,-633.139160110,
+        -638.049499000,-643.118225000,-648.694641110,-655.095459000,-662.597473110,-671.431945000,-681.781494110,-693.779174000,
+        -707.509704000,-723.013183000,-740.289856000,-759.307129000,-780.007019000,-802.314636220,-826.146362330,-851.418640110,
+        -878.054870000,-905.992431000,-935.187500000,-965.618835440,-997.289367000,-1030.226684400,-1064.481080000,-1100.122430000,
+        -1137.235840000,-1175.915650000,-1216.259270000,-1258.360100000,-1302.301140000,-1348.147820000,-1395.942993300,-1445.701293300,
+        -1497.406372200,-1551.007934400,-1606.421874400,-1663.530761100,-1722.186401100,-1782.213134400,-1843.413452200,-1905.572753300,
+        -1968.465942200,-2031.863770000,-2095.539062200,-2159.272210000,-2222.855460000,-2286.097900000,-2348.825683300,-2410.885253300,
+        -2472.140130000,-2532.470214400,-2591.767090000,-2649.928460000,-2706.852294400,-2762.428950000,-2816.533440000,-2869.017333300,
+        -2919.700190000,-2968.362304400,-3014.738520000,-3058.511710000,-3099.308830000,-3136.698974400,-3170.191162200,-3199.236572200,
+        -3223.229492200,-3241.513671100,-3253.386710000,-3258.107421100,-3254.905273300,-3242.988030000,-3221.553710000,-3189.798340000,
+        -3146.928950000,-3092.171142200,-3024.780761100,-2944.051020000,-2849.322510000,-2739.990234400,-2615.509270000,-2475.402343300,
+        -2319.263183300,-2146.761962200,-1957.648560000,-1751.756340000,-1529.004510000,-1289.401733300,-1033.048700000,-760.140625000,
+        -470.970947220,-165.934036220,154.471374000,489.638946000,838.851257330,1201.276733300,1575.967041100,1961.854370000,
+        2357.750480000,2762.345940000,3174.210693300,3591.797120000,4013.442382200,4437.375480000,4861.724610000,5284.524414400,
+        5703.730950000,6117.232910000,6522.867180000,6918.436030000,7301.727050000,7670.530761100,8022.663080000,8355.986320000,
+        8668.431640000,8958.021484400,9222.888671100,9461.302734400,9671.681640000,9852.618164400,10002.893500000,10121.489200000,
+        10207.603511000,10260.660100000,10280.314400000,10266.458900000,10219.221600000,10138.971600000,10026.307611000,9882.054680000,
+        9707.252930000,9503.147460000,9271.170890000,9012.928710000,8730.179680000,8424.816400000,8098.844230000,7754.357421100,
+        7393.519531100,7018.538080000,6631.642570000,6235.063470000,5831.010742200,5421.653800000,5009.104003300,4595.396972200,
+        4182.478510000,3772.192871100,3366.271484400,2966.325440000,2573.839350000,2190.168450000,1816.535644400,1454.033320000,
+        1103.623901100,766.144043000,442.308807330,132.717117330,-162.142257000,-441.884216330,-706.220947220,-954.955566440,
+        -1187.975830000,-1405.248901100,-1606.816040000,-1792.787720000,-1963.340570000,-2118.713134400,-2259.203370000,-2385.165283300,
+        -2497.007812200,-2595.190430000,-2680.222412200,-2752.658203300,-2813.093994400,-2862.164550000,-2900.537590000,-2928.907950000,
+        -2947.991700000,-2958.517822200,-2961.222650000,-2956.839350000,-2946.091552200,-2929.683350000,-2908.292230000,-2882.560540000,
+        -2853.090080000,-2820.434082200,-2785.094482200,-2747.517333300,-2708.092041100,-2667.150140000,-2624.968500000,-2581.769770000,
+        -2537.729730000,-2492.980224400,-2447.618164400,-2401.711420000,-2355.307120000,-2308.439690000,-2261.137940000,-2213.432861100,
+        -2165.363520000,-2116.981440000,-2068.355224400,-2019.572380000,-1970.740350000,-1921.984980000,-1873.449340000,-1825.289550000,
+        -1777.670043300,-1730.759150000,-1684.721923300,-1639.715080000,-1595.880493300,-1553.340080000,-1512.190790000,-1472.501342200,
+        -1434.309570000,-1397.621210000,-1362.410760000,-1328.622680000,-1296.175292200,-1264.964600000,-1234.869873300,-1205.759880000,
+        -1177.499140000,-1149.954711100,-1123.002800000,-1096.534050000,-1070.459472200,-1044.713134400,-1019.255790000,-994.075744000,
+        -969.188293440,-944.634338330,-920.477295000,-896.798462000,-873.692077000,-851.259216330,-829.601562440,-808.814880330,
+        -788.983154220,-770.172973000,-752.429016110,-735.770691000,-720.190063440,-705.651245110,-692.091308000,-679.422546330,
+        -667.536193000,-656.307312000,-645.600402000,-635.275817000,-625.196350000,-615.233886000,-605.275512000,-595.229004000,
+        -585.027282000,-574.631591000,-564.033447220,-553.254821000,-542.346923000,-531.387756330,-520.477966330,-509.735595000,
+        -499.290527330,-489.277618440,-479.830261220,-471.073608330,-463.118743000,-456.057190000,-449.956909110,-444.859253000,
+        -440.777649000,-437.697479220,-435.577880000,-434.354522000,-433.944000220,-434.248993000,-435.164337110,-436.583465000,
+        -438.404754000,-440.537933330,-442.909454330,-445.467254000,-448.184082000,-451.059661000,-454.121124220,-457.422180110,
+        -461.040466330,-465.073761000,-469.634948000,-474.846222000,-480.832397440,-487.714386000,-495.602508000,-504.590667000,
+        -514.750915000,-526.129760000,-538.745178220,-552.586059000,-567.612426000,-583.757812440,-600.933227000,-619.031982440,
+        -637.936096110,-657.523071220,-677.673339000,-698.277160000,-719.241394000,-740.495056110,-761.993713330,-783.721740000,
+        -805.693237330,-827.950134220,-850.558349000,-873.601989000,-897.174682000,-921.369873000,-946.269043000,-971.929199220,
+        -998.369567000,-1025.558350000,-1053.400024400,-1081.723632200,-1110.273071100,-1138.698974400,-1166.554560000,-1193.292602200,
+        -1218.267211100,-1240.738770000,-1259.881470000,-1274.795280000,-1284.520751100,-1288.056152200,-1284.377800000,-1272.461420000,
+        -1251.305900000,-1219.956910000,-1177.530883300,-1123.238160000,-1056.406250000,-976.499206000,-883.136718000,-776.109497000,
+        -655.391845000,-521.151001000,-373.752929000,-213.764404220,-41.951789800,140.723938000,333.115356440,533.900024440,
+        741.594665000,954.571960440,1171.079711100,1389.261840000,1607.181152200,1822.842651100,2034.219230000,2239.276360000,
+        2435.997802200,2622.412110000,2796.617180000,2956.805420000,3101.288080000,3228.517822200,3337.110590000,3425.865234400,
+        3493.781000000,3540.073974400,3564.189453300,3565.812744400,3544.875244400,3501.559570000,3436.298820000,3349.772700000,
+        3242.901850000,3116.834472200,2972.933350000,2812.756340000,2638.033691100,2450.644531100,2252.587640000,2045.952140000,
+        1832.885253300,1615.559814400,1396.141350000,1176.755120000,959.454528000,746.191284110,538.787902000,338.913055440,
+        148.060974110,-32.465728700,-201.568542440,-358.361267000,-502.173584000,-632.550109000,-749.244995110,-852.212768000,
+        -941.595398000,-1017.705932200,-1081.010010000,-1132.105340000,-1171.699584400,-1200.587760000,-1219.629394400,-1229.727172200,
+        -1231.805170000,-1226.789910000,-1215.592890000,-1199.095330000,-1178.135864400,-1153.499633300,-1125.911621100,-1096.030510000,
+        -1064.447021100,-1031.682000000,-998.188537000,-964.353820000,-930.503479000,-896.906066000,-863.778503440,-831.291504000,
+        -799.575500440,-768.725891110,-738.808532000,-709.864502000,-681.914245000,-654.961486000,-628.996582000,-603.999267000,
+        -579.941101000,-556.787353000,-534.498901330,-513.033691440,-492.348114000,-472.398071220,-453.140289330,-434.533050000,
+        -416.537292440,-399.117187440,-382.240600000,-365.879577000,-350.010192000,-334.612487000,-319.669769220,-305.167755110,
+        -291.093566000,-277.434234000,-264.175262440,-251.299041000,-238.783386220,-226.600113000,-214.714141000,-203.082885000,
+        -191.656234000,-180.377243000,-169.183487000,-158.009185000,-146.788055440,-135.456863440,-123.959488000,-112.251495330,
+        -100.304824000,-88.112571711,-75.693458500,-63.095802300,-50.400745400,-37.724391933,-25.218734744,-13.071174622
+    } ;
+   
+    // We will be using the LCD so instantiate an object locally
+    static LCD_DISCO_F429ZI st_lcd;
+    
+// ----------------------------------------------------------------------
+// MainProcessDecimatedSpectrum()
+//
+// This function will:
+//      o Search through the AC spectra and the DC spectra looking for
+//        the smallest and the highest numeric values in both
+//      o Convert the smallest number in to an absolute value by removing
+//        the sign bit using the ANSI absolute function
+//      o Add that now-positive smallest number to every sample in the
+//        AC and DC spectrums so that each value in the spectrums are
+//        positive values
+//      o Add that smallest value to the known largest value so that it
+//        agrees with the rest of the spectrum again
+//      o Compute a ratio derived by dividing the ;argest known value 
+//        by the "width" of the screen
+//      o Goes through all 512 samples and averages them two by two,
+//        compressing the spectrum in place (in the same array of values)
+//        so that we end up with 256 samples which can fit on the LCD
+//      o Divides each of the 256 samples by the radio so that each of
+//        the samples will fit on the LCD
+//
+// ----------------------------------------------------------------------
+static void MainProcessDecimatedSpectrum(void)
+{
+    uint16_t u16_searchLoop  = 0;
+    uint32_t u32_sampleRatio = 0;
+    uint16_t u16_resultIndex = 0;
+    float    f_smallNumber   = 10000000.0f;
+    float    f_largeNumber   = 0.0f;
+    
+    // Search through the AC spectrum for the smallest and the largest values
+    for (u16_searchLoop = 0; u16_searchLoop < 512; u16_searchLoop++)
+    {
+        if (f_decimatedACSpectrum[u16_searchLoop] < f_smallNumber)
+        {
+            // This value is the new lowest number
+            f_smallNumber = f_decimatedACSpectrum[u16_searchLoop];
+        }
+        
+        if (f_decimatedACSpectrum[u16_searchLoop] > f_largeNumber)
+        {
+            // This value is the new highest number
+            f_largeNumber = f_decimatedACSpectrum[u16_searchLoop];
+        }        
+    }
+    
+    // Is the smallest number negative? We know that it is but we check
+    if (f_smallNumber < 0.0f)
+    {
+        // Make the value absolute so that it is positive
+        f_smallNumber = abs(f_smallNumber);
+    }
+    
+    // To make all values positive, add the newly-derived positive value.
+    // This makes all numbers positive and ensures that the smallest
+    // value in the spectra is zero.
+    for (u16_searchLoop = 0; u16_searchLoop < 512; u16_searchLoop++)
+    {
+        f_decimatedACSpectrum[u16_searchLoop] += f_smallNumber;
+    }
+    
+    // Also add the newly-positive number to our largest known value
+    f_largeNumber += f_smallNumber;
+    
+    // Determine the ratio to use, the LCD screen is 240 by 320 so we 
+    // take the highest number value in the spectra and divide by 240
+    // for the verticle plot, and we have 512 samples which will be
+    // plotted across the 320 by averaging every 2 samples to yield
+    // 256 horizontal positions. We plot with the screen laying on
+    // its side
+    u32_sampleRatio = (f_largeNumber / LCD_WIDTH);
+    
+    // Go through each pairs of samples and average them and then
+    // divide the result by the ratio. Step through in twos
+    for (u16_searchLoop = 0; u16_searchLoop < 512; u16_searchLoop += 2, u16_resultIndex++)
+    {
+        // Take the next two samples and average them, putting the result
+        // in to the next result index of the same buffer we're processing
+        f_decimatedACSpectrum[u16_resultIndex] =
+            (f_decimatedACSpectrum[u16_searchLoop] + f_decimatedACSpectrum[u16_searchLoop]) / 2;
+    
+        // Divide the sample value by the ratio
+        f_decimatedACSpectrum[u16_resultIndex] /= u32_sampleRatio;
+    }
+}
+
+// ----------------------------------------------------------------------
+// MainPlotFinalSpectrum()
+//
+// This function will step through the "normalized" and compressed AC
+// and DC spectrums consisting of 256 samples and will plot a line from
+// one sample to the next on the LCD
+//
+// ----------------------------------------------------------------------
+static void MainPlotFinalSpectrum(void)
+{
+    uint16_t u16_searchLoop = 0;
+
+    // Plot the spectrum values, plotting a line from point to point
+    for (u16_searchLoop = 0; u16_searchLoop < ((512 / 2) - 1); u16_searchLoop++)
+    {
+        // Ensure that we actually fit. The sample should
+        if (f_decimatedACSpectrum[u16_searchLoop] <= LCD_WIDTH)
+        {
+            st_lcd.DrawLine(f_decimatedACSpectrum[u16_searchLoop], u16_searchLoop, 
+                f_decimatedACSpectrum[u16_searchLoop + 1], u16_searchLoop + 1);
+        }
+        else
+        {
+            // Mathematically we topped-out past the maximum some how
+            st_lcd.DrawPixel(0, u16_searchLoop, LCD_COLOR_WHITE);
+        }
+    }
+}
+
+// ----------------------------------------------------------------------
+// MainInit()
+//
+// This function will:
+//      o Clear and initialize the LCD
+//      o Process the scanned decimated 2f AC and DC spectrums
+//      o Plot the processed spectrums
+//
+// ----------------------------------------------------------------------
+static void MainInit(void)
+{
+    // Bring the LCD up and set its various colors
+    st_lcd.Clear(LCD_COLOR_BLACK);
+    st_lcd.SetBackColor(LCD_COLOR_BLACK);
+    st_lcd.SetTextColor(LCD_COLOR_WHITE);
+    
+    // Process the decimated spectrum and then plot it
+    MainProcessDecimatedSpectrum();
+    MainPlotFinalSpectrum();
+}
+
+// ----------------------------------------------------------------------
+// MainThread()
+//
+// Called once a second.
+//
+// ----------------------------------------------------------------------
+static void MainThread(void)
+{
+}
+
+// ----------------------------------------------------------------------
+// main()
+//
+// This is the main entry point which the operating system will load 
+// and execute on power-up after the OS completes its own start-up
+// functionality.
+//
+// The function will:
+//      o Initialize this module's functionality
+//      o Launch the MODBUS CPUM thread
+//      o Launch the Modbus Tunnel thread
+//      o Enter in to a forever loop which should never terminate,
+//        waking up once a second to perform any additional desired
+//        functionality
+//
+// ----------------------------------------------------------------------
+int main(void)
+{
+    // Perform local module initialization, if any
+    MainInit();
+
+    // Initialize and launch the MODBUS CPUM thread
+    ModbusInit();
+    
+    // Initialize and launch the MODBUS Tunnel thread
+    TunnelInit();
+
+    // Enter in to a forever loop which wakes up once a second
+    while (true)
+    {
+        // Sleep for one second
+        wait(1.0);
+   
+        MainThread();
+    }
+}
+
+// End of file
+