System Management code
Dependencies: CANBuffer mbed SystemManagement mbed-rtos
System Management code for Penn Electric Racing
Functions:
Controls Fans and Pumps via instruction from CAN Messages, ramps them up over time to prevent damage
Turns on/off DC-DC converter via instruction from CAN Messages
SysMngmt.cpp@2:baeb80c778f7, 2014-10-05 (annotated)
- Committer:
- martydd3
- Date:
- Sun Oct 05 17:35:20 2014 +0000
- Revision:
- 2:baeb80c778f7
- Parent:
- 1:e02eb179aed3
- Child:
- 3:1345f882d8f3
Add preliminary code to control fans and pumps using PWM; To do: Decide CANMessage format for controlling fans and pumps; Debug and test code; Add to toggle DC-DC converter via CAN;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:e516fcccccda | 7 | #include"SysMngmt.h" |
martydd3 | 0:e516fcccccda | 8 | #include"Get_IMD.h" |
martydd3 | 0:e516fcccccda | 9 | #include"PollSwitch.h" |
martydd3 | 0:e516fcccccda | 10 | #include"TemperatureRead.h" |
martydd3 | 0:e516fcccccda | 11 | #include"Store_RTC.h" |
martydd3 | 0:e516fcccccda | 12 | #include"XBee_Lib.h" |
martydd3 | 0:e516fcccccda | 13 | #include"CANBuffer.h" |
martydd3 | 0:e516fcccccda | 14 | |
martydd3 | 2:baeb80c778f7 | 15 | #include"mbed.h" |
martydd3 | 2:baeb80c778f7 | 16 | #include"rtos.h" |
martydd3 | 0:e516fcccccda | 17 | |
martydd3 | 0:e516fcccccda | 18 | //Possible problems in IMD coz change of counter |
martydd3 | 0:e516fcccccda | 19 | //Possible problems in BatteryStatus coz change in library |
martydd3 | 0:e516fcccccda | 20 | |
martydd3 | 0:e516fcccccda | 21 | /* |
martydd3 | 0:e516fcccccda | 22 | |
martydd3 | 0:e516fcccccda | 23 | Attach Ticker every 10msec to |
martydd3 | 0:e516fcccccda | 24 | Get IMD |
martydd3 | 0:e516fcccccda | 25 | Poll Switches |
martydd3 | 0:e516fcccccda | 26 | Temperature Read |
martydd3 | 0:e516fcccccda | 27 | Get Battery State |
martydd3 | 0:e516fcccccda | 28 | End Ticker Send message through CAN |
martydd3 | 0:e516fcccccda | 29 | |
martydd3 | 0:e516fcccccda | 30 | CAN interrupt Rx Interrupt |
martydd3 | 0:e516fcccccda | 31 | Recieve CAN message into a buffer. Return |
martydd3 | 0:e516fcccccda | 32 | Buffer values(as long as !empty) -> SD Card, Xbee -> remove element |
martydd3 | 0:e516fcccccda | 33 | |
martydd3 | 0:e516fcccccda | 34 | extern "C" void CAN_IRQHandler(void) |
martydd3 | 0:e516fcccccda | 35 | { |
martydd3 | 0:e516fcccccda | 36 | CANMessage Rxmsg; |
martydd3 | 0:e516fcccccda | 37 | CAN_SysM.read(Rxmsg); |
martydd3 | 0:e516fcccccda | 38 | RecieveBuffer.add(Rxmsg); |
martydd3 | 0:e516fcccccda | 39 | } |
martydd3 | 0:e516fcccccda | 40 | |
martydd3 | 1:e02eb179aed3 | 41 | http://developer.mbed.org/users/AjK/notebook/getting-closer-to-the-hardware/ |
martydd3 | 1:e02eb179aed3 | 42 | |
martydd3 | 1:e02eb179aed3 | 43 | extern "C" means this is linked assuming it's C code |
martydd3 | 1:e02eb179aed3 | 44 | C++ linker appearently adds extra crap with the function arguments keeping functions of this kind from linking properly |
martydd3 | 1:e02eb179aed3 | 45 | |
martydd3 | 1:e02eb179aed3 | 46 | Interrupt handler, This is probably linked to the Timer 2 interrupt request somewhere by the libraries |
martydd3 | 2:baeb80c778f7 | 47 | |
martydd3 | 0:e516fcccccda | 48 | extern "C" void TIMER2_IRQHandler(void) |
martydd3 | 0:e516fcccccda | 49 | { |
martydd3 | 0:e516fcccccda | 50 | if((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt |
martydd3 | 0:e516fcccccda | 51 | { |
martydd3 | 1:e02eb179aed3 | 52 | // This probably shouldn't be here, never have a printf() in an interrupt, locks up the main thread |
martydd3 | 1:e02eb179aed3 | 53 | // printf("Every 1ms\n\r"); |
martydd3 | 1:e02eb179aed3 | 54 | |
martydd3 | 0:e516fcccccda | 55 | LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag |
martydd3 | 0:e516fcccccda | 56 | |
martydd3 | 1:e02eb179aed3 | 57 | // gonna hope all these calculations are correct |
martydd3 | 1:e02eb179aed3 | 58 | // writes to RTC store, but there's no read code here |
martydd3 | 1:e02eb179aed3 | 59 | // 2 misleading things. First, RTC store writes to a general purpose register |
martydd3 | 1:e02eb179aed3 | 60 | // secondly, no code reads from this register in this .cpp, but maybe some other code does |
martydd3 | 1:e02eb179aed3 | 61 | |
martydd3 | 0:e516fcccccda | 62 | Bat_I_Ratio=BatISense.read(); |
martydd3 | 0:e516fcccccda | 63 | BATA_msec=(((Bat_I_Ratio*3.3) - BAT_ISENSE_OFFSET_V)/BAT_ISENSE_INCREMENT); |
martydd3 | 0:e516fcccccda | 64 | BATmA_Hr+=(BATA_msec*MSEC_HRS); |
martydd3 | 0:e516fcccccda | 65 | store.write(BATmA_Hr,0); |
martydd3 | 0:e516fcccccda | 66 | |
martydd3 | 0:e516fcccccda | 67 | DC_I_Ratio=DCSense.read(); |
martydd3 | 0:e516fcccccda | 68 | DCA_msec=(((DC_I_Ratio*3.3) - DC_DC_ISENSE_OFFSET_V)/DC_DC_ISENSE_INCREMENT); |
martydd3 | 0:e516fcccccda | 69 | store.write(DCA_msec,1); |
martydd3 | 0:e516fcccccda | 70 | |
martydd3 | 0:e516fcccccda | 71 | LPC_TIM2->TCR |= (1<<1); //Reset Timer1 |
martydd3 | 1:e02eb179aed3 | 72 | LPC_TIM2->TCR &= ~(1<<1); //Re Enable Timer1 |
martydd3 | 0:e516fcccccda | 73 | } |
martydd3 | 0:e516fcccccda | 74 | } |
martydd3 | 0:e516fcccccda | 75 | |
martydd3 | 1:e02eb179aed3 | 76 | Appears to read a whole bunch of DigitalOut pins in void PollSwitch(), store the value in uint16_t Rxpoll, |
martydd3 | 1:e02eb179aed3 | 77 | and write the result to the CAN bus |
martydd3 | 2:baeb80c778f7 | 78 | |
martydd3 | 0:e516fcccccda | 79 | void Poll() |
martydd3 | 0:e516fcccccda | 80 | { |
martydd3 | 0:e516fcccccda | 81 | uint16_t Rxpoll; |
martydd3 | 0:e516fcccccda | 82 | uint16_t recv,temp,i=0; //Test |
martydd3 | 0:e516fcccccda | 83 | char Result[4]={0}; |
martydd3 | 0:e516fcccccda | 84 | Rxpoll=PollSwitch(); |
martydd3 | 0:e516fcccccda | 85 | |
martydd3 | 0:e516fcccccda | 86 | Result[0]=(char)(Rxpoll&0x00ff); |
martydd3 | 0:e516fcccccda | 87 | Result[1]=(char)((Rxpoll&0xff00)>>8); |
martydd3 | 0:e516fcccccda | 88 | CANMessage Txmsg(410,Result,sizeof(Result)); |
martydd3 | 0:e516fcccccda | 89 | CAN_SysM.write(Txmsg); |
martydd3 | 0:e516fcccccda | 90 | |
martydd3 | 0:e516fcccccda | 91 | //Test |
martydd3 | 0:e516fcccccda | 92 | recv=(((uint16_t)Txmsg.data[1]<<8) | (0x00ff&(uint16_t)Txmsg.data[0])); |
martydd3 | 0:e516fcccccda | 93 | printf("Recv:%d\n\r",recv); |
martydd3 | 0:e516fcccccda | 94 | |
martydd3 | 0:e516fcccccda | 95 | while(i <= 12) |
martydd3 | 0:e516fcccccda | 96 | { |
martydd3 | 0:e516fcccccda | 97 | temp=recv; |
martydd3 | 0:e516fcccccda | 98 | if(((temp & (1 << i))>>i)==1) |
martydd3 | 0:e516fcccccda | 99 | pc.printf("Switch OFF:%d\n\r",i); |
martydd3 | 0:e516fcccccda | 100 | ++i; |
martydd3 | 0:e516fcccccda | 101 | } |
martydd3 | 0:e516fcccccda | 102 | } |
martydd3 | 0:e516fcccccda | 103 | |
martydd3 | 0:e516fcccccda | 104 | |
martydd3 | 0:e516fcccccda | 105 | void Temp() |
martydd3 | 0:e516fcccccda | 106 | { |
martydd3 | 0:e516fcccccda | 107 | float DC_DC_Temperature, Coolant1_Temperature, Coolant2_Temperature, ChargerFET_Temperature; |
martydd3 | 0:e516fcccccda | 108 | float Resistance; |
martydd3 | 0:e516fcccccda | 109 | float Vadc; |
martydd3 | 0:e516fcccccda | 110 | int i; |
martydd3 | 0:e516fcccccda | 111 | ftc send, recv; |
martydd3 | 0:e516fcccccda | 112 | recv.FLOAT=0.0; |
martydd3 | 0:e516fcccccda | 113 | send.FLOAT=0.0; |
martydd3 | 0:e516fcccccda | 114 | |
martydd3 | 0:e516fcccccda | 115 | Vadc=DC_DC.read()*VDD; |
martydd3 | 0:e516fcccccda | 116 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
martydd3 | 0:e516fcccccda | 117 | DC_DC_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA); |
martydd3 | 0:e516fcccccda | 118 | send.FLOAT=DC_DC_Temperature; |
martydd3 | 0:e516fcccccda | 119 | CANMessage Txmsg_DC_DC(450,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 120 | CAN_SysM.write(Txmsg_DC_DC); |
martydd3 | 0:e516fcccccda | 121 | |
martydd3 | 0:e516fcccccda | 122 | for(i=0; i<4;i++) |
martydd3 | 0:e516fcccccda | 123 | recv.C_FLOAT[i]=Txmsg_DC_DC.data[i]; |
martydd3 | 0:e516fcccccda | 124 | pc.printf("DC_DC:%f\n\r",recv.FLOAT); |
martydd3 | 0:e516fcccccda | 125 | |
martydd3 | 0:e516fcccccda | 126 | Vadc=ChargerFET.read()*VDD; |
martydd3 | 0:e516fcccccda | 127 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
martydd3 | 0:e516fcccccda | 128 | ChargerFET_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA); |
martydd3 | 0:e516fcccccda | 129 | send.FLOAT=ChargerFET_Temperature; |
martydd3 | 0:e516fcccccda | 130 | CANMessage Txmsg_ChargerFET(451,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 131 | CAN_SysM.write(Txmsg_ChargerFET); |
martydd3 | 0:e516fcccccda | 132 | |
martydd3 | 0:e516fcccccda | 133 | for(i=0; i<4;i++) |
martydd3 | 0:e516fcccccda | 134 | recv.C_FLOAT[i]=Txmsg_ChargerFET.data[i]; |
martydd3 | 0:e516fcccccda | 135 | pc.printf("ChargerFET:%f\n\r",recv.FLOAT); |
martydd3 | 0:e516fcccccda | 136 | |
martydd3 | 0:e516fcccccda | 137 | Vadc=Coolant1.read()*VDD; |
martydd3 | 0:e516fcccccda | 138 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
martydd3 | 0:e516fcccccda | 139 | Coolant1_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H); |
martydd3 | 0:e516fcccccda | 140 | send.FLOAT=Coolant1_Temperature; |
martydd3 | 0:e516fcccccda | 141 | CANMessage Txmsg_Coolant1(452,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 142 | CAN_SysM.write(Txmsg_Coolant1); |
martydd3 | 0:e516fcccccda | 143 | //Control Fans |
martydd3 | 0:e516fcccccda | 144 | |
martydd3 | 0:e516fcccccda | 145 | for(i=0; i<4;i++) |
martydd3 | 0:e516fcccccda | 146 | recv.C_FLOAT[i]=Txmsg_Coolant1.data[i]; |
martydd3 | 0:e516fcccccda | 147 | pc.printf("Coolant1:%f\n\r",recv.FLOAT); |
martydd3 | 0:e516fcccccda | 148 | |
martydd3 | 0:e516fcccccda | 149 | Vadc=Coolant2.read()*VDD; |
martydd3 | 0:e516fcccccda | 150 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
martydd3 | 0:e516fcccccda | 151 | Coolant2_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H); |
martydd3 | 0:e516fcccccda | 152 | send.FLOAT=Coolant2_Temperature; |
martydd3 | 0:e516fcccccda | 153 | CANMessage Txmsg_Coolant2(453,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 154 | CAN_SysM.write(Txmsg_Coolant2); |
martydd3 | 0:e516fcccccda | 155 | //Control Fans |
martydd3 | 0:e516fcccccda | 156 | |
martydd3 | 0:e516fcccccda | 157 | for(i=0; i<4;i++) |
martydd3 | 0:e516fcccccda | 158 | recv.C_FLOAT[i]=Txmsg_Coolant2.data[i]; |
martydd3 | 0:e516fcccccda | 159 | pc.printf("Coolant2:%f\n\r",recv.FLOAT); |
martydd3 | 0:e516fcccccda | 160 | } |
martydd3 | 0:e516fcccccda | 161 | |
martydd3 | 0:e516fcccccda | 162 | void IMD() |
martydd3 | 0:e516fcccccda | 163 | { |
martydd3 | 0:e516fcccccda | 164 | IMD_Measurement_Output IMD_Signal; |
martydd3 | 0:e516fcccccda | 165 | char status[4]; |
martydd3 | 0:e516fcccccda | 166 | ftc send; |
martydd3 | 0:e516fcccccda | 167 | |
martydd3 | 0:e516fcccccda | 168 | IMD_Signal=Get_Measurement(); |
martydd3 | 0:e516fcccccda | 169 | send.FLOAT=IMD_Signal.Frequency; |
martydd3 | 0:e516fcccccda | 170 | CANMessage Txmsg_Frequency(421,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 171 | CAN_SysM.write(Txmsg_Frequency); |
martydd3 | 0:e516fcccccda | 172 | |
martydd3 | 0:e516fcccccda | 173 | send.FLOAT=IMD_Signal.Duty_Cycle; |
martydd3 | 0:e516fcccccda | 174 | CANMessage Txmsg_DutyCycle(422,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 175 | CAN_SysM.write(Txmsg_DutyCycle); |
martydd3 | 0:e516fcccccda | 176 | |
martydd3 | 0:e516fcccccda | 177 | status[0]=Result.Encoded_Status; |
martydd3 | 0:e516fcccccda | 178 | CANMessage Txmsg_Status(423,status,sizeof(status)); |
martydd3 | 0:e516fcccccda | 179 | CAN_SysM.write(Txmsg_Status); |
martydd3 | 0:e516fcccccda | 180 | } |
martydd3 | 0:e516fcccccda | 181 | |
martydd3 | 0:e516fcccccda | 182 | void Battery() |
martydd3 | 0:e516fcccccda | 183 | { |
martydd3 | 0:e516fcccccda | 184 | RTCStore reg; |
martydd3 | 0:e516fcccccda | 185 | ftc send; |
martydd3 | 0:e516fcccccda | 186 | |
martydd3 | 0:e516fcccccda | 187 | send.FLOAT=reg.read(0); |
martydd3 | 0:e516fcccccda | 188 | CANMessage Txmsg_BATmA_Hr(440,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 189 | CAN_SysM.write(Txmsg_BATmA_Hr); |
martydd3 | 0:e516fcccccda | 190 | |
martydd3 | 0:e516fcccccda | 191 | send.FLOAT=reg.read(1); |
martydd3 | 0:e516fcccccda | 192 | CANMessage Txmsg_DCA_msec(442,send.C_FLOAT,sizeof(send.C_FLOAT)); |
martydd3 | 0:e516fcccccda | 193 | CAN_SysM.write(Txmsg_DCA_msec); |
martydd3 | 0:e516fcccccda | 194 | } |
martydd3 | 0:e516fcccccda | 195 | |
martydd3 | 0:e516fcccccda | 196 | Activates a whole crapload of functions and pins on the chip |
martydd3 | 2:baeb80c778f7 | 197 | |
martydd3 | 0:e516fcccccda | 198 | void Init() |
martydd3 | 0:e516fcccccda | 199 | { |
martydd3 | 2:baeb80c778f7 | 200 | |
martydd3 | 0:e516fcccccda | 201 | Timers to call various functions at different intervals |
martydd3 | 0:e516fcccccda | 202 | These things behave weirdly when wait(ms) is involved. Probably have to rewrite |
martydd3 | 2:baeb80c778f7 | 203 | |
martydd3 | 0:e516fcccccda | 204 | |
martydd3 | 0:e516fcccccda | 205 | //ReadIMD.attach(&IMD,0.1); |
martydd3 | 0:e516fcccccda | 206 | //PollSDSwitch.attach(&Poll,0.1); |
martydd3 | 0:e516fcccccda | 207 | |
martydd3 | 0:e516fcccccda | 208 | //ReadTemperature.attach(&Temp,0.1); |
martydd3 | 0:e516fcccccda | 209 | //ReadBatteryState.attach(&Battery,0.1); |
martydd3 | 0:e516fcccccda | 210 | |
martydd3 | 2:baeb80c778f7 | 211 | |
martydd3 | 0:e516fcccccda | 212 | Initialize Timer2 for Battery State |
martydd3 | 0:e516fcccccda | 213 | |
martydd3 | 0:e516fcccccda | 214 | LPC_SC 0x400F C000 (System Control) |
martydd3 | 0:e516fcccccda | 215 | ->PCONP 0x400F C0C4 (Power Control for Peripherals Register) |
martydd3 | 0:e516fcccccda | 216 | |= (1<<22) 22 Bit (Timer 2 power/clock control bit) |
martydd3 | 0:e516fcccccda | 217 | |
martydd3 | 0:e516fcccccda | 218 | ->PCLKSEL1 Peripheral Clock Selection register 1 (controls rate of clock signal supplied to peripheral) |
martydd3 | 0:e516fcccccda | 219 | |= ((1<<12) | (1<<13)); 12:13 Bits (Peripheral Clock Selection for TIMER2) |
martydd3 | 0:e516fcccccda | 220 | |
martydd3 | 0:e516fcccccda | 221 | LPC_TIM2 0x4009 0000 (Timer 2) |
martydd3 | 0:e516fcccccda | 222 | ->TCR 0x4009 0004 (Timer Control Register) |
martydd3 | 0:e516fcccccda | 223 | |= (1<<0); 0 Bit (Counter Enable) |
martydd3 | 0:e516fcccccda | 224 | |
martydd3 | 0:e516fcccccda | 225 | ->MR0 0x4009 0018 (Match Register) |
martydd3 | 0:e516fcccccda | 226 | |
martydd3 | 1:e02eb179aed3 | 227 | ->MCR 0x4009 0014 (Match Control Register) What to do when Match Register matches the Timer Counter |
martydd3 | 1:e02eb179aed3 | 228 | |= (1<<0); 0 Bit (Interrupt on MR0, interrupt generated when MR0 matches the value in TC) |
martydd3 | 0:e516fcccccda | 229 | |
martydd3 | 2:baeb80c778f7 | 230 | |
martydd3 | 0:e516fcccccda | 231 | LPC_SC->PCONP |= (1<<22); //PoewerOn Timer/Counter2 |
martydd3 | 0:e516fcccccda | 232 | LPC_SC->PCLKSEL1 |= ((1<<12) | (1<<13)); //Prescale Timer2 CCLK/8 |
martydd3 | 0:e516fcccccda | 233 | LPC_TIM2->TCR |= (1<<0); //Enable Timer2 |
martydd3 | 0:e516fcccccda | 234 | LPC_TIM2->MR0 = 11999; // 1msec |
martydd3 | 0:e516fcccccda | 235 | LPC_TIM2->MCR |= (1<<0); |
martydd3 | 2:baeb80c778f7 | 236 | |
martydd3 | 1:e02eb179aed3 | 237 | Nested Vectored Interrupt Controller (NVIC) |
martydd3 | 1:e02eb179aed3 | 238 | |
martydd3 | 1:e02eb179aed3 | 239 | NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) |
martydd3 | 1:e02eb179aed3 | 240 | sets priority of an interrupt |
martydd3 | 1:e02eb179aed3 | 241 | |
martydd3 | 1:e02eb179aed3 | 242 | IRQn_Type |
martydd3 | 1:e02eb179aed3 | 243 | Interrupt number definitions |
martydd3 | 1:e02eb179aed3 | 244 | Interrupt Request (IRQ) |
martydd3 | 1:e02eb179aed3 | 245 | |
martydd3 | 1:e02eb179aed3 | 246 | NVIC_EnableIRQ(IRQn_Type IRQn) |
martydd3 | 1:e02eb179aed3 | 247 | Enable external interrupt (in this case, the TIMER2_IRQHandler(void) function above gets called every time |
martydd3 | 1:e02eb179aed3 | 248 | Timer2 generates an interrupt signal) |
martydd3 | 1:e02eb179aed3 | 249 | |
martydd3 | 2:baeb80c778f7 | 250 | |
martydd3 | 0:e516fcccccda | 251 | NVIC_SetPriority(TIMER0_IRQn,200); //IMD Capture Interrupt |
martydd3 | 0:e516fcccccda | 252 | NVIC_SetPriority(TIMER1_IRQn,200); //IMD 1msec sampling Interrupt |
martydd3 | 0:e516fcccccda | 253 | NVIC_SetPriority(TIMER2_IRQn,1); //Battery 1msec sampling Interrupt |
martydd3 | 0:e516fcccccda | 254 | NVIC_SetPriority(TIMER3_IRQn,255); //mbed Timer/Ticker/Wait Interrupt |
martydd3 | 0:e516fcccccda | 255 | NVIC_SetPriority(CAN_IRQn,2); |
martydd3 | 0:e516fcccccda | 256 | |
martydd3 | 0:e516fcccccda | 257 | NVIC_EnableIRQ(TIMER2_IRQn); //Enable TIMER2 IRQ |
martydd3 | 0:e516fcccccda | 258 | |
martydd3 | 0:e516fcccccda | 259 | CAN_SysM.mode(CAN::GlobalTest); |
martydd3 | 2:baeb80c778f7 | 260 | |
martydd3 | 2:baeb80c778f7 | 261 | |
martydd3 | 0:e516fcccccda | 262 | //NVIC_EnableIRQ(CAN_IRQn); |
martydd3 | 0:e516fcccccda | 263 | //NVIC_EnableIRQ(CANActivity_IRQn); |
martydd3 | 0:e516fcccccda | 264 | } |
martydd3 | 0:e516fcccccda | 265 | |
martydd3 | 2:baeb80c778f7 | 266 | |
martydd3 | 0:e516fcccccda | 267 | Main Loop: Currently reads CANMessages from Can interface (Pins: rd = p30, td = p29) |
martydd3 | 0:e516fcccccda | 268 | Send CANMessage data through XBee radio transmitters |
martydd3 | 2:baeb80c778f7 | 269 | */ |
martydd3 | 2:baeb80c778f7 | 270 | |
martydd3 | 2:baeb80c778f7 | 271 | |
martydd3 | 2:baeb80c778f7 | 272 | CANBuffer rxBuffer(CAN1, MEDIUM); |
martydd3 | 2:baeb80c778f7 | 273 | XBee250x XbeeTx; |
martydd3 | 2:baeb80c778f7 | 274 | |
martydd3 | 2:baeb80c778f7 | 275 | int sysCANId = 0; // change this later |
martydd3 | 2:baeb80c778f7 | 276 | |
martydd3 | 2:baeb80c778f7 | 277 | char fanId = 0; // first byte of CANData, last byte states the duty cycle, second-last states which fan to control |
martydd3 | 2:baeb80c778f7 | 278 | |
martydd3 | 2:baeb80c778f7 | 279 | Thread *fan_Threads[4] = {NULL, NULL, NULL, NULL}; // Threads currently running on the 4 output pins |
martydd3 | 2:baeb80c778f7 | 280 | |
martydd3 | 2:baeb80c778f7 | 281 | /* |
martydd3 | 2:baeb80c778f7 | 282 | Pins for Fans and Pumps |
martydd3 | 2:baeb80c778f7 | 283 | PUMP_PWM = p2.0 |
martydd3 | 2:baeb80c778f7 | 284 | FAN1_PWM = p2.1 |
martydd3 | 2:baeb80c778f7 | 285 | FAN2_PWM = p2.2 |
martydd3 | 2:baeb80c778f7 | 286 | FAN3_PWM = p2.3 |
martydd3 | 2:baeb80c778f7 | 287 | */ |
martydd3 | 2:baeb80c778f7 | 288 | |
martydd3 | 2:baeb80c778f7 | 289 | PwmOut pump_pwm(P2_0); |
martydd3 | 2:baeb80c778f7 | 290 | PwmOut fan1_pwm(P2_1); |
martydd3 | 2:baeb80c778f7 | 291 | PwmOut fan2_pwm(P2_2); |
martydd3 | 2:baeb80c778f7 | 292 | PwmOut fan3_pwm(P2_3); |
martydd3 | 2:baeb80c778f7 | 293 | |
martydd3 | 2:baeb80c778f7 | 294 | // duty goes from 0 to 100 |
martydd3 | 2:baeb80c778f7 | 295 | void rampFans(void const *arg){ |
martydd3 | 0:e516fcccccda | 296 | |
martydd3 | 2:baeb80c778f7 | 297 | static int fan1_duty = 0; |
martydd3 | 2:baeb80c778f7 | 298 | static int fan2_duty = 0; |
martydd3 | 2:baeb80c778f7 | 299 | static int fan3_duty = 0; |
martydd3 | 2:baeb80c778f7 | 300 | static int pump_duty = 0; |
martydd3 | 2:baeb80c778f7 | 301 | |
martydd3 | 2:baeb80c778f7 | 302 | int *data = (int *)arg; |
martydd3 | 2:baeb80c778f7 | 303 | |
martydd3 | 2:baeb80c778f7 | 304 | int duty = data[7]; |
martydd3 | 2:baeb80c778f7 | 305 | if(duty < 0 || duty > 100) |
martydd3 | 2:baeb80c778f7 | 306 | return; |
martydd3 | 2:baeb80c778f7 | 307 | |
martydd3 | 2:baeb80c778f7 | 308 | int pin_ID = data[6]; |
martydd3 | 2:baeb80c778f7 | 309 | int *cur_duty; |
martydd3 | 2:baeb80c778f7 | 310 | PwmOut *out_pin; |
martydd3 | 2:baeb80c778f7 | 311 | |
martydd3 | 2:baeb80c778f7 | 312 | switch(pin_ID){ |
martydd3 | 2:baeb80c778f7 | 313 | case 0: cur_duty = &pump_duty; |
martydd3 | 2:baeb80c778f7 | 314 | out_pin = &pump_pwm; |
martydd3 | 2:baeb80c778f7 | 315 | break; |
martydd3 | 2:baeb80c778f7 | 316 | case 1: cur_duty = &fan1_duty; |
martydd3 | 2:baeb80c778f7 | 317 | out_pin = &fan1_pwm; |
martydd3 | 2:baeb80c778f7 | 318 | break; |
martydd3 | 2:baeb80c778f7 | 319 | case 2: cur_duty = &fan2_duty; |
martydd3 | 2:baeb80c778f7 | 320 | out_pin = &fan2_pwm; |
martydd3 | 2:baeb80c778f7 | 321 | break; |
martydd3 | 2:baeb80c778f7 | 322 | case 3: cur_duty = &fan3_duty; |
martydd3 | 2:baeb80c778f7 | 323 | out_pin = &fan3_pwm; |
martydd3 | 2:baeb80c778f7 | 324 | break; |
martydd3 | 2:baeb80c778f7 | 325 | default: |
martydd3 | 2:baeb80c778f7 | 326 | return; |
martydd3 | 2:baeb80c778f7 | 327 | } |
martydd3 | 2:baeb80c778f7 | 328 | |
martydd3 | 2:baeb80c778f7 | 329 | while(*cur_duty != duty){ |
martydd3 | 2:baeb80c778f7 | 330 | |
martydd3 | 2:baeb80c778f7 | 331 | if(duty > *cur_duty){ |
martydd3 | 2:baeb80c778f7 | 332 | *cur_duty += 10; |
martydd3 | 2:baeb80c778f7 | 333 | |
martydd3 | 2:baeb80c778f7 | 334 | if(*cur_duty > duty) |
martydd3 | 2:baeb80c778f7 | 335 | *cur_duty = duty; |
martydd3 | 2:baeb80c778f7 | 336 | } else if(duty < *cur_duty){ |
martydd3 | 2:baeb80c778f7 | 337 | *cur_duty -= 10; |
martydd3 | 2:baeb80c778f7 | 338 | |
martydd3 | 2:baeb80c778f7 | 339 | if(*cur_duty < duty) |
martydd3 | 2:baeb80c778f7 | 340 | *cur_duty = duty; |
martydd3 | 2:baeb80c778f7 | 341 | } |
martydd3 | 2:baeb80c778f7 | 342 | |
martydd3 | 2:baeb80c778f7 | 343 | out_pin->write((*cur_duty)*0.01); |
martydd3 | 2:baeb80c778f7 | 344 | |
martydd3 | 2:baeb80c778f7 | 345 | Thread::wait(10); |
martydd3 | 2:baeb80c778f7 | 346 | } |
martydd3 | 2:baeb80c778f7 | 347 | } |
martydd3 | 2:baeb80c778f7 | 348 | |
martydd3 | 0:e516fcccccda | 349 | int main() { |
martydd3 | 2:baeb80c778f7 | 350 | CANMessage rx_msg; |
martydd3 | 2:baeb80c778f7 | 351 | |
martydd3 | 0:e516fcccccda | 352 | while(1) |
martydd3 | 0:e516fcccccda | 353 | { |
martydd3 | 2:baeb80c778f7 | 354 | if(rxBuffer.rxRead(rx_msg) && rx_msg.id == sysCANId){ |
martydd3 | 2:baeb80c778f7 | 355 | if(rx_msg.data[0] == fanId){ |
martydd3 | 2:baeb80c778f7 | 356 | |
martydd3 | 2:baeb80c778f7 | 357 | int pin_id = (int)rx_msg.data[6]; |
martydd3 | 2:baeb80c778f7 | 358 | |
martydd3 | 2:baeb80c778f7 | 359 | if(pin_id < 4 && pin_id >= 0 && fan_Threads[pin_id] != NULL){ |
martydd3 | 2:baeb80c778f7 | 360 | fan_Threads[pin_id]->terminate(); |
martydd3 | 2:baeb80c778f7 | 361 | free(fan_Threads[pin_id]); |
martydd3 | 2:baeb80c778f7 | 362 | } |
martydd3 | 2:baeb80c778f7 | 363 | |
martydd3 | 2:baeb80c778f7 | 364 | fan_Threads[pin_id] = new Thread(rampFans, rx_msg.data); |
martydd3 | 2:baeb80c778f7 | 365 | } |
martydd3 | 2:baeb80c778f7 | 366 | } |
martydd3 | 0:e516fcccccda | 367 | } |
martydd3 | 0:e516fcccccda | 368 | } |