System Management code

Dependencies:   CANBuffer mbed SystemManagement mbed-rtos

Dependents:   SystemManagement

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

Committer:
martydd3
Date:
Fri Oct 10 20:59:36 2014 +0000
Revision:
7:5f6e31faa08e
Parent:
6:6a04210a3f4f
PollSwitch code;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martydd3 6:6a04210a3f4f 1 #include "mbed.h"
martydd3 6:6a04210a3f4f 2 #include "FanPump.h"
martydd3 6:6a04210a3f4f 3
martydd3 6:6a04210a3f4f 4 PwmOut pwmPins[PIN_NUM] = {
martydd3 7:5f6e31faa08e 5 PwmOut(P2_0), // pump
martydd3 7:5f6e31faa08e 6 PwmOut(P2_1), // fan 1
martydd3 7:5f6e31faa08e 7 PwmOut(P2_2), // fan 2
martydd3 7:5f6e31faa08e 8 PwmOut(P2_3), // fan 3
martydd3 6:6a04210a3f4f 9 };
martydd3 6:6a04210a3f4f 10
martydd3 6:6a04210a3f4f 11 PinStatus pin_status[PIN_NUM];
martydd3 7:5f6e31faa08e 12 CANBuffer *tx_Fan_Buffer;
martydd3 6:6a04210a3f4f 13
martydd3 6:6a04210a3f4f 14 FanPump::FanPump(CANBuffer *can){
martydd3 6:6a04210a3f4f 15 for(int i = 0; i < PIN_NUM; i++){
martydd3 6:6a04210a3f4f 16 pin_status[i].cur_duty = 0;
martydd3 6:6a04210a3f4f 17 pin_status[i].new_duty = 0;
martydd3 6:6a04210a3f4f 18
martydd3 6:6a04210a3f4f 19 pin_status[i].pin = &pwmPins[i];
martydd3 6:6a04210a3f4f 20 pin_status[i].pin->period_ms(10);
martydd3 6:6a04210a3f4f 21 pin_status[i].pin->write(0.0);
martydd3 6:6a04210a3f4f 22
martydd3 6:6a04210a3f4f 23 pin_threads[i] = NULL;
martydd3 6:6a04210a3f4f 24 }
martydd3 6:6a04210a3f4f 25
martydd3 7:5f6e31faa08e 26 tx_Fan_Buffer = can;
martydd3 6:6a04210a3f4f 27 }
martydd3 6:6a04210a3f4f 28
martydd3 6:6a04210a3f4f 29 // this is not a member function. For some reason, thread does weird things
martydd3 6:6a04210a3f4f 30 // with functions in classes. Apparently pointers get messed up when pointing to
martydd3 6:6a04210a3f4f 31 // a function in a class
martydd3 6:6a04210a3f4f 32 void ramp_fan(void const *arg)
martydd3 6:6a04210a3f4f 33 {
martydd3 6:6a04210a3f4f 34 PinStatus *pin_stat = (PinStatus *)arg;
martydd3 6:6a04210a3f4f 35 unsigned char *cur_duty = &(pin_stat->cur_duty);
martydd3 6:6a04210a3f4f 36 unsigned char *new_duty = &(pin_stat->new_duty);
martydd3 6:6a04210a3f4f 37
martydd3 6:6a04210a3f4f 38 while(*cur_duty != *new_duty)
martydd3 6:6a04210a3f4f 39 {
martydd3 6:6a04210a3f4f 40 if(*new_duty > *cur_duty){
martydd3 6:6a04210a3f4f 41 *cur_duty += 1;
martydd3 6:6a04210a3f4f 42
martydd3 6:6a04210a3f4f 43 if(*cur_duty > *new_duty)
martydd3 6:6a04210a3f4f 44 *cur_duty = *new_duty;
martydd3 6:6a04210a3f4f 45 } else if(*new_duty < *cur_duty){
martydd3 6:6a04210a3f4f 46 *cur_duty -= 1;
martydd3 6:6a04210a3f4f 47
martydd3 6:6a04210a3f4f 48 if(*cur_duty < *new_duty)
martydd3 6:6a04210a3f4f 49 *cur_duty = *new_duty;
martydd3 6:6a04210a3f4f 50 }
martydd3 6:6a04210a3f4f 51
martydd3 6:6a04210a3f4f 52 pin_stat->pin->write((*cur_duty)*0.01);
martydd3 6:6a04210a3f4f 53
martydd3 6:6a04210a3f4f 54 Thread::wait(5); //1% duty cycle per 0.005 s
martydd3 6:6a04210a3f4f 55 }
martydd3 6:6a04210a3f4f 56 }
martydd3 6:6a04210a3f4f 57
martydd3 6:6a04210a3f4f 58 void FanPump::set_fan(FanSelect fan, unsigned char duty){
martydd3 6:6a04210a3f4f 59 if((int)fan >= PIN_NUM || duty > 100)
martydd3 6:6a04210a3f4f 60 return;
martydd3 6:6a04210a3f4f 61
martydd3 6:6a04210a3f4f 62 free_pin(fan);
martydd3 6:6a04210a3f4f 63
martydd3 6:6a04210a3f4f 64 pin_status[fan].new_duty = duty;
martydd3 6:6a04210a3f4f 65 pin_threads[fan] = new Thread(ramp_fan, &pin_status[fan]);
martydd3 6:6a04210a3f4f 66 }
martydd3 6:6a04210a3f4f 67
martydd3 6:6a04210a3f4f 68 void FanPump::shutdown(FanSelect fan){
martydd3 6:6a04210a3f4f 69 free_pin(fan);
martydd3 6:6a04210a3f4f 70
martydd3 6:6a04210a3f4f 71 pin_status[fan].cur_duty = 0;
martydd3 6:6a04210a3f4f 72 pin_status[fan].new_duty = 0;
martydd3 6:6a04210a3f4f 73 pin_status[fan].pin->write(0);
martydd3 6:6a04210a3f4f 74 }
martydd3 6:6a04210a3f4f 75
martydd3 6:6a04210a3f4f 76 void FanPump::shutdown_all(){
martydd3 6:6a04210a3f4f 77 for(int i = 0; i < PIN_NUM; i++)
martydd3 6:6a04210a3f4f 78 FanPump::shutdown((FanSelect)i);
martydd3 6:6a04210a3f4f 79 }
martydd3 6:6a04210a3f4f 80
martydd3 6:6a04210a3f4f 81 void FanPump::free_pin(FanSelect fan){
martydd3 6:6a04210a3f4f 82 if(pin_threads[fan] != NULL){
martydd3 6:6a04210a3f4f 83 pin_threads[fan]->terminate();
martydd3 6:6a04210a3f4f 84 free(pin_threads[fan]);
martydd3 6:6a04210a3f4f 85 }
martydd3 6:6a04210a3f4f 86 }
martydd3 6:6a04210a3f4f 87
martydd3 6:6a04210a3f4f 88 void update_fans(void const *arg){
martydd3 6:6a04210a3f4f 89 char data[4] = {0};
martydd3 6:6a04210a3f4f 90 while(1){
martydd3 6:6a04210a3f4f 91 for(int i = 0; i < PIN_NUM; i++)
martydd3 6:6a04210a3f4f 92 {
martydd3 6:6a04210a3f4f 93 data[i] = pin_status[i].cur_duty;
martydd3 6:6a04210a3f4f 94 }
martydd3 6:6a04210a3f4f 95
martydd3 6:6a04210a3f4f 96 CANMessage txMessage(TX_FAN_ID, data, 4);
martydd3 7:5f6e31faa08e 97 tx_Fan_Buffer->txWrite(txMessage);
martydd3 6:6a04210a3f4f 98
martydd3 6:6a04210a3f4f 99 Thread::wait(100); // 10 Hz update
martydd3 6:6a04210a3f4f 100 }
martydd3 6:6a04210a3f4f 101 }
martydd3 6:6a04210a3f4f 102
martydd3 6:6a04210a3f4f 103 void FanPump::start_update()
martydd3 6:6a04210a3f4f 104 {
martydd3 6:6a04210a3f4f 105 Thread update_thread(update_fans);
martydd3 6:6a04210a3f4f 106 }