annotate Discovery/Src/bonex_mini.c @ 100:b364c75005bb kittz

Stable.fix.
author Dmitry Romanov <kitt@bk.ru>
date Tue, 27 Nov 2018 12:59:56 +0300
parents 5f11787b4f42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 ///////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 /// -*- coding: UTF-8 -*-
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 /// \file Discovery/Src/bonex_mini.c
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 /// \brief voltage to battery percentage based on bonex.c for BIS PCB
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 /// \author Heinrichs Weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 /// \date 26-March-2017
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 /// \details
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 /// $Id$
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ///////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 /// This program is free software: you can redistribute it and/or modify
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 /// it under the terms of the GNU General Public License as published by
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 /// the Free Software Foundation, either version 3 of the License, or
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 /// (at your option) any later version.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 /// This program is distributed in the hope that it will be useful,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 /// GNU General Public License for more details.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 /// You should have received a copy of the GNU General Public License
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 //////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 ##### CAN data #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 [..] is stored static in BONEX_CAN_Config
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 see example CAN_Networking for STM32303C_EVAL
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #include "bonex_mini.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 /* Private variables ---------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 enum
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 TYPE_ECOS = 0,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 TYPE_RS = 1,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 TYPE_MAX
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 const uint16_t loadVoltageInverted[TYPE_MAX][21] =
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 { // ECOS
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 0
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 },
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 { // RS
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 38000, // 0% >= index *5 ist Ergebnis Kapazit�t
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 38875, // 5%
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 39750, // 10%
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 40625,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 41500,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 42050,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 42600,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 43150,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 43700,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 44250,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 44800,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 45350,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 45900,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 46450,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 47000, // 70%
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 47550, // 75%
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 48100,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 48450, // 85%
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 48800,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 49150,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 49500, //100% , index = 20
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 uint8_t BONEX_mini_ResidualCapacityVoltageBased(float voltage_V, uint16_t ageInMilliSecondsSinceLast)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 static uint8_t capacityStorage = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 static uint32_t voltage_mV_storage_32bit = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 static uint16_t storageCounter = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 uint16_t voltage_mV = (uint16_t)(1000 * voltage_V);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 uint8_t calcNow = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 if(ageInMilliSecondsSinceLast < 2000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 voltage_mV_storage_32bit += voltage_mV;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 storageCounter++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 storageCounter = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 voltage_mV_storage_32bit = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 if(storageCounter >= 600)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 voltage_mV_storage_32bit /= storageCounter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 voltage_mV = (uint16_t)voltage_mV_storage_32bit;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 storageCounter = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 voltage_mV_storage_32bit = voltage_mV;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 calcNow = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 else if(storageCounter == 1) // value immediately but not called after 600 counter ;-)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 voltage_mV = (uint16_t)voltage_mV_storage_32bit;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 calcNow = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 if(calcNow)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 for(int i = 20; i>=0; i--)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 if(voltage_mV >= loadVoltageInverted[1][i])
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 capacityStorage = i*5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 return capacityStorage;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 uint8_t BONEX_mini_ResidualCapacityVoltageBased(float voltage_V, uint16_t ageInMilliSecondsSinceLast)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 static uint8_t capacityStorage = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 static uint16_t voltage_mV_storage[5] = {0,0,0,0,0}; // number six is used directly from voltage_mV
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 uint32_t voltage_mV = (uint32_t)(1000 * voltage_V);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 // if necessary reset container and return actual voltage_V as capacity
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 if(ageInMilliSecondsSinceLast > 2000)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 capacityStorage = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 for(int i = 0; i<5; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147 voltage_mV_storage[i] = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 // find storage container or, if full, use it as number six and recalc voltage_mV based on those six values
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 int ptr = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 do
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 ptr++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 } while ((ptr < 5) && voltage_mV_storage[ptr] != 0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 if(ptr == 5)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 for(int i = 0; i<5; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 voltage_mV += voltage_mV_storage[i];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 voltage_mV_storage[i] = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 voltage_mV += 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 voltage_mV /= 6;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 capacityStorage = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 voltage_mV_storage[ptr] = voltage_mV;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 // calc result if update necessary
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175 if(capacityStorage == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177 for(int i = 20; i>=0; i--)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
179 if(voltage_mV >= loadVoltageInverted[1][i])
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
180 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
181 capacityStorage = i*5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
184 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
186 return capacityStorage;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
187 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
188
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
189 #define ECOS_VMAX 290
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
190 #define ECOS_VMIN 195
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191 #define ECOS_STEP 5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
192
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
193 #define RS_VMAX 500
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
194 #define RS_VMIN 360
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
195 #define RS_STEP 5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
196
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
197 #define ECOS_LENGTH (((ECOS_VMAX - ECOS_VMIN) / ECOS_STEP) + 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
198 #define RS_LENGTH (((RS_VMAX - RS_VMIN) / RS_STEP) + 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199 #define MAX_LENGTH (ECOS_LENGTH>RS_LENGTH? ECOS_LENGTH:RS_LENGTH)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
200
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
201
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
202 typedef struct
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
203 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
204 uint8_t load[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
205 } load;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
206
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
207
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
208 const int32_t currentMaxLoad[TYPE_MAX] = { 17000,14000};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
209 const int32_t currentPartialLoad[TYPE_MAX] = { 1000, 1000};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
210 const uint16_t voltageCharged[TYPE_MAX] = { 280, 480};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
211 const uint16_t voltageMax[TYPE_MAX] = { ECOS_VMAX, RS_VMAX};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
212 const uint16_t voltageMin[TYPE_MAX] = { ECOS_VMIN, RS_VMIN};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
213 const uint8_t voltageSteps[TYPE_MAX] = { ECOS_STEP, RS_STEP};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
214 const uint8_t length[TYPE_MAX] = { ECOS_LENGTH, RS_LENGTH};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
215
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
216
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
217
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
218
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
219
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
220 const uint8_t loadVoltage[TYPE_MAX][MAX_LENGTH][3] =
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
221 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
222 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
223 // ECOS
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
224 // no,teil,voll
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
225 { 0, 5, 0}, // voltageMin 19.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
226 { 0, 5, 0}, // voltageMin + 0.5V
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
227 { 0, 5, 0}, // 20.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
228 { 5, 5, 5}, // 21
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
229 { 5, 5, 5}, // 21.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
230 { 5, 10, 10}, // 22
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
231 { 5, 10, 15}, // 22.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
232 { 10, 15, 30}, // 23
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
233 { 20, 30, 45}, // 23.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
234 { 30, 40, 60}, // 24
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
235 { 40, 50, 65}, // 24.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
236 { 50, 60, 75}, // 25
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
237 { 60, 70, 80}, // 25.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
238 { 70, 80, 85}, // 26
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
239 { 80, 90, 85}, // 26.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
240 { 85, 90, 90}, // 27
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
241 { 90, 95, 90}, // 27.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
242 { 95, 95, 95}, // 28
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
243 {100,100,100}, // 28.5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
244 {100,100,100}, // voltageMax 29
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
245 },
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
246 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
247 // RS
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
248 // no,teil,voll
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
249 { 0, 0, 0}, // voltageMin 36 V
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
250 { 2, 0, 2}, // voltageMin + 0.5V
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
251 { 5, 0, 5}, // 37
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
252 { 5, 2, 5}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
253 { 5, 5, 5}, // 38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
254 { 5, 5, 10}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
255 { 5, 5, 15}, // 39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
256 { 7, 7, 17}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
257 { 10, 10, 20}, // 40
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
258 { 15, 12, 27}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
259 { 20, 15, 35}, // 41
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
260 { 27, 22, 42}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
261 { 35, 30, 50}, // 42
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
262 { 42, 37, 55}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
263 { 50, 45, 60}, // 43
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
264 { 55, 50, 67}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
265 { 60, 55, 75}, // 44
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
266 { 67, 57, 80}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
267 { 75, 60, 85}, // 45
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
268 { 77, 65, 87}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
269 { 80, 70, 90}, // 46
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
270 { 85, 75, 90}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
271 { 90, 80, 90}, // 47
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
272 { 92, 85, 92}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
273 { 95, 90, 95}, // 48
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
274 { 95, 92, 97}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
275 { 95, 95,100}, // 49
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
276 { 97, 97,100}, //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
277 {100,100,100} // 50
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
278 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
279 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
280
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
281
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
282 void BONEX_calc_new_ResidualCapacity(uint8_t *residualC, uint32_t voltage_mV, int32_t current_mA, uint8_t scooterType) // as in BIS
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
283 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
284 uint8_t actualLoad = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
285 uint8_t remainder = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
286 uint32_t voltagePointer = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
287
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
288 if(voltage_mV == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
289 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
290
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
291 if(scooterType >= TYPE_MAX)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
292 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
293
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
294 if(voltage_mV < (voltageMin[scooterType] * 100))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
295 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
296 *residualC = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
297 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
298 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
299 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
300 if(voltage_mV >= (voltageMax[scooterType] * 100))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
301 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
302 *residualC = 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
303 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
304 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
305 else // check if charged and reset residualC for further calculation
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
306 if(voltage_mV >= (voltageCharged[scooterType] * 100))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
307 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
308 *residualC = 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
309 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
310 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
311
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
312 // define the line we are working
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
313 if(current_mA >= currentMaxLoad[scooterType])
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
314 actualLoad = 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
315 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
316 if(current_mA >= currentPartialLoad[scooterType])
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
317 actualLoad = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
318 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
319 actualLoad = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
320
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
321 voltagePointer = (voltage_mV - ((uint32_t)(voltageMin[scooterType])) * 100) / (voltageSteps[scooterType] * 100);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
322
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
323 // should be checked with if(... >= voltageMax) but for safety
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
324 if(voltagePointer >= length[scooterType])
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
325 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
326 *residualC = 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
327 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
328 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
329
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
330 if(loadVoltage[scooterType][voltagePointer][actualLoad] < *residualC)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
331 *residualC = loadVoltage[scooterType][voltagePointer][actualLoad];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
332 else if(loadVoltage[scooterType][voltagePointer][actualLoad] >= (*residualC + 20))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
333 *residualC = loadVoltage[scooterType][voltagePointer][actualLoad];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
334
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
335 // steps of 5
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
336 remainder = (*residualC)%5;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
337 if(remainder)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
338 *residualC += (5 - remainder);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
339
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
340 // safety
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
341 if(*residualC > 100)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
342 *residualC = 100;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
343
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
344 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
345 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
346 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
347