Skip to content

Commit 2317a01

Browse files
committed
Add multiplexed frame
1 parent bf9dabd commit 2317a01

File tree

1 file changed

+87
-9
lines changed

1 file changed

+87
-9
lines changed

cansim.cpp

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ namespace
3030
{
3131

3232

33+
#pragma pack(push, 1)
34+
3335
struct Frame_veh_state
3436
{
3537
std::uint8_t crc;
@@ -42,7 +44,7 @@ struct Frame_veh_state
4244
std::uint8_t wiper_position_2 : 2;
4345
};
4446

45-
struct Frame_flux_state
47+
struct Frame_flux
4648
{
4749
std::uint32_t power_level;
4850
std::uint32_t dispersal_rate;
@@ -63,6 +65,32 @@ struct Frame_date_time
6365
};
6466

6567

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+
6694
} // namespace
6795

6896

@@ -85,12 +113,12 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
85113
veh_state_data->wiper_position_1 = 2122 >> 4;
86114
veh_state_data->wiper_position_2 = 2122 >> 12;
87115

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;
94122

95123
can_frame date_time{0};
96124
date_time.can_id = 245;
@@ -105,6 +133,27 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
105133
date_time_data->minute_0 = 31;
106134
date_time_data->minute_1 = 31 >> 2;
107135

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+
108157
#pragma GCC diagnostic pop
109158

110159
can_frame radar{0};
@@ -142,14 +191,22 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
142191
udp_socket.transmit(&veh_state);
143192
}
144193
if (time_ms % 100 == 0) {
145-
udp_socket.transmit(&flux_state);
194+
udp_socket.transmit(&flux);
146195
}
147196
if (time_ms % 225 == 0) {
148197
udp_socket.transmit(&radar);
149198
}
150199
if (time_ms % 1333 == 0) {
151200
udp_socket.transmit(&date_time);
152201
}
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+
}
153210
};
154211

155212
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
162219
}
163220
if (time_ms % 100 == 0) {
164221
*time_buffer = time_ms;
165-
std::memcpy(frame_buffer, &flux_state, sizeof(can_frame));
222+
std::memcpy(frame_buffer, &flux, sizeof(can_frame));
166223
udp_socket.transmit(buffer);
167224
}
168225
if (time_ms % 225 == 0) {
@@ -175,6 +232,27 @@ void simulate(std::atomic<bool>& stop, std::string&& ip, std::uint16_t port, boo
175232
std::memcpy(frame_buffer, &date_time, sizeof(can_frame));
176233
udp_socket.transmit(buffer);
177234
}
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+
}
178256
};
179257

180258
// Looping and timing

0 commit comments

Comments
 (0)