First working, tested and calibrated unit

Dependencies:   mbed USBDevice

Revision:
2:ddc96642fcdb
Parent:
1:0d88cfafe20e
Child:
3:a170b248ead8
--- a/main.cpp	Wed Mar 22 21:59:37 2017 +0000
+++ b/main.cpp	Mon Mar 27 20:54:42 2017 +0000
@@ -1,45 +1,75 @@
-/* BATTERY ESR MEASUREMENT
+/* mbed USBJoystick Library Demo
+ * Copyright (c) 2012, v01:  Initial version, WH,
+ *                           Modified USBMouse code ARM Limited.
+ *                           (c) 2010-2011 mbed.org, MIT License
+ *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, inclumosig without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
 
-Note: The design intent was to calibrate each board rather than designing 
-    and assembling precision analog circuits for each shield-board.
-      Use compiler directive switch in this firmware to perform routine to 
-    obtain calibration coeficients for each board. Place these obtained cal 
-    coefficients in the CALIBRATION CONSTANTS section and then recompile for test mode
-    
-    See ENG-1493 for more details and results
+// Note: you must connect the usb cable to your computer before the program will proceed
 
-**** Be sure to set the approriate BOARDNUMBER before compiling ***********
-
-*/
-
+// USB parts from: https://developer.mbed.org/forum/helloworld/topic/3496/
 
 #include "mbed.h"
+#include "USBKeyboard.h"
+
+//USBMouse mouse;
+USBKeyboard mbedKeyBoard;
+
+// Variables for Heartbeat and Status monitoring
+PwmOut myled1(LED1);
+PwmOut myled2(LED2);
+PwmOut myled3(LED3);
+DigitalOut heartbeatLED(LED4);
+
 
 Serial pc(SERIAL_TX, SERIAL_RX);
 DigitalOut MOSFET(PA_4);      // Connected to Gate of MOSFET
 AnalogIn   VBin(PA_1);        // To measure battery Voltage
 DigitalOut LED(D4);           // Activity indicator
 
+DigitalIn enable(PA_5);
+
 #define WIRE_LEAD_RES (0.23F)
 
 // BOARD SPECIFIC CALIBRATION CONSTANTS
 #define BOARDNUMBER 1
 
 #if BOARDNUMBER == 1
-#define V_SCALE  3.735935F 
- 
-    
+#define V_SCALE  3.735935F
+
+
 #elif BOARDNUMBER == 2
-#define V_SCALE 3.3F                                                       
+#define V_SCALE 3.3F
 
 #elif BOARDNUMBER == 3
-#define V_SCALE 3.3F                                                       
+#define V_SCALE 3.3F
 
-#else 
-#define V_SCALE 3.3F                                                       
+#else
+#define V_SCALE 3.3F
 
 #endif
 
+Ticker heartbeat;
+
+
 
 
 // Parameters
@@ -52,14 +82,44 @@
     waiting = 0;
 }
 
- 
-int main() {
-    float OpenVoltage,LoadVoltage;
+
+int16_t map(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max) // found here: C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\WMath.cpp
+{
+    return (int16_t)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
+}
+
+float mapf(float x, float in_min, float in_max, float out_min, float out_max)
+{
+    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
 
-    pc.baud(115200);    
+// Heartbeat monitor
+//void pulse()
+//{
+//    heartbeatLED = !heartbeatLED;
+//}
+//
+//void heartbeat_start()
+//{
+//    heartbeatLED = 1;
+//    heartbeat.attach(&pulse, 0.5);
+//}
+//
+//void heartbeat_stop()
+//{
+//    heartbeat.detach();
+//}
+
+
+int main()
+{
+
+    float OpenVoltage, LoadVoltage, Esr;
+
+    pc.baud(115200);
     printf("Battery ESR Tester\n\r");
     pc.attach(&OnSerial);
-    
+
     MOSFET = 0;
     LED = 0;
 
@@ -67,11 +127,17 @@
 #if 1  // 0 = Calibration Mode, 1= Discharge test mode  
 
     printf("Test Mode\n\r");
+
     while(1) {
+
         waiting = 1;
-        while(waiting){
+
+        while(enable) {
             wait(0.1);
-        }    
+        }
+        mbedKeyBoard.printf("Test running: ");
+        printf("Test running: ");
+        wait(3); //debounce and force a minimum time between tests to ensure the battery has time to recovery.
         OpenVoltage = VBin.read()* V_SCALE;
         LED = 1;
         MOSFET = 1;
@@ -79,33 +145,35 @@
         LoadVoltage = VBin.read()* V_SCALE;
         LED = 0;
         MOSFET = 0;
-        
+        Esr = (OpenVoltage-LoadVoltage)/(LoadVoltage / 3.0f)- WIRE_LEAD_RES;
+        mbedKeyBoard.printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
+                            OpenVoltage, LoadVoltage, Esr);
         printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
-            OpenVoltage,LoadVoltage,(OpenVoltage-LoadVoltage)/(LoadVoltage/3.0f)-WIRE_LEAD_RES);
+                            OpenVoltage, LoadVoltage, Esr);
     }
 
 
-    
+
 #else
 
     //Perform Board Calibration
     printf("\n\rCalibration Mode\n\r");
     waiting = 1;
     while (pc.readable()) { // flush buffer
-            serial_inchar = pc.getc();
-        }    
+        serial_inchar = pc.getc();
+    }
     printf("Set Vin to 3.600V then [press any key]\n\r");
-    while(waiting == 1){
+    while(waiting == 1) {
         wait(0.05);
-    }    
+    }
     printf("Reading...\n\r");
     wait(0.5);
     OpenVoltage = VBin.read();
 
     printf("Cut/paste this calibration into the calibration section...\n\r\n\r");
     printf("#define V_SCALE %fF\n\r",3.6/OpenVoltage);
-    
-    
-#endif  
- 
+
+
+#endif
+  
 }
\ No newline at end of file