@@ -30,6 +30,8 @@ namespace
30
30
{
31
31
32
32
33
+ #pragma pack(push, 1)
34
+
33
35
struct Frame_veh_state
34
36
{
35
37
std::uint8_t crc;
@@ -42,7 +44,7 @@ struct Frame_veh_state
42
44
std::uint8_t wiper_position_2 : 2 ;
43
45
};
44
46
45
- struct Frame_flux_state
47
+ struct Frame_flux
46
48
{
47
49
std::uint32_t power_level;
48
50
std::uint32_t dispersal_rate;
@@ -63,6 +65,32 @@ struct Frame_date_time
63
65
};
64
66
65
67
68
+ struct Frame_fuel
69
+ {
70
+ std::uint8_t fuel_type : 1 ;
71
+ std::uint8_t reserved : 7 ;
72
+ union {
73
+ struct {
74
+ std::uint8_t e_range;
75
+ std::uint8_t e_cons_0;
76
+ std::uint8_t e_cons_1 : 2 ;
77
+ std::uint8_t bat_volt_0 : 6 ;
78
+ std::uint8_t bat_volt_1 : 6 ;
79
+ std::uint8_t charging : 1 ;
80
+ std::uint8_t super_charging : 1 ;
81
+ } m0;
82
+ struct {
83
+ std::uint8_t gas_range_0;
84
+ std::uint8_t gas_range_1 : 2 ;
85
+ std::uint8_t gas_cons_0 : 6 ;
86
+ std::uint8_t gas_cons_1 : 4 ;
87
+ } m1;
88
+ } mux;
89
+ };
90
+
91
+ #pragma pack(pop)
92
+
93
+
66
94
} // namespace
67
95
68
96
@@ -85,12 +113,12 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
85
113
veh_state_data->wiper_position_1 = 2122 >> 4 ;
86
114
veh_state_data->wiper_position_2 = 2122 >> 12 ;
87
115
88
- can_frame flux_state {0 };
89
- flux_state .can_id = 233 ;
90
- flux_state .can_dlc = 6 ;
91
- auto * flux_state_data = reinterpret_cast <Frame_flux_state *>(flux_state .data );
92
- flux_state_data ->power_level = 1210000000 ;
93
- flux_state_data ->dispersal_rate = 7743 ;
116
+ can_frame flux {0 };
117
+ flux .can_id = 233 ;
118
+ flux .can_dlc = 6 ;
119
+ auto * flux_data = reinterpret_cast <Frame_flux *>(flux .data );
120
+ flux_data ->power_level = 1210000000 ;
121
+ flux_data ->dispersal_rate = 7743 ;
94
122
95
123
can_frame date_time{0 };
96
124
date_time.can_id = 245 ;
@@ -105,6 +133,27 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
105
133
date_time_data->minute_0 = 31 ;
106
134
date_time_data->minute_1 = 31 >> 2 ;
107
135
136
+ can_frame fuel{0 };
137
+ fuel.can_id = 502 ;
138
+ fuel.can_dlc = 5 ;
139
+ Frame_fuel electric;
140
+ electric.mux .m0 .e_range = 149 ;
141
+ electric.mux .m0 .e_cons_0 = 354 ;
142
+ electric.mux .m0 .e_cons_1 = 354 >> 8 ;
143
+ electric.mux .m0 .bat_volt_0 = 3751 ;
144
+ electric.mux .m0 .bat_volt_1 = 3751 >> 6 ;
145
+ electric.mux .m0 .charging = 0 ;
146
+ electric.mux .m0 .super_charging = 0 ;
147
+ Frame_fuel gasoline;
148
+ gasoline.mux .m1 .gas_range_0 = 381 ;
149
+ gasoline.mux .m1 .gas_range_1 = 381 >> 8 ;
150
+ gasoline.mux .m1 .gas_cons_0 = 178 ;
151
+ gasoline.mux .m1 .gas_cons_1 = 178 >> 6 ;
152
+ auto * fuel_data = reinterpret_cast <Frame_fuel*>(fuel.data );
153
+ fuel_data->reserved = 0 ;
154
+ fuel_data->fuel_type = 0 ;
155
+ fuel_data->mux = electric.mux ;
156
+
108
157
#pragma GCC diagnostic pop
109
158
110
159
can_frame radar{0 };
@@ -142,14 +191,22 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
142
191
udp_socket.transmit (&veh_state);
143
192
}
144
193
if (time_ms % 100 == 0 ) {
145
- udp_socket.transmit (&flux_state );
194
+ udp_socket.transmit (&flux );
146
195
}
147
196
if (time_ms % 225 == 0 ) {
148
197
udp_socket.transmit (&radar);
149
198
}
150
199
if (time_ms % 1333 == 0 ) {
151
200
udp_socket.transmit (&date_time);
152
201
}
202
+ if (time_ms % 500 == 0 ) {
203
+ fuel_data->fuel_type = ~fuel_data->fuel_type ;
204
+ if (fuel_data->fuel_type == 0 )
205
+ fuel_data->mux = electric.mux ;
206
+ else
207
+ fuel_data->mux = gasoline.mux ;
208
+ udp_socket.transmit (&fuel);
209
+ }
153
210
};
154
211
155
212
auto transmit_with_timestamp = [&](std::uint64_t time_ms) {
@@ -162,7 +219,7 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
162
219
}
163
220
if (time_ms % 100 == 0 ) {
164
221
*time_buffer = time_ms;
165
- std::memcpy (frame_buffer, &flux_state , sizeof (can_frame));
222
+ std::memcpy (frame_buffer, &flux , sizeof (can_frame));
166
223
udp_socket.transmit (buffer);
167
224
}
168
225
if (time_ms % 225 == 0 ) {
@@ -175,6 +232,27 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
175
232
std::memcpy (frame_buffer, &date_time, sizeof (can_frame));
176
233
udp_socket.transmit (buffer);
177
234
}
235
+ if (time_ms % 500 == 0 ) {
236
+ fuel_data->fuel_type = ~fuel_data->fuel_type ;
237
+ if (fuel_data->fuel_type == 0 ) {
238
+ // Update range
239
+ if (electric.mux .m0 .super_charging == 1 ) electric.mux .m0 .e_range += 4 ;
240
+ else if (electric.mux .m0 .charging == 1 ) electric.mux .m0 .e_range ++;
241
+ else electric.mux .m0 .e_range --;
242
+ // Set charging state
243
+ if (electric.mux .m0 .e_range == 0 ) electric.mux .m0 .charging = 1 ;
244
+ if (electric.mux .m0 .charging == 1 && electric.mux .m0 .e_range == 100 ) electric.mux .m0 .super_charging = 1 ;
245
+ else if (electric.mux .m0 .e_range > 200 ) electric.mux .m0 .charging = electric.mux .m0 .super_charging = 0 ;
246
+ fuel_data->mux = electric.mux ;
247
+ }
248
+ else {
249
+ fuel_data->mux = gasoline.mux ;
250
+ }
251
+
252
+ *time_buffer = time_ms;
253
+ std::memcpy (frame_buffer, &fuel, sizeof (can_frame));
254
+ udp_socket.transmit (buffer);
255
+ }
178
256
};
179
257
180
258
// Looping and timing
0 commit comments