Skip to content

Commit 35745bb

Browse files
committed
Fixes in EVSE FindTargets to remove signed/unsigned comparison. Calculation of charging time was out by factor of 10.
1 parent 11b5d9c commit 35745bb

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

examples/energy-management-app/energy-management-common/include/EnergyEvseDelegateImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ CHIP_ERROR GetDayOfWeekNow(uint8_t & dayOfWeekMap);
5757
*/
5858
uint8_t GetDayOfWeekUnixEpoch(time_t unixEpoch);
5959

60-
CHIP_ERROR GetMinutesPastMidnight(uint32_t & minutesPastMidnight);
60+
CHIP_ERROR GetMinutesPastMidnight(uint16_t & minutesPastMidnight);
6161

6262
namespace chip {
6363
namespace app {

examples/energy-management-app/energy-management-common/src/EVSEManufacturerImpl.cpp

+20-15
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,18 @@ CHIP_ERROR EVSEManufacturer::Shutdown()
103103
return CHIP_NO_ERROR;
104104
}
105105

106-
CHIP_ERROR FindNextTarget(const uint8_t dayOfWeekMap, uint32_t minutesPastMidnight_m, DataModel::Nullable<uint32_t> & targetTime_m,
107-
DataModel::Nullable<Percent> & targetSoC, DataModel::Nullable<int64_t> & addedEnergy_mWh)
106+
CHIP_ERROR FindNextTarget(const uint8_t dayOfWeekMap, uint16_t minutesPastMidnightNow_m,
107+
DataModel::Nullable<uint32_t> & targetTime_m, DataModel::Nullable<Percent> & targetSoC,
108+
DataModel::Nullable<int64_t> & addedEnergy_mWh)
108109
{
109110

110111
CHIP_ERROR err = CHIP_NO_ERROR;
111112

112113
EvseTargetEntry chargingTargetScheduleEntry;
113114
EnergyEvse::Structs::ChargingTargetScheduleStruct::Type entry;
114115

115-
int32_t minTimeToTarget_m = 1440; // 24 hours
116-
int32_t timeToTarget_m;
116+
uint16_t minTimeToTarget_m = 1440; // 24 hours
117+
uint16_t timeToTarget_m;
117118
bool bFound = false;
118119

119120
EVSEManufacturer * mn = GetEvseManufacturer();
@@ -133,18 +134,22 @@ CHIP_ERROR FindNextTarget(const uint8_t dayOfWeekMap, uint32_t minutesPastMidnig
133134
// We've found today's schedule
134135
for (const EvseChargingTarget & chargingTarget : chargingTargetScheduleEntry.dailyChargingTargets)
135136
{
136-
timeToTarget_m = chargingTarget.targetTimeMinutesPastMidnight - minutesPastMidnight_m;
137-
if (timeToTarget_m < 0)
137+
if (chargingTarget.targetTimeMinutesPastMidnight < minutesPastMidnightNow_m)
138138
{
139139
// This target is in the past - ignore it
140140
continue;
141141
}
142142

143+
timeToTarget_m = chargingTarget.targetTimeMinutesPastMidnight - minutesPastMidnightNow_m;
144+
143145
if (timeToTarget_m < minTimeToTarget_m)
144146
{
145147
// This is the closest target found in the day's targets so far
148+
bFound = true;
146149
minTimeToTarget_m = timeToTarget_m;
150+
147151
targetTime_m.SetNonNull(chargingTarget.targetTimeMinutesPastMidnight);
152+
148153
if (chargingTarget.targetSoC.HasValue())
149154
{
150155
targetSoC.SetNonNull(chargingTarget.targetSoC.Value());
@@ -153,6 +158,7 @@ CHIP_ERROR FindNextTarget(const uint8_t dayOfWeekMap, uint32_t minutesPastMidnig
153158
{
154159
targetSoC.SetNull();
155160
}
161+
156162
if (chargingTarget.addedEnergy.HasValue())
157163
{
158164
addedEnergy_mWh.SetNonNull(chargingTarget.addedEnergy.Value());
@@ -161,7 +167,6 @@ CHIP_ERROR FindNextTarget(const uint8_t dayOfWeekMap, uint32_t minutesPastMidnig
161167
{
162168
addedEnergy_mWh.SetNull();
163169
}
164-
bFound = true;
165170
}
166171
}
167172
}
@@ -194,8 +199,8 @@ CHIP_ERROR EVSEManufacturer::ComputeChargingSchedule()
194199
uint8_t dayOfWeekMap = 0;
195200
ReturnErrorOnFailure(GetDayOfWeekNow(dayOfWeekMap));
196201

197-
uint32_t minutesPastMidnight_m = 0;
198-
ReturnErrorOnFailure(GetMinutesPastMidnight(minutesPastMidnight_m));
202+
uint16_t minutesPastMidnightNow_m = 0;
203+
ReturnErrorOnFailure(GetMinutesPastMidnight(minutesPastMidnightNow_m));
199204

200205
DataModel::Nullable<uint32_t> startTime_m;
201206
DataModel::Nullable<uint32_t> targetTime_m;
@@ -214,7 +219,7 @@ CHIP_ERROR EVSEManufacturer::ComputeChargingSchedule()
214219
uint8_t dayOffset = 0;
215220
while (dayOffset < 2)
216221
{
217-
err = FindNextTarget(dayOfWeekMap, minutesPastMidnight_m, targetTime_m, targetSoC, addedEnergy_mWh);
222+
err = FindNextTarget(dayOfWeekMap, minutesPastMidnightNow_m, targetTime_m, targetSoC, addedEnergy_mWh);
218223
if (err == CHIP_ERROR_NOT_FOUND)
219224
{
220225
// We didn't find one for today, try tomorrow
@@ -241,7 +246,7 @@ CHIP_ERROR EVSEManufacturer::ComputeChargingSchedule()
241246
}
242247
// We don't know the Vehicle SoC so we must charge now
243248
// TODO make this use the SoC featureMap to determine if this is an error
244-
startTime_m.SetNonNull(minutesPastMidnight_m);
249+
startTime_m.SetNonNull(minutesPastMidnightNow_m);
245250
}
246251
else
247252
{
@@ -261,8 +266,8 @@ CHIP_ERROR EVSEManufacturer::ComputeChargingSchedule()
261266
power_W = 7000;
262267

263268
// Time to charge(seconds) = (3600 * Energy(mWh) / Power(W)) / 1000
264-
// to avoid using floats we multiply by 36 and then divide by 100 (instead of x3600 and dividing by 1000)
265-
chargingDuration_s = static_cast<uint32_t>(((addedEnergy_mWh.Value() / power_W) * 36) / 100);
269+
// to avoid using floats we multiply by 36 and then divide by 10 (instead of x3600 and dividing by 1000)
270+
chargingDuration_s = static_cast<uint32_t>(((addedEnergy_mWh.Value() / power_W) * 36) / 10);
266271
chargingDuration_m = chargingDuration_s / 60;
267272

268273
// Add in 15 minutes leeway to account for slow starting vehicles
@@ -275,10 +280,10 @@ CHIP_ERROR EVSEManufacturer::ComputeChargingSchedule()
275280
int tempStartTime_m = targetTime_m.Value() - chargingDuration_m + (dayOffset * 1440);
276281
// if tempStartTime_m is negative it means that it will take more than 24hrs to charge the vehicle
277282

278-
if ((tempStartTime_m < 0) || (tempStartTime_m < static_cast<int>(minutesPastMidnight_m)))
283+
if ((tempStartTime_m < 0) || (tempStartTime_m < static_cast<int>(minutesPastMidnightNow_m)))
279284
{
280285
// we need to turn on the EVSE now - it won't have enough time to reach the target
281-
startTime_m.SetNonNull(minutesPastMidnight_m);
286+
startTime_m.SetNonNull(minutesPastMidnightNow_m);
282287
// TODO call function to turn on the EV
283288
}
284289
else

examples/energy-management-app/energy-management-common/src/EnergyEvseDelegateImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ CHIP_ERROR GetDayOfWeekNow(uint8_t & dayOfWeekMap)
19521952
*
19531953
* @param reference to hold the number of minutes past midnight
19541954
*/
1955-
CHIP_ERROR GetMinutesPastMidnight(uint32_t & minutesPastMidnight)
1955+
CHIP_ERROR GetMinutesPastMidnight(uint16_t & minutesPastMidnight)
19561956
{
19571957
chip::System::Clock::Milliseconds64 cTMs;
19581958
CHIP_ERROR err = chip::System::SystemClock().GetClock_RealTimeMS(cTMs);
@@ -1968,7 +1968,7 @@ CHIP_ERROR GetMinutesPastMidnight(uint32_t & minutesPastMidnight)
19681968
struct tm local_time;
19691969
localtime_r(&unixEpoch, &local_time);
19701970

1971-
minutesPastMidnight = static_cast<uint32_t>((local_time.tm_hour * 60) + local_time.tm_min);
1971+
minutesPastMidnight = static_cast<uint16_t>((local_time.tm_hour * 60) + local_time.tm_min);
19721972

19731973
return err;
19741974
}

0 commit comments

Comments
 (0)