Subversion Repositories FlightCtrl

Rev

Rev 1672 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1672 Rev 2050
1
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
 // + Copyright (c) Holger Buss, Ingo Busker
-
 
3
 // + Thanks to Marcel Haller (Lion) for the nice idea and first implementation
-
 
4
 // + Nur für den privaten Gebrauch
-
 
5
 // + www.MikroKopter.com
2
// + www.MikroKopter.com
6
 // + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
-
 
7
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
4
// + Software Nutzungsbedingungen (english version: see below)
8
 // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
5
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
 // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
6
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
 // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
7
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
 // + bzgl. der Nutzungsbedingungen aufzunehmen.
8
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
 // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
9
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
 // + Verkauf von Luftbildaufnahmen, usw.
-
 
14
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
 // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
11
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
16
 // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
12
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
17
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
18
 // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
14
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
19
 // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
15
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
20
 // + eindeutig als Ursprung verlinkt werden
16
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
21
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
22
 // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
18
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
23
 // + Benutzung auf eigene Gefahr
19
// + des Mitverschuldens offen.
-
 
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.
24
 // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
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
25
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
26
 // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
27
 // + mit unserer Zustimmung zulässig
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.
28
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
 // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
29
// + Software LICENSING TERMS
30
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
 // + Redistributions of source code (with or without modifications) must retain the above copyright notice,
31
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
32
 // + this list of conditions and the following disclaimer.
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
 // +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
33
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
34
 // +     from this software without specific prior written permission.
34
// + The Software may only be used with the Licensor's products.
35
 // +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
35
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
36
 // +     for non-commercial use (directly or indirectly)
-
 
37
 // +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
38
 // +     with our written permission
37
// + agreement shall be the property of the Licensor.
39
 // +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
40
 // +     clearly linked as origin
39
// + features that can be used to identify the program may not be altered or defaced by the customer.
41
 // +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// + The customer shall be responsible for taking reasonable precautions
42
 // +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
43
 // +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
44
 // +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
45
 // +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
46
 // +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
47
 // +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
48
 // +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
49
 // +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
50
 // +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
51
 // +  POSSIBILITY OF SUCH DAMAGE.
50
// + #### END OF LICENSING TERMS ####
-
 
51
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
52
 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
53
 
53
#include "capacity.h"
54
#include "capacity.h"
54
#include "twimaster.h"
55
#include "twimaster.h"
55
#include "main.h"
56
#include "main.h"
56
#include "timer0.h"
57
#include "timer0.h"
57
#include "analog.h"
58
#include "analog.h"
58
 
59
 
59
#define CAPACITY_UPDATE_INTERVAL 10 // 10 ms
60
#define CAPACITY_UPDATE_INTERVAL 10 // 10 ms
60
#define FC_OFFSET_CURRENT 5  // calculate with a current of 0.5A
61
#define FC_OFFSET_CURRENT 5  // calculate with a current of 0.5A
61
#define BL_OFFSET_CURRENT 2  // calculate with a current of 0.2A
62
#define BL_OFFSET_CURRENT 2  // calculate with a current of 0.2A
62
 
63
 
63
// global varialbles
64
// global varialbles
64
unsigned short update_timer =  0;
65
unsigned short update_timer =  0;
65
Capacity_t Capacity;
66
Capacity_t Capacity;
66
 
67
 
67
// initialize capacity calculation
68
// initialize capacity calculation
68
void Capacity_Init(void)
69
void Capacity_Init(void)
69
{
70
{
70
        Capacity.ActualCurrent = 0;
71
        Capacity.ActualCurrent = 0;
71
        Capacity.UsedCapacity = 0;
72
        Capacity.UsedCapacity = 0;
72
        Capacity.ActualPower = 0;
73
        Capacity.ActualPower = 0;
73
        Capacity.MinOfMaxPWM = 0;
74
        Capacity.MinOfMaxPWM = 0;
74
        update_timer = SetDelay(CAPACITY_UPDATE_INTERVAL);
75
        update_timer = SetDelay(CAPACITY_UPDATE_INTERVAL);
75
}
76
}
76
 
77
 
77
 
78
 
78
// called in main loop at a regular interval
79
// called in main loop at a regular interval
79
void Capacity_Update(void)
80
void Capacity_Update(void)
80
{
81
{
81
        unsigned short Current, SetSum; // max value will be 255 * 12 = 3060
82
        unsigned short Current, SetSum; // max value will be 255 * 12 = 3060
82
        static unsigned short SubCounter = 0;
83
        static unsigned short SubCounter = 0;
83
        static unsigned short CurrentOffset = 0;
84
        static unsigned short CurrentOffset = 0;
84
        static unsigned long SumCurrentOffset = 0;
85
        static unsigned long SumCurrentOffset = 0;
85
        unsigned char i, NumOfMotors, MinOfMaxPWM;
86
        unsigned char i, NumOfMotors, MinOfMaxPWM;
86
 
87
 
87
        if(CheckDelay(update_timer))
88
        if(CheckDelay(update_timer))
88
        {
89
        {
89
                update_timer += CAPACITY_UPDATE_INTERVAL; // do not use SetDelay to avoid timing leaks
90
                update_timer += CAPACITY_UPDATE_INTERVAL; // do not use SetDelay to avoid timing leaks
90
                // determine sum of all present BL currents and setpoints
91
                // determine sum of all present BL currents and setpoints
91
                Current = 0;
92
                Current = 0;
92
                SetSum = 0;
93
                SetSum = 0;
93
                NumOfMotors = 0;
94
                NumOfMotors = 0;
94
                MinOfMaxPWM = 255;
95
                MinOfMaxPWM = 255;
95
                for(i = 0; i < MAX_MOTORS; i++)
96
                for(i = 0; i < MAX_MOTORS; i++)
96
                {
97
                {
97
                        if(Motor[i].State & MOTOR_STATE_PRESENT_MASK)
98
                        if(Motor[i].State & MOTOR_STATE_PRESENT_MASK)
98
                        {
99
                        {
99
                                NumOfMotors++;
100
                                NumOfMotors++;
100
                                Current += (unsigned int)(Motor[i].Current);
101
                                Current += (unsigned int)(Motor[i].Current);
101
                                SetSum +=  (unsigned int)(Motor[i].SetPoint);
102
                                SetSum +=  (unsigned int)(Motor[i].SetPoint);
102
                                if(Motor[i].MaxPWM < MinOfMaxPWM) MinOfMaxPWM = Motor[i].MaxPWM;
103
                                if(Motor[i].MaxPWM < MinOfMaxPWM) MinOfMaxPWM = Motor[i].MaxPWM;
103
                        }
104
                        }
104
                }
105
                }
105
                Capacity.MinOfMaxPWM = MinOfMaxPWM;
106
                Capacity.MinOfMaxPWM = MinOfMaxPWM;
106
 
107
 
107
                if(SetSum == 0) // if all setpoints are 0
108
                if(SetSum == 0) // if all setpoints are 0
108
                { // determine offsets of motor currents
109
                { // determine offsets of motor currents
109
                        #define CURRENT_AVERAGE 8  // 8bit = 256 * 10 ms = 2.56s average time
110
                        #define CURRENT_AVERAGE 8  // 8bit = 256 * 10 ms = 2.56s average time
110
                        CurrentOffset = (unsigned int)(SumCurrentOffset>>CURRENT_AVERAGE);
111
                        CurrentOffset = (unsigned int)(SumCurrentOffset>>CURRENT_AVERAGE);
111
                        SumCurrentOffset -= CurrentOffset;
112
                        SumCurrentOffset -= CurrentOffset;
112
                        SumCurrentOffset += Current;
113
                        SumCurrentOffset += Current;
113
                        // after averaging set current to static offset
114
                        // after averaging set current to static offset
114
                        Current = FC_OFFSET_CURRENT;
115
                        Current = FC_OFFSET_CURRENT;
115
                }
116
                }
116
                else // some motors are running, includes also motor test condition, where "MotorRunning" is false
117
                else // some motors are running, includes also motor test condition, where "MotorRunning" is false
117
                {   // subtract offset
118
                {   // subtract offset
118
                        if(Current > CurrentOffset) Current -= CurrentOffset;
119
                        if(Current > CurrentOffset) Current -= CurrentOffset;
119
                        else Current = 0;
120
                        else Current = 0;
120
                        // add the FC and BL Offsets
121
                        // add the FC and BL Offsets
121
                        Current += FC_OFFSET_CURRENT + NumOfMotors * BL_OFFSET_CURRENT;
122
                        Current += FC_OFFSET_CURRENT + NumOfMotors * BL_OFFSET_CURRENT;
122
                }
123
                }
123
 
124
 
124
                // update actual Current
125
                // update actual Current
125
                Capacity.ActualCurrent = Current;
126
                Capacity.ActualCurrent = Current;
126
                // update actual Power
127
                // update actual Power
127
                if(Current < 255)       Capacity.ActualPower = (UBat * Current) / 100; // in W higher resolution
128
                if(Current < 255)       Capacity.ActualPower = (UBat * Current) / 100; // in W higher resolution
128
                else                            Capacity.ActualPower = (UBat * (Current/4)) / 25; // in W
129
                else                            Capacity.ActualPower = (UBat * (Current/4)) / 25; // in W
129
 
130
 
130
                // update used capacity
131
                // update used capacity
131
                SubCounter += Current;
132
                SubCounter += Current;
132
 
133
 
133
                // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL
134
                // 100mA * 1ms * CAPACITY_UPDATE_INTERVAL = 1 mA * 100 ms * CAPACITY_UPDATE_INTERVAL
134
                // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL)
135
                // = 1mA * 0.1s * CAPACITY_UPDATE_INTERVAL = 1mA * 1min / (600 / CAPACITY_UPDATE_INTERVAL)
135
                // = 1mAh / (36000 / CAPACITY_UPDATE_INTERVAL)
136
                // = 1mAh / (36000 / CAPACITY_UPDATE_INTERVAL)
136
                #define SUB_COUNTER_LIMIT (36000 / CAPACITY_UPDATE_INTERVAL)
137
                #define SUB_COUNTER_LIMIT (36000 / CAPACITY_UPDATE_INTERVAL)
137
                if(SubCounter > SUB_COUNTER_LIMIT)
138
                if(SubCounter > SUB_COUNTER_LIMIT)
138
                {
139
                {
139
                        Capacity.UsedCapacity++;                        // we have one mAh more
140
                        Capacity.UsedCapacity++;                        // we have one mAh more
140
                        SubCounter -= SUB_COUNTER_LIMIT;        // keep the remaining sub part
141
                        SubCounter -= SUB_COUNTER_LIMIT;        // keep the remaining sub part
141
                }
142
                }
142
        } // EOF check delay update timer
143
        } // EOF check delay update timer
143
}
144
}
144
 
145