Skip to content

Commit 775b423

Browse files
author
Tobias Haeckel
committed
added couple new Channels
1 parent 48aa03e commit 775b423

3 files changed

Lines changed: 88 additions & 2 deletions

File tree

lib/APPRemoteControl/src/App.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static void App_cmdChannelCallback(const uint8_t* payload, const uint8_t payload
6464
static void App_motorSpeedSetpointsChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);
6565
static void App_statusChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);
6666
static void App_robotSpeedSetpointChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);
67+
static void App_timeSyncReqChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);
6768

6869
/******************************************************************************
6970
* Local Variables
@@ -78,6 +79,7 @@ App::App() :
7879
m_serialMuxProtChannelIdCurrentVehicleData(0U),
7980
m_serialMuxProtChannelIdStatus(0U),
8081
m_serialMuxProtChannelIdLineSensors(0U),
82+
m_serialMuxProtChannelIdTimeSyncRsp(0U),
8183
m_systemStateMachine(),
8284
m_controlInterval(),
8385
m_reportTimer(),
@@ -340,6 +342,9 @@ void App::reportVehicleData()
340342
averageCounts = m_movAvgProximitySensor.write(maxCounts);
341343

342344
odometry.getPosition(xPos, yPos);
345+
346+
payload.timestamp = static_cast<int64_t>(millis());
347+
343348
payload.xPos = xPos;
344349
payload.yPos = yPos;
345350
payload.orientation = odometry.getOrientation();
@@ -361,6 +366,7 @@ bool App::setupSerialMuxProt()
361366
m_smpServer.subscribeToChannel(MOTOR_SPEED_SETPOINT_CHANNEL_NAME, App_motorSpeedSetpointsChannelCallback);
362367
m_smpServer.subscribeToChannel(STATUS_CHANNEL_NAME, App_statusChannelCallback);
363368
m_smpServer.subscribeToChannel(ROBOT_SPEED_SETPOINT_CHANNEL_NAME, App_robotSpeedSetpointChannelCallback);
369+
m_smpServer.subscribeToChannel(TIME_SYNC_REQUEST_CHANNEL_NAME, App_timeSyncReqChannelCallback);
364370

365371
/* Channel creation. */
366372
m_serialMuxProtChannelIdRemoteCtrlRsp =
@@ -369,10 +375,13 @@ bool App::setupSerialMuxProt()
369375
m_smpServer.createChannel(CURRENT_VEHICLE_DATA_CHANNEL_NAME, CURRENT_VEHICLE_DATA_CHANNEL_DLC);
370376
m_serialMuxProtChannelIdStatus = m_smpServer.createChannel(STATUS_CHANNEL_NAME, STATUS_CHANNEL_DLC);
371377
m_serialMuxProtChannelIdLineSensors = m_smpServer.createChannel(LINE_SENSOR_CHANNEL_NAME, LINE_SENSOR_CHANNEL_DLC);
378+
m_serialMuxProtChannelIdTimeSyncRsp =
379+
m_smpServer.createChannel(TIME_SYNC_RESPONSE_CHANNEL_NAME, TIME_SYNC_RESPONSE_CHANNEL_DLC);
372380

373381
/* Channels succesfully created? */
374382
if ((0U != m_serialMuxProtChannelIdCurrentVehicleData) && (0U != m_serialMuxProtChannelIdRemoteCtrlRsp) &&
375-
(0U != m_serialMuxProtChannelIdStatus) && (0U != m_serialMuxProtChannelIdLineSensors))
383+
(0U != m_serialMuxProtChannelIdStatus) && (0U != m_serialMuxProtChannelIdLineSensors) &&
384+
(0U != m_serialMuxProtChannelIdTimeSyncRsp))
376385
{
377386
isSuccessful = true;
378387
}
@@ -483,4 +492,42 @@ void App_robotSpeedSetpointChannelCallback(const uint8_t* payload, const uint8_t
483492
/* Set the robot speeds. */
484493
DrivingState::getInstance().setRobotSpeeds(centerSpeed, angularSpeed);
485494
}
495+
}
496+
497+
/**
498+
* Receives time sync requests and responds with timestamps.
499+
*
500+
* @param[in] payload TimeSyncRequest structure.
501+
* @param[in] payloadSize Size of TimeSyncRequest structure.
502+
* @param[in] userData Instance of App class.
503+
*/
504+
void App_timeSyncReqChannelCallback(const uint8_t* payload,
505+
uint8_t payloadSize,
506+
void* userData)
507+
{
508+
const uint32_t t2 = millis();
509+
if ((payload == nullptr) ||
510+
(userData == nullptr) ||
511+
(payloadSize != TIME_SYNC_REQUEST_CHANNEL_DLC))
512+
{
513+
return;
514+
}
515+
516+
App* app = static_cast<App*>(userData);
517+
518+
TimeSyncRequest req;
519+
memcpy(&req, payload, sizeof(req));
520+
521+
app->handleTimeSyncRequest(req, t2);
522+
}
523+
524+
void App::handleTimeSyncRequest(const TimeSyncRequest& req, const uint32_t t2)
525+
{
526+
TimeSyncResponse rsp{req.seq, req.t1_ms, t2, millis()};
527+
528+
if (m_serialMuxProtChannelIdTimeSyncRsp == 0U)
529+
{
530+
return;
531+
}
532+
(void)m_smpServer.sendData(m_serialMuxProtChannelIdTimeSyncRsp, &rsp, sizeof(rsp));
486533
}

lib/APPRemoteControl/src/App.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ class App
9797
*/
9898
void systemStatusCallback(SMPChannelPayload::Status status);
9999

100+
/**
101+
* Handle a Time Sync request and send response via SerialMuxProt.
102+
*
103+
* @param[in] req Time sync request payload.
104+
*/
105+
void handleTimeSyncRequest(const TimeSyncRequest& req, uint32_t t2);
106+
100107
private:
101108
/** Differential drive control period in ms. */
102109
static const uint32_t DIFFERENTIAL_DRIVE_CONTROL_PERIOD = 5U;
@@ -133,6 +140,9 @@ class App
133140
/** SerialMuxProt Channel id for sending line sensors data. */
134141
uint8_t m_serialMuxProtChannelIdLineSensors;
135142

143+
/** SerialMuxProt Channel id for sending time sync responses. */
144+
uint8_t m_serialMuxProtChannelIdTimeSyncRsp;
145+
136146
/** The system state machine. */
137147
StateMachine m_systemStateMachine;
138148

lib/APPRemoteControl/src/SerialMuxChannels.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@
8888
/** DLC of Line Sensor Channel */
8989
#define LINE_SENSOR_CHANNEL_DLC (sizeof(LineSensorData))
9090

91+
/** Name of the Channel to receive Time Sync Request from the DCS. */
92+
#define TIME_SYNC_REQUEST_CHANNEL_NAME "TIME_SYNC_REQ"
93+
94+
/** DLC of Time Sync Request Channel */
95+
#define TIME_SYNC_REQUEST_CHANNEL_DLC (sizeof(TimeSyncRequest))
96+
97+
/** Name of the Channel to send Time Sync Response to the DCS. */
98+
#define TIME_SYNC_RESPONSE_CHANNEL_NAME "TIME_SYNC_RSP"
99+
100+
/** DLC of Time Sync Response Channel */
101+
#define TIME_SYNC_RESPONSE_CHANNEL_DLC (sizeof(TimeSyncResponse))
102+
91103
/******************************************************************************
92104
* Types and Classes
93105
*****************************************************************************/
@@ -196,6 +208,7 @@ typedef struct _RobotSpeed
196208
/** Struct of the "Current Vehicle Data" channel payload. */
197209
typedef struct _VehicleData
198210
{
211+
int64_t timestamp; /**< Timestamp [ms]. */
199212
int32_t xPos; /**< X position [mm]. */
200213
int32_t yPos; /**< Y position [mm]. */
201214
int32_t orientation; /**< Orientation [mrad]. */
@@ -217,8 +230,24 @@ typedef struct _LineSensorData
217230
uint16_t lineSensorData[5U]; /**< Line sensor data [digits] normalized to max 1000 digits. */
218231
} __attribute__((packed)) LineSensorData;
219232

233+
/** Struct of the "Time Sync Request" channel payload. */
234+
typedef struct _TimeSyncRequest
235+
{
236+
uint32_t seq; /**< Sequence number to match request/response. */
237+
uint32_t t1_ms;/**< Timestamp at sender when request left [ms]. */
238+
} __attribute__((packed)) TimeSyncRequest;
239+
240+
/** Struct of the "Time Sync Response" channel payload. */
241+
typedef struct _TimeSyncResponse
242+
{
243+
uint32_t seq; /**< Sequence number to match request/response. */
244+
uint32_t t1_ms; /**< Echo of request timestamp [ms]. */
245+
uint32_t t2_ms; /**< Timestamp at receiver when request arrived [ms]. */
246+
uint32_t t3_ms; /**< Timestamp at receiver when response sent [ms]. */
247+
} __attribute__((packed)) TimeSyncResponse;
248+
220249
/******************************************************************************
221250
* Functions
222251
*****************************************************************************/
223252

224-
#endif /* SERIAL_MUX_CHANNELS_H_ */
253+
#endif /* SERIAL_MUX_CHANNELS_H_ */

0 commit comments

Comments
 (0)