Adds Balancing function (on the top of SPI_I2C_Parser.cpp) and changes main function adding a test case for balancing to show that it is working as intended.

Dependencies:   CUER_CAN mbed

Fork of BMS_BMUCore_cellvoltage_Mrinank by Mrinank Sharma

Committer:
ItsJustZi
Date:
Thu Sep 01 15:21:15 2016 +0000
Revision:
6:91b54ccbdf2d
Parent:
5:fc2503217ad4
Child:
7:fbe8e07fe96e
Debug attempt for SPI comms failure;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ItsJustZi 3:2238838234e1 1 /*CUER Battery Management System - Battery Management Unit (BMU) Code
ItsJustZi 3:2238838234e1 2
ItsJustZi 3:2238838234e1 3 @author
ItsJustZi 3:2238838234e1 4 Zi Koon Pong (zkp20)
ItsJustZi 3:2238838234e1 5
ItsJustZi 3:2238838234e1 6 @brief
ItsJustZi 3:2238838234e1 7 This file contains the core code which runs on the BMU LPC1768 MBED. The primary function of the BMU is to obtain the relevant parameters of the battery pack which we are interested in monitoring and]
ItsJustZi 6:91b54ccbdf2d 8 placing it on the CAN Bus for the other nodes on the network to utilise. In the event that one of our parameters goes out of a predefined safe threshold (typically specified in the cell manufacturers
ItsJustZi 3:2238838234e1 9 datasheet) we begin to limit the performance of the car so that it gets back to a region needed for safe operation (to be discussed further with telemetry).
ItsJustZi 3:2238838234e1 10
ItsJustZi 6:91b54ccbdf2d 11 There are 3 primary communication protocols that the BMU utilises: CAN, SPI and I2C.
ItsJustZi 3:2238838234e1 12
ItsJustZi 3:2238838234e1 13 -CAN is used to dump battery status information to the rest of the car
ItsJustZi 6:91b54ccbdf2d 14 -I2C is used to commmunicate with the state of charge (SoC) board
ItsJustZi 4:1881b14c977b 15 -SPI is used to communicate with the CMUs which are individually addressed
ItsJustZi 3:2238838234e1 16
ItsJustZi 6:91b54ccbdf2d 17 The mbed itself utilises conventional SPI and I2C which both get converted into differential signals to give isoSPI and dI2C to give
ItsJustZi 3:2238838234e1 18 greater resilience to EM noise reducing the chance of transmission errors.
ItsJustZi 3:2238838234e1 19
ItsJustZi 3:2238838234e1 20 dI2C conversion is done by the PCA9615 IC
ItsJustZi 3:2238838234e1 21 isoSPI conversion is done by the LTC6820 IC
ItsJustZi 3:2238838234e1 22
ItsJustZi 3:2238838234e1 23 Most of the low-level code which involves accessing individual registers in the LTC6804 and LTC2943 have been ported over from existing Linduino code provided by Linear Tech (disclaimer below).
ItsJustZi 3:2238838234e1 24 If any of the source code is unclear, feel free to contact me through email at zkp20@cam.ac.uk and I will be more than happy to help out.
ItsJustZi 3:2238838234e1 25
ItsJustZi 3:2238838234e1 26
ItsJustZi 3:2238838234e1 27 *** FOR ZI'S REFERENCE ***
ItsJustZi 3:2238838234e1 28 Initialise BMU
ItsJustZi 3:2238838234e1 29
ItsJustZi 3:2238838234e1 30 Check status of SoC and all CMU boards (comms. etc.)
ItsJustZi 3:2238838234e1 31
ItsJustZi 3:2238838234e1 32 Perform first voltage measurement and check that it coincides with the number of cells we are expecting
ItsJustZi 3:2238838234e1 33
ItsJustZi 3:2238838234e1 34 Give state of BMS. If ok, go to run. Default into safe state and set error flag.
ItsJustZi 3:2238838234e1 35
ItsJustZi 6:91b54ccbdf2d 36 LOOP
ItsJustZi 6:91b54ccbdf2d 37 Start off displaying state
ItsJustZi 3:2238838234e1 38 Run SoC updating algorithm.
ItsJustZi 3:2238838234e1 39 Run current measurement.
ItsJustZi 3:2238838234e1 40 Perform voltage measurement.
ItsJustZi 3:2238838234e1 41 Run isDischargeNeeded function.
ItsJustZi 3:2238838234e1 42 Run isSafetoDischarge function.
ItsJustZi 3:2238838234e1 43
ItsJustZi 3:2238838234e1 44
ItsJustZi 3:2238838234e1 45 Dump following data on CAN network:
ItsJustZi 3:2238838234e1 46 - Cell voltage
ItsJustZi 3:2238838234e1 47 - Cell temperatures
ItsJustZi 3:2238838234e1 48 - State of charge
ItsJustZi 6:91b54ccbdf2d 49 - Balancing status
ItsJustZi 3:2238838234e1 50 - Error flag
ItsJustZi 3:2238838234e1 51
ItsJustZi 3:2238838234e1 52 Will add helper function for communicating with both CMU (SPI) and SoC (I2C)
ItsJustZi 3:2238838234e1 53 */
ItsJustZi 3:2238838234e1 54
ItsJustZi 3:2238838234e1 55 /*! LINEAR TECH DISCLAIMER
ItsJustZi 3:2238838234e1 56 LTC6804-2 Multicell Battery Monitor
ItsJustZi 3:2238838234e1 57 @verbatim
ItsJustZi 3:2238838234e1 58 The LTC6804 is a 3rd generation multicell battery stack
ItsJustZi 3:2238838234e1 59 monitor that measures up to 12 series connected battery
ItsJustZi 3:2238838234e1 60 cells with a total measurement error of less than 1.2mV. The
ItsJustZi 3:2238838234e1 61 cell measurement range of 0V to 5V makes the LTC6804
ItsJustZi 3:2238838234e1 62 suitable for most battery chemistries. All 12 cell voltages
ItsJustZi 3:2238838234e1 63 can be captured in 290uS, and lower data acquisition rates
ItsJustZi 3:2238838234e1 64 can be selected for high noise reduction.
ItsJustZi 3:2238838234e1 65
ItsJustZi 3:2238838234e1 66 Using the LTC6804-2, multiple devices are connected in
ItsJustZi 3:2238838234e1 67 parallel to the host processor, with each device
ItsJustZi 3:2238838234e1 68 individually addressed.
ItsJustZi 3:2238838234e1 69 @endverbatim
ItsJustZi 3:2238838234e1 70 REVISION HISTORY
ItsJustZi 3:2238838234e1 71 $Revision: 1000 $
ItsJustZi 3:2238838234e1 72 $Date: 2013-12-13
ItsJustZi 3:2238838234e1 73
ItsJustZi 3:2238838234e1 74 Copyright (c) 2013, Linear Technology Corp.(LTC)
ItsJustZi 3:2238838234e1 75 All rights reserved.
ItsJustZi 3:2238838234e1 76
ItsJustZi 3:2238838234e1 77 Redistribution and use in source and binary forms, with or without
ItsJustZi 3:2238838234e1 78 modification, are permitted provided that the following conditions are met:
ItsJustZi 3:2238838234e1 79
ItsJustZi 3:2238838234e1 80 1. Redistributions of source code must retain the above copyright notice, this
ItsJustZi 3:2238838234e1 81 list of conditions and the following disclaimer.
ItsJustZi 3:2238838234e1 82 2. Redistributions in binary form must reproduce the above copyright notice,
ItsJustZi 3:2238838234e1 83 this list of conditions and the following disclaimer in the documentation
ItsJustZi 3:2238838234e1 84 and/or other materials provided with the distribution.
ItsJustZi 3:2238838234e1 85
ItsJustZi 3:2238838234e1 86 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ItsJustZi 3:2238838234e1 87 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
ItsJustZi 3:2238838234e1 88 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ItsJustZi 3:2238838234e1 89 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ItsJustZi 3:2238838234e1 90 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
ItsJustZi 3:2238838234e1 91 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
ItsJustZi 3:2238838234e1 92 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ItsJustZi 3:2238838234e1 93 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
ItsJustZi 3:2238838234e1 94 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ItsJustZi 3:2238838234e1 95 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ItsJustZi 3:2238838234e1 96
ItsJustZi 3:2238838234e1 97 The views and conclusions contained in the software and documentation are those
ItsJustZi 3:2238838234e1 98 of the authors and should not be interpreted as representing official policies,
ItsJustZi 3:2238838234e1 99 either expressed or implied, of Linear Technology Corp.
ItsJustZi 3:2238838234e1 100
ItsJustZi 3:2238838234e1 101 The Linear Technology Linduino is not affiliated with the official Arduino team.
ItsJustZi 3:2238838234e1 102 However, the Linduino is only possible because of the Arduino team's commitment
ItsJustZi 3:2238838234e1 103 to the open-source community. Please, visit http://www.arduino.cc and
ItsJustZi 3:2238838234e1 104 http://store.arduino.cc , and consider a purchase that will help fund their
ItsJustZi 3:2238838234e1 105 ongoing work.
ItsJustZi 3:2238838234e1 106
ItsJustZi 3:2238838234e1 107 Copyright 2013 Linear Technology Corp. (LTC)
ItsJustZi 3:2238838234e1 108 ***********************************************************/
ItsJustZi 3:2238838234e1 109
ItsJustZi 3:2238838234e1 110 #include "BMU.h"
ItsJustZi 3:2238838234e1 111 #include "SPI_I2C_Parser.h"
ItsJustZi 3:2238838234e1 112 #include "mbed.h"
ItsJustZi 3:2238838234e1 113 #include "CAN_Data.h"
ItsJustZi 3:2238838234e1 114 #include "CAN_IDs.h"
ItsJustZi 3:2238838234e1 115 #include <stdlib.h>
ItsJustZi 3:2238838234e1 116
ItsJustZi 4:1881b14c977b 117 using namespace std;
ItsJustZi 4:1881b14c977b 118
ItsJustZi 3:2238838234e1 119
ItsJustZi 3:2238838234e1 120 //Define battery pack cooling fan PWM pins
ItsJustZi 3:2238838234e1 121 PwmOut fan1(p21);
ItsJustZi 3:2238838234e1 122 PwmOut fan2(p22);
ItsJustZi 3:2238838234e1 123
ItsJustZi 3:2238838234e1 124 //Define SoC ALCC digital in pin
ItsJustZi 3:2238838234e1 125 DigitalIn alcc(p20);
ItsJustZi 3:2238838234e1 126
ItsJustZi 6:91b54ccbdf2d 127 int main ()
ItsJustZi 6:91b54ccbdf2d 128 {
ItsJustZi 5:fc2503217ad4 129 //spi.format(8,3); //All data transfer on LTC6804 occur in byte groups. LTC6820 set up such that POL=1 and PHA=3, this corresponds to mode 3 in mbed library. spi.frequency(spiBitrate);
ItsJustZi 6:91b54ccbdf2d 130 //spi.frequency(spiBitrate);
ItsJustZi 4:1881b14c977b 131 uint8_t configReg [1][8];
ItsJustZi 3:2238838234e1 132
ItsJustZi 6:91b54ccbdf2d 133 wake_LTC6804();//ensures CMU's are in ready state and wakes it up from low power mode
ItsJustZi 6:91b54ccbdf2d 134 LTC6804_init(MD_FAST, DCP_DISABLED, CELL_CH_ALL, AUX_CH_VREF2);
ItsJustZi 3:2238838234e1 135
ItsJustZi 6:91b54ccbdf2d 136 while(-1) {
ItsJustZi 6:91b54ccbdf2d 137 LTC6804_rdcfg(1, configReg);
ItsJustZi 6:91b54ccbdf2d 138 wait(0.5);
ItsJustZi 6:91b54ccbdf2d 139 }
ItsJustZi 6:91b54ccbdf2d 140 return 0;
ItsJustZi 6:91b54ccbdf2d 141 }
ItsJustZi 6:91b54ccbdf2d 142
ItsJustZi 6:91b54ccbdf2d 143
ItsJustZi 6:91b54ccbdf2d 144 /*uint16_t vCells[numberOfCMUs][12];
ItsJustZi 6:91b54ccbdf2d 145
ItsJustZi 6:91b54ccbdf2d 146
ItsJustZi 6:91b54ccbdf2d 147 LTC6804_init(MD_FILTERED, DCP_ENABLED, CELL_CH_ALL, AUX_CH_ALL);//set_adc
ItsJustZi 6:91b54ccbdf2d 148 LTC6804_setConfigReg(
ItsJustZi 6:91b54ccbdf2d 149
ItsJustZi 6:91b54ccbdf2d 150 LTC6804_acquireVoltageTx();//adcv
ItsJustZi 6:91b54ccbdf2d 151 LTC6804_acquireAllVoltageRegRx(0, numberOfCMUs, vCells); //rdcv, 0 means read all cell voltages
ItsJustZi 6:91b54ccbdf2d 152 */
ItsJustZi 6:91b54ccbdf2d 153