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
Revision 7:5f6e31faa08e, committed 2014-10-10
- Comitter:
- martydd3
- Date:
- Fri Oct 10 20:59:36 2014 +0000
- Parent:
- 6:6a04210a3f4f
- Commit message:
- PollSwitch code;
Changed in this revision
--- a/FanPump/FanPump.cpp Fri Oct 10 20:24:22 2014 +0000
+++ b/FanPump/FanPump.cpp Fri Oct 10 20:59:36 2014 +0000
@@ -2,14 +2,14 @@
#include "FanPump.h"
PwmOut pwmPins[PIN_NUM] = {
- (P2_0), // pump
- (P2_1), // fan 1
- (P2_2), // fan 2
- (P2_3), // fan 3
+ PwmOut(P2_0), // pump
+ PwmOut(P2_1), // fan 1
+ PwmOut(P2_2), // fan 2
+ PwmOut(P2_3), // fan 3
};
PinStatus pin_status[PIN_NUM];
-CANBuffer *txBuffer;
+CANBuffer *tx_Fan_Buffer;
FanPump::FanPump(CANBuffer *can){
for(int i = 0; i < PIN_NUM; i++){
@@ -23,7 +23,7 @@
pin_threads[i] = NULL;
}
- txBuffer = can;
+ tx_Fan_Buffer = can;
}
// this is not a member function. For some reason, thread does weird things
@@ -94,7 +94,7 @@
}
CANMessage txMessage(TX_FAN_ID, data, 4);
- txBuffer->txWrite(txMessage);
+ tx_Fan_Buffer->txWrite(txMessage);
Thread::wait(100); // 10 Hz update
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PollSwitch/PollSwitch.cpp Fri Oct 10 20:59:36 2014 +0000
@@ -0,0 +1,61 @@
+#include "PollSwitch.h"
+
+LPC_pin PollPin[12]={p1_0, p1_1, p1_4, p1_8, p1_9, p1_10, p1_14, p1_15, p1_16, p1_17, p1_27, p1_28};
+LPCDigitalOut poll[12]={ LPCDigitalOut(PollPin[0]),
+ LPCDigitalOut(PollPin[1]),
+ LPCDigitalOut(PollPin[2]),
+ LPCDigitalOut(PollPin[3]),
+ LPCDigitalOut(PollPin[4]),
+ LPCDigitalOut(PollPin[5]),
+ LPCDigitalOut(PollPin[6]),
+ LPCDigitalOut(PollPin[7]),
+ LPCDigitalOut(PollPin[8]),
+ LPCDigitalOut(PollPin[9]),
+ LPCDigitalOut(PollPin[10]),
+ LPCDigitalOut(PollPin[11])};
+
+CANBuffer *tx_Poll_Buffer;
+
+union int_to_char {
+ char ch[2];
+ uint16_t i;
+} converter;
+
+PollSwitch::PollSwitch(CANBuffer *can){
+ tx_Poll_Buffer = can;
+ for(int i = 0; i < 12; i++){
+ poll[i].mode(PullDown);
+ }
+}
+
+uint16_t poll_switches(){
+ uint16_t a = 0;
+ int i = 0;
+
+ // if a low signal is detected, previous switch is broken
+ for(i = 1; i < 12; i++){
+ if(!poll[i].read())
+ break;
+ }
+
+ // bit on: switch may be broken
+ a = 0 & (0xFF >> (i-1));
+ return a;
+}
+
+void update_poll(const void *arg){
+ char data[4] = {0};
+ while(1){
+ converter.i = poll_switches();
+ data[0] = converter.ch[0];
+ data[1] = converter.ch[1];
+ CANMessage txMessage(TX_POLL_ID, data, 4);
+ tx_Poll_Buffer->txWrite(txMessage);
+
+ Thread::wait(100); //10 Hz update
+ }
+}
+
+void PollSwitch::start_update(){
+ Thread update_thread(update_poll);
+}
\ No newline at end of file
--- a/PollSwitch/PollSwitch.h Fri Oct 10 20:24:22 2014 +0000
+++ b/PollSwitch/PollSwitch.h Fri Oct 10 20:59:36 2014 +0000
@@ -1,42 +1,16 @@
+#ifndef _FILE_POLLSWITCH_H
+#define _FILE_POLLSWITCH_H
+
#include "LPCDigitalOut.h"
#include "LPCDigitalIn.h"
+#include "CANBuffer.h"
+#include "rtos.h"
-
+const int TX_POLL_ID = ((4 << 8) | 6);
-uint16_t PollSwitch()
-{
- uint16_t a=0;
- int switchn=0, i=0;
- LPC_pin PollPin[12]={p1_0, p1_1, p1_4, p1_8, p1_9, p1_10, p1_14, p1_15, p1_16, p1_17, p1_27, p1_28};
- LPCDigitalOut poll[12]={ LPCDigitalOut(PollPin[0]),
- LPCDigitalOut(PollPin[1]),
- LPCDigitalOut(PollPin[2]),
- LPCDigitalOut(PollPin[3]),
- LPCDigitalOut(PollPin[4]),
- LPCDigitalOut(PollPin[5]),
- LPCDigitalOut(PollPin[6]),
- LPCDigitalOut(PollPin[7]),
- LPCDigitalOut(PollPin[8]),
- LPCDigitalOut(PollPin[9]),
- LPCDigitalOut(PollPin[10]),
- LPCDigitalOut(PollPin[11])};
-
- // poll each switch 1 at a time
- // first failed switch is returned
-
- for(i=0; i<11; i++){
- ++switchn;
-
- poll[i].write(1);
-
- wait_ms(25);
-
- //poll[i+1].mode(PullDown);
- a|=((1 - poll[i+1].read()) << switchn);
-
- //poll[i].mode(PullNone);
- //poll[i+1].mode(PullNone);
- }
- // poll[i].mode(neither);
- return a;
-}
\ No newline at end of file
+class PollSwitch{
+ public:
+ PollSwitch(CANBuffer *can);
+ void start_update();
+};
+#endif
\ No newline at end of file
--- a/SysMngmt.cpp Fri Oct 10 20:24:22 2014 +0000
+++ b/SysMngmt.cpp Fri Oct 10 20:59:36 2014 +0000
@@ -18,6 +18,7 @@
#include "Watchdog.cpp"
#include "FanPump.h"
#include "DC_DC.h"
+#include "PollSwitch.h"
//Possible problems in IMD coz change of counter
//Possible problems in BatteryStatus coz change in library
@@ -267,17 +268,20 @@
char sys_src_id = 4; // source address of system management
-Watchdog wdt;
-
int main() {
- CANMessage rx_msg;
+ CANMessage rx_msg;
+ Watchdog wdt;
+
wdt.kick(10.0);
pc1.baud(115200);
FanPump fanPump(&rxBuffer);
DC dc_dc(&fanPump, &rxBuffer);
+ PollSwitch pollSwitch(&rxBuffer);
fanPump.start_update();
+ dc_dc.start_update();
+ pollSwitch.start_update();
while(1)
{