Skip to content

Commit

Permalink
Allow synchronized nodes to manage timestamping without any different…
Browse files Browse the repository at this point in the history
…ial computation, fix #444
  • Loading branch information
nicolas-rabault committed Aug 28, 2023
1 parent fa8d53b commit 31a9a41
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
7 changes: 4 additions & 3 deletions engine/IO/inc/luos_phy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C"

// Phy creation
luos_phy_t *Phy_Create(JOB_CB job_cb, RUN_TOPO run_topo, RESET_PHY reset_phy); // Use it to reference your phy to Luos.
void Phy_DisableSynchro(luos_phy_t *phy_ptr); // Use it to disable the luos synchronisation mechanism with this phy.

// Topology management
void Phy_TopologyNext(void); // Use it to find the next node that need to be detected accross phys.
Expand All @@ -44,9 +45,9 @@ extern "C"
void Phy_ResetMsg(luos_phy_t *phy_ptr); // Call this function to reset the rx process.

// Tx management
time_luos_t Phy_ComputeMsgTimestamp(phy_job_t *job); // Use it to compute the timestamp of the message to send.
uint64_t Phy_GetTimestamp(void); // Use it to get the current timestamp in ns.
uint16_t Phy_GetNodeId(void); // Use it to get your current node id. (This can be used to compute priority or controled latency avoiding infinite collision condition)
time_luos_t Phy_ComputeMsgTimestamp(luos_phy_t *phy_ptr, phy_job_t *job); // Use it to compute the timestamp of the message to send.
uint64_t Phy_GetTimestamp(void); // Use it to get the current timestamp in ns.
uint16_t Phy_GetNodeId(void); // Use it to get your current node id. (This can be used to compute priority or controled latency avoiding infinite collision condition)

// Job management
void Phy_FailedJob(luos_phy_t *phy_ptr, phy_job_t *job); // If some messages failed to be sent, call this function to consider the target as dead
Expand Down
1 change: 1 addition & 0 deletions engine/IO/inc/struct_phy.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef struct luos_phy_t
volatile uint8_t rx_alloc_job : 1; // If true, Luosio_Loop funciton will need to filter already received data, if filter is ok, set rx_keep, alloc needed space, move *rx_data to point at the good place on allocator, put the number of bytes remaining on rx_size, and copy already received data in the allocated space.
volatile uint8_t rx_keep : 1; // True if we want to keep the received data.
volatile uint8_t rx_ack : 1; // True if we need to generate an acknoledgement for this message.
uint8_t enable_synchro : 1; // True if we want Luos to manage time synchronisation for this phy.
};

// *** RX Private data ***
Expand Down
23 changes: 21 additions & 2 deletions engine/IO/src/luos_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,22 @@ luos_phy_t *Phy_Get(uint8_t id, JOB_CB job_cb, RUN_TOPO run_topo, RESET_PHY rese
phy_ctx.phy[id].run_topo = run_topo;
phy_ctx.phy[id].reset_phy = reset_phy;
phy_ctx.phy[id].job_nb = 0;
// By default enable synchronisation for all phys
phy_ctx.phy[id].enable_synchro = true;
// Return the phy pointer
return &phy_ctx.phy[id];
}

/******************************************************************************
* @brief Disable synchronisation for a specific phy. Use it if your network already manage synchronisation.
* @param phy_ptr pointer on the phy we want to disable synchronisation
* @return None
******************************************************************************/
void Phy_DisableSynchro(luos_phy_t *phy_ptr)
{
phy_ptr->enable_synchro = false;
}

/******************************************************************************
* @brief return a local physical layer pointer (only used by LuosIO, this function is private)
* @param id of the phy we want
Expand Down Expand Up @@ -694,9 +706,16 @@ _CRITICAL void Phy_ResetMsg(luos_phy_t *phy_ptr)
* @param job Pointer to the job concerned by this message
* @return None
******************************************************************************/
time_luos_t Phy_ComputeMsgTimestamp(phy_job_t *job)
time_luos_t Phy_ComputeMsgTimestamp(luos_phy_t *phy_ptr, phy_job_t *job)
{
LUOS_ASSERT((job != NULL) && (job->msg_pt != NULL) && (job->timestamp == true));
LUOS_ASSERT((job != NULL) && (job->msg_pt != NULL) && (job->timestamp == true) && (phy_ptr != NULL));
if (phy_ptr->enable_synchro == false)
{
// We don't want to synchronize this phy, just return the timestamp
time_luos_t timestamp_date;
memcpy(&timestamp_date, &job->msg_pt->data[job->msg_pt->header.size], sizeof(time_luos_t));
return timestamp_date;
}
return Timestamp_ConvertToLatency(job->msg_pt);
}

Expand Down
2 changes: 1 addition & 1 deletion network/robus_network/src/transmission.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ _CRITICAL void Transmit_Process()
{

// Convert date to a sendable timestamp and put it on the encapsulation
jobEncaps->timestamp = Phy_ComputeMsgTimestamp(job);
jobEncaps->timestamp = Phy_ComputeMsgTimestamp(robus_phy, job);

jobEncaps->timestamped_crc = ll_crc_compute(jobEncaps->unmaped, sizeof(time_luos_t), crc_val);
jobEncaps->size = sizeof(time_luos_t) + CRC_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion network/serial_network/src/serial_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ _CRITICAL void Serial_Send(void)
if (job->timestamp)
{
// Convert date to a sendable timestamp and put it in the end of the message
time_luos_t timestamp = Phy_ComputeMsgTimestamp(job);
time_luos_t timestamp = Phy_ComputeMsgTimestamp(phy_serial, job);
memcpy(&TX_data[sizeof(SerialHeader_t) + job->size - +sizeof(timestamp)], &timestamp, sizeof(time_luos_t));
TX_data[sizeof(SerialHeader_t) + job->size] = SERIAL_FOOTER;
}
Expand Down
36 changes: 25 additions & 11 deletions test/tests_io/test_phy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ void unittest_phy_ComputeHeader()
{
phy_test_reset();
// Create a fake service with id 1
Phy_AddLocalServices(1, 1);
Phy_AddLocalServices(1, 1);

msg_t msg;
msg.header.config = BASE_PROTOCOL;
Expand Down Expand Up @@ -740,7 +740,7 @@ void unittest_phy_ComputeHeader()
{
phy_test_reset();
// Create a fake service with id 1
Phy_AddLocalServices(1, 1);
Phy_AddLocalServices(1, 1);

msg_t msg;
msg.header.config = TIMESTAMP_PROTOCOL;
Expand Down Expand Up @@ -781,7 +781,7 @@ void unittest_phy_ComputeHeader()
{
phy_test_reset();
// Create a fake service with id 1
Phy_AddLocalServices(1, 1);
Phy_AddLocalServices(1, 1);

msg_t msg;
msg.header.config = BASE_PROTOCOL;
Expand Down Expand Up @@ -822,7 +822,7 @@ void unittest_phy_ComputeHeader()
{
phy_test_reset();
// Create a fake service with id 1
Phy_AddLocalServices(1, 1);
Phy_AddLocalServices(1, 1);

msg_t msg;
msg.header.config = TIMESTAMP_PROTOCOL;
Expand Down Expand Up @@ -863,7 +863,7 @@ void unittest_phy_ComputeHeader()
{
phy_test_reset();
// Create a fake service with id 1
Phy_AddLocalServices(1, 1);
Phy_AddLocalServices(1, 1);

msg_t msg;
msg.header.config = TIMESTAMP_PROTOCOL;
Expand Down Expand Up @@ -903,7 +903,7 @@ void unittest_phy_ComputeHeader()
{
phy_test_reset();
// Create a fake service with id 1
Phy_AddLocalServices(1, 1);
Phy_AddLocalServices(1, 1);

msg_t msg;
msg.header.config = TIMESTAMP_PROTOCOL;
Expand Down Expand Up @@ -1066,7 +1066,16 @@ void unittest_phy_ComputeTimestamp()
{
TRY
{
Phy_ComputeMsgTimestamp(NULL);
phy_job_t job;
Phy_ComputeMsgTimestamp(NULL, &job);
}
TEST_ASSERT_TRUE(IS_ASSERT());
END_TRY;

TRY
{
luos_phy phy;
Phy_ComputeMsgTimestamp(luos_phy, NULL);
}
TEST_ASSERT_TRUE(IS_ASSERT());
END_TRY;
Expand All @@ -1076,7 +1085,7 @@ void unittest_phy_ComputeTimestamp()
phy_job_t job;
job.timestamp = false;
job.data_pt = (uint8_t *)msg_buffer;
Phy_ComputeMsgTimestamp(&job);
Phy_ComputeMsgTimestamp(luos_phy, &job);
}
TEST_ASSERT_TRUE(IS_ASSERT());
END_TRY;
Expand All @@ -1086,7 +1095,7 @@ void unittest_phy_ComputeTimestamp()
phy_job_t job;
job.timestamp = true;
job.data_pt = NULL;
Phy_ComputeMsgTimestamp(&job);
Phy_ComputeMsgTimestamp(luos_phy, &job);
}
TEST_ASSERT_TRUE(IS_ASSERT());
END_TRY;
Expand Down Expand Up @@ -1115,11 +1124,16 @@ void unittest_phy_ComputeTimestamp()
volatile time_luos_t timestamp = TimeOD_TimeFrom_ns(10);
memcpy(&msg->data[msg->header.size], (void *)&timestamp, sizeof(time_luos_t));

volatile time_luos_t resulting_latency = Phy_ComputeMsgTimestamp(&job);
volatile time_luos_t resulting_latency = Phy_ComputeMsgTimestamp(luos_phy, &job);

TEST_ASSERT_EQUAL(0xAE, job.msg_pt->data[0]);
#ifndef _WIN32
TEST_ASSERT_NOT_EQUAL(TimeOD_TimeTo_ns(timestamp), TimeOD_TimeTo_ns(resulting_latency));
#endif
Phy_DisableSynchro(luos_phy);
resulting_latency = Phy_ComputeMsgTimestamp(luos_phy, &job);

#ifndef _WIN32
TEST_ASSERT_EQUAL(TimeOD_TimeTo_ns(timestamp), TimeOD_TimeTo_ns(resulting_latency));
#endif
}
CATCH
Expand Down

0 comments on commit 31a9a41

Please sign in to comment.