Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
pspatel321
Date:
Fri Oct 24 22:09:04 2014 +0000
Revision:
13:fbd9b3f5a07c
Parent:
12:e0adb697fcdb
Child:
17:c9ce210f6654
Fork showing Parth's changes to current monitor, IMD, and fanpump.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martydd3 6:6a04210a3f4f 1 #include "FanPump.h"
martydd3 6:6a04210a3f4f 2
pspatel321 13:fbd9b3f5a07c 3 static FanPump* instance[6] = { NULL };
martydd3 6:6a04210a3f4f 4
pspatel321 13:fbd9b3f5a07c 5 // Interrupt handler, must be called from static context, calls all the slew functions
pspatel321 13:fbd9b3f5a07c 6 void pwmIRQ() {
pspatel321 13:fbd9b3f5a07c 7 if (LPC_PWM1->IR & 1) {
pspatel321 13:fbd9b3f5a07c 8 for (int i = 0; i < 6; i++) {
pspatel321 13:fbd9b3f5a07c 9 if (instance[i] != NULL) instance[i]->slew();
pspatel321 13:fbd9b3f5a07c 10 }
pspatel321 13:fbd9b3f5a07c 11 }
pspatel321 13:fbd9b3f5a07c 12 LPC_PWM1->IR = 0x73F; // Clear interrupts
martydd3 6:6a04210a3f4f 13 }
martydd3 6:6a04210a3f4f 14
pspatel321 13:fbd9b3f5a07c 15 // Called on each timer expire for each pwm object
pspatel321 13:fbd9b3f5a07c 16 void FanPump::slew() {
pspatel321 13:fbd9b3f5a07c 17 uint32_t currPulseT = *(&LPC_PWM1->MR0+pin);
pspatel321 13:fbd9b3f5a07c 18 uint32_t currPulse_us = currPulseT / 24;
pspatel321 13:fbd9b3f5a07c 19 uint32_t setPointT = setPoint_us * 24; // Convert us into ticks
pspatel321 13:fbd9b3f5a07c 20 if (currPulseT == setPointT) return; // Nothing to slew here, already at its setpoint
pspatel321 13:fbd9b3f5a07c 21
pspatel321 13:fbd9b3f5a07c 22 if (currPulseT < setPointT) {
pspatel321 13:fbd9b3f5a07c 23 if (setPoint_us - currPulse_us < maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly
pspatel321 13:fbd9b3f5a07c 24 else pwm.pulsewidth_us(currPulse_us + maxChange_us);
pspatel321 13:fbd9b3f5a07c 25 } else {
pspatel321 13:fbd9b3f5a07c 26 if (currPulse_us - setPoint_us < maxChange_us) pwm.pulsewidth_us(setPoint_us);
pspatel321 13:fbd9b3f5a07c 27 else pwm.pulsewidth_us(currPulse_us - maxChange_us);
martydd3 6:6a04210a3f4f 28 }
martydd3 6:6a04210a3f4f 29 }
martydd3 6:6a04210a3f4f 30
pspatel321 13:fbd9b3f5a07c 31 FanPump::FanPump(PinName pin, float period, float slew) : pwm(pin) {
pspatel321 13:fbd9b3f5a07c 32 setPoint_us = 0;
pspatel321 13:fbd9b3f5a07c 33 period_us = period / 1.0e6;
pspatel321 13:fbd9b3f5a07c 34 pwm.period_us(period_us);
pspatel321 13:fbd9b3f5a07c 35 maxChange_us = (period / slew) * period_us;
martydd3 6:6a04210a3f4f 36
pspatel321 13:fbd9b3f5a07c 37 // Match the pin# to the PWM object for the interrupt
pspatel321 13:fbd9b3f5a07c 38 if (pin == p26 || pin == LED1) pin = 1;
pspatel321 13:fbd9b3f5a07c 39 if (pin == p25 || pin == LED2) pin = 2;
pspatel321 13:fbd9b3f5a07c 40 if (pin == p24 || pin == LED3) pin = 3;
pspatel321 13:fbd9b3f5a07c 41 if (pin == p23 || pin == LED4) pin = 4;
pspatel321 13:fbd9b3f5a07c 42 if (pin == p22) pin = 5;
pspatel321 13:fbd9b3f5a07c 43 if (pin == p21) pin = 6;
pspatel321 13:fbd9b3f5a07c 44 instance[pin-1] = this;
martydd3 6:6a04210a3f4f 45
pspatel321 13:fbd9b3f5a07c 46 LPC_PWM1->IR = 0x73F; // Clear interrupts
pspatel321 13:fbd9b3f5a07c 47 NVIC_SetVector(PWM1_IRQn, (uint32_t)&pwmIRQ);
pspatel321 13:fbd9b3f5a07c 48 NVIC_SetPriority(PWM1_IRQn, 0);
pspatel321 13:fbd9b3f5a07c 49 NVIC_EnableIRQ(PWM1_IRQn);
pspatel321 13:fbd9b3f5a07c 50 LPC_PWM1->MR |= 1; // Enable interrupt on MR0 (when the pwm period expires)
martydd3 6:6a04210a3f4f 51 }
pspatel321 13:fbd9b3f5a07c 52 void FanPump::write(float duty) {
pspatel321 13:fbd9b3f5a07c 53 if (duty < 0) duty = 0;
pspatel321 13:fbd9b3f5a07c 54 if (duty > 1) duty = 1;
pspatel321 13:fbd9b3f5a07c 55 setPoint_us = duty * period_us;
martydd3 6:6a04210a3f4f 56 }