Skip to content

Commit

Permalink
Merge pull request #182 from ut-issl/feature/add_utl_cmd_tests
Browse files Browse the repository at this point in the history
Pre Release (v3.5.0-beta.1): UTL cmd のテストを追加する
  • Loading branch information
chutaro authored Jan 19, 2022
2 parents 32e5562 + 770a63d commit 18be6ed
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import sys
import time
import random

import isslwings as wings
import pytest
Expand All @@ -18,28 +19,31 @@


# C2Aでのdefine値
OBCT_STEP_IN_MSEC = 1 # 1 step で何 ms か
OBCT_STEPS_PER_CYCLE = 100 # 何 step で 1 cycle か
OBCT_CYCLES_PER_SEC = 1000 // OBCT_STEP_IN_MSEC // OBCT_STEPS_PER_CYCLE # 1 s で何 cycle か
TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL = 1577836800.0
TL_ID_DEPLOY_FROM_GS = 0

TL_DFAULT_PAGE_NO = 0

@pytest.mark.sils
@pytest.mark.real
def test_tmgr_set_time():

ret = wings.util.send_cmd_and_confirm(
assert "PRM" == wings.util.send_rt_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_TIME, (0xFFFFFFFF,), c2a_enum.Tlm_CODE_HK
)
assert ret == "PRM"

# TL2のテレメループが途切れないように、現在時刻より未来のTIに飛ばす
tlm_HK = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_HK
)
target_ti = tlm_HK["HK.SH.TI"] + 1000

ret = wings.util.send_cmd_and_confirm(
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_TIME, (target_ti,), c2a_enum.Tlm_CODE_HK
)
assert ret == "SUC"
tlm_HK = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_HK
)
Expand All @@ -51,57 +55,198 @@ def test_tmgr_set_time():
@pytest.mark.real
def test_tmgr_set_unixtime():

# unixtime_at_ti0 を current_unixtime とランダムな TI で更新
current_unixtime = time.time()

ti = 1000
step = 60
ret = wings.util.send_cmd_and_confirm(
ti = random.randrange(1000)
step = random.randrange(OBCT_STEPS_PER_CYCLE) # step は OBCT_STEPS_PER_CYCLE 未満 とすること
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TMGR_SET_UNIXTIME,
(current_unixtime, ti, step),
c2a_enum.Tlm_CODE_HK,
)
assert ret == "SUC"

# 0.05秒の精度でunixtimeが正確に登録されているか確認
tlm_HK = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_HK
)
unixtime_at_ti0 = current_unixtime - (ti / 10) - (step / 1000)
assert tlm_HK["HK.OBC_TM_UNIXTIME_AT_TI0"] > unixtime_at_ti0 - 0.05
assert tlm_HK["HK.OBC_TM_UNIXTIME_AT_TI0"] < unixtime_at_ti0 + 0.05
unixtime_at_ti0 = current_unixtime - (ti / OBCT_CYCLES_PER_SEC) - (step / OBCT_CYCLES_PER_SEC / OBCT_STEPS_PER_CYCLE)
assert tlm_HK["HK.OBC_TM_UNIXTIME_AT_TI0"] < unixtime_at_ti0 + OBCT_STEP_IN_MSEC / 1000
assert tlm_HK["HK.OBC_TM_UNIXTIME_AT_TI0"] > unixtime_at_ti0 - OBCT_STEP_IN_MSEC / 1000


@pytest.mark.sils
@pytest.mark.real
def test_tmgr_set_utl_unixtime_epoch():

epoch_set = time.time() # 現在のunixtimeをepochに設定する

ret = wings.util.send_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_UTL_UNIXTIME_EPOCH, (epoch_set,), c2a_enum.Tlm_CODE_HK
# epoch を現在の unixtime に変更
new_epoch = time.time()
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_UTL_UNIXTIME_EPOCH, (new_epoch,), c2a_enum.Tlm_CODE_HK
)
assert ret == "SUC"

tlm_MOBC = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_MOBC
)
assert tlm_MOBC["MOBC.TM_UTL_UNIXTIME_EPOCH"] == epoch_set
assert tlm_MOBC["MOBC.TM_UTL_UNIXTIME_EPOCH"] == new_epoch


@pytest.mark.sils
@pytest.mark.real
def test_tmgr_utl_cmd():

# unixtime_at_ti0 > epoch の場合(正常時)
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_UTL_UNIXTIME_EPOCH, (TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL,), c2a_enum.Tlm_CODE_HK
)
unixtime_at_ti0 = time.time()
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TMGR_SET_UNIXTIME,
(unixtime_at_ti0, 0, 0),
c2a_enum.Tlm_CODE_HK,
)

test_utl_cmd_ten_times(unixtime_at_ti0, TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL)


# unixtime_at_ti0 < epoch の場合(TL0に登録されないことを確認する)
unixtime_at_ti0 = TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL - 100
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TMGR_SET_UNIXTIME,
(unixtime_at_ti0, 0, 0),
c2a_enum.Tlm_CODE_HK,
)

test_utl_cmd_ten_times(unixtime_at_ti0, TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL)


# epoch が変わった場合
new_epoch = time.time()
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_UTL_UNIXTIME_EPOCH, (new_epoch,), c2a_enum.Tlm_CODE_HK
)

unixtime_at_ti0 = time.time() + 100
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TMGR_SET_UNIXTIME,
(unixtime_at_ti0, 0, 0),
c2a_enum.Tlm_CODE_HK,
)

test_utl_cmd_ten_times(unixtime_at_ti0, new_epoch)


@pytest.mark.sils
@pytest.mark.real
def test_tmgr_final_check():
# unixtime_at_ti0 を初期化する
unixtime_at_ti0 = 0.0
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TMGR_SET_UNIXTIME,
(unixtime_at_ti0, 0, 0),
c2a_enum.Tlm_CODE_HK,
)

# epochをデフォルトに戻す
ret = wings.util.send_cmd_and_confirm(
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope, c2a_enum.Cmd_CODE_TMGR_SET_UTL_UNIXTIME_EPOCH, (TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL,), c2a_enum.Tlm_CODE_HK
)
assert ret == "SUC"

tlm_MOBC = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_MOBC

def test_utl_cmd_ten_times(unixtime_at_ti0, utl_unixtime_epoch):
# 最初にTL0をクリアしておく
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TLCD_CLEAR_ALL_TIMELINE,
(TL_ID_DEPLOY_FROM_GS, ),
c2a_enum.Tlm_CODE_HK,
)

tlm_HK = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_HK
)
assert tlm_MOBC["MOBC.TM_UTL_UNIXTIME_EPOCH"] == TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL
unixtime_now = unixtime_at_ti0 + tlm_HK["HK.SH.TI"] / OBCT_CYCLES_PER_SEC

# NOP を10個, 未来のランダムな unixtime で登録する
unixtime_of_cmds = generate_random_unixtime(unixtime_now, 10)
send_utl_nops(unixtime_of_cmds)

# 重複を削除して時刻順に並べ替え
unixtime_of_cmds = list(set(unixtime_of_cmds))
unixtime_of_cmds.sort()


# TL0 に正しく登録されているか確認
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TLCD_SET_LINE_NO_FOR_TIMELINE_TLM,
(TL_ID_DEPLOY_FROM_GS, ),
c2a_enum.Tlm_CODE_HK,
)
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TLCD_SET_PAGE_FOR_TLM,
(TL_DFAULT_PAGE_NO, ),
c2a_enum.Tlm_CODE_HK,
)

tlm_TL = wings.util.generate_and_receive_tlm(
ope, c2a_enum.Cmd_CODE_GENERATE_TLM, c2a_enum.Tlm_CODE_TL
)
assert tlm_TL["TL.LINE_NO"] == TL_ID_DEPLOY_FROM_GS
assert tlm_TL["TL.PAGE_NO"] == TL_DFAULT_PAGE_NO

for i, unixtime in enumerate(unixtime_of_cmds):
tlm_name = "TL.CMD" + str(i) + "_TI"
ti = calc_ti_from_unixtime(unixtime, unixtime_at_ti0, utl_unixtime_epoch)

assert tlm_TL[tlm_name] > ti - 1
assert tlm_TL[tlm_name] < ti + 1

# 最後にTL0をもう一度クリアする
assert "SUC" == wings.util.send_rt_cmd_and_confirm(
ope,
c2a_enum.Cmd_CODE_TLCD_CLEAR_ALL_TIMELINE,
(TL_ID_DEPLOY_FROM_GS, ),
c2a_enum.Tlm_CODE_HK,
)


# 未来のランダムな時刻の unixtime を num 個生成する
def generate_random_unixtime(unixtime_now, num):
# TODO: wingsがutl_cmdの時刻引数を0.1秒刻みで受け付けるように改修されたら, 整数縛りをなくす
unixtime_future = (int)(unixtime_now) + 1000

return [unixtime_future + random.randrange(100) for i in range(num)]


def send_utl_nops(unixtime_of_cmds):
for unixtime in unixtime_of_cmds:
# utl_cmd のwings側の時刻引数は一般の unixtime であることに注意
wings.util.send_utl_cmd(
ope, unixtime, c2a_enum.Cmd_CODE_NOP, (),
)


def calc_ti_from_unixtime(unixtime, unixtime_at_ti0, epoch):
if unixtime_at_ti0 <= epoch:
# この場合, 意図した TI で登録できないので C2A では実行時刻 TI = 0 が返され, TL0 には登録されない
# TLM_TL の TI の値はデフォルト値のゼロになっているべきなので, 整合するようにゼロを返す
return 0

ti = (unixtime - unixtime_at_ti0) * OBCT_CYCLES_PER_SEC # 概算値なので小数のまま

# utl_unixtime_epoch をデフォルトから変更した場合, wings側とずれが生じる
# epoch が増えた分だけ, C2A 上では utl_unixtime_at_ti0 が小さくなり, 実行時刻 TI は大きく見積もられる
return ti + (epoch - TMGR_DEFAULT_UNIXTIME_EPOCH_FOR_UTL) * OBCT_CYCLES_PER_SEC


if __name__ == "__main__":
# test_tmgr_set_time()
# test_tmgr_set_unixtime()
# test_tmgr_set_utl_unixtime_epoch()
# test_tmgr_utl_cmd()
pass
7 changes: 7 additions & 0 deletions System/TimeManager/time_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ cycle_t TMGR_get_utl_unixtime_from_unixtime(const double unixtime)
cycle_t TMGR_get_ti_from_utl_unixtime(const cycle_t utl_unixtime)
{
cycle_t utl_unixtime_at_ti0 = TMGR_get_utl_unixtime_from_unixtime(time_manager_.unixtime_info_.unixtime_at_ti0);

// unixtime_at_ti0 <= epoch となるのはおかしいのでゼロを返す
if (utl_unixtime_at_ti0 == 0) return 0;

// unixtime_at_ti0 より小さい実行時刻は無効なのでゼロを返す
if (utl_unixtime < utl_unixtime_at_ti0) return 0;

return utl_unixtime - utl_unixtime_at_ti0;
}

Expand Down
3 changes: 2 additions & 1 deletion System/TimeManager/time_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ cycle_t TMGR_get_utl_unixtime_from_unixtime(const double unixtime);
* @brief 引数で指定された utl_unixtime に対応する TI を返す
* @note UTL_cmd で実行時刻情報を TI に変換する際に用いる
* @param[in] utl_unixtime
* @return TI (total_cycleのこと)
* @retval 0 : "unixtime_at_ti0 <= utl_unixtime_epoch" or "utl_unixtime < utl_unixtime_at_ti0" の場合
* @retval TI (total_cycleのこと) : それ以外の場合
*/
cycle_t TMGR_get_ti_from_utl_unixtime(const cycle_t utl_unixtime);

Expand Down
2 changes: 1 addition & 1 deletion c2a_core_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ void C2A_core_main(void);
#define C2A_CORE_VER_MAJOR (3)
#define C2A_CORE_VER_MINOR (5)
#define C2A_CORE_VER_PATCH (0)
#define C2A_CORE_VER_PRE ("beta.0")
#define C2A_CORE_VER_PRE ("beta.1")

#endif

0 comments on commit 18be6ed

Please sign in to comment.