System Management code
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
Revision 22:fc802e7715f8, committed 2014-11-07
- Comitter:
- martydd3
- Date:
- Fri Nov 07 21:09:50 2014 +0000
- Parent:
- 21:2e83002d452d
- Child:
- 23:ebdb30592a4b
- Commit message:
- CANController to link DC_DC and FanPump, as well as listen to CAN Messages (since only these 2 functions act based on incoming messages)
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANController/CANController.c Fri Nov 07 21:09:50 2014 +0000 @@ -0,0 +1,1 @@ +#include "CANController.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANController/CANController.h Fri Nov 07 21:09:50 2014 +0000 @@ -0,0 +1,23 @@ +#ifndef _FILE_CANCONTRL_H +#define _FILE_CANCONTRL_H + +#include "DC_DC.h" +#include "FanPump.h" + +enum ContrlPinName{ + FAN1, FAN2, FAN3, PUMP +} + +class CANController{ +public: + CANController(); + bool dc_on(); + void set_dc(bool status); + bool write_contrl(ContrlPinName name, float duty); + void direct_off(ContrlPinName name, float duty); + float *read_control(); + +private: + FanPump *contrl_pins[6]; + DC dc; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANController/DC_DC/DC_DC.cpp Fri Nov 07 21:09:50 2014 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "DC_DC.h" + +DigitalOut dc_pin(p20); +DigitalOut dc_control(p18); + +#define OFF 1 +#define ON 0 + +DC::DC(){ + dc_control = OFF; +} + +bool DC::is_on(){ + return dc_pin; +} + +void DC::set(bool s){ + if(s) + dc_control = ON; + else + dc_control = OFF; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANController/DC_DC/DC_DC.h Fri Nov 07 21:09:50 2014 +0000 @@ -0,0 +1,18 @@ +#ifndef _FILE_DC_DC_H +#define _FILE_DC_DC_H + +#include "CANBuffer.h" +#include "FanPump.h" +#include "rtos.h" + +const int TX_DC_DC_ID = ((0x4 << 8) | 0x01); +const int RX_DC_DC_ID = ((0x4 << 8) | 0x90); + +class DC{ + public: + DC(); //constructor takes function to shut down certain processes when off + bool is_on(); + void set(bool status); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANController/FanPump/FanPump.cpp Fri Nov 07 21:09:50 2014 +0000 @@ -0,0 +1,82 @@ +#include "FanPump.h" + +static FanPump* instance[6] = { NULL }; // Access pwm object by channel# +const int PCLK = 24e6; // 24Mhz clock +uint32_t FanPump::period_us = 0.0; + +// Interrupt handler, must be called from static context, calls all the slew functions +void pwmIRQ() { + int sum = 0; + int items = 0; + if (LPC_PWM1->IR & 1) { + for (int i = 0; i < 6; i++) { + if (instance[i] != NULL) { + items++; + sum += instance[i]->slew(); + } + } + } + LPC_PWM1->IR = 0x73F; // Clear interrupts + if (items == sum) LPC_PWM1->MCR = 0; // Detach all the interrupts, every pin is already where it needs to be +} + +// Called on each timer expire for each pwm object +int FanPump::slew() { + uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks + uint32_t setPointT = setPoint_us * (PCLK/1e6); // Convert us into ticks + if (currPulseT == setPointT) return 1; // Nothing to slew here, already at its setpoint + + uint32_t currPulse_us = currPulseT / (PCLK/1e6); // Convert to us + if (currPulseT < setPointT) { + if (setPoint_us - currPulse_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly + else pwm.pulsewidth_us(currPulse_us + maxChange_us); + } else { + if (currPulse_us - setPoint_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly + else pwm.pulsewidth_us(currPulse_us - maxChange_us); + } + return 0; +} + +FanPump::FanPump(PinName pin, float period, float slew) : pwm(pin) { + + // Match the pin# to the PWM object for the interrupt + int chan=0; + if (pin == p26 || pin == LED1) chan = 1; + if (pin == p25 || pin == LED2) chan = 2; + if (pin == p24 || pin == LED3) chan = 3; + if (pin == p23 || pin == LED4) chan = 4; + if (pin == p22) chan = 5; + if (pin == p21) chan = 6; + if (chan == 0) return; // Invalid pin + instance[chan-1] = this; + + setPoint_us = 0; + period_us = period / 1.0e6; + pwm.period_us(period_us); + maxChange_us = (period / slew) * period_us; + + LPC_PWM1->IR = 0x73F; // Clear interrupts + NVIC_SetVector(PWM1_IRQn, (uint32_t)&pwmIRQ); + NVIC_SetPriority(PWM1_IRQn, 0); + NVIC_EnableIRQ(PWM1_IRQn); + LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires) +} +void FanPump::write(float duty) { + if (duty < 0) duty = 0; + if (duty > 1) duty = 1; + setPoint_us = duty * period_us; + LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires) +} +void FanPump::directOff() { + __disable_irq(); + pwm.pulsewidth_us(0); + setPoint_us = 0; + __enable_irq(); +} +float FanPump::read() { + return (float)(setPoint_us)/(float)(period_us); +} +float FanPump::readRaw() { + uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks + return ((float)(currPulseT) / (float)(PCLK/1e6)) / (float)(period_us); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANController/FanPump/FanPump.h Fri Nov 07 21:09:50 2014 +0000 @@ -0,0 +1,24 @@ +#ifndef _FILE_FANPUMP_H +#define _FILE_FANPUMP_H + +#include "mbed.h" + +class FanPump{ +public: + // Takes Pwmout pin, period (seconds), duty cycle slew rate in second^-1 (1 means duty 0 to 1 occurs over 1 second, 0 means no slew) + // Use slew rate to implement soft start + FanPump(PinName pin, float period, float slew); + void write(float duty); + float read(); // Read the last setpoint + float readRaw(); // Read the raw current duty (may be mid-transition) + void directOff(); // Turn off the channel immediately (no slew) + int slew(); // Slew rate callback function +private: + PwmOut pwm; // mbed PWM out + volatile int chan; // pwm channel# + static uint32_t period_us; // Period in microseconds (shared by all channels) + volatile uint32_t setPoint_us; + volatile uint32_t maxChange_us; // Max pulsewidth change allowed to achieve the slew rate +}; + +#endif \ No newline at end of file
--- a/DC_DC/DC_DC.cpp Fri Nov 07 01:26:37 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -#include "mbed.h" -#include "DC_DC.h" - -FanPump *fanPump; -CANBuffer *tx_DC_Buffer; -bool status; - -DigitalOut dc_pin(p20); - -DC::DC(FanPump *fp, CANBuffer *can){ - status = false; - dc_pin = !status; - tx_DC_Buffer = can; - fanPump = fp; -} - -bool DC::is_on(){ - return status; -} - -void DC::set(bool s){/* - status = s; - if(!status){ - fanPump->shutdown_all(); - } - - dc_pin = !status;*/ -} - -void update_dcdc(const void *arg){ - char data[1] = {0}; - while(1){ - data[0] = status; - CANMessage txMessage(TX_DC_DC_ID, data, 1); - tx_DC_Buffer->txWrite(txMessage); - - Thread::wait(100); //10 Hz update - } -} - -void DC::start_update(){ - Thread update_thread(update_dcdc); -} \ No newline at end of file
--- a/DC_DC/DC_DC.h Fri Nov 07 01:26:37 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -#ifndef _FILE_DC_DC_H -#define _FILE_DC_DC_H - -#include "CANBuffer.h" -#include "FanPump.h" -#include "rtos.h" - -const int TX_DC_DC_ID = ((0x4 << 8) | 0x01); -const int RX_DC_DC_ID = ((0x4 << 8) | 0x90); - -class DC{ -public: - DC(FanPump *fanPump, CANBuffer *can); //constructor takes function to shut down certain processes when off - bool is_on(); - void set(bool status); - void start_update(); -}; - -#endif \ No newline at end of file
--- a/FanPump/FanPump.cpp Fri Nov 07 01:26:37 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -#include "FanPump.h" - -static FanPump* instance[6] = { NULL }; // Access pwm object by channel# -const int PCLK = 24e6; // 24Mhz clock -uint32_t FanPump::period_us = 0.0; - -// Interrupt handler, must be called from static context, calls all the slew functions -void pwmIRQ() { - int sum = 0; - int items = 0; - if (LPC_PWM1->IR & 1) { - for (int i = 0; i < 6; i++) { - if (instance[i] != NULL) { - items++; - sum += instance[i]->slew(); - } - } - } - LPC_PWM1->IR = 0x73F; // Clear interrupts - if (items == sum) LPC_PWM1->MCR = 0; // Detach all the interrupts, every pin is already where it needs to be -} - -// Called on each timer expire for each pwm object -int FanPump::slew() { - uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks - uint32_t setPointT = setPoint_us * (PCLK/1e6); // Convert us into ticks - if (currPulseT == setPointT) return 1; // Nothing to slew here, already at its setpoint - - uint32_t currPulse_us = currPulseT / (PCLK/1e6); // Convert to us - if (currPulseT < setPointT) { - if (setPoint_us - currPulse_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly - else pwm.pulsewidth_us(currPulse_us + maxChange_us); - } else { - if (currPulse_us - setPoint_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly - else pwm.pulsewidth_us(currPulse_us - maxChange_us); - } - return 0; -} - -FanPump::FanPump(PinName pin, float period, float slew) : pwm(pin) { - - // Match the pin# to the PWM object for the interrupt - int chan=0; - if (pin == p26 || pin == LED1) chan = 1; - if (pin == p25 || pin == LED2) chan = 2; - if (pin == p24 || pin == LED3) chan = 3; - if (pin == p23 || pin == LED4) chan = 4; - if (pin == p22) chan = 5; - if (pin == p21) chan = 6; - if (chan == 0) return; // Invalid pin - instance[chan-1] = this; - - setPoint_us = 0; - period_us = period / 1.0e6; - pwm.period_us(period_us); - maxChange_us = (period / slew) * period_us; - - LPC_PWM1->IR = 0x73F; // Clear interrupts - NVIC_SetVector(PWM1_IRQn, (uint32_t)&pwmIRQ); - NVIC_SetPriority(PWM1_IRQn, 0); - NVIC_EnableIRQ(PWM1_IRQn); - LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires) -} -void FanPump::write(float duty) { - if (duty < 0) duty = 0; - if (duty > 1) duty = 1; - setPoint_us = duty * period_us; - LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires) -} -void FanPump::directOff() { - __disable_irq(); - pwm.pulsewidth_us(0); - setPoint_us = 0; - __enable_irq(); -} -float FanPump::read() { - return (float)(setPoint_us)/(float)(period_us); -} -float FanPump::readRaw() { - uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks - return ((float)(currPulseT) / (float)(PCLK/1e6)) / (float)(period_us); -} \ No newline at end of file
--- a/FanPump/FanPump.h Fri Nov 07 01:26:37 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -#ifndef _FILE_FANPUMP_H -#define _FILE_FANPUMP_H - -#include "mbed.h" - -class FanPump{ -public: - // Takes Pwmout pin, period (seconds), duty cycle slew rate in second^-1 (1 means duty 0 to 1 occurs over 1 second, 0 means no slew) - // Use slew rate to implement soft start - FanPump(PinName pin, float period, float slew); - void write(float duty); - float read(); // Read the last setpoint - float readRaw(); // Read the raw current duty (may be mid-transition) - void directOff(); // Turn off the channel immediately (no slew) - int slew(); // Slew rate callback function -private: - PwmOut pwm; // mbed PWM out - volatile int chan; // pwm channel# - static uint32_t period_us; // Period in microseconds (shared by all channels) - volatile uint32_t setPoint_us; - volatile uint32_t maxChange_us; // Max pulsewidth change allowed to achieve the slew rate -}; - -#endif \ No newline at end of file
--- a/IOobjects.cpp Fri Nov 07 01:26:37 2014 +0000 +++ b/IOobjects.cpp Fri Nov 07 21:09:50 2014 +0000 @@ -6,4 +6,5 @@ Watchdog wdt(0.11); // Watchdog timer set to 110ms PollSwitch pollSwitch; IMD imd; -CoulombCounter coulombCounter(10, 0, 0); \ No newline at end of file +CoulombCounter coulombCounter(10, 0, 0); +DC dc; \ No newline at end of file
--- a/IOobjects.h Fri Nov 07 01:26:37 2014 +0000 +++ b/IOobjects.h Fri Nov 07 21:09:50 2014 +0000 @@ -9,6 +9,7 @@ #include "PollSwitch.h" #include "IMD.h" #include "CoulombCounter.h" +#include "DC_DC.h" extern CANBuffer can; extern MODSERIAL pc; @@ -16,5 +17,6 @@ extern PollSwitch pollSwitch; extern IMD imd; extern CoulombCounter coulombCounter; +extern DC dc; #endif \ No newline at end of file
--- a/SerialDiagnostics/SerialDiagnostics.cpp Fri Nov 07 01:26:37 2014 +0000 +++ b/SerialDiagnostics/SerialDiagnostics.cpp Fri Nov 07 21:09:50 2014 +0000 @@ -77,11 +77,21 @@ } // Reading Coulomb Counter - padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line padCenter(line, max_charsPerLine-2, " CoulombCounter ", '*'); ADD_LINE - sprintf(temp, "Current: %f ampHours: %f Capacity: %f SOC: %f", coulombCounter.current(), coulombCounter.ampHours(), coulombCounter.capacity(), coulombCounter.SOC()); ADD_SPRINTF_LINE + sprintf(temp, "Current: %f ampHours: %f ", coulombCounter.current(), coulombCounter.ampHours()); ADD_SPRINTF_LINE + sprintf(temp, "Capacity: %f SOC: %f ", coulombCounter.capacity(), coulombCounter.SOC()); ADD_SPRINTF_LINE + + // Reading DC_DC + padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line + padCenter(line, max_charsPerLine-2, " DC-DC Converter ", '*'); ADD_LINE + + sprintf(temp, "DC Converter on: %d ", dc.is_on()); ADD_SPRINTF_LINE + + // Reading FanPump + padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line + padCenter(line, max_charsPerLine-2, " ", '*'); ADD_LINE // Write it all at once to output tx buffer for (int i = 0; i < strlen(buff); i++) {
--- a/mbed-rtos.lib Fri Nov 07 01:26:37 2014 +0000 +++ b/mbed-rtos.lib Fri Nov 07 21:09:50 2014 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#aaa1b2c7c64c +http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#77c8e4604045
--- a/mbed.bld Fri Nov 07 01:26:37 2014 +0000 +++ b/mbed.bld Fri Nov 07 21:09:50 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file