forked from AgOpenGPS-Official/AOG-TaskController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
278 lines (243 loc) · 11.6 KB
/
app.cpp
File metadata and controls
278 lines (243 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @author Daan Steenbergen
* @brief The main application class
* @version 0.1
* @date 2025-1-20
*
* @copyright 2025 Daan Steenbergen
*/
#include "app.hpp"
#include "isobus/hardware_integration/available_can_drivers.hpp"
#include "isobus/hardware_integration/can_hardware_interface.hpp"
#include "isobus/isobus/can_network_manager.hpp"
#include "isobus/isobus/isobus_preferred_addresses.hpp"
#include "isobus/isobus/isobus_standard_data_description_indices.hpp"
#include "isobus/utility/system_timing.hpp"
#include "task_controller.hpp"
using boost::asio::ip::udp;
Application::Application(std::shared_ptr<isobus::CANHardwarePlugin> canDriver) :
canDriver(canDriver)
{
}
bool Application::initialize()
{
settings->load();
if (nullptr == canDriver)
{
std::cout << "Unable to find a CAN driver. Please make sure the selected driver is installed." << std::endl;
return false;
}
isobus::CANHardwareInterface::set_number_of_can_channels(1);
isobus::CANHardwareInterface::assign_can_channel_frame_handler(0, canDriver);
if ((!isobus::CANHardwareInterface::start()) || (!canDriver->get_is_valid()))
{
std::cout << "Failed to start CAN hardware interface." << std::endl;
return false;
}
isobus::CANNetworkManager::CANNetwork.get_configuration().set_number_of_packets_per_cts_message(255);
isobus::NAME ourNAME(0);
//! Make sure you change these for your device!!!!
ourNAME.set_arbitrary_address_capable(true);
ourNAME.set_industry_group(2);
ourNAME.set_device_class(0);
ourNAME.set_identity_number(20);
ourNAME.set_ecu_instance(0);
ourNAME.set_function_instance(0); // TC #1. If you want to change the TC number, change this.
ourNAME.set_device_class_instance(0);
ourNAME.set_manufacturer_code(1407);
isobus::NAME tcNAME = ourNAME;
tcNAME.set_function_code(static_cast<std::uint8_t>(isobus::NAME::Function::TaskController));
isobus::NAME tecuNAME = ourNAME;
tecuNAME.set_function_code(static_cast<std::uint8_t>(isobus::NAME::Function::TractorECU));
tecuNAME.set_arbitrary_address_capable(false); // TECU address is fixed
tecuNAME.set_ecu_instance(0);
std::cout << "[Init] Creating Task Controller control function..." << std::endl;
auto tcCF = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(tcNAME, 0, isobus::preferred_addresses::IndustryGroup2::TaskController_MappingComputer); // The preferred address for a TC is defined in ISO 11783
auto tcAddressClaimedFuture = std::async(std::launch::async, [&tcCF]() {
while (!tcCF->get_address_valid())
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
isobus::CANNetworkManager::CANNetwork.update();
}
});
// If this fails, probably the update thread is not started
tcAddressClaimedFuture.wait_for(std::chrono::seconds(5));
if (!tcCF->get_address_valid())
{
std::cout << "Failed to claim address for TC server. The control function might be invalid." << std::endl;
return false;
}
// Create TECU control function
// TODO: Should we wait between this and TC?
// TODO: If there's already a TECU on the bus we should not create ours
if (tcCF)
{ // Only create TECU if TC was created
std::cout << "[Init] Creating Tractor ECU control function..." << std::endl;
tecuCF = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(tecuNAME, 0, isobus::preferred_addresses::IndustryGroup2::TractorECU);
std::cout << "[Init] Tractor ECU control function created, waiting 1.5 seconds..." << std::endl;
// Update the network manager to process TECU CF claiming
for (int i = 0; i < 15; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
isobus::CANNetworkManager::CANNetwork.update();
}
}
tcServer = std::make_shared<MyTCServer>(tcCF);
auto &languageInterface = tcServer->get_language_command_interface();
languageInterface.set_language_code("en"); // This is the default, but you can change it if you want
languageInterface.set_country_code("US"); // This is the default, but you can change it if you want
tcServer->initialize();
tcServer->set_task_totals_active(true); // TODO: make this dynamic based on status in AOG
// Initialize speed and distance messages
if (tecuCF)
{
std::cout << "[Init] Creating Speed Messages Interface on TECU..." << std::endl;
speedMessagesInterface = std::make_unique<isobus::SpeedMessagesInterface>(tecuCF, true, true, true, false); //TODO: make configurable whether to send these messages
speedMessagesInterface->initialize();
speedMessagesInterface->wheelBasedSpeedTransmitData.set_implement_start_stop_operations_state(isobus::SpeedMessagesInterface::WheelBasedMachineSpeedData::ImplementStartStopOperations::NotAvailable);
speedMessagesInterface->wheelBasedSpeedTransmitData.set_key_switch_state(isobus::SpeedMessagesInterface::WheelBasedMachineSpeedData::KeySwitchState::NotAvailable);
speedMessagesInterface->wheelBasedSpeedTransmitData.set_operator_direction_reversed_state(isobus::SpeedMessagesInterface::WheelBasedMachineSpeedData::OperatorDirectionReversed::NotAvailable);
speedMessagesInterface->machineSelectedSpeedTransmitData.set_speed_source(isobus::SpeedMessagesInterface::MachineSelectedSpeedData::SpeedSource::NavigationBasedSpeed);
std::cout << "[Init] Speed Messages Interface created and initialized." << std::endl;
std::cout << "[Init] Creating NMEA2000 Message Interface on TECU..." << std::endl;
nmea2000MessageInterface = std::make_unique<isobus::NMEA2000MessageInterface>(tecuCF, false, false, false, false, false, false, false);
nmea2000MessageInterface->initialize();
nmea2000MessageInterface->set_enable_sending_cog_sog_cyclically(true); // TODO: make configurable whether to send these messages
std::cout << "[Init] NMEA2000 Message Interface created and initialized." << std::endl;
}
else
{
std::cout << "[Warning] TECU Control Function not available, Speed/NMEA interfaces not created" << std::endl;
}
std::cout << "Task controller server started." << std::endl;
static std::uint8_t xteSid = 0;
static std::uint32_t lastXteTransmit = 0;
auto packetHandler = [this, tcCF](std::uint8_t src, std::uint8_t pgn, std::span<std::uint8_t> data) {
if (src == 0x7F && pgn == 0xE5) // 229 - Section Data 1 to 64
{
std::vector<bool> sectionStates;
for (std::uint8_t scb = 0; scb < 8; scb++)
{
for (std::uint8_t i = 0; i < 8; i++)
{
sectionStates.push_back(data[scb] & (1 << i));
}
}
tcServer->update_section_states(sectionStates);
}
else if (src == 0x7F && pgn == 0xF1) // 241 - Section Control
{
std::uint8_t sectionControlState = data[0];
std::cout << "Received request from AOG to change section control state to " << (sectionControlState == 1 ? "enabled" : "disabled") << std::endl;
tcServer->update_section_control_enabled(sectionControlState == 1);
}
else if (src == 0x7F && pgn == 0xF2) // Process Data
{
auto identifier = static_cast<isobus::DataDescriptionIndex>(data[0] | (data[1] << 8));
std::int32_t value = data[2] | (data[3] << 8) | (data[4] << 16) | (data[5] << 24);
if (identifier == isobus::DataDescriptionIndex::ActualSpeed)
{
std::uint16_t speed = std::abs(value);
auto direction = value < 0 ? isobus::SpeedMessagesInterface::MachineDirection::Reverse : isobus::SpeedMessagesInterface::MachineDirection::Forward;
if (speedMessagesInterface)
{
speedMessagesInterface->groundBasedSpeedTransmitData.set_machine_direction_of_travel(direction);
speedMessagesInterface->wheelBasedSpeedTransmitData.set_machine_direction_of_travel(direction);
speedMessagesInterface->machineSelectedSpeedTransmitData.set_machine_direction_of_travel(direction);
speedMessagesInterface->groundBasedSpeedTransmitData.set_machine_speed(speed);
speedMessagesInterface->wheelBasedSpeedTransmitData.set_machine_speed(speed);
speedMessagesInterface->machineSelectedSpeedTransmitData.set_machine_speed(speed);
speedMessagesInterface->groundBasedSpeedTransmitData.set_machine_distance(0); // TODO: Implement distance
speedMessagesInterface->wheelBasedSpeedTransmitData.set_machine_distance(0); // TODO: Implement distance
speedMessagesInterface->machineSelectedSpeedTransmitData.set_machine_distance(0); // TODO: Implement distance
}
if (nmea2000MessageInterface)
{
auto &cog_sog_message = nmea2000MessageInterface->get_cog_sog_transmit_message();
cog_sog_message.set_sequence_id(nmea2000SequenceIdentifier++);
cog_sog_message.set_speed_over_ground(speed / 10);
cog_sog_message.set_course_over_ground(0); // TODO: Implement course
cog_sog_message.set_course_over_ground_reference(isobus::NMEA2000Messages::CourseOverGroundSpeedOverGroundRapidUpdate::CourseOverGroundReference::NotApplicableOrNull);
}
}
else if (identifier == isobus::DataDescriptionIndex::GuidanceLineDeviation)
{
std::int32_t xte = value / 1000; // Convert from mm to m
static const std::uint8_t xteMode = 0b00000001;
xteSid = xteSid % 253 + 1;
std::uint8_t status = 0; // TODO: navigation terminated status
std::array<std::uint8_t, 8> xteData = {
xteSid, // Sequence ID
static_cast<std::uint8_t>(xteMode | 0b00110000 | (status == 1 ? 0b00000000 : 0b01000000)), // XTE mode (4 bits) + Reserved (2 bits set to 1) + Navigation Terminated (2 bits)
static_cast<std::uint8_t>(xte & 0xFF), // XTE LSB
static_cast<std::uint8_t>((xte >> 8) & 0xFF), // XTE
static_cast<std::uint8_t>((xte >> 16) & 0xFF), // XTE
static_cast<std::uint8_t>((xte >> 24) & 0xFF), // XTE MSB
0xFF, // Reserved byte 1 (all bits set to 1)
0xFF // Reserved byte 2 (all bits set to 1)
};
if (isobus::SystemTiming::time_expired_ms(lastXteTransmit, 1000)) // Transmit every second
{
if (isobus::CANNetworkManager::CANNetwork.send_can_message(0x1F903, xteData.data(), xteData.size(), tcCF))
{
lastXteTransmit = isobus::SystemTiming::get_timestamp_ms();
}
}
}
else if (static_cast<std::uint16_t>(identifier) == 597 /*isobus::DataDescriptionIndex::TotalDistance*/ && speedMessagesInterface)
{
auto distance = static_cast<std::uint32_t>(value);
speedMessagesInterface->groundBasedSpeedTransmitData.set_machine_distance(distance);
speedMessagesInterface->wheelBasedSpeedTransmitData.set_machine_distance(distance);
speedMessagesInterface->machineSelectedSpeedTransmitData.set_machine_distance(distance);
}
}
};
udpConnections->set_packet_handler(packetHandler);
udpConnections->open();
std::cout << "UDP connections opened." << std::endl;
return true;
}
bool Application::update()
{
static std::uint32_t lastHeartbeatTransmit = 0;
static std::uint32_t lastTECUStatusTransmit = 0;
udpConnections->handle_address_detection();
udpConnections->handle_incoming_packets();
tcServer->request_measurement_commands();
tcServer->update();
if (speedMessagesInterface)
speedMessagesInterface->update();
if (nmea2000MessageInterface)
nmea2000MessageInterface->update();
if (isobus::SystemTiming::time_expired_ms(lastHeartbeatTransmit, 100))
{
for (auto &client : tcServer->get_clients())
{
auto &state = client.second;
std::vector<uint8_t> data = { state.is_section_control_enabled(), state.get_number_of_sections() };
std::uint8_t sectionIndex = 0;
while (sectionIndex < state.get_number_of_sections())
{
std::uint8_t byte = 0;
for (std::uint8_t i = 0; i < 8; i++)
{
if (sectionIndex < state.get_number_of_sections())
{
byte |= (state.get_section_actual_state(sectionIndex) == SectionState::ON) << i;
sectionIndex++;
}
}
data.push_back(byte);
}
udpConnections->send(0x80, 0xF0, data);
}
lastHeartbeatTransmit = isobus::SystemTiming::get_timestamp_ms();
}
return true;
}
void Application::stop()
{
tcServer->terminate();
isobus::CANHardwareInterface::stop();
}