C avatar image
C asked Felix Möhlmann commented

Help on custom coding for battery charging

Hi all,

I hope you all are fine and healthy during this testing times.

I need help on customs coding. In v5, we have completed the battery discharging code thanks to your help. In v6, we are adding the renewable charging component to the model. I throwed in some comments and pseudo codes in custom codes in the model in the hopes of guiding you.

What needs to be done is as follows:

1) SetRenewableRate is the logic for solar energy charging. It needs to select corresponding value from the global tale RenewableRatesTable, depending on the month and hour and then define the global variable "hourlyRenewableRate". Therefore, ALL custom codes only in SetRenewableRate logic needs fixing. That is all I need.


2) Code below needs to be changed so that it selects a predefined shipPVCapacity value for each ship depeding on the name of the ship. Defined the values as labels to ships, manually.

/**Custom Code*/
Object current = param(1);
treenode activity = param(2);
Token token = param(3);
Variant assignTo = param(4);
string labelName = param(5);
treenode processFlow = ownerobject(activity);

TrackedVariable battery = current.labels["Battery"];
battery.rate = dailyDischargeRate*current.dischargeRate + hourlyRenewableRate*shipPVCapacity/1000 ;

//We need to define shipPVCapacity for 4 groups depending on names of transports.
//LC1 to LC13, F1 and F3, F2 and F4, and HSC1-2
//something like this: if is "LC1" then shipPVCapacity == "750"
//These values needs to be set only once.

return battery.value/current.dischargeRate;
FlexSim 19.0.0
custom codebattery
v6.fsm (227.9 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Felix Möhlmann avatar image
1 Like"
Felix Möhlmann answered Felix Möhlmann commented

Since their is no counting/exlucing certain values like with the daily discharge rates, the code is actually quite simply. You just have to read the value from the table, where the current month is the row and the hour is the column (with an offset of one, because hours go from 0-23).

// Get month and hour
int month = Model.dateTime.month;
int hour = Model.dateTime.hour;
// Set rate
hourlyRenewableRate = Table("RenewableRatesTable")[month][hour+1];

Then will also want to change the delay to wait until the next full hour, instead of day.



1652339846581.png (7.6 KiB)
v61.fsm (227.3 KiB)
· 21
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

C avatar image C commented ·

Thank you, Felix!

Before I open another question, I need your recommendation on how to proceed on this: I want to track how much power is charged from each dock and how many ships are charging at the same time. Then I think I will not disturb you anymore as modeling will be mostly over :)

There could be a better way but I think I can utilize the loc. labels for this. There are .loc labels given for each ship after it departs to the destination dock e.g. BOS_F, BOS1, BOS2, BOS3..., KSK, KSK1, KSK2, KSK3... etc. When the ship is idle at the dock, it starts charging at a preset rate but I need to see the amount of power drawn from the dock "battery" at the completion of each ship's charge (when it is not at idle anymore) and how many ships are charging at the same time at a dock. I also need the daily total charge is drawn and daily average rates for each dock "batteries".

The math is simple as we already track the battery levels for each ship, but real coding is confusing for me at this point.

There should be 4 sets of batteries: 1 for UCK_F, 1 for BOS_F, 1 for all other BOSs, and 1 for all KSKs.

The pseudocode thingy for each dock should be like this, or maybe not :)

set battery size for UCK_F == 20000
set battery recharge rate from the city grid for UCK_F == 1000
//this should record in a table that includes the ships name, how much is charged, and the duration of the charge
if ships' state == idle
record ship name
get and record the battery level on idle start
get and record the battery level on idle end
calculate and record the battery level difference
calculate and record duration
//charge rate control for debug
level difference divided by duration should be equal to the chargeRate of the particular dock (which is set to 4000 for UCK_F at the moment)

The daily total should just sum up the battery level differences and daily avg. rate just divides it by 24 for each day.


0 Likes 0 ·
v7.fsm (227.7 KiB)
Felix Möhlmann avatar image Felix Möhlmann C commented ·

You don't really need any coding for that if I understand your goal correctly. The token that sets the charge rate when the ship goes idle can create another token that stores the current time and battery level as labels.

This token then waits until a token enters the activity that sets the discharge rate. At that point you can write the info about what ship charged how much over what time and at what location to a global table with the pick option in the custom code activity.

1652368718344.pngYou can also split the writing up and add the ship, location and start time immediately. Then fill in the rest of the values later. You only have to store the number of the row that was created (Table(...).numRows).



1 Like 1 ·
1652368705620.png (49.9 KiB)
1652368718344.png (79.8 KiB)
v7-1.fsm (238.0 KiB)
C avatar image C Felix Möhlmann commented ·
Thank you Felix! First off, I get lots of console errors on drawing things so that I can not properly see the model file.

From the SS above, secondly, yes this is part of what I wanted. I need it to show the start and end battery levels and the rate that it was charged.

What I mainly wanted was to set a main battery reserve for a dock and track its levels, e.g. BOS dock and any ship that recharges there needs to spend from this reserve.

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.