Rev 2389 | Rev 2418 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2389 | Rev 2391 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + www.MikroKopter.com |
2 | // + www.MikroKopter.com |
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // + Software Nutzungsbedingungen (english version: see below) |
4 | // + Software Nutzungsbedingungen (english version: see below) |
5 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
5 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
6 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
6 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
7 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
7 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
8 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
8 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
9 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
9 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
11 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
12 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
12 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
13 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
13 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
14 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
14 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
15 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
15 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
16 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
16 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
17 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
17 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
18 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
18 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
19 | // + des Mitverschuldens offen. |
19 | // + des Mitverschuldens offen. |
20 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
20 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
21 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
21 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
22 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
22 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
23 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
23 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
24 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
24 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
25 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
25 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
26 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
26 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
27 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
27 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // + Software LICENSING TERMS |
29 | // + Software LICENSING TERMS |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
31 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
32 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
32 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
33 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
33 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
34 | // + The Software may only be used with the Licensor's products. |
34 | // + The Software may only be used with the Licensor's products. |
35 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
35 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
36 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
36 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
37 | // + agreement shall be the property of the Licensor. |
37 | // + agreement shall be the property of the Licensor. |
38 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
38 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
39 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
39 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
40 | // + The customer shall be responsible for taking reasonable precautions |
40 | // + The customer shall be responsible for taking reasonable precautions |
41 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
41 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
42 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
42 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
43 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
43 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
44 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
44 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
45 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
45 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
46 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
46 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
47 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
47 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
48 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
48 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
49 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
49 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
50 | // + #### END OF LICENSING TERMS #### |
50 | // + #### END OF LICENSING TERMS #### |
51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
53 | 53 | ||
54 | #include "capacity.h" |
54 | #include "capacity.h" |
55 | #include "twimaster.h" |
55 | #include "twimaster.h" |
56 | #include "main.h" |
56 | #include "main.h" |
57 | #include "timer0.h" |
57 | #include "timer0.h" |
58 | #include "analog.h" |
58 | #include "analog.h" |
59 | 59 | ||
60 | //#define CAPACITY_UPDATE_INTERVAL 10 // 10 ms |
60 | //#define CAPACITY_UPDATE_INTERVAL 10 // 10 ms |
61 | #define CAPACITY_UPDATE_INTERVAL 50 // 50 ms |
61 | #define CAPACITY_UPDATE_INTERVAL 50 // 50 ms |
62 | #define FC_OFFSET_CURRENT 5 // calculate with a current of 0.5A |
62 | #define FC_OFFSET_CURRENT 5 // calculate with a current of 0.5A |
63 | #define BL_OFFSET_CURRENT 2 // calculate with a current of 0.2A |
63 | #define BL_OFFSET_CURRENT 2 // calculate with a current of 0.2A |
64 | 64 | ||
65 | // global varialbles |
65 | // global varialbles |
66 | unsigned short update_timer = 0; |
66 | unsigned short update_timer = 0; |
67 | Capacity_t Capacity; |
67 | Capacity_t Capacity; |
68 | 68 | ||
69 | // initialize capacity calculation |
69 | // initialize capacity calculation |
70 | void Capacity_Init(void) |
70 | void Capacity_Init(void) |
71 | { |
71 | { |
72 | Capacity.ActualCurrent = 0; |
72 | Capacity.ActualCurrent = 0; |
73 | Capacity.UsedCapacity = 0; |
73 | Capacity.UsedCapacity = 0; |
74 | Capacity.ActualPower = 0; |
74 | Capacity.ActualPower = 0; |
75 | Capacity.MinOfMaxPWM = 0; |
75 | Capacity.MinOfMaxPWM = 0; |
76 | update_timer = SetDelay(CAPACITY_UPDATE_INTERVAL); |
76 | update_timer = SetDelay(CAPACITY_UPDATE_INTERVAL); |
77 | } |
77 | } |
78 | 78 | ||
79 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
79 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
80 | // + extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55) |
80 | // + extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55) |
81 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
81 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
82 | unsigned int BL3_Current(unsigned char who) // in 0,1A |
82 | unsigned int BL3_Current(unsigned char who) // in 0,1A |
83 | { |
83 | { |
- | 84 | if(Motor[who].Current == 255) return(0); // invalid |
|
84 | if(Motor[who].Current <= 200) return(Motor[who].Current); |
85 | if(Motor[who].Current <= 200) return(Motor[who].Current); |
85 | else |
86 | else |
86 | { |
87 | { |
87 | if(Motor[who].Version & MOTOR_STATE_BL30) return(200 + 10 * ((unsigned int)Motor[who].Current-200)); |
88 | if(Motor[who].Version & MOTOR_STATE_BL30) return(200 + 10 * ((unsigned int)Motor[who].Current-200)); |
88 | else return(Motor[who].Current); |
89 | else return(Motor[who].Current); |
89 | } |
90 | } |
90 | } |
91 | } |
91 | 92 | ||
92 | // called in main loop at a regular interval |
93 | // called in main loop at a regular interval |
93 | void Capacity_Update(void) |
94 | void Capacity_Update(void) |
94 | { |
95 | { |
95 | unsigned short Current, SetSum; // max value will be 255 * 12 = 3060 |
96 | unsigned short Current, SetSum; // max value will be 255 * 12 = 3060 |
96 | static unsigned short SubCounter = 0; |
97 | static unsigned short SubCounter = 0; |
97 | static unsigned short CurrentOffset = 0; |
98 | static unsigned short CurrentOffset = 0; |
98 | static unsigned long SumCurrentOffset = 0; |
99 | static unsigned long SumCurrentOffset = 0; |
99 | unsigned char i, NumOfMotors, MinOfMaxPWM; |
100 | unsigned char i, NumOfMotors, MinOfMaxPWM; |
100 | 101 | ||
101 | if(CheckDelay(update_timer)) |
102 | if(CheckDelay(update_timer)) |
102 | { |
103 | { |
103 | update_timer += CAPACITY_UPDATE_INTERVAL; // do not use SetDelay to avoid timing leaks |
104 | update_timer += CAPACITY_UPDATE_INTERVAL; // do not use SetDelay to avoid timing leaks |
104 | // determine sum of all present BL currents and setpoints |
105 | // determine sum of all present BL currents and setpoints |
105 | Current = 0; |
106 | Current = 0; |
106 | SetSum = 0; |
107 | SetSum = 0; |
107 | NumOfMotors = 0; |
108 | NumOfMotors = 0; |
108 | MinOfMaxPWM = 255; |
109 | MinOfMaxPWM = 255; |
109 | for(i = 0; i < MAX_MOTORS; i++) |
110 | for(i = 0; i < MAX_MOTORS; i++) |
110 | { |
111 | { |
111 | if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) |
112 | if(Motor[i].State & MOTOR_STATE_PRESENT_MASK/* && Mixer.Motor[i][MIX_GAS]*/) |
112 | { |
113 | { |
113 | NumOfMotors++; |
114 | NumOfMotors++; |
114 | if(Motor[i].Current > 200) |
115 | if(Motor[i].Current > 200) |
115 | { |
116 | { |
116 | Current += BL3_Current(i); // extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55) |
117 | Current += BL3_Current(i); // extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55) |
117 | } |
118 | } |
118 | else Current += (unsigned int)(Motor[i].Current); |
119 | else Current += (unsigned int)(Motor[i].Current); |
119 | SetSum += (unsigned int)(Motor[i].SetPoint); |
120 | SetSum += (unsigned int)(Motor[i].SetPoint); |
120 | if(Motor[i].MaxPWM < MinOfMaxPWM) MinOfMaxPWM = Motor[i].MaxPWM; |
121 | if(Motor[i].MaxPWM < MinOfMaxPWM) MinOfMaxPWM = Motor[i].MaxPWM; |
121 | } |
122 | } |
122 | } |
123 | } |
123 | Capacity.MinOfMaxPWM = MinOfMaxPWM; |
124 | Capacity.MinOfMaxPWM = MinOfMaxPWM; |
124 | 125 | ||
125 | if(SetSum == 0) // if all setpoints are 0 |
126 | if(SetSum == 0) // if all setpoints are 0 |
126 | { // determine offsets of motor currents |
127 | { // determine offsets of motor currents |
127 | #define CURRENT_AVERAGE 8 // 8bit = 256 * 10 ms = 2.56s average time |
128 | #define CURRENT_AVERAGE 8 // 8bit = 256 * 10 ms = 2.56s average time |
128 | CurrentOffset = (unsigned int)(SumCurrentOffset>>CURRENT_AVERAGE); |
129 | CurrentOffset = (unsigned int)(SumCurrentOffset>>CURRENT_AVERAGE); |
129 | SumCurrentOffset -= CurrentOffset; |
130 | SumCurrentOffset -= CurrentOffset; |
130 | SumCurrentOffset += Current; |
131 | SumCurrentOffset += Current; |
131 | // after averaging set current to static offset |
132 | // after averaging set current to static offset |
132 | Current = FC_OFFSET_CURRENT; |
133 | Current = FC_OFFSET_CURRENT; |
133 | } |
134 | } |
134 | else // some motors are running, includes also motor test condition, where "MotorRunning" is false |
135 | else // some motors are running, includes also motor test condition, where "MotorRunning" is false |
135 | { // subtract offset |
136 | { // subtract offset |
136 | if(Current > CurrentOffset) Current -= CurrentOffset; |
137 | if(Current > CurrentOffset) Current -= CurrentOffset; |
137 | else Current = 0; |
138 | else Current = 0; |
138 | // add the FC and BL Offsets |
139 | // add the FC and BL Offsets |
139 | Current += FC_OFFSET_CURRENT + NumOfMotors * BL_OFFSET_CURRENT; |
140 | Current += FC_OFFSET_CURRENT + NumOfMotors * BL_OFFSET_CURRENT; |
140 | } |
141 | } |
141 | 142 | ||
142 | // update actual Current |
143 | // update actual Current |
143 | Capacity.ActualCurrent = Current; |
144 | Capacity.ActualCurrent = Current; |
144 | // update actual Power |
145 | // update actual Power |
145 | if(Current < 255) Capacity.ActualPower = (UBat * Current) / 100; // in W higher resolution |
146 | if(Current < 255) Capacity.ActualPower = (UBat * Current) / 100; // in W higher resolution |
146 | else Capacity.ActualPower = (UBat * (Current/4)) / 25; // in W |
147 | else Capacity.ActualPower = (UBat * (Current/4)) / 25; // in W |
147 | 148 | ||
148 | // update used capacity |
149 | // update used capacity |
149 | SubCounter += Current; |
150 | SubCounter += Current; |
150 | 151 | ||
151 | // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL |
152 | // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL |
152 | // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL) |
153 | // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL) |
153 | // = 1mAh / (36000 / CAPACITY_UPDATE_INTERVAL) |
154 | // = 1mAh / (36000 / CAPACITY_UPDATE_INTERVAL) |
154 | #define SUB_COUNTER_LIMIT (36000 / CAPACITY_UPDATE_INTERVAL) |
155 | #define SUB_COUNTER_LIMIT (36000 / CAPACITY_UPDATE_INTERVAL) |
155 | while(SubCounter > SUB_COUNTER_LIMIT) |
156 | while(SubCounter > SUB_COUNTER_LIMIT) |
156 | { |
157 | { |
157 | Capacity.UsedCapacity++; // we have one mAh more |
158 | Capacity.UsedCapacity++; // we have one mAh more |
158 | SubCounter -= SUB_COUNTER_LIMIT; // keep the remaining sub part |
159 | SubCounter -= SUB_COUNTER_LIMIT; // keep the remaining sub part |
159 | } |
160 | } |
160 | } // EOF check delay update timer |
161 | } // EOF check delay update timer |
161 | } |
162 | } |
162 | 163 |