Skip to content

Commit 721f197

Browse files
committed
Moved pub sub test from OS to template repo
1 parent a8f7342 commit 721f197

6 files changed

Lines changed: 341 additions & 1 deletion

File tree

.cproject

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest}&quot;"/>
6363
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest/Inc}&quot;"/>
6464
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/SystemTypes}&quot;"/>
65+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest}&quot;"/>
66+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest/Inc}&quot;"/>
6567
</option>
6668
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.input.196614514" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.input"/>
6769
</tool>
@@ -112,6 +114,8 @@
112114
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest}&quot;"/>
113115
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest/Inc}&quot;"/>
114116
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/SystemTypes}&quot;"/>
117+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest}&quot;"/>
118+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest/Inc}&quot;"/>
115119
</option>
116120
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.2034336582" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c"/>
117121
</tool>
@@ -162,6 +166,8 @@
162166
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest}&quot;"/>
163167
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest/Inc}&quot;"/>
164168
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/SystemTypes}&quot;"/>
169+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest}&quot;"/>
170+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest/Inc}&quot;"/>
165171
</option>
166172
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard.959475609" name="Language standard" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard" useByScannerDiscovery="true" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.languagestandard.value.gnupp20" valueType="enumerated"/>
167173
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.otherflags.396192781" name="Other flags" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.option.otherflags" useByScannerDiscovery="true" valueType="stringList">
@@ -257,6 +263,8 @@
257263
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest}&quot;"/>
258264
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest/Inc}&quot;"/>
259265
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/SystemTypes}&quot;"/>
266+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest}&quot;"/>
267+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest/Inc}&quot;"/>
260268
</option>
261269
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.input.1577376816" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.input"/>
262270
</tool>
@@ -306,6 +314,8 @@
306314
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest}&quot;"/>
307315
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest/Inc}&quot;"/>
308316
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/SystemTypes}&quot;"/>
317+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest}&quot;"/>
318+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest/Inc}&quot;"/>
309319
</option>
310320
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.881679915" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c"/>
311321
</tool>
@@ -355,6 +365,8 @@
355365
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest}&quot;"/>
356366
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/PubSubTest/Inc}&quot;"/>
357367
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/SoarOS/Components/SystemTypes}&quot;"/>
368+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest}&quot;"/>
369+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Components/PubSubTest/Inc}&quot;"/>
358370
</option>
359371
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp.653368400" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp"/>
360372
</tool>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
********************************************************************************
3+
* @file PubSubReceive.hpp
4+
* @author shiva
5+
* @date Dec 14, 2024
6+
* @brief
7+
********************************************************************************
8+
*/
9+
10+
#ifndef PUBSUBRECEIEVE_HPP_
11+
#define PUBSUBRECEIEVE_HPP_
12+
13+
/************************************
14+
* INCLUDES
15+
************************************/
16+
#include "Task.hpp"
17+
#include "SystemDefines.hpp"
18+
19+
/************************************
20+
* MACROS AND DEFINES
21+
************************************/
22+
23+
/************************************
24+
* TYPEDEFS
25+
************************************/
26+
27+
/************************************
28+
* CLASS DEFINITIONS
29+
************************************/
30+
class PubSubReceive : public Task {
31+
public:
32+
static PubSubReceive& Inst() {
33+
static PubSubReceive inst;
34+
return inst;
35+
}
36+
37+
void InitTask();
38+
39+
protected:
40+
static void RunTask(void* pvParams) {
41+
PubSubReceive::Inst().Run(pvParams);
42+
} // Static Task Interface, passes control to the instance Run();
43+
void Run(void* pvParams); // Main run code
44+
void HandleCommand(Command& cm);
45+
void HandleDataBrokerCommand(const Command& cm);
46+
47+
private:
48+
// Private Functions
49+
PubSubReceive(); // Private constructor
50+
PubSubReceive(const PubSubReceive&); // Prevent copy-construction
51+
PubSubReceive& operator=(const PubSubReceive&); // Prevent assignment
52+
};
53+
54+
/************************************
55+
* FUNCTION DECLARATIONS
56+
************************************/
57+
58+
#endif /* PUBSUBRECEIEVE_HPP_ */
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
********************************************************************************
3+
* @file PubSubSend.hpp
4+
* @author shiva
5+
* @date Dec 14, 2024
6+
* @brief
7+
********************************************************************************
8+
*/
9+
10+
#ifndef PUBSUBSEND_HPP_
11+
#define PUBSUBSEND_HPP_
12+
13+
/************************************
14+
* INCLUDES
15+
************************************/
16+
#include "Task.hpp"
17+
#include "SystemDefines.hpp"
18+
19+
/************************************
20+
* MACROS AND DEFINES
21+
************************************/
22+
23+
/************************************
24+
* TYPEDEFS
25+
************************************/
26+
27+
/************************************
28+
* CLASS DEFINITIONS
29+
************************************/
30+
class PubSubSend : public Task {
31+
public:
32+
static PubSubSend& Inst() {
33+
static PubSubSend inst;
34+
return inst;
35+
}
36+
37+
void InitTask();
38+
39+
protected:
40+
static void RunTask(void* pvParams) {
41+
PubSubSend::Inst().Run(pvParams);
42+
} // Static Task Interface, passes control to the instance Run();
43+
void Run(void* pvParams); // Main run code
44+
void HandleCommand(Command& cm);
45+
46+
private:
47+
// Private Functions
48+
PubSubSend(); // Private constructor
49+
PubSubSend(const PubSubSend&); // Prevent copy-construction
50+
PubSubSend& operator=(const PubSubSend&); // Prevent assignment
51+
};
52+
53+
/************************************
54+
* FUNCTION DECLARATIONS
55+
************************************/
56+
57+
#endif /* PUBSUBSEND_HPP_ */
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
********************************************************************************
3+
* @file PubSubReceive.cpp
4+
* @author shiva
5+
* @date Dec 14, 2024
6+
* @brief
7+
********************************************************************************
8+
*/
9+
10+
/************************************
11+
* INCLUDES
12+
************************************/
13+
#include "PubSubReceive.hpp"
14+
#include "SystemDefines.hpp"
15+
#include "SensorDataTypes.hpp"
16+
#include "DataBroker.hpp"
17+
18+
/************************************
19+
* PRIVATE MACROS AND DEFINES
20+
************************************/
21+
22+
/************************************
23+
* VARIABLES
24+
************************************/
25+
26+
/************************************
27+
* FUNCTION DECLARATIONS
28+
************************************/
29+
30+
/************************************
31+
* FUNCTION DEFINITIONS
32+
************************************/
33+
34+
/**
35+
* @brief Constructor for PubSubReceive
36+
*/
37+
PubSubReceive::PubSubReceive() : Task(PUBSUB_RECEIVE_TASK_QUEUE_DEPTH_OBJS) {}
38+
39+
/**
40+
* @brief Initialize the PubSubReceive
41+
* Do not modify this function aside from adding the task name
42+
*/
43+
void PubSubReceive::InitTask() {
44+
// Make sure the task is not already initialized
45+
SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice");
46+
47+
BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubReceive::RunTask, (const char*)"PubSubReceive",
48+
(uint16_t)PUBSUB_RECEIVE_TASK_STACK_DEPTH_WORDS, (void*)this,
49+
(UBaseType_t)PUBSUB_RECEIVE_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle);
50+
51+
SOAR_ASSERT(rtValue == pdPASS, "PubSubReceive::InitTask() - xTaskCreate() failed");
52+
}
53+
54+
/**
55+
* @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized.
56+
* @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used
57+
*/
58+
void PubSubReceive::Run(void* pvParams) {
59+
// SOAR_PRINT("PUBSUB RECIEVE STARTED\n");
60+
DataBroker::Subscribe<IMUData>(this);
61+
// DataBroker::Unsubscribe<IMUData>(this);
62+
while (1) {
63+
/* Process commands in blocking mode */
64+
Command cm;
65+
bool res = qEvtQueue->ReceiveWait(cm);
66+
if (res) {
67+
HandleCommand(cm);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* @brief Handles a command
74+
* @param cm Command reference to handle
75+
*/
76+
void PubSubReceive::HandleCommand(Command& cm) {
77+
switch (cm.GetCommand()) {
78+
case DATA_BROKER_COMMAND:
79+
HandleDataBrokerCommand(cm);
80+
break;
81+
82+
default:
83+
SOAR_PRINT("PubSubReceive - Received Unsupported Command {%d}\n", cm.GetCommand());
84+
break;
85+
}
86+
87+
// No matter what we happens, we must reset allocated data
88+
cm.Reset();
89+
}
90+
91+
/**
92+
* @brief Handle all data broker commands
93+
* @param cm The command object with the data
94+
* Use cm.GetTaskCommand() to get the message type
95+
* Message types must be cast back into DataBrokerMessageTypes enum
96+
* Use cm.GetDataPointer() to get the pointer to the data
97+
*/
98+
void PubSubReceive::HandleDataBrokerCommand(const Command& cm) {
99+
DataBrokerMessageTypes messageType = DataBroker::getMessageType(cm);
100+
switch (messageType) {
101+
case DataBrokerMessageTypes::IMU_DATA: {
102+
IMUData imu_data = DataBroker::ExtractData<IMUData>(cm);
103+
SOAR_PRINT("\n IMU DATA : \n");
104+
SOAR_PRINT(" X -> %d \n", imu_data.accelX);
105+
SOAR_PRINT(" Y -> %d \n", imu_data.accelY);
106+
SOAR_PRINT(" Z -> %d \n", imu_data.accelZ);
107+
SOAR_PRINT("--DATA_END--\n\n");
108+
break;
109+
}
110+
111+
case DataBrokerMessageTypes::THERMOCOUPLE_DATA:
112+
break;
113+
114+
case DataBrokerMessageTypes::INVALID:
115+
[[fallthrough]];
116+
default:
117+
break;
118+
}
119+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
********************************************************************************
3+
* @file PubSubSend.cpp
4+
* @author shiva
5+
* @date Dec 14, 2024
6+
* @brief
7+
********************************************************************************
8+
*/
9+
10+
/************************************
11+
* INCLUDES
12+
************************************/
13+
#include "PubSubSend.hpp"
14+
#include "SystemDefines.hpp"
15+
#include "SensorDataTypes.hpp"
16+
#include "DataBroker.hpp"
17+
18+
/************************************
19+
* PRIVATE MACROS AND DEFINES
20+
************************************/
21+
22+
/************************************
23+
* VARIABLES
24+
************************************/
25+
26+
/************************************
27+
* FUNCTION DECLARATIONS
28+
************************************/
29+
30+
/************************************
31+
* FUNCTION DEFINITIONS
32+
************************************/
33+
34+
/**
35+
* @brief Constructor for PubSubSend
36+
*/
37+
PubSubSend::PubSubSend() : Task(PUBSUB_SEND_TASK_QUEUE_DEPTH_OBJS) {}
38+
39+
/**
40+
* @brief Initialize the PubSubSend
41+
* Do not modify this function aside from adding the task name
42+
*/
43+
void PubSubSend::InitTask() {
44+
// Make sure the task is not already initialized
45+
SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice");
46+
47+
BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubSend::RunTask, (const char*)"PubSubSend",
48+
(uint16_t)PUBSUB_SEND_TASK_STACK_DEPTH_WORDS, (void*)this,
49+
(UBaseType_t)PUBSUB_SEND_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle);
50+
51+
SOAR_ASSERT(rtValue == pdPASS, "PubSubSend::InitTask() - xTaskCreate() failed");
52+
}
53+
54+
/**
55+
* @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized.
56+
* @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used
57+
*/
58+
void PubSubSend::Run(void* pvParams) {
59+
// SOAR_PRINT("\nPUBSUB SEND STARTED\n");
60+
61+
while (1) {
62+
Command cm;
63+
if (qEvtQueue->Receive(cm, 5000)) {
64+
HandleCommand(cm);
65+
} else {
66+
IMUData imuData = {
67+
.accelX = 1,
68+
.accelY = 2,
69+
.accelZ = 3,
70+
};
71+
DataBroker::Publish<IMUData>(&imuData);
72+
73+
ThermocoupleData thermData = {
74+
.temperature = -52,
75+
};
76+
DataBroker::Publish<ThermocoupleData>(&thermData);
77+
}
78+
}
79+
}
80+
81+
/**
82+
* @brief Handles a command
83+
* @param cm Command reference to handle
84+
*/
85+
void PubSubSend::HandleCommand(Command& cm) {
86+
switch (cm.GetCommand()) {
87+
default:
88+
SOAR_PRINT("PubSubSend - Received Unsupported Command {%d}\n", cm.GetCommand());
89+
break;
90+
}
91+
92+
// No matter what we happens, we must reset allocated data
93+
cm.Reset();
94+
}

0 commit comments

Comments
 (0)