Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: CANBuffer mbed SystemManagement mbed-rtos
FanPump.cpp
00001 #include "mbed.h" 00002 #include "FanPump.h" 00003 00004 PwmOut pwmPins[PIN_NUM] = { 00005 PwmOut(P2_0), // pump 00006 PwmOut(P2_1), // fan 1 00007 PwmOut(P2_2), // fan 2 00008 PwmOut(P2_3), // fan 3 00009 }; 00010 00011 PinStatus pin_status[PIN_NUM]; 00012 CANBuffer *tx_Fan_Buffer; 00013 00014 FanPump::FanPump(CANBuffer *can){ 00015 for(int i = 0; i < PIN_NUM; i++){ 00016 pin_status[i].cur_duty = 0; 00017 pin_status[i].new_duty = 0; 00018 00019 pin_status[i].pin = &pwmPins[i]; 00020 pin_status[i].pin->period_ms(10); 00021 pin_status[i].pin->write(0.0); 00022 00023 pin_threads[i] = NULL; 00024 } 00025 00026 tx_Fan_Buffer = can; 00027 } 00028 00029 // this is not a member function. For some reason, thread does weird things 00030 // with functions in classes. Apparently pointers get messed up when pointing to 00031 // a function in a class 00032 void ramp_fan(void const *arg) 00033 { 00034 PinStatus *pin_stat = (PinStatus *)arg; 00035 unsigned char *cur_duty = &(pin_stat->cur_duty); 00036 unsigned char *new_duty = &(pin_stat->new_duty); 00037 00038 while(*cur_duty != *new_duty) 00039 { 00040 if(*new_duty > *cur_duty){ 00041 *cur_duty += 1; 00042 00043 if(*cur_duty > *new_duty) 00044 *cur_duty = *new_duty; 00045 } else if(*new_duty < *cur_duty){ 00046 *cur_duty -= 1; 00047 00048 if(*cur_duty < *new_duty) 00049 *cur_duty = *new_duty; 00050 } 00051 00052 pin_stat->pin->write((*cur_duty)*0.01); 00053 00054 Thread::wait(5); //1% duty cycle per 0.005 s 00055 } 00056 } 00057 00058 void FanPump::set_fan(FanSelect fan, unsigned char duty){ 00059 if((int)fan >= PIN_NUM || duty > 100) 00060 return; 00061 00062 free_pin(fan); 00063 00064 pin_status[fan].new_duty = duty; 00065 pin_threads[fan] = new Thread(ramp_fan, &pin_status[fan]); 00066 } 00067 00068 void FanPump::shutdown(FanSelect fan){ 00069 free_pin(fan); 00070 00071 pin_status[fan].cur_duty = 0; 00072 pin_status[fan].new_duty = 0; 00073 pin_status[fan].pin->write(0); 00074 } 00075 00076 void FanPump::shutdown_all(){ 00077 for(int i = 0; i < PIN_NUM; i++) 00078 FanPump::shutdown((FanSelect)i); 00079 } 00080 00081 void FanPump::free_pin(FanSelect fan){ 00082 if(pin_threads[fan] != NULL){ 00083 pin_threads[fan]->terminate(); 00084 free(pin_threads[fan]); 00085 } 00086 } 00087 00088 void update_fans(void const *arg){ 00089 char data[4] = {0}; 00090 while(1){ 00091 for(int i = 0; i < PIN_NUM; i++) 00092 { 00093 data[i] = pin_status[i].cur_duty; 00094 } 00095 00096 CANMessage txMessage(TX_FAN_ID, data, 4); 00097 tx_Fan_Buffer->txWrite(txMessage); 00098 00099 Thread::wait(100); // 10 Hz update 00100 } 00101 } 00102 00103 void FanPump::start_update() 00104 { 00105 Thread update_thread(update_fans); 00106 }
Generated on Sat Aug 13 2022 03:42:38 by
1.7.2