-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a class that logs all CAN messages related to the VT to a Vector ASCII CAN log file.
- Loading branch information
Showing
6 changed files
with
150 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//================================================================================================ | ||
/// @file ASCIILogFile.hpp | ||
/// | ||
/// @brief Defines a CAN logger that saves messages in a Vector .asc file. | ||
/// @author Adrian Del Grosso | ||
/// | ||
/// @copyright 2023 Adrian Del Grosso | ||
//================================================================================================ | ||
#ifndef ASCII_LOG_FILE_HPP | ||
#define ASCII_LOG_FILE_HPP | ||
|
||
#include "isobus/hardware_integration/can_hardware_interface.hpp" | ||
|
||
#include "JuceHeader.h" | ||
|
||
/// @brief Logs to Vector .asc file | ||
class ASCIILogFile | ||
{ | ||
public: | ||
ASCIILogFile(); | ||
|
||
~ASCIILogFile() = default; | ||
|
||
private: | ||
File logFile; | ||
std::shared_ptr<void> canFrameReceivedListener; | ||
std::shared_ptr<void> canFrameSentListener; | ||
Time initialTimestamp; | ||
}; | ||
|
||
#endif // ASCII_LOG_FILE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/******************************************************************************* | ||
** @file ASCIILogFile.cpp | ||
** @author Adrian Del Grosso | ||
** @copyright The Open-Agriculture Developers | ||
*******************************************************************************/ | ||
#include "ASCIILogFile.hpp" | ||
|
||
#include "isobus/utility/system_timing.hpp" | ||
#include "isobus/utility/to_string.hpp" | ||
|
||
ASCIILogFile::ASCIILogFile() | ||
{ | ||
auto currentTime = Time::getCurrentTime().toString(true, true, true, false); | ||
initialTimestamp = Time::getCurrentTime(); | ||
auto fileNameTime = currentTime; | ||
fileNameTime = currentTime.replaceCharacter(' ', '_'); | ||
fileNameTime = currentTime.replaceCharacter(':', '_'); | ||
|
||
logFile = File(File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + | ||
File::getSeparatorString() + | ||
"Open-Agriculture" + | ||
File::getSeparatorString() + | ||
"CANLog_" + | ||
fileNameTime + | ||
".asc"); | ||
|
||
// Prune old log files | ||
auto logDirectory = logFile.getParentDirectory(); | ||
auto childFiles = logDirectory.findChildFiles(File::findFiles, false, "*.asc"); | ||
|
||
for (auto &file : childFiles) | ||
{ | ||
if (file.getCreationTime() < Time::getCurrentTime() - RelativeTime::days(3)) | ||
{ | ||
file.deleteFile(); | ||
} | ||
} | ||
|
||
// Write vector ascii header | ||
if (logFile.hasWriteAccess()) | ||
{ | ||
logFile.appendText("date " + currentTime + "\n"); | ||
logFile.appendText("base hex timestamps absolute\n"); | ||
logFile.appendText("no internal events logged\n"); | ||
canFrameReceivedListener = isobus::CANHardwareInterface::get_can_frame_received_event_dispatcher().add_listener([this](const isobus::CANMessageFrame &canFrame) { | ||
logFile.appendText(" "); | ||
auto currentTime = Time::getCurrentTime() - initialTimestamp; | ||
auto milliseconds = isobus::to_string(currentTime.inMilliseconds() % 1000); | ||
|
||
while (milliseconds.length() < 3) | ||
{ | ||
milliseconds = "0" + milliseconds; | ||
} | ||
|
||
logFile.appendText(isobus::to_string(std::floor(currentTime.inSeconds())) + | ||
"." + | ||
milliseconds + | ||
"000 1 " + | ||
String::toHexString(canFrame.identifier).toUpperCase().toStdString() + | ||
"x Rx d " + | ||
isobus::to_string(static_cast<int>(canFrame.dataLength)) + | ||
" "); | ||
|
||
for (std::uint_fast8_t i = 0; i < canFrame.dataLength; i++) | ||
{ | ||
logFile.appendText(String::toHexString(canFrame.data[i]).paddedLeft('0', 2).toUpperCase().toStdString() + " "); | ||
} | ||
|
||
for (std::uint_fast8_t i = canFrame.dataLength; i < 8; i++) | ||
{ | ||
logFile.appendText("00 "); | ||
} | ||
logFile.appendText("\n"); | ||
}); | ||
|
||
canFrameSentListener = isobus::CANHardwareInterface::get_can_frame_transmitted_event_dispatcher().add_listener([this](const isobus::CANMessageFrame &canFrame) { | ||
logFile.appendText(" "); | ||
auto currentTime = Time::getCurrentTime() - initialTimestamp; | ||
auto milliseconds = isobus::to_string(currentTime.inMilliseconds() % 1000); | ||
|
||
while (milliseconds.length() < 3) | ||
{ | ||
milliseconds = "0" + milliseconds; | ||
} | ||
|
||
logFile.appendText(isobus::to_string(std::floor(currentTime.inSeconds())) + | ||
"." + | ||
milliseconds + | ||
"000 1 " + | ||
String::toHexString(canFrame.identifier).toUpperCase().toStdString() + | ||
"x Tx d " + | ||
isobus::to_string(static_cast<int>(canFrame.dataLength)) + | ||
" "); | ||
|
||
for (std::uint_fast8_t i = 0; i < canFrame.dataLength; i++) | ||
{ | ||
logFile.appendText(String::toHexString(canFrame.data[i]).paddedLeft('0', 2).toUpperCase().toStdString() + " "); | ||
} | ||
|
||
for (std::uint_fast8_t i = canFrame.dataLength; i < 8; i++) | ||
{ | ||
logFile.appendText("00 "); | ||
} | ||
logFile.appendText("\n"); | ||
}); | ||
} | ||
else | ||
{ | ||
RuntimePermissions::request(RuntimePermissions::writeExternalStorage, nullptr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters