i2c working with old hk

Dependencies:   mbed-rtos mbed

Fork of BAE_vr2_1_1 by green rosh

Committer:
greenroshks
Date:
Wed Dec 17 05:25:04 2014 +0000
Revision:
13:1b37d98840d3
Parent:
0:8b0d43fe6c05
Child:
14:ef6be8ac6569
The one with the new HK

Who changed what in which revision?

UserRevisionLine numberNew contents of line
greenroshks 0:8b0d43fe6c05 1 #include "HK.h"
greenroshks 0:8b0d43fe6c05 2
greenroshks 13:1b37d98840d3 3 MAX17048 master(A4,A5,100000);//object for battery gauge class--CHECK SDA,SCL LINES,FREQUENCY
greenroshks 13:1b37d98840d3 4 void FUNC_BATTERYGAUGE_INIT();
greenroshks 0:8b0d43fe6c05 5
greenroshks 13:1b37d98840d3 6 //GPIO pins used=> D2-D12, A0-A1
greenroshks 13:1b37d98840d3 7
greenroshks 13:1b37d98840d3 8 DigitalOut SelectLinesA[]={D2,D3,D4,D5};//to mux1=>voltage mux
greenroshks 13:1b37d98840d3 9 DigitalOut SelectLinesB[]={PTB18,D7,PTB19};//to mux2=>current mux(differential mux) Is this 3 or 4?
greenroshks 13:1b37d98840d3 10 DigitalOut SelectLinesC[]={PTC0,PTC4,PTC6,PTC7};//to mux3=>temp mux
greenroshks 13:1b37d98840d3 11
greenroshks 13:1b37d98840d3 12 //--------------------------------------------MSB is SelectLines[0],LSB is SelectLines[3]--------------------------------
greenroshks 0:8b0d43fe6c05 13
greenroshks 13:1b37d98840d3 14 AnalogIn CurrentInput(A0); // Input from Current Mux
greenroshks 13:1b37d98840d3 15 AnalogIn VoltageInput(A1); // Input from Voltage Multiplexer
greenroshks 13:1b37d98840d3 16 AnalogIn TemperatureInput(A2); /*Input from Temperature Multiplexer,thermistor Multiplexer- same multiplexer for both(lines 1-4 for thermistor,line 0 for temperature sensor)*/
greenroshks 13:1b37d98840d3 17
greenroshks 13:1b37d98840d3 18
greenroshks 13:1b37d98840d3 19
greenroshks 13:1b37d98840d3 20
greenroshks 13:1b37d98840d3 21 int quantiz(float start,float step,float x) // accepts min and measured values and step->quantises on a scale 0-15..(4 bit quantisation)
greenroshks 0:8b0d43fe6c05 22 {
greenroshks 0:8b0d43fe6c05 23 int y=(x-start)/step;
greenroshks 0:8b0d43fe6c05 24 if(y<=0)y=0;
greenroshks 0:8b0d43fe6c05 25 if(y>=15)y=15;
greenroshks 0:8b0d43fe6c05 26 return y;
greenroshks 0:8b0d43fe6c05 27 }
greenroshks 0:8b0d43fe6c05 28
greenroshks 13:1b37d98840d3 29 void init_beacon(ShortBeacy* x,SensorDataQuantised y)
greenroshks 0:8b0d43fe6c05 30 {
greenroshks 13:1b37d98840d3 31 (*x).Voltage[0]=y.Vcell_soc>>4;//quantised value
greenroshks 13:1b37d98840d3 32 (*x).Temp[0]=y.PanelTemperature[0];//quantised value
greenroshks 13:1b37d98840d3 33 (*x).Temp[1]=y.PanelTemperature[1];//quantised value
greenroshks 13:1b37d98840d3 34 (*x).AngularSpeed[0]=y.AngularSpeed[0];
greenroshks 13:1b37d98840d3 35 (*x).AngularSpeed[1]=y.AngularSpeed[1];
greenroshks 0:8b0d43fe6c05 36
greenroshks 13:1b37d98840d3 37 (*x).SubsystemStatus[0]=145;//dummy values----------to be changed-------------------
greenroshks 13:1b37d98840d3 38 (*x).ErrorFlag[0]=3;//dummy values----------to be changed-------------------
greenroshks 13:1b37d98840d3 39 }
greenroshks 13:1b37d98840d3 40 SensorData SensorUQ;
greenroshks 13:1b37d98840d3 41 SensorDataQuantised SensorQuantised;
greenroshks 13:1b37d98840d3 42 ShortBeacy Shortbeacon;
greenroshks 13:1b37d98840d3 43
greenroshks 13:1b37d98840d3 44 void FUNC_HK_MAIN()
greenroshks 13:1b37d98840d3 45 {
greenroshks 13:1b37d98840d3 46 //define structure variables
greenroshks 13:1b37d98840d3 47
greenroshks 13:1b37d98840d3 48
greenroshks 13:1b37d98840d3 49
greenroshks 13:1b37d98840d3 50
greenroshks 13:1b37d98840d3 51 //initialise all selectlines to zeroes->1st line of muxes selected
greenroshks 13:1b37d98840d3 52 SelectLinesA[0]=SelectLinesA[1]=SelectLinesA[2]=SelectLinesA[3]=0;
greenroshks 13:1b37d98840d3 53 SelectLinesB[0]=SelectLinesB[1]=SelectLinesB[2]=0;
greenroshks 13:1b37d98840d3 54 SelectLinesC[0]=SelectLinesC[1]=SelectLinesC[2]=SelectLinesC[3]=0;
greenroshks 0:8b0d43fe6c05 55
greenroshks 0:8b0d43fe6c05 56 int LoopIterator;
greenroshks 13:1b37d98840d3 57 int SelectLineIterator;
greenroshks 13:1b37d98840d3 58
greenroshks 13:1b37d98840d3 59 float resistance_thermistor,voltage_thermistor;//for thermistor
greenroshks 0:8b0d43fe6c05 60
greenroshks 13:1b37d98840d3 61 //measurement from voltage sensor=> 16 sensors in place
greenroshks 13:1b37d98840d3 62 for(LoopIterator=0; LoopIterator<16; LoopIterator++)
greenroshks 13:1b37d98840d3 63 {
greenroshks 13:1b37d98840d3 64 //following lines read the sensor values and stores them in 'SensorData' structure's variable 'Sensor'
greenroshks 13:1b37d98840d3 65 SensorUQ.Voltage[LoopIterator]=(VoltageInput.read()*3.3*5.545454);//resistors in voltage divider=>15Mohm,3.3Mohm
greenroshks 13:1b37d98840d3 66
greenroshks 13:1b37d98840d3 67 if(LoopIterator%2==0)
greenroshks 13:1b37d98840d3 68 SensorQuantised.Voltage[LoopIterator/2]=quantiz(vstart,vstep,SensorUQ.Voltage[LoopIterator]);
greenroshks 13:1b37d98840d3 69
greenroshks 13:1b37d98840d3 70 else
greenroshks 13:1b37d98840d3 71 SensorQuantised.Voltage[(LoopIterator)/2]=SensorQuantised.Voltage[(LoopIterator)/2]<<4+quantiz(vstart,vstep,SensorUQ.Voltage[LoopIterator]);
greenroshks 13:1b37d98840d3 72
greenroshks 13:1b37d98840d3 73
greenroshks 13:1b37d98840d3 74
greenroshks 13:1b37d98840d3 75 // The following lines are used to iterate the select lines from 0 to 15
greenroshks 13:1b37d98840d3 76 //following is an algorithm similar to counting binary numbers of 4 bit
greenroshks 13:1b37d98840d3 77 for(SelectLineIterator=3;SelectLineIterator>=0;SelectLineIterator--)
greenroshks 13:1b37d98840d3 78 {
greenroshks 13:1b37d98840d3 79 if(SelectLinesA[SelectLineIterator]==0)
greenroshks 13:1b37d98840d3 80 {
greenroshks 13:1b37d98840d3 81 SelectLinesA[SelectLineIterator]=1;
greenroshks 13:1b37d98840d3 82 break;
greenroshks 13:1b37d98840d3 83 }
greenroshks 13:1b37d98840d3 84 else SelectLinesA[SelectLineIterator]=0;
greenroshks 13:1b37d98840d3 85
greenroshks 13:1b37d98840d3 86 }
greenroshks 13:1b37d98840d3 87
greenroshks 13:1b37d98840d3 88
greenroshks 13:1b37d98840d3 89 wait_us(10.0); // A delay of 10 microseconds between each sensor output. Can be changed.
greenroshks 0:8b0d43fe6c05 90
greenroshks 13:1b37d98840d3 91 }
greenroshks 13:1b37d98840d3 92
greenroshks 13:1b37d98840d3 93
greenroshks 13:1b37d98840d3 94
greenroshks 13:1b37d98840d3 95
greenroshks 13:1b37d98840d3 96
greenroshks 13:1b37d98840d3 97 //measurement from current sensor=> 8 sensors in place
greenroshks 13:1b37d98840d3 98
greenroshks 13:1b37d98840d3 99 for(LoopIterator=0; LoopIterator<8; LoopIterator++)
greenroshks 13:1b37d98840d3 100 {
greenroshks 13:1b37d98840d3 101 //following lines read the sensor values and stores them in 'SensorData' structure variable 'Sensor'
greenroshks 13:1b37d98840d3 102 SensorUQ.Current[LoopIterator]=(CurrentInput.read()*3.3/(50*rsens));
greenroshks 13:1b37d98840d3 103 if(LoopIterator%2==0)
greenroshks 13:1b37d98840d3 104 SensorQuantised.Current[LoopIterator/2]=quantiz(cstart,cstep,SensorUQ.Current[LoopIterator]);
greenroshks 13:1b37d98840d3 105 else
greenroshks 13:1b37d98840d3 106 SensorQuantised.Current[(LoopIterator)/2]=SensorQuantised.Current[(LoopIterator)/2]<<4+quantiz(cstart,cstep,SensorUQ.Current[LoopIterator]);
greenroshks 13:1b37d98840d3 107
greenroshks 0:8b0d43fe6c05 108
greenroshks 13:1b37d98840d3 109 // The following lines are used to iterate the select lines from 0 to 7
greenroshks 13:1b37d98840d3 110 //following is an algorithm similar to counting binary numbers of 3 bits
greenroshks 13:1b37d98840d3 111 for(SelectLineIterator=2;SelectLineIterator>=0;SelectLineIterator--)
greenroshks 13:1b37d98840d3 112 {
greenroshks 13:1b37d98840d3 113 if(SelectLinesB[SelectLineIterator]==0)
greenroshks 13:1b37d98840d3 114 {
greenroshks 13:1b37d98840d3 115 SelectLinesB[SelectLineIterator]=1;
greenroshks 13:1b37d98840d3 116 break;
greenroshks 13:1b37d98840d3 117 }
greenroshks 13:1b37d98840d3 118 else SelectLinesB[SelectLineIterator]=0;
greenroshks 13:1b37d98840d3 119
greenroshks 0:8b0d43fe6c05 120 }
greenroshks 13:1b37d98840d3 121
greenroshks 13:1b37d98840d3 122
greenroshks 0:8b0d43fe6c05 123 wait_us(10.0); // A delay of 10 microseconds between each sensor output. Can be changed.
greenroshks 0:8b0d43fe6c05 124
greenroshks 13:1b37d98840d3 125 }
greenroshks 13:1b37d98840d3 126
greenroshks 13:1b37d98840d3 127
greenroshks 13:1b37d98840d3 128 //measurement of temperature
greenroshks 13:1b37d98840d3 129 //temperature measurement=> 4 thermistors, 1 temperature sensor
greenroshks 13:1b37d98840d3 130 //mux line 1=>temp sensor, mux lines 2 to 5 =>thermistors
greenroshks 13:1b37d98840d3 131
greenroshks 13:1b37d98840d3 132 for(LoopIterator=0; LoopIterator<5; LoopIterator++)
greenroshks 13:1b37d98840d3 133 {
greenroshks 13:1b37d98840d3 134 //following lines read the sensor values and stores them in 'SensorData' structure variable 'Sensor'
greenroshks 13:1b37d98840d3 135 SensorUQ.Temperature[LoopIterator]=(-90.7*3.3*TemperatureInput.read()+190.1543);
greenroshks 13:1b37d98840d3 136 voltage_thermistor=TemperatureInput.read()*3.3;//voltage across thermistor
greenroshks 13:1b37d98840d3 137 resistance_thermistor=24000*voltage_thermistor/(3.3-voltage_thermistor);//resistance of thermistor
greenroshks 13:1b37d98840d3 138 //PanelTemperature will be updated depending on voltage_thermistor value later in the lines to follow
greenroshks 13:1b37d98840d3 139
greenroshks 13:1b37d98840d3 140 if(LoopIterator%2==0)
greenroshks 13:1b37d98840d3 141 {
greenroshks 13:1b37d98840d3 142 if(LoopIterator<1) //->corresponding to temperature sensor
greenroshks 13:1b37d98840d3 143 SensorQuantised.Temperature[(LoopIterator)/2]=quantiz(tstart,tstep,SensorUQ.Temperature[LoopIterator]);
greenroshks 13:1b37d98840d3 144
greenroshks 13:1b37d98840d3 145 else //->corresponding to thermistor
greenroshks 13:1b37d98840d3 146 {
greenroshks 13:1b37d98840d3 147 if(voltage_thermistor<1.378) //Temperature>12 degC
greenroshks 13:1b37d98840d3 148 SensorUQ.PanelTemperature[(LoopIterator-1)]=(3694/log(24.032242*resistance_thermistor));
greenroshks 13:1b37d98840d3 149
greenroshks 13:1b37d98840d3 150 else
greenroshks 13:1b37d98840d3 151 SensorUQ.PanelTemperature[(LoopIterator-1)]=(3365.4792/log(7.60404*resistance_thermistor));
greenroshks 13:1b37d98840d3 152
greenroshks 13:1b37d98840d3 153
greenroshks 13:1b37d98840d3 154 SensorQuantised.PanelTemperature[(LoopIterator-1)/2]=quantiz(tstart_thermistor,tstep_thermistor,SensorUQ.PanelTemperature[(LoopIterator-1)]);
greenroshks 13:1b37d98840d3 155
greenroshks 13:1b37d98840d3 156 }
greenroshks 13:1b37d98840d3 157
greenroshks 13:1b37d98840d3 158 }
greenroshks 13:1b37d98840d3 159
greenroshks 13:1b37d98840d3 160 else
greenroshks 13:1b37d98840d3 161 {
greenroshks 13:1b37d98840d3 162 if(LoopIterator<1)
greenroshks 13:1b37d98840d3 163 SensorQuantised.Temperature[(LoopIterator)/2]=SensorQuantised.Temperature[(LoopIterator)/2]<<4+quantiz(tstart,tstep,SensorUQ.Temperature[LoopIterator]);
greenroshks 13:1b37d98840d3 164
greenroshks 13:1b37d98840d3 165 else
greenroshks 13:1b37d98840d3 166 {
greenroshks 13:1b37d98840d3 167 if(voltage_thermistor<1.378) //Temperature>12 degC
greenroshks 13:1b37d98840d3 168 SensorUQ.PanelTemperature[LoopIterator-1]=(3694/log(24.032242*resistance_thermistor));
greenroshks 13:1b37d98840d3 169
greenroshks 13:1b37d98840d3 170
greenroshks 13:1b37d98840d3 171 else
greenroshks 13:1b37d98840d3 172 SensorUQ.PanelTemperature[LoopIterator-1]=(3365.4792/log(7.60404*resistance_thermistor));
greenroshks 13:1b37d98840d3 173
greenroshks 13:1b37d98840d3 174 SensorQuantised.PanelTemperature[(LoopIterator-1)/2]=SensorQuantised.PanelTemperature[(LoopIterator-1)/2]<<4+quantiz(tstart_thermistor,tstep_thermistor,SensorUQ.PanelTemperature[LoopIterator-1]);
greenroshks 13:1b37d98840d3 175
greenroshks 13:1b37d98840d3 176 }
greenroshks 13:1b37d98840d3 177
greenroshks 13:1b37d98840d3 178 }
greenroshks 13:1b37d98840d3 179
greenroshks 13:1b37d98840d3 180
greenroshks 13:1b37d98840d3 181
greenroshks 13:1b37d98840d3 182
greenroshks 13:1b37d98840d3 183 // The following lines are used to iterate the select lines from 0 to 4
greenroshks 13:1b37d98840d3 184
greenroshks 13:1b37d98840d3 185 //following is an algorithm similar to counting binary numbers of 4 bit
greenroshks 13:1b37d98840d3 186 for(SelectLineIterator=3;SelectLineIterator>=0;SelectLineIterator--)
greenroshks 13:1b37d98840d3 187 {
greenroshks 13:1b37d98840d3 188 if(SelectLinesC[SelectLineIterator]==0)
greenroshks 13:1b37d98840d3 189 {
greenroshks 13:1b37d98840d3 190 SelectLinesC[SelectLineIterator]=1;
greenroshks 13:1b37d98840d3 191 break;
greenroshks 13:1b37d98840d3 192 }
greenroshks 13:1b37d98840d3 193 else SelectLinesC[SelectLineIterator]=0;
greenroshks 13:1b37d98840d3 194
greenroshks 13:1b37d98840d3 195 }
greenroshks 13:1b37d98840d3 196
greenroshks 13:1b37d98840d3 197
greenroshks 13:1b37d98840d3 198 wait_us(10.0); // A delay of 10 microseconds between each sensor output. Can be changed.
greenroshks 13:1b37d98840d3 199
greenroshks 13:1b37d98840d3 200 }
greenroshks 13:1b37d98840d3 201
greenroshks 13:1b37d98840d3 202
greenroshks 13:1b37d98840d3 203
greenroshks 13:1b37d98840d3 204 //update battery gauge parameters->
greenroshks 13:1b37d98840d3 205 float batteryparameters[4];//to populate battery parameters of struct variable Sensor
greenroshks 13:1b37d98840d3 206 FUNC_BATTERYGAUGE_MAIN(batteryparameters);//passing array to function
greenroshks 13:1b37d98840d3 207
greenroshks 13:1b37d98840d3 208 SensorUQ.Vcell=batteryparameters[0];
greenroshks 13:1b37d98840d3 209 SensorUQ.soc=batteryparameters[1];
greenroshks 13:1b37d98840d3 210 SensorUQ.crate=batteryparameters[2];
greenroshks 13:1b37d98840d3 211 SensorUQ.alerts=batteryparameters[3];
greenroshks 13:1b37d98840d3 212
greenroshks 13:1b37d98840d3 213 SensorQuantised.Vcell_soc=quantiz(Vcell_start,Vcell_step,SensorUQ.Vcell);
greenroshks 13:1b37d98840d3 214 SensorQuantised.Vcell_soc=SensorQuantised.Vcell_soc<<4+quantiz(soc_start,soc_step,SensorUQ.soc);
greenroshks 13:1b37d98840d3 215 SensorQuantised.alerts=SensorUQ.alerts;
greenroshks 13:1b37d98840d3 216 SensorQuantised.crate=quantiz(crate_start,crate_step,SensorUQ.crate);
greenroshks 13:1b37d98840d3 217
greenroshks 13:1b37d98840d3 218
greenroshks 13:1b37d98840d3 219 //update magnetometer data->
greenroshks 13:1b37d98840d3 220 //populate values in structure variable 'Sensor' from data to be given by Green
greenroshks 13:1b37d98840d3 221 SensorQuantised.AngularSpeed[0]=quantiz(AngularSpeed_start,AngularSpeed_step,SensorUQ.AngularSpeed[1]);
greenroshks 13:1b37d98840d3 222 SensorQuantised.AngularSpeed[0]=SensorQuantised.AngularSpeed[0]<<4+quantiz(AngularSpeed_start,AngularSpeed_step,SensorUQ.AngularSpeed[0]);
greenroshks 13:1b37d98840d3 223 SensorQuantised.AngularSpeed[1]=quantiz(AngularSpeed_start,AngularSpeed_step,SensorUQ.AngularSpeed[2]);
greenroshks 13:1b37d98840d3 224
greenroshks 13:1b37d98840d3 225 //update gyro data->
greenroshks 13:1b37d98840d3 226 //populate values in structure variable 'Sensor' from data to be given by Green
greenroshks 13:1b37d98840d3 227 SensorQuantised.Bnewvalue[0]=quantiz(Bnewvalue_start,Bnewvalue_step,SensorUQ.Bnewvalue[1]);
greenroshks 13:1b37d98840d3 228 SensorQuantised.Bnewvalue[0]=SensorQuantised.Bnewvalue[0]<<4+quantiz(Bnewvalue_start,Bnewvalue_step,SensorUQ.Bnewvalue[0]);
greenroshks 13:1b37d98840d3 229 SensorQuantised.Bnewvalue[1]=quantiz(Bnewvalue_start,Bnewvalue_step,SensorUQ.Bnewvalue[2]);
greenroshks 13:1b37d98840d3 230
greenroshks 13:1b37d98840d3 231
greenroshks 13:1b37d98840d3 232
greenroshks 13:1b37d98840d3 233
greenroshks 13:1b37d98840d3 234 //update beacon structure
greenroshks 13:1b37d98840d3 235 init_beacon(&Shortbeacon,SensorQuantised);//Shortbeacon is passed
greenroshks 13:1b37d98840d3 236
greenroshks 13:1b37d98840d3 237
greenroshks 13:1b37d98840d3 238 }
greenroshks 13:1b37d98840d3 239
greenroshks 13:1b37d98840d3 240
greenroshks 13:1b37d98840d3 241 void FUNC_BATTERYGAUGE_MAIN(float array[])
greenroshks 13:1b37d98840d3 242 {
greenroshks 13:1b37d98840d3 243 float vcell=master.vcell();
greenroshks 13:1b37d98840d3 244 float soc=master.soc();
greenroshks 13:1b37d98840d3 245 float crate=master.crate();
greenroshks 13:1b37d98840d3 246
greenroshks 13:1b37d98840d3 247 printf("\nVcell=%f",vcell);
greenroshks 13:1b37d98840d3 248 printf("\nSOC=%f",soc);
greenroshks 13:1b37d98840d3 249 printf("\nC_rate=%f",crate);
greenroshks 13:1b37d98840d3 250
greenroshks 13:1b37d98840d3 251 array[0]=vcell;
greenroshks 13:1b37d98840d3 252 array[1]=soc;
greenroshks 13:1b37d98840d3 253 array[2]=crate;
greenroshks 13:1b37d98840d3 254 if (master.alerting()== true) //alert is on
greenroshks 13:1b37d98840d3 255 {
greenroshks 13:1b37d98840d3 256 array[3]=master.alertFlags();
greenroshks 13:1b37d98840d3 257 master.clearAlert();//clear alert
greenroshks 13:1b37d98840d3 258 master.clearAlertFlags();//clear all alert flags
greenroshks 0:8b0d43fe6c05 259 }
greenroshks 13:1b37d98840d3 260
greenroshks 0:8b0d43fe6c05 261
greenroshks 0:8b0d43fe6c05 262 }
greenroshks 13:1b37d98840d3 263
greenroshks 13:1b37d98840d3 264 void FUNC_BATTERYGAUGE_INIT()
greenroshks 13:1b37d98840d3 265 {
greenroshks 13:1b37d98840d3 266 master.disable_sleep();
greenroshks 13:1b37d98840d3 267 master.disable_hibernate();
greenroshks 13:1b37d98840d3 268 master.socChangeAlertEnabled(true);//enabling alert on soc changing by 1%
greenroshks 13:1b37d98840d3 269 master.emptyAlertThreshold(1);//giving minimum value to disable it to disabling it----------------------
greenroshks 13:1b37d98840d3 270 master.vAlertMinMaxThreshold();//set min, max value of Valrt register
greenroshks 13:1b37d98840d3 271 master.vResetThresholdSet();//set threshold voltage for reset
greenroshks 13:1b37d98840d3 272 master.vResetAlertEnabled(true);//enable alert on reset for V < Vreset
greenroshks 13:1b37d98840d3 273 }