Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
martydd3
Date:
Sun Oct 19 22:46:46 2014 +0000
Revision:
10:db13782f05d9
Parent:
9:ada056631cac
Child:
11:1d086618dd18
Copied Kiran's calculations for monitoring batttery currents; To do: Test on car, Xbees, 10 Hz Can Message (what data is needed?)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martydd3 0:e516fcccccda 1 /*
martydd3 0:e516fcccccda 2 Reads CAN Messages and various data inputs, outputs using Xbee radio modules
martydd3 0:e516fcccccda 3
martydd3 0:e516fcccccda 4 Revised Sept 30, 2014: Began analyzing and commenting program, trying to figure out what the hell it does (Martin Deng)
martydd3 0:e516fcccccda 5 */
martydd3 0:e516fcccccda 6
martydd3 10:db13782f05d9 7 //#include "SysMngmt.h"
martydd3 10:db13782f05d9 8
martydd3 10:db13782f05d9 9 /*
martydd3 6:6a04210a3f4f 10 #include "Get_IMD.h"
martydd3 6:6a04210a3f4f 11 #include "PollSwitch.h"
martydd3 6:6a04210a3f4f 12 #include "TemperatureRead.h"
martydd3 10:db13782f05d9 13 */
martydd3 10:db13782f05d9 14
martydd3 6:6a04210a3f4f 15 #include "Store_RTC.h"
martydd3 6:6a04210a3f4f 16 #include "XBee_Lib.h"
martydd3 10:db13782f05d9 17
martydd3 10:db13782f05d9 18
martydd3 6:6a04210a3f4f 19 #include "CANBuffer.h"
martydd3 0:e516fcccccda 20
martydd3 6:6a04210a3f4f 21 #include "mbed.h"
martydd3 6:6a04210a3f4f 22 #include "rtos.h"
martydd3 6:6a04210a3f4f 23
martydd3 9:ada056631cac 24 #include "Watchdog.h"
martydd3 6:6a04210a3f4f 25 #include "FanPump.h"
martydd3 6:6a04210a3f4f 26 #include "DC_DC.h"
martydd3 7:5f6e31faa08e 27 #include "PollSwitch.h"
martydd3 8:ecf68db484af 28 #include "IMD.h"
martydd3 10:db13782f05d9 29 #include "CurrentMonitor.h"
martydd3 10:db13782f05d9 30
martydd3 10:db13782f05d9 31 CANBuffer rxBuffer(CAN1, MEDIUM);
martydd3 10:db13782f05d9 32 //XBee250x XbeeTx;
martydd3 10:db13782f05d9 33 Serial pc1(USBTX,USBRX);
martydd3 10:db13782f05d9 34
martydd3 10:db13782f05d9 35 char sys_src_id = 4; // source address of system management
martydd3 10:db13782f05d9 36 char reset_id = 0x010;
martydd3 10:db13782f05d9 37
martydd3 10:db13782f05d9 38 extern "C" void mbed_reset();
martydd3 10:db13782f05d9 39
martydd3 10:db13782f05d9 40 void soft_reset(){
martydd3 10:db13782f05d9 41 //http://developer.mbed.org/forum/mbed/topic/890/
martydd3 10:db13782f05d9 42 mbed_reset();
martydd3 10:db13782f05d9 43 }
martydd3 10:db13782f05d9 44
martydd3 10:db13782f05d9 45 int main() {
martydd3 10:db13782f05d9 46 CANMessage rx_msg;
martydd3 10:db13782f05d9 47 Watchdog wdt;
martydd3 10:db13782f05d9 48
martydd3 10:db13782f05d9 49 wdt.kick(10.0);
martydd3 10:db13782f05d9 50 pc1.baud(115200);
martydd3 10:db13782f05d9 51
martydd3 10:db13782f05d9 52 FanPump fanPump(&rxBuffer);
martydd3 10:db13782f05d9 53 DC dc_dc(&fanPump, &rxBuffer);
martydd3 10:db13782f05d9 54 PollSwitch pollSwitch(&rxBuffer);
martydd3 10:db13782f05d9 55 IMD imdMonitor(&rxBuffer);
martydd3 10:db13782f05d9 56 CurrentMonitor curMonitor(&rxBuffer);
martydd3 10:db13782f05d9 57
martydd3 10:db13782f05d9 58 fanPump.start_update();
martydd3 10:db13782f05d9 59 dc_dc.start_update();
martydd3 10:db13782f05d9 60 pollSwitch.start_update();
martydd3 10:db13782f05d9 61 imdMonitor.start_update();
martydd3 10:db13782f05d9 62 curMonitor.start_update();
martydd3 10:db13782f05d9 63
martydd3 10:db13782f05d9 64 while(1)
martydd3 10:db13782f05d9 65 {
martydd3 10:db13782f05d9 66 if(rxBuffer.rxRead(rx_msg)){
martydd3 10:db13782f05d9 67 if(rx_msg.id == reset_id)
martydd3 10:db13782f05d9 68 soft_reset();
martydd3 10:db13782f05d9 69
martydd3 10:db13782f05d9 70 char src_addr = (rx_msg.id & 0x0700) >> 8; // get bits 10:8
martydd3 10:db13782f05d9 71
martydd3 10:db13782f05d9 72 if(src_addr == sys_src_id){
martydd3 10:db13782f05d9 73 char cont_id = (rx_msg.id & 0x00FF); // get bits 7:0
martydd3 10:db13782f05d9 74
martydd3 10:db13782f05d9 75 // only control fans of dc_dc converter is on
martydd3 10:db13782f05d9 76 if(cont_id == RX_FAN_ID && dc_dc.is_on())
martydd3 10:db13782f05d9 77 {
martydd3 10:db13782f05d9 78 fanPump.set_fan((FanSelect)rx_msg.data[0], rx_msg.data[1]);
martydd3 10:db13782f05d9 79 }
martydd3 10:db13782f05d9 80
martydd3 10:db13782f05d9 81 if(cont_id == RX_DC_DC_ID){
martydd3 10:db13782f05d9 82 dc_dc.set(rx_msg.data[0]);
martydd3 10:db13782f05d9 83 }
martydd3 10:db13782f05d9 84 } // check for correct src_addr
martydd3 10:db13782f05d9 85 } // check CANBuffer
martydd3 10:db13782f05d9 86
martydd3 10:db13782f05d9 87 wdt.kick();
martydd3 10:db13782f05d9 88 } // main while loop
martydd3 10:db13782f05d9 89 }
martydd3 10:db13782f05d9 90
martydd3 0:e516fcccccda 91
martydd3 0:e516fcccccda 92 //Possible problems in IMD coz change of counter
martydd3 0:e516fcccccda 93 //Possible problems in BatteryStatus coz change in library
martydd3 0:e516fcccccda 94
martydd3 0:e516fcccccda 95 /*
martydd3 0:e516fcccccda 96
martydd3 0:e516fcccccda 97 Attach Ticker every 10msec to
martydd3 0:e516fcccccda 98 Get IMD
martydd3 0:e516fcccccda 99 Poll Switches
martydd3 0:e516fcccccda 100 Temperature Read
martydd3 0:e516fcccccda 101 Get Battery State
martydd3 0:e516fcccccda 102 End Ticker Send message through CAN
martydd3 0:e516fcccccda 103
martydd3 0:e516fcccccda 104 CAN interrupt Rx Interrupt
martydd3 0:e516fcccccda 105 Recieve CAN message into a buffer. Return
martydd3 0:e516fcccccda 106 Buffer values(as long as !empty) -> SD Card, Xbee -> remove element
martydd3 0:e516fcccccda 107
martydd3 0:e516fcccccda 108 extern "C" void CAN_IRQHandler(void)
martydd3 0:e516fcccccda 109 {
martydd3 0:e516fcccccda 110 CANMessage Rxmsg;
martydd3 0:e516fcccccda 111 CAN_SysM.read(Rxmsg);
martydd3 0:e516fcccccda 112 RecieveBuffer.add(Rxmsg);
martydd3 0:e516fcccccda 113 }
martydd3 0:e516fcccccda 114
martydd3 1:e02eb179aed3 115 http://developer.mbed.org/users/AjK/notebook/getting-closer-to-the-hardware/
martydd3 1:e02eb179aed3 116
martydd3 1:e02eb179aed3 117 extern "C" means this is linked assuming it's C code
martydd3 1:e02eb179aed3 118 C++ linker appearently adds extra crap with the function arguments keeping functions of this kind from linking properly
martydd3 1:e02eb179aed3 119
martydd3 1:e02eb179aed3 120 Interrupt handler, This is probably linked to the Timer 2 interrupt request somewhere by the libraries
martydd3 2:baeb80c778f7 121
martydd3 10:db13782f05d9 122 */
martydd3 0:e516fcccccda 123
martydd3 10:db13782f05d9 124
martydd3 10:db13782f05d9 125 /*
martydd3 1:e02eb179aed3 126 Appears to read a whole bunch of DigitalOut pins in void PollSwitch(), store the value in uint16_t Rxpoll,
martydd3 1:e02eb179aed3 127 and write the result to the CAN bus
martydd3 2:baeb80c778f7 128
martydd3 0:e516fcccccda 129 void Poll()
martydd3 0:e516fcccccda 130 {
martydd3 0:e516fcccccda 131 uint16_t Rxpoll;
martydd3 0:e516fcccccda 132 uint16_t recv,temp,i=0; //Test
martydd3 0:e516fcccccda 133 char Result[4]={0};
martydd3 0:e516fcccccda 134 Rxpoll=PollSwitch();
martydd3 0:e516fcccccda 135
martydd3 0:e516fcccccda 136 Result[0]=(char)(Rxpoll&0x00ff);
martydd3 0:e516fcccccda 137 Result[1]=(char)((Rxpoll&0xff00)>>8);
martydd3 0:e516fcccccda 138 CANMessage Txmsg(410,Result,sizeof(Result));
martydd3 0:e516fcccccda 139 CAN_SysM.write(Txmsg);
martydd3 0:e516fcccccda 140
martydd3 0:e516fcccccda 141 //Test
martydd3 0:e516fcccccda 142 recv=(((uint16_t)Txmsg.data[1]<<8) | (0x00ff&(uint16_t)Txmsg.data[0]));
martydd3 0:e516fcccccda 143 printf("Recv:%d\n\r",recv);
martydd3 0:e516fcccccda 144
martydd3 0:e516fcccccda 145 while(i <= 12)
martydd3 0:e516fcccccda 146 {
martydd3 0:e516fcccccda 147 temp=recv;
martydd3 0:e516fcccccda 148 if(((temp & (1 << i))>>i)==1)
martydd3 0:e516fcccccda 149 pc.printf("Switch OFF:%d\n\r",i);
martydd3 0:e516fcccccda 150 ++i;
martydd3 0:e516fcccccda 151 }
martydd3 0:e516fcccccda 152 }
martydd3 0:e516fcccccda 153
martydd3 0:e516fcccccda 154
martydd3 0:e516fcccccda 155 void Temp()
martydd3 0:e516fcccccda 156 {
martydd3 0:e516fcccccda 157 float DC_DC_Temperature, Coolant1_Temperature, Coolant2_Temperature, ChargerFET_Temperature;
martydd3 0:e516fcccccda 158 float Resistance;
martydd3 0:e516fcccccda 159 float Vadc;
martydd3 0:e516fcccccda 160 int i;
martydd3 0:e516fcccccda 161 ftc send, recv;
martydd3 0:e516fcccccda 162 recv.FLOAT=0.0;
martydd3 0:e516fcccccda 163 send.FLOAT=0.0;
martydd3 0:e516fcccccda 164
martydd3 0:e516fcccccda 165 Vadc=DC_DC.read()*VDD;
martydd3 0:e516fcccccda 166 Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
martydd3 0:e516fcccccda 167 DC_DC_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA);
martydd3 0:e516fcccccda 168 send.FLOAT=DC_DC_Temperature;
martydd3 0:e516fcccccda 169 CANMessage Txmsg_DC_DC(450,send.C_FLOAT,sizeof(send.C_FLOAT));
martydd3 0:e516fcccccda 170 CAN_SysM.write(Txmsg_DC_DC);
martydd3 0:e516fcccccda 171
martydd3 0:e516fcccccda 172 for(i=0; i<4;i++)
martydd3 0:e516fcccccda 173 recv.C_FLOAT[i]=Txmsg_DC_DC.data[i];
martydd3 0:e516fcccccda 174 pc.printf("DC_DC:%f\n\r",recv.FLOAT);
martydd3 0:e516fcccccda 175
martydd3 0:e516fcccccda 176 Vadc=ChargerFET.read()*VDD;
martydd3 0:e516fcccccda 177 Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
martydd3 0:e516fcccccda 178 ChargerFET_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA);
martydd3 0:e516fcccccda 179 send.FLOAT=ChargerFET_Temperature;
martydd3 0:e516fcccccda 180 CANMessage Txmsg_ChargerFET(451,send.C_FLOAT,sizeof(send.C_FLOAT));
martydd3 0:e516fcccccda 181 CAN_SysM.write(Txmsg_ChargerFET);
martydd3 0:e516fcccccda 182
martydd3 0:e516fcccccda 183 for(i=0; i<4;i++)
martydd3 0:e516fcccccda 184 recv.C_FLOAT[i]=Txmsg_ChargerFET.data[i];
martydd3 0:e516fcccccda 185 pc.printf("ChargerFET:%f\n\r",recv.FLOAT);
martydd3 0:e516fcccccda 186
martydd3 0:e516fcccccda 187 Vadc=Coolant1.read()*VDD;
martydd3 0:e516fcccccda 188 Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
martydd3 0:e516fcccccda 189 Coolant1_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H);
martydd3 0:e516fcccccda 190 send.FLOAT=Coolant1_Temperature;
martydd3 0:e516fcccccda 191 CANMessage Txmsg_Coolant1(452,send.C_FLOAT,sizeof(send.C_FLOAT));
martydd3 0:e516fcccccda 192 CAN_SysM.write(Txmsg_Coolant1);
martydd3 0:e516fcccccda 193 //Control Fans
martydd3 0:e516fcccccda 194
martydd3 0:e516fcccccda 195 for(i=0; i<4;i++)
martydd3 0:e516fcccccda 196 recv.C_FLOAT[i]=Txmsg_Coolant1.data[i];
martydd3 0:e516fcccccda 197 pc.printf("Coolant1:%f\n\r",recv.FLOAT);
martydd3 0:e516fcccccda 198
martydd3 0:e516fcccccda 199 Vadc=Coolant2.read()*VDD;
martydd3 0:e516fcccccda 200 Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
martydd3 0:e516fcccccda 201 Coolant2_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H);
martydd3 0:e516fcccccda 202 send.FLOAT=Coolant2_Temperature;
martydd3 0:e516fcccccda 203 CANMessage Txmsg_Coolant2(453,send.C_FLOAT,sizeof(send.C_FLOAT));
martydd3 0:e516fcccccda 204 CAN_SysM.write(Txmsg_Coolant2);
martydd3 0:e516fcccccda 205 //Control Fans
martydd3 0:e516fcccccda 206
martydd3 0:e516fcccccda 207 for(i=0; i<4;i++)
martydd3 0:e516fcccccda 208 recv.C_FLOAT[i]=Txmsg_Coolant2.data[i];
martydd3 0:e516fcccccda 209 pc.printf("Coolant2:%f\n\r",recv.FLOAT);
martydd3 0:e516fcccccda 210 }
martydd3 0:e516fcccccda 211
martydd3 0:e516fcccccda 212 void IMD()
martydd3 0:e516fcccccda 213 {
martydd3 0:e516fcccccda 214 IMD_Measurement_Output IMD_Signal;
martydd3 0:e516fcccccda 215 char status[4];
martydd3 0:e516fcccccda 216 ftc send;
martydd3 0:e516fcccccda 217
martydd3 0:e516fcccccda 218 IMD_Signal=Get_Measurement();
martydd3 0:e516fcccccda 219 send.FLOAT=IMD_Signal.Frequency;
martydd3 0:e516fcccccda 220 CANMessage Txmsg_Frequency(421,send.C_FLOAT,sizeof(send.C_FLOAT));
martydd3 0:e516fcccccda 221 CAN_SysM.write(Txmsg_Frequency);
martydd3 0:e516fcccccda 222
martydd3 0:e516fcccccda 223 send.FLOAT=IMD_Signal.Duty_Cycle;
martydd3 0:e516fcccccda 224 CANMessage Txmsg_DutyCycle(422,send.C_FLOAT,sizeof(send.C_FLOAT));
martydd3 0:e516fcccccda 225 CAN_SysM.write(Txmsg_DutyCycle);
martydd3 0:e516fcccccda 226
martydd3 0:e516fcccccda 227 status[0]=Result.Encoded_Status;
martydd3 0:e516fcccccda 228 CANMessage Txmsg_Status(423,status,sizeof(status));
martydd3 0:e516fcccccda 229 CAN_SysM.write(Txmsg_Status);
martydd3 0:e516fcccccda 230 }
martydd3 5:9258b685fea6 231 */
martydd3 0:e516fcccccda 232
martydd3 5:9258b685fea6 233 /*
martydd3 0:e516fcccccda 234 Activates a whole crapload of functions and pins on the chip
martydd3 2:baeb80c778f7 235
martydd3 0:e516fcccccda 236 void Init()
martydd3 0:e516fcccccda 237 {
martydd3 2:baeb80c778f7 238
martydd3 0:e516fcccccda 239 Timers to call various functions at different intervals
martydd3 0:e516fcccccda 240 These things behave weirdly when wait(ms) is involved. Probably have to rewrite
martydd3 2:baeb80c778f7 241
martydd3 0:e516fcccccda 242
martydd3 0:e516fcccccda 243 //ReadIMD.attach(&IMD,0.1);
martydd3 0:e516fcccccda 244 //PollSDSwitch.attach(&Poll,0.1);
martydd3 0:e516fcccccda 245
martydd3 0:e516fcccccda 246 //ReadTemperature.attach(&Temp,0.1);
martydd3 0:e516fcccccda 247 //ReadBatteryState.attach(&Battery,0.1);
martydd3 0:e516fcccccda 248
martydd3 2:baeb80c778f7 249
martydd3 0:e516fcccccda 250 Initialize Timer2 for Battery State
martydd3 0:e516fcccccda 251
martydd3 0:e516fcccccda 252 LPC_SC 0x400F C000 (System Control)
martydd3 0:e516fcccccda 253 ->PCONP 0x400F C0C4 (Power Control for Peripherals Register)
martydd3 0:e516fcccccda 254 |= (1<<22) 22 Bit (Timer 2 power/clock control bit)
martydd3 0:e516fcccccda 255
martydd3 0:e516fcccccda 256 ->PCLKSEL1 Peripheral Clock Selection register 1 (controls rate of clock signal supplied to peripheral)
martydd3 0:e516fcccccda 257 |= ((1<<12) | (1<<13)); 12:13 Bits (Peripheral Clock Selection for TIMER2)
martydd3 0:e516fcccccda 258
martydd3 0:e516fcccccda 259 LPC_TIM2 0x4009 0000 (Timer 2)
martydd3 0:e516fcccccda 260 ->TCR 0x4009 0004 (Timer Control Register)
martydd3 0:e516fcccccda 261 |= (1<<0); 0 Bit (Counter Enable)
martydd3 0:e516fcccccda 262
martydd3 0:e516fcccccda 263 ->MR0 0x4009 0018 (Match Register)
martydd3 0:e516fcccccda 264
martydd3 1:e02eb179aed3 265 ->MCR 0x4009 0014 (Match Control Register) What to do when Match Register matches the Timer Counter
martydd3 1:e02eb179aed3 266 |= (1<<0); 0 Bit (Interrupt on MR0, interrupt generated when MR0 matches the value in TC)
martydd3 0:e516fcccccda 267
martydd3 2:baeb80c778f7 268
martydd3 0:e516fcccccda 269 LPC_SC->PCONP |= (1<<22); //PoewerOn Timer/Counter2
martydd3 0:e516fcccccda 270 LPC_SC->PCLKSEL1 |= ((1<<12) | (1<<13)); //Prescale Timer2 CCLK/8
martydd3 0:e516fcccccda 271 LPC_TIM2->TCR |= (1<<0); //Enable Timer2
martydd3 0:e516fcccccda 272 LPC_TIM2->MR0 = 11999; // 1msec
martydd3 0:e516fcccccda 273 LPC_TIM2->MCR |= (1<<0);
martydd3 2:baeb80c778f7 274
martydd3 1:e02eb179aed3 275 Nested Vectored Interrupt Controller (NVIC)
martydd3 1:e02eb179aed3 276
martydd3 1:e02eb179aed3 277 NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
martydd3 1:e02eb179aed3 278 sets priority of an interrupt
martydd3 1:e02eb179aed3 279
martydd3 1:e02eb179aed3 280 IRQn_Type
martydd3 1:e02eb179aed3 281 Interrupt number definitions
martydd3 1:e02eb179aed3 282 Interrupt Request (IRQ)
martydd3 1:e02eb179aed3 283
martydd3 1:e02eb179aed3 284 NVIC_EnableIRQ(IRQn_Type IRQn)
martydd3 1:e02eb179aed3 285 Enable external interrupt (in this case, the TIMER2_IRQHandler(void) function above gets called every time
martydd3 1:e02eb179aed3 286 Timer2 generates an interrupt signal)
martydd3 1:e02eb179aed3 287
martydd3 2:baeb80c778f7 288
martydd3 0:e516fcccccda 289 NVIC_SetPriority(TIMER0_IRQn,200); //IMD Capture Interrupt
martydd3 0:e516fcccccda 290 NVIC_SetPriority(TIMER1_IRQn,200); //IMD 1msec sampling Interrupt
martydd3 0:e516fcccccda 291 NVIC_SetPriority(TIMER2_IRQn,1); //Battery 1msec sampling Interrupt
martydd3 0:e516fcccccda 292 NVIC_SetPriority(TIMER3_IRQn,255); //mbed Timer/Ticker/Wait Interrupt
martydd3 0:e516fcccccda 293 NVIC_SetPriority(CAN_IRQn,2);
martydd3 0:e516fcccccda 294
martydd3 0:e516fcccccda 295 NVIC_EnableIRQ(TIMER2_IRQn); //Enable TIMER2 IRQ
martydd3 0:e516fcccccda 296
martydd3 0:e516fcccccda 297 CAN_SysM.mode(CAN::GlobalTest);
martydd3 2:baeb80c778f7 298
martydd3 2:baeb80c778f7 299
martydd3 0:e516fcccccda 300 //NVIC_EnableIRQ(CAN_IRQn);
martydd3 0:e516fcccccda 301 //NVIC_EnableIRQ(CANActivity_IRQn);
martydd3 0:e516fcccccda 302 }
martydd3 0:e516fcccccda 303
martydd3 2:baeb80c778f7 304
martydd3 0:e516fcccccda 305 Main Loop: Currently reads CANMessages from Can interface (Pins: rd = p30, td = p29)
martydd3 0:e516fcccccda 306 Send CANMessage data through XBee radio transmitters
martydd3 2:baeb80c778f7 307 */