Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C2A command sender for accelerated SILS test #485

Merged
merged 35 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b817620
Command analyze function
200km Aug 26, 2023
2c6cbbd
Rename function
200km Aug 26, 2023
251cede
Add magic-enum with FetchContents
200km Aug 26, 2023
f6d5fbf
Test magic_enum
200km Aug 26, 2023
1f93dd5
Add C2A command database read feature
200km Aug 30, 2023
a22f4fe
Add argument decode function
200km Aug 30, 2023
9c65c07
Add doxygen comment
200km Aug 31, 2023
69ec000
Remove magic enu,
200km Aug 31, 2023
3c71763
Remove magic enum
200km Aug 31, 2023
8c20b69
Fix doxygen comment decription
200km Aug 31, 2023
a39e84b
Remove debug codes
200km Aug 31, 2023
3d3aed5
Add doxygen comment
200km Aug 31, 2023
d8111ef
Rename file name
200km Aug 31, 2023
46da534
Fix typo
200km Aug 31, 2023
aa62d00
Add WingsOperationFile class
200km Aug 31, 2023
5d7dc40
Add command execution
200km Aug 31, 2023
638581e
Remove unnecessary newline
200km Aug 31, 2023
61ee104
Fix format
200km Aug 31, 2023
97cf531
Add component for command sender
200km Sep 1, 2023
e6b711c
Move command parse function to component
200km Sep 1, 2023
49f3ec5
Add ifdef for C2A_USE
200km Sep 1, 2023
5fc1c59
Add ifdef for C2A_USE
200km Sep 1, 2023
3e3831a
Add header for macro
200km Sep 1, 2023
094690b
Add initialize files
200km Sep 1, 2023
340608d
Revert unnecessary change in this PR
200km Sep 1, 2023
89ada36
Revert unnecessary change in this PR
200km Sep 1, 2023
69fe068
Add assertion for let commands
200km Sep 2, 2023
ed32688
Fix format
200km Sep 2, 2023
54582e1
Add enable flag
200km Sep 2, 2023
db4b6a9
Fix to use S2E's endian_memcpy
200km Sep 4, 2023
0c67731
Fix format
200km Sep 4, 2023
6ed998c
Add comments to describe used C2A features
200km Sep 11, 2023
9c9369c
Remove Cmd_ prefix check for v3.9.0 C2A
200km Sep 27, 2023
c7bcbd1
Merge with develop
200km Oct 4, 2023
46d1cfa
Update src/components/real/communication/wings_command_sender_to_c2a.cpp
200km Oct 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.# ===== Example =====
.#
.# コメントは読み飛ばされる
.#
.MOBC_RT.Cmd_NOP
wait_sec 5
MOBC_RT.Cmd_MM_START_TRANSITION 2
wait_sec 3
check_value # check_valueは今は使えず読み飛ばされる
let mode_id = 2 # letはいまはつかえない
AOBC_RT.Cmd_MM_START_TRANSITION {mode_id} # letを前提とした{}付きの引数があるとコマンドは送信されない
.#
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[WINGS_COMMAND_SENDER_TO_C2A]
command_send_enable = ENABLE
prescaler = 1
c2a_command_database_file = ../../data/initialize_files/components/wings_command_sender_to_c2a/SAMPLE_MOBC_CMD_DB_CMD_DB.csv
wings_operation_file = ../../data/initialize_files/components/wings_command_sender_to_c2a/example.ops
1 change: 1 addition & 0 deletions src/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ real/cdh/on_board_computer_with_c2a.cpp
real/communication/antenna.cpp
real/communication/antenna_radiation_pattern.cpp
real/communication/ground_station_calculator.cpp
real/communication/wings_command_sender_to_c2a.cpp

examples/example_change_structure.cpp
examples/example_serial_communication_with_obc.cpp
Expand Down
1 change: 1 addition & 0 deletions src/components/real/cdh/on_board_computer_with_c2a.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* @file on_board_computer_with_c2a.cpp
* @brief Class to emulate on board computer with C2A flight software
* @note Used C2A functions: TMGR_init, C2A_core_init, WDT_init, TMGR_clear, TMGR_count_up_master_clock, TDSP_execute_pl_as_task_list
*/

#include "on_board_computer_with_c2a.hpp"
Expand Down
147 changes: 147 additions & 0 deletions src/components/real/communication/wings_command_sender_to_c2a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* @file wings_command_sender_to_c2a.cpp
* @brief A component to send C2A command using WINGS operation file
* @note Used C2A functions: CCP_register_rtc, CCP_register_tlc
* Used C2A structures: CMD_CODE
*/

#include "wings_command_sender_to_c2a.hpp"

#include <library/initialize/initialize_file_access.hpp>
#include <library/utilities/macros.hpp>
#include <regex>

#ifdef USE_C2A
#if C2A_CORE_VER_MAJOR == 4
// c2a-core v4
#include "src_core/tlm_cmd/common_cmd_packet_util.h"
#elif C2A_CORE_VER_MAJOR <= 3
// c2a-core <= v3
#include "src_core/TlmCmd/common_cmd_packet_util.h"
#else
#error "c2a-core version is not supported"
#endif // c2a-core version header
#endif // USE_C2A

void WingsCommandSenderToC2a::MainRoutine(const int time_count) {
UNUSED(time_count);
if (is_enabled_ == false) return;
if (is_end_of_line_ == true) return;
if (wait_s_ <= 0.0) {
std::string line = wings_operation_file_.GetLatestLine();
if (line == "EOL") {
is_end_of_line_ = true;
} else {
ExecuteCommandLine(line);
}
} else {
wait_s_ -= step_width_s_;
}
}

void WingsCommandSenderToC2a::ExecuteCommandLine(const std::string line) {
// Separate with space
std::istringstream token_stream(line);
std::string token;
std::vector<std::string> tokens;
while (token_stream >> token) {
tokens.push_back(token);
}

// Handle WINGS commands
if (tokens[0].find("wait_sec") == 0) {
// wait process
wait_s_ = std::stod(tokens[1]);
} else if (tokens[0].find("check_value") == 0) {
// TODO: Support check_value
wait_s_ = 0;
} else if (tokens[0].find("let") == 0) {
// TODO: Support let command
wait_s_ = 0;
} else {
wait_s_ = 0;
AnalyzeC2aCommand(tokens);
}

return;
}

void WingsCommandSenderToC2a::AnalyzeC2aCommand(const std::vector<std::string> tokens) {
#ifdef USE_C2A
// Recognize command
size_t first_underscore_position = tokens[0].find('_');
if (first_underscore_position == std::string::npos) return;
size_t first_dot_position = tokens[0].find('.');
if (first_underscore_position == std::string::npos) return;

std::string target_name = tokens[0].substr(0, first_underscore_position);
// TODO check target?
std::string command_type = tokens[0].substr(first_underscore_position + 1, first_dot_position - (first_underscore_position + 1));
std::string command_name = tokens[0].substr(first_dot_position + 1);

// Get command code
C2aCommandInformation cmd_info = c2a_command_database_.GetCommandInformation(command_name);
if (cmd_info.GetCommandName() == "Error") {
// TODO: error handling
}
CMD_CODE cmd_id = (CMD_CODE)cmd_info.GetCommandId();

// Get arguments
std::vector<std::string> arguments;
for (size_t i = 1; i < tokens.size(); i++) {
if (tokens[i].find("{") == 0) return; // let command is not supported now TODO: support let command
arguments.push_back(tokens[i]);
}
// Check number of arguments
if (arguments.size() != cmd_info.GetNumberOfArgument()) {
// TODO: error handling
}

// Decode arguments
// Read TI
uint32_t ti = 0;
if (command_type == "TL" || command_type == "BL") {
ti = (uint32_t)std::stoi(arguments[0]);
arguments.erase(arguments.begin());
}
// Read arguments
uint8_t param[CSP_MAX_LEN];
uint16_t param_len = 0;
for (size_t arg_num = 0; arg_num < arguments.size(); arg_num++) {
size_t len = 0;
DecodeC2aCommandArgument(cmd_info.GetArgumentType(arg_num), arguments[arg_num], param + param_len, len);
param_len += (uint16_t)len;
}

// Send command
if (command_type == "RT") {
CCP_register_rtc(cmd_id, param, param_len);
} else if (command_type == "TL") {
CCP_register_tlc(ti, TLCD_ID_FROM_GS, cmd_id, param, param_len);
} else if (command_type == "BL") {
// TODO: BL実装したいが、register_blがない?
} else {
// Not reach
}
#else
UNUSED(tokens);
#endif
}

WingsCommandSenderToC2a InitWingsCommandSenderToC2a(ClockGenerator* clock_generator, const double compo_update_step_s,
const std::string initialize_file) {
IniAccess ini_access(initialize_file);
std::string section = "WINGS_COMMAND_SENDER_TO_C2A";

bool is_enabled = ini_access.ReadEnable(section.c_str(), "command_send_enable");

int prescaler = ini_access.ReadInt(section.c_str(), "prescaler");
if (prescaler <= 1) prescaler = 1;

double step_width_s = (double)prescaler * compo_update_step_s;

std::string c2a_command_data_base_file = ini_access.ReadString(section.c_str(), "c2a_command_database_file");
std::string wings_operation_file = ini_access.ReadString(section.c_str(), "wings_operation_file");

return WingsCommandSenderToC2a(prescaler, clock_generator, step_width_s, c2a_command_data_base_file, wings_operation_file, is_enabled);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* @file wings_command_sender_to_c2a.hpp
* @brief A component to send command using WINGS operation file
*/

#ifndef S2E_COMPONENTS_REAL_COMMUNICATION_C2A_COMMAND_SENDER_HPP_
#define S2E_COMPONENTS_REAL_COMMUNICATION_C2A_COMMAND_SENDER_HPP_

#include <library/initialize/c2a_command_database.hpp>
#include <library/initialize/wings_operation_file.hpp>

#include "../../base/component.hpp"

/*
* @class C2aCommandSender
* @brief A component to send C2A command using WINGS operation file
*/
class WingsCommandSenderToC2a : public Component {
public:
/**
* @fn WingsCommandSenderToC2a
* @brief Constructor
* @param [in]
*/
WingsCommandSenderToC2a(int prescaler, ClockGenerator* clock_generator, const double step_width_s, const std::string command_database_file,
const std::string operation_file, const bool is_enabled)
: Component(prescaler, clock_generator),
c2a_command_database_(command_database_file),
wings_operation_file_(operation_file),
is_enabled_(is_enabled),
step_width_s_(step_width_s) {}

/**
* @fn ~WingsCommandSenderToC2a
* @brief Destructor
*/
~WingsCommandSenderToC2a() {}

protected:
C2aCommandDatabase c2a_command_database_; //!< Command database
WingsOperationFile wings_operation_file_; //!< WINGS operation file
bool is_enabled_; //!< Enable flag
const double step_width_s_; //!< Step width to execute this component [s]
double wait_s_ = 0.0; //!< Wait counter [s]
bool is_end_of_line_ = false; //!< Flag to detect end of line

// Override functions for Component
/**
* @fn MainRoutine
* @brief Main routine to send command
*/
void MainRoutine(const int time_count) override;

/**
* @fn ExecuteCommandLine
* @brief Execute command line
* @param[in] line: Executed line
*/
void ExecuteCommandLine(const std::string line);

/**
* @fn AnalyzeC2aCommand
* @brief Analyze C2A command Line
* @param[in] tokens: Command line after space separation
*/
void AnalyzeC2aCommand(const std::vector<std::string> tokens);
};

/**
* @fn InitWingsCommandSenderToC2a
* @brief Initialize WingsCommandSenderToC2a
* @param[in] clock_generator: Clock generator
* @param[in] compo_update_step_s: Component update step time [s]
* @param[in] initialize_file: Initialize file name
*/
WingsCommandSenderToC2a InitWingsCommandSenderToC2a(ClockGenerator* clock_generator, const double compo_update_step_s,
const std::string initialize_file);

#endif // S2E_COMPONENTS_REAL_COMMUNICATION_C2A_COMMAND_SENDER_HPP_
2 changes: 2 additions & 0 deletions src/library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_library(${PROJECT_NAME} STATIC
geodesy/geodetic_position.cpp

initialize/initialize_file_access.cpp
initialize/c2a_command_database.cpp
initialize/wings_operation_file.cpp

logger/logger.cpp
logger/initialize_log.cpp
Expand Down
Loading