Line data Source code
1 : /*
2 : This program is free software: you can redistribute it and/or modify
3 : it under the terms of the GNU General Public License as published by
4 : the Free Software Foundation, either version 3 of the License, or
5 : (at your option) any later version.
6 :
7 : This program is distributed in the hope that it will be useful,
8 : but WITHOUT ANY WARRANTY; without even the implied warranty of
9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 : GNU General Public License for more details.
11 :
12 : You should have received a copy of the GNU General Public License
13 : along with this program. If not, see <http://www.gnu.org/licenses/>.
14 : */
15 : /*
16 : Simulator for the IntelligentEnergy 2.4kWh FuelCell generator
17 : */
18 :
19 : #include <AP_Math/AP_Math.h>
20 :
21 : #include "SIM_IntelligentEnergy24.h"
22 : #include "SITL.h"
23 :
24 : #include <errno.h>
25 :
26 : #include <GCS_MAVLink/GCS.h>
27 :
28 : extern const AP_HAL::HAL& hal;
29 :
30 : using namespace SITL;
31 :
32 : #define MAX_TANK_PRESSURE 300 //(bar)
33 :
34 : // table of user settable parameters
35 : const AP_Param::GroupInfo IntelligentEnergy24::var_info[] = {
36 :
37 : // @Param: ENABLE
38 : // @DisplayName: IntelligentEnergy 2.4kWh FuelCell sim enable/disable
39 : // @Description: Allows you to enable (1) or disable (0) the FuelCell simulator
40 : // @Values: 0:Disabled,1:V1 Protocol,2:V2 Protocol
41 : // @User: Advanced
42 : AP_GROUPINFO("ENABLE", 1, IntelligentEnergy24, enabled, 0),
43 :
44 : // @Param: STATE
45 : // @DisplayName: Explicitly set state
46 : // @Description: Explicitly specify a state for the generator to be in
47 : // @User: Advanced
48 : AP_GROUPINFO("STATE", 2, IntelligentEnergy24, set_state, -1),
49 :
50 : // @Param: ERROR
51 : // @DisplayName: Explicitly set error code
52 : // @Description: Explicitly specify an error code to send to the generator
53 : // @User: Advanced
54 : AP_GROUPINFO("ERROR", 3, IntelligentEnergy24, err_code, 0),
55 :
56 : AP_GROUPEND
57 : };
58 :
59 2096 : IntelligentEnergy24::IntelligentEnergy24() : IntelligentEnergy::IntelligentEnergy()
60 : {
61 2096 : AP_Param::setup_object_defaults(this, var_info);
62 2096 : }
63 :
64 420295 : void IntelligentEnergy24::update(const struct sitl_input &input)
65 : {
66 420295 : if (!enabled.get()) {
67 6011 : return;
68 : }
69 414284 : update_send();
70 : }
71 :
72 414284 : void IntelligentEnergy24::update_send()
73 : {
74 : // just send a chunk of data at 2 Hz:
75 414284 : const uint32_t now = AP_HAL::millis();
76 414284 : if (now - last_data_sent_ms < 500) {
77 413596 : return;
78 : }
79 :
80 : // Simulate constant current charge/discharge of the battery
81 688 : float amps = discharge ? -20.0f : 20.0f;
82 :
83 : // Update pack capacity remaining
84 688 : bat_capacity_mAh += amps*(now - last_data_sent_ms)/3600.0f;
85 :
86 : // From capacity remaining approximate voltage by linear interpolation
87 688 : const float min_bat_vol = 42.0f;
88 688 : const float max_bat_vol = 50.4f;
89 688 : const float max_bat_capactiy_mAh = 3300;
90 :
91 : // Simulate tank pressure
92 : // Scale tank pressure linearly to a percentage.
93 : // Min = 5 bar, max = 300 bar, PRESS_GRAD = 1/295.
94 688 : const int16_t tank_bar = linear_interpolate(5, MAX_TANK_PRESSURE, bat_capacity_mAh / max_bat_capactiy_mAh, 0, 1);
95 :
96 688 : battery_voltage = bat_capacity_mAh / max_bat_capactiy_mAh * (max_bat_vol - min_bat_vol) + min_bat_vol;
97 :
98 : // Decide if we need to charge or discharge the battery
99 688 : if (battery_voltage <= min_bat_vol) {
100 0 : discharge = false;
101 688 : } else if (battery_voltage >= max_bat_vol) {
102 0 : discharge = true;
103 : }
104 :
105 688 : int32_t battery_pwr = battery_voltage * amps; // Watts
106 :
107 : // These are non-physical values
108 688 : const int32_t pwr_out = float_to_int32(battery_pwr*1.4f);
109 688 : const uint32_t spm_pwr = float_to_uint32(battery_pwr*0.3f);
110 :
111 688 : uint32_t state = set_state;
112 688 : if (set_state == -1) {
113 660 : state = 2; // Running
114 : }
115 :
116 688 : last_data_sent_ms = now;
117 :
118 : char message[128];
119 :
120 688 : if (enabled.get() == 1) {
121 : // V1 Protocol
122 684 : hal.util->snprintf(message, ARRAY_SIZE(message), "<%i,%.1f,%i,%u,%i,%u,%u>\n",
123 : tank_bar,
124 342 : battery_voltage,
125 : (signed)pwr_out,
126 : (unsigned)spm_pwr,
127 : (signed)battery_pwr,
128 : (unsigned)state,
129 342 : (unsigned)err_code);
130 :
131 : } else {
132 : // V2 Protocol
133 :
134 : // version message sent at 0.2 Hz
135 346 : if (now - last_ver_sent_ms > 5e3) {
136 : // PCM software part number, software version number, protocol number, hardware serial number, check-sum
137 31 : hal.util->snprintf(message, ARRAY_SIZE(message), "[10011867,2.132,4,IE12160A8040015,7]\n");
138 :
139 31 : if ((unsigned)write_to_autopilot(message, strlen(message)) != strlen(message)) {
140 0 : AP_HAL::panic("Failed to write to autopilot: %s", strerror(errno));
141 : }
142 31 : last_ver_sent_ms = now;
143 : }
144 :
145 : // data message
146 346 : memset(&message, 0, sizeof(message));
147 346 : int8_t tank_remaining_pct = (float)tank_bar / MAX_TANK_PRESSURE * 100.0;
148 :
149 346 : hal.util->snprintf(message, ARRAY_SIZE(message), "<%i,%.2f,%.1f,%i,%u,%i,%i,%u,%u,%i,%s,", // last blank , is for fuel cell to send info string up to 32 char ASCII
150 : tank_remaining_pct,
151 : 0.67f, // inlet pressure (bar)
152 346 : battery_voltage,
153 : (signed)pwr_out,
154 : (unsigned)spm_pwr,
155 : 0, // unit at fault (0 = no fault)
156 : (signed)battery_pwr,
157 : (unsigned)state,
158 346 : (unsigned)err_code,
159 : 0, // fault state 2 (0 = no fault)
160 346 : get_error_string(err_code));
161 :
162 : // calculate the checksum
163 346 : uint8_t checksum = 0;
164 12916 : for (uint8_t i = 0; i < ARRAY_SIZE(message); i++) {
165 12916 : if (message[i] == 0) {
166 346 : break;
167 : }
168 12570 : checksum += message[i];
169 : }
170 : // checksum is inverted 8-bit
171 346 : checksum = ~checksum;
172 :
173 : // add the checksum to the end of the message
174 : char data_end[7];
175 346 : hal.util->snprintf(data_end, ARRAY_SIZE(data_end), "%u>\n", checksum);
176 346 : strncat(message, data_end, ARRAY_SIZE(data_end));
177 :
178 : }
179 :
180 688 : if ((unsigned)write_to_autopilot(message, strlen(message)) != strlen(message)) {
181 0 : AP_HAL::panic("Failed to write to autopilot: %s", strerror(errno));
182 : }
183 : }
184 :
185 346 : const char * IntelligentEnergy24::get_error_string(const uint32_t code)
186 : {
187 346 : switch (code) {
188 0 : case 20:
189 0 : return "THERMAL MNGMT";
190 :
191 346 : default:
192 346 : break;
193 : }
194 :
195 346 : return "";
196 : }
|