First working, tested and calibrated unit

Dependencies:   mbed USBDevice

Committer:
ruairilong
Date:
Mon Mar 27 20:54:42 2017 +0000
Revision:
2:ddc96642fcdb
Parent:
1:0d88cfafe20e
Child:
3:a170b248ead8
Add USB HID library.; Printf the tests results data over the HID keyboard device link; Add support for a push button that the user can push to initiate the battery tests.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ruairilong 2:ddc96642fcdb 1 /* mbed USBJoystick Library Demo
ruairilong 2:ddc96642fcdb 2 * Copyright (c) 2012, v01: Initial version, WH,
ruairilong 2:ddc96642fcdb 3 * Modified USBMouse code ARM Limited.
ruairilong 2:ddc96642fcdb 4 * (c) 2010-2011 mbed.org, MIT License
ruairilong 2:ddc96642fcdb 5 * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button
ruairilong 2:ddc96642fcdb 6 *
ruairilong 2:ddc96642fcdb 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
ruairilong 2:ddc96642fcdb 8 * of this software and associated documentation files (the "Software"), to deal
ruairilong 2:ddc96642fcdb 9 * in the Software without restriction, inclumosig without limitation the rights
ruairilong 2:ddc96642fcdb 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ruairilong 2:ddc96642fcdb 11 * copies of the Software, and to permit persons to whom the Software is
ruairilong 2:ddc96642fcdb 12 * furnished to do so, subject to the following conditions:
ruairilong 2:ddc96642fcdb 13 *
ruairilong 2:ddc96642fcdb 14 * The above copyright notice and this permission notice shall be included in
ruairilong 2:ddc96642fcdb 15 * all copies or substantial portions of the Software.
ruairilong 2:ddc96642fcdb 16 *
ruairilong 2:ddc96642fcdb 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ruairilong 2:ddc96642fcdb 18 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ruairilong 2:ddc96642fcdb 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ruairilong 2:ddc96642fcdb 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ruairilong 2:ddc96642fcdb 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ruairilong 2:ddc96642fcdb 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ruairilong 2:ddc96642fcdb 23 * THE SOFTWARE.
ruairilong 2:ddc96642fcdb 24 */
JoeMiller 0:197cfa57c3c7 25
ruairilong 2:ddc96642fcdb 26 // Note: you must connect the usb cable to your computer before the program will proceed
JoeMiller 0:197cfa57c3c7 27
ruairilong 2:ddc96642fcdb 28 // USB parts from: https://developer.mbed.org/forum/helloworld/topic/3496/
JoeMiller 0:197cfa57c3c7 29
JoeMiller 0:197cfa57c3c7 30 #include "mbed.h"
ruairilong 2:ddc96642fcdb 31 #include "USBKeyboard.h"
ruairilong 2:ddc96642fcdb 32
ruairilong 2:ddc96642fcdb 33 //USBMouse mouse;
ruairilong 2:ddc96642fcdb 34 USBKeyboard mbedKeyBoard;
ruairilong 2:ddc96642fcdb 35
ruairilong 2:ddc96642fcdb 36 // Variables for Heartbeat and Status monitoring
ruairilong 2:ddc96642fcdb 37 PwmOut myled1(LED1);
ruairilong 2:ddc96642fcdb 38 PwmOut myled2(LED2);
ruairilong 2:ddc96642fcdb 39 PwmOut myled3(LED3);
ruairilong 2:ddc96642fcdb 40 DigitalOut heartbeatLED(LED4);
ruairilong 2:ddc96642fcdb 41
JoeMiller 0:197cfa57c3c7 42
JoeMiller 0:197cfa57c3c7 43 Serial pc(SERIAL_TX, SERIAL_RX);
JoeMiller 1:0d88cfafe20e 44 DigitalOut MOSFET(PA_4); // Connected to Gate of MOSFET
JoeMiller 0:197cfa57c3c7 45 AnalogIn VBin(PA_1); // To measure battery Voltage
JoeMiller 0:197cfa57c3c7 46 DigitalOut LED(D4); // Activity indicator
JoeMiller 0:197cfa57c3c7 47
ruairilong 2:ddc96642fcdb 48 DigitalIn enable(PA_5);
ruairilong 2:ddc96642fcdb 49
JoeMiller 1:0d88cfafe20e 50 #define WIRE_LEAD_RES (0.23F)
JoeMiller 0:197cfa57c3c7 51
JoeMiller 0:197cfa57c3c7 52 // BOARD SPECIFIC CALIBRATION CONSTANTS
JoeMiller 0:197cfa57c3c7 53 #define BOARDNUMBER 1
JoeMiller 0:197cfa57c3c7 54
JoeMiller 0:197cfa57c3c7 55 #if BOARDNUMBER == 1
ruairilong 2:ddc96642fcdb 56 #define V_SCALE 3.735935F
ruairilong 2:ddc96642fcdb 57
ruairilong 2:ddc96642fcdb 58
JoeMiller 0:197cfa57c3c7 59 #elif BOARDNUMBER == 2
ruairilong 2:ddc96642fcdb 60 #define V_SCALE 3.3F
JoeMiller 0:197cfa57c3c7 61
JoeMiller 0:197cfa57c3c7 62 #elif BOARDNUMBER == 3
ruairilong 2:ddc96642fcdb 63 #define V_SCALE 3.3F
JoeMiller 0:197cfa57c3c7 64
ruairilong 2:ddc96642fcdb 65 #else
ruairilong 2:ddc96642fcdb 66 #define V_SCALE 3.3F
JoeMiller 0:197cfa57c3c7 67
JoeMiller 0:197cfa57c3c7 68 #endif
JoeMiller 0:197cfa57c3c7 69
ruairilong 2:ddc96642fcdb 70 Ticker heartbeat;
ruairilong 2:ddc96642fcdb 71
ruairilong 2:ddc96642fcdb 72
JoeMiller 0:197cfa57c3c7 73
JoeMiller 0:197cfa57c3c7 74
JoeMiller 0:197cfa57c3c7 75 // Parameters
JoeMiller 1:0d88cfafe20e 76 #define PULSEWIDTH 1.0f // seconds
JoeMiller 0:197cfa57c3c7 77 char serial_inchar,waiting;
JoeMiller 0:197cfa57c3c7 78
JoeMiller 0:197cfa57c3c7 79 void OnSerial(void) // serial port interrupt used in calibration mode
JoeMiller 0:197cfa57c3c7 80 {
JoeMiller 0:197cfa57c3c7 81 serial_inchar = pc.getc();
JoeMiller 0:197cfa57c3c7 82 waiting = 0;
JoeMiller 0:197cfa57c3c7 83 }
JoeMiller 0:197cfa57c3c7 84
ruairilong 2:ddc96642fcdb 85
ruairilong 2:ddc96642fcdb 86 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
ruairilong 2:ddc96642fcdb 87 {
ruairilong 2:ddc96642fcdb 88 return (int16_t)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
ruairilong 2:ddc96642fcdb 89 }
ruairilong 2:ddc96642fcdb 90
ruairilong 2:ddc96642fcdb 91 float mapf(float x, float in_min, float in_max, float out_min, float out_max)
ruairilong 2:ddc96642fcdb 92 {
ruairilong 2:ddc96642fcdb 93 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
ruairilong 2:ddc96642fcdb 94 }
JoeMiller 0:197cfa57c3c7 95
ruairilong 2:ddc96642fcdb 96 // Heartbeat monitor
ruairilong 2:ddc96642fcdb 97 //void pulse()
ruairilong 2:ddc96642fcdb 98 //{
ruairilong 2:ddc96642fcdb 99 // heartbeatLED = !heartbeatLED;
ruairilong 2:ddc96642fcdb 100 //}
ruairilong 2:ddc96642fcdb 101 //
ruairilong 2:ddc96642fcdb 102 //void heartbeat_start()
ruairilong 2:ddc96642fcdb 103 //{
ruairilong 2:ddc96642fcdb 104 // heartbeatLED = 1;
ruairilong 2:ddc96642fcdb 105 // heartbeat.attach(&pulse, 0.5);
ruairilong 2:ddc96642fcdb 106 //}
ruairilong 2:ddc96642fcdb 107 //
ruairilong 2:ddc96642fcdb 108 //void heartbeat_stop()
ruairilong 2:ddc96642fcdb 109 //{
ruairilong 2:ddc96642fcdb 110 // heartbeat.detach();
ruairilong 2:ddc96642fcdb 111 //}
ruairilong 2:ddc96642fcdb 112
ruairilong 2:ddc96642fcdb 113
ruairilong 2:ddc96642fcdb 114 int main()
ruairilong 2:ddc96642fcdb 115 {
ruairilong 2:ddc96642fcdb 116
ruairilong 2:ddc96642fcdb 117 float OpenVoltage, LoadVoltage, Esr;
ruairilong 2:ddc96642fcdb 118
ruairilong 2:ddc96642fcdb 119 pc.baud(115200);
JoeMiller 1:0d88cfafe20e 120 printf("Battery ESR Tester\n\r");
JoeMiller 0:197cfa57c3c7 121 pc.attach(&OnSerial);
ruairilong 2:ddc96642fcdb 122
JoeMiller 1:0d88cfafe20e 123 MOSFET = 0;
JoeMiller 0:197cfa57c3c7 124 LED = 0;
JoeMiller 0:197cfa57c3c7 125
JoeMiller 0:197cfa57c3c7 126
JoeMiller 0:197cfa57c3c7 127 #if 1 // 0 = Calibration Mode, 1= Discharge test mode
JoeMiller 0:197cfa57c3c7 128
JoeMiller 1:0d88cfafe20e 129 printf("Test Mode\n\r");
ruairilong 2:ddc96642fcdb 130
JoeMiller 1:0d88cfafe20e 131 while(1) {
ruairilong 2:ddc96642fcdb 132
JoeMiller 1:0d88cfafe20e 133 waiting = 1;
ruairilong 2:ddc96642fcdb 134
ruairilong 2:ddc96642fcdb 135 while(enable) {
JoeMiller 1:0d88cfafe20e 136 wait(0.1);
ruairilong 2:ddc96642fcdb 137 }
ruairilong 2:ddc96642fcdb 138 mbedKeyBoard.printf("Test running: ");
ruairilong 2:ddc96642fcdb 139 printf("Test running: ");
ruairilong 2:ddc96642fcdb 140 wait(3); //debounce and force a minimum time between tests to ensure the battery has time to recovery.
JoeMiller 1:0d88cfafe20e 141 OpenVoltage = VBin.read()* V_SCALE;
JoeMiller 1:0d88cfafe20e 142 LED = 1;
JoeMiller 1:0d88cfafe20e 143 MOSFET = 1;
JoeMiller 1:0d88cfafe20e 144 wait(PULSEWIDTH);
JoeMiller 1:0d88cfafe20e 145 LoadVoltage = VBin.read()* V_SCALE;
JoeMiller 0:197cfa57c3c7 146 LED = 0;
JoeMiller 1:0d88cfafe20e 147 MOSFET = 0;
ruairilong 2:ddc96642fcdb 148 Esr = (OpenVoltage-LoadVoltage)/(LoadVoltage / 3.0f)- WIRE_LEAD_RES;
ruairilong 2:ddc96642fcdb 149 mbedKeyBoard.printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
ruairilong 2:ddc96642fcdb 150 OpenVoltage, LoadVoltage, Esr);
JoeMiller 1:0d88cfafe20e 151 printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
ruairilong 2:ddc96642fcdb 152 OpenVoltage, LoadVoltage, Esr);
JoeMiller 1:0d88cfafe20e 153 }
JoeMiller 0:197cfa57c3c7 154
JoeMiller 0:197cfa57c3c7 155
ruairilong 2:ddc96642fcdb 156
JoeMiller 0:197cfa57c3c7 157 #else
JoeMiller 1:0d88cfafe20e 158
JoeMiller 0:197cfa57c3c7 159 //Perform Board Calibration
JoeMiller 0:197cfa57c3c7 160 printf("\n\rCalibration Mode\n\r");
JoeMiller 0:197cfa57c3c7 161 waiting = 1;
JoeMiller 0:197cfa57c3c7 162 while (pc.readable()) { // flush buffer
ruairilong 2:ddc96642fcdb 163 serial_inchar = pc.getc();
ruairilong 2:ddc96642fcdb 164 }
JoeMiller 1:0d88cfafe20e 165 printf("Set Vin to 3.600V then [press any key]\n\r");
ruairilong 2:ddc96642fcdb 166 while(waiting == 1) {
JoeMiller 0:197cfa57c3c7 167 wait(0.05);
ruairilong 2:ddc96642fcdb 168 }
JoeMiller 1:0d88cfafe20e 169 printf("Reading...\n\r");
JoeMiller 0:197cfa57c3c7 170 wait(0.5);
JoeMiller 1:0d88cfafe20e 171 OpenVoltage = VBin.read();
JoeMiller 0:197cfa57c3c7 172
JoeMiller 1:0d88cfafe20e 173 printf("Cut/paste this calibration into the calibration section...\n\r\n\r");
JoeMiller 1:0d88cfafe20e 174 printf("#define V_SCALE %fF\n\r",3.6/OpenVoltage);
ruairilong 2:ddc96642fcdb 175
ruairilong 2:ddc96642fcdb 176
ruairilong 2:ddc96642fcdb 177 #endif
ruairilong 2:ddc96642fcdb 178
JoeMiller 0:197cfa57c3c7 179 }