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: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
CurrentMonitor/CurrentMonitor.cpp@10:db13782f05d9, 2014-10-19 (annotated)
- Committer:
- martydd3
- Date:
- Sun Oct 19 22:46:46 2014 +0000
- Revision:
- 10:db13782f05d9
Copied Kiran's calculations for monitoring batttery currents; To do: Test on car, Xbees, 10 Hz Can Message (what data is needed?)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martydd3 | 10:db13782f05d9 | 1 | #include "mbed.h" |
| martydd3 | 10:db13782f05d9 | 2 | #include "CurrentMonitor.h" |
| martydd3 | 10:db13782f05d9 | 3 | #include "Store_RTC.h" |
| martydd3 | 10:db13782f05d9 | 4 | |
| martydd3 | 10:db13782f05d9 | 5 | #define MSEC_HRS 2.77778e-7 |
| martydd3 | 10:db13782f05d9 | 6 | #define DC_DC_ISENSE_OFFSET_V 0.5 |
| martydd3 | 10:db13782f05d9 | 7 | #define DC_DC_ISENSE_INCREMENT 0.133 |
| martydd3 | 10:db13782f05d9 | 8 | |
| martydd3 | 10:db13782f05d9 | 9 | #define BAT_ISENSE_OFFSET_V 1.65 |
| martydd3 | 10:db13782f05d9 | 10 | #define BAT_ISENSE_INCREMENT 0.5297 |
| martydd3 | 10:db13782f05d9 | 11 | |
| martydd3 | 10:db13782f05d9 | 12 | CANBuffer *tx_Current_Buffer; |
| martydd3 | 10:db13782f05d9 | 13 | |
| martydd3 | 10:db13782f05d9 | 14 | double BATmA_Hr; |
| martydd3 | 10:db13782f05d9 | 15 | float DCA_msec,BATA_msec; |
| martydd3 | 10:db13782f05d9 | 16 | float Bat_I_Ratio,DC_I_Ratio; |
| martydd3 | 10:db13782f05d9 | 17 | |
| martydd3 | 10:db13782f05d9 | 18 | AnalogIn BatISense(p19); |
| martydd3 | 10:db13782f05d9 | 19 | AnalogIn DCSense(p20); |
| martydd3 | 10:db13782f05d9 | 20 | |
| martydd3 | 10:db13782f05d9 | 21 | RTCStore store; |
| martydd3 | 10:db13782f05d9 | 22 | |
| martydd3 | 10:db13782f05d9 | 23 | union converter{ |
| martydd3 | 10:db13782f05d9 | 24 | float data; |
| martydd3 | 10:db13782f05d9 | 25 | char ch[4]; |
| martydd3 | 10:db13782f05d9 | 26 | } convert; |
| martydd3 | 10:db13782f05d9 | 27 | |
| martydd3 | 10:db13782f05d9 | 28 | CurrentMonitor::CurrentMonitor(CANBuffer *can){ |
| martydd3 | 10:db13782f05d9 | 29 | tx_Current_Buffer = can; |
| martydd3 | 10:db13782f05d9 | 30 | } |
| martydd3 | 10:db13782f05d9 | 31 | |
| martydd3 | 10:db13782f05d9 | 32 | void update_current(const void *arg){ |
| martydd3 | 10:db13782f05d9 | 33 | char data[4] = {0}; |
| martydd3 | 10:db13782f05d9 | 34 | while(1){ |
| martydd3 | 10:db13782f05d9 | 35 | float bat_reading = store.read(1); |
| martydd3 | 10:db13782f05d9 | 36 | convert.data = bat_reading; |
| martydd3 | 10:db13782f05d9 | 37 | |
| martydd3 | 10:db13782f05d9 | 38 | data[0] = convert.ch[0]; |
| martydd3 | 10:db13782f05d9 | 39 | data[1] = convert.ch[1]; |
| martydd3 | 10:db13782f05d9 | 40 | data[2] = convert.ch[2]; |
| martydd3 | 10:db13782f05d9 | 41 | data[3] = convert.ch[3]; |
| martydd3 | 10:db13782f05d9 | 42 | |
| martydd3 | 10:db13782f05d9 | 43 | CANMessage txMessage(TX_CURRENT_ID, data, 4); |
| martydd3 | 10:db13782f05d9 | 44 | tx_Current_Buffer->txWrite(txMessage); |
| martydd3 | 10:db13782f05d9 | 45 | |
| martydd3 | 10:db13782f05d9 | 46 | Thread::wait(100); //10 Hz update |
| martydd3 | 10:db13782f05d9 | 47 | } |
| martydd3 | 10:db13782f05d9 | 48 | } |
| martydd3 | 10:db13782f05d9 | 49 | |
| martydd3 | 10:db13782f05d9 | 50 | void monitor_current(const void *arg){ |
| martydd3 | 10:db13782f05d9 | 51 | while(1){ |
| martydd3 | 10:db13782f05d9 | 52 | Bat_I_Ratio=BatISense.read(); |
| martydd3 | 10:db13782f05d9 | 53 | BATA_msec=(((Bat_I_Ratio*3.3) - BAT_ISENSE_OFFSET_V)/BAT_ISENSE_INCREMENT); |
| martydd3 | 10:db13782f05d9 | 54 | BATmA_Hr+=(BATA_msec*MSEC_HRS); |
| martydd3 | 10:db13782f05d9 | 55 | store.write(BATmA_Hr,0); |
| martydd3 | 10:db13782f05d9 | 56 | |
| martydd3 | 10:db13782f05d9 | 57 | DC_I_Ratio=DCSense.read(); |
| martydd3 | 10:db13782f05d9 | 58 | DCA_msec=(((DC_I_Ratio*3.3) - DC_DC_ISENSE_OFFSET_V)/DC_DC_ISENSE_INCREMENT); |
| martydd3 | 10:db13782f05d9 | 59 | store.write(DCA_msec,1); |
| martydd3 | 10:db13782f05d9 | 60 | |
| martydd3 | 10:db13782f05d9 | 61 | Thread::wait(1); //1 Khz coulomb counter |
| martydd3 | 10:db13782f05d9 | 62 | } |
| martydd3 | 10:db13782f05d9 | 63 | } |
| martydd3 | 10:db13782f05d9 | 64 | |
| martydd3 | 10:db13782f05d9 | 65 | void CurrentMonitor::start_update(){ |
| martydd3 | 10:db13782f05d9 | 66 | Thread monitor_thread(monitor_current); |
| martydd3 | 10:db13782f05d9 | 67 | Thread update_thread(update_current); |
| martydd3 | 10:db13782f05d9 | 68 | } |
