Rev 1501 | Rev 1510 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1501 | Rev 1506 | ||
---|---|---|---|
Line 54... | Line 54... | ||
54 | #include "twimaster.h" |
54 | #include "twimaster.h" |
55 | #include "main.h" |
55 | #include "main.h" |
56 | #include "timer0.h" |
56 | #include "timer0.h" |
Line 57... | Line 57... | ||
57 | 57 | ||
58 | #define CAPACITY_UPDATE_INTERVAL 10 // 10 ms |
58 | #define CAPACITY_UPDATE_INTERVAL 10 // 10 ms |
- | 59 | unsigned char OwnConsumptionCurrent; // always calculate with a Current of 0,xA |
|
Line 59... | Line 60... | ||
59 | #define OFFSET_CURRENT 4 // always calculate with a Current of 0,4A |
60 | |
60 | 61 | ||
61 | // global varialbles |
62 | // global varialbles |
Line 73... | Line 74... | ||
73 | 74 | ||
74 | 75 | ||
75 | // called in main loop at a regular interval |
76 | // called in main loop at a regular interval |
76 | void Capacity_Update(void) |
77 | void Capacity_Update(void) |
77 | { |
78 | { |
78 | unsigned short Current, SetSum; // max value will be 255 * 12 = 3060 |
79 | unsigned short Current; // max value will be 255 * 12 = 3060 |
79 | static unsigned short SubCounter = 0; |
80 | static unsigned short SubCounter = 0; |
80 | static unsigned short CurrentOffset = 0; |
81 | static unsigned short CurrentOffset = 0; |
Line 81... | Line 82... | ||
81 | static unsigned long SumCurrentOffset = 0; |
82 | static unsigned long SumCurrentOffset = 0; |
82 | unsigned char i; |
83 | unsigned char i; |
83 | 84 | ||
84 | if(CheckDelay(update_timer)) |
- | |
85 | { |
85 | if(CheckDelay(update_timer)) |
86 | update_timer += CAPACITY_UPDATE_INTERVAL; // do not use SetDelay to avoid timing leaks |
86 | { |
87 | - | ||
88 | // determine sum of all present BL currents and setpoints |
87 | update_timer += CAPACITY_UPDATE_INTERVAL; // do not use SetDelay to avoid timing leaks |
89 | Current = 0; |
88 | // determine sum of all present BL currents and setpoints |
90 | SetSum = 0; |
89 | Current = 0; |
91 | for(i = 0; i < MAX_MOTORS; i++) |
90 | for(i = 0; i < MAX_MOTORS; i++) |
92 | { |
91 | { |
93 | if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) |
- | |
94 | { |
92 | if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) |
95 | Current += (int16_t)(Motor[i].Current); |
93 | { |
96 | SetSum += (int16_t)(Motor[i].SetPoint); |
- | |
97 | } |
94 | Current += (int16_t)(Motor[i].Current); |
98 | } |
95 | } |
99 | 96 | } |
|
100 | if(SetSum != 0) // if one of the present motors has a setpoint > 0 |
97 | if(MotorenEin) // if one of the present motors has a setpoint > 0 |
101 | { |
98 | { |
102 | if(Current > CurrentOffset) Capacity.ActualCurrent = Current - CurrentOffset; |
99 | if(Current > CurrentOffset) Capacity.ActualCurrent = Current - CurrentOffset; |
103 | else Capacity.ActualCurrent = 0; |
- | |
104 | // update used capacity |
- | |
105 | SubCounter += Capacity.ActualCurrent; |
- | |
106 | - | ||
107 | /* |
- | |
108 | // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL |
- | |
109 | // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL) |
- | |
110 | // = 1mAh / (36000 / CAPACITY_UPDATE_INTERVAL) |
- | |
111 | #define SUB_COUNTER_LIMIT (36000 / CAPACITY_UPDATE_INTERVAL) |
- | |
112 | if(SubCounter > SUB_COUNTER_LIMIT) |
- | |
113 | { |
- | |
114 | Capacity.UsedCapacity++; // we have one mAh more |
- | |
115 | SubCounter -= SUB_COUNTER_LIMIT; // keep the remaining sub part |
100 | else Capacity.ActualCurrent = 0; |
116 | } |
101 | Capacity.ActualCurrent += OwnConsumptionCurrent; |
117 | */ |
102 | // SubCounter += Capacity.ActualCurrent; |
118 | } |
103 | } |
119 | else // motors are stopped |
104 | else // motors are stopped |
120 | { // determine offsets of motor currents |
105 | { // determine offsets of motor currents |
121 | Capacity.ActualCurrent = 0; |
106 | Capacity.ActualCurrent = STATIC_CURRENT; |
122 | #define CURRENT_AVERAGE 256L // 256 * 10 ms = 2.56s average time |
107 | #define CURRENT_AVERAGE 256L // 256 * 10 ms = 2.56s average time |
123 | CurrentOffset = (unsigned short)(SumCurrentOffset/CURRENT_AVERAGE); |
108 | CurrentOffset = (unsigned short)(SumCurrentOffset/CURRENT_AVERAGE); |
- | 109 | SumCurrentOffset -= CurrentOffset; |
|
124 | SumCurrentOffset -= CurrentOffset; |
110 | SumCurrentOffset += Current; |
125 | SumCurrentOffset += Current; |
111 | } |
126 | } |
112 | // update used capacity |
127 | SubCounter += OFFSET_CURRENT; |
113 | SubCounter += Capacity.ActualCurrent; |
128 | // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL |
114 | // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL |
129 | // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL) |
115 | // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL) |