Add broadcast state (for temperature sensor)

This commit is contained in:
Pascal Serrarens 2024-09-07 17:21:34 +02:00
parent 2134d64c80
commit 2d83c0296b
5 changed files with 66 additions and 3 deletions

View File

@ -210,10 +210,27 @@ void NetworkSync::PublishClient() {
#endif #endif
} }
void NetworkSync::BroadcastState(Sensor* sensor) {
unsigned char ix = 0;
buffer[ix++] = StateMsg;
buffer[ix++] = sensor->type;
float* value = (float*)sensor->GetValue();
SendFloat16(buffer, &ix, *value);
SendBuffer(ix);
}
void NetworkSync::BroadcastPerception(Roboid *roboid) { void NetworkSync::BroadcastPerception(Roboid *roboid) {
if (roboid->perception == nullptr) Perception* perception = roboid->perception;
if (perception == nullptr)
return; return;
for (int sensorIx = 0; sensorIx < perception->sensorCount; sensorIx++) {
Sensor* sensor = perception->sensors[sensorIx];
if (sensor == nullptr)
continue;
// sensor->BroadcastState();
BroadcastState(sensor);
}
PublishTrackedObjects(roboid, roboid->perception->GetTrackedObjects()); PublishTrackedObjects(roboid, roboid->perception->GetTrackedObjects());
} }

View File

@ -11,6 +11,7 @@ namespace RoboidControl {
/// @brief Interface for synchronizaing state between clients across a network /// @brief Interface for synchronizaing state between clients across a network
class NetworkSync { class NetworkSync {
public: public:
NetworkSync() {};
NetworkSync(Roboid* roboid); NetworkSync(Roboid* roboid);
unsigned char networkId; unsigned char networkId;
@ -29,6 +30,7 @@ class NetworkSync {
static const unsigned char PoseMsg = 0x10; static const unsigned char PoseMsg = 0x10;
static const unsigned char PoseTypeMsg = 0x11; static const unsigned char PoseTypeMsg = 0x11;
static const unsigned char RelativePoseMsg = 0x12; static const unsigned char RelativePoseMsg = 0x12;
static const unsigned char StateMsg = 0x18;
/// @brief A bit pattern for the pose, stating that this message contains a /// @brief A bit pattern for the pose, stating that this message contains a
/// position in world coordinates /// position in world coordinates
static const unsigned char Pose_Position = 0x01; static const unsigned char Pose_Position = 0x01;
@ -82,6 +84,8 @@ class NetworkSync {
Roboid* roboid; Roboid* roboid;
NetworkPerception* networkPerception; NetworkPerception* networkPerception;
void BroadcastState(Sensor* sensor);
void PublishTrackedObject(Roboid* roboid, InterestingThing* object); void PublishTrackedObject(Roboid* roboid, InterestingThing* object);
void PublishRelativeObject(Buffer sendBuffer, void PublishRelativeObject(Buffer sendBuffer,
UInt8 parentId, UInt8 parentId,

View File

@ -14,6 +14,14 @@ public:
/// @brief Sets the parent Thing /// @brief Sets the parent Thing
/// @param parent The Thing which should become the parent /// @param parent The Thing which should become the parent
virtual void SetParent(Thing *parent) override; virtual void SetParent(Thing *parent) override;
virtual void BroadcastState() {};
enum SensorType {
Unknown = 0,
Temperature = 1,
};
virtual void* GetValue() { return nullptr; };
}; };
} // namespace RoboidControl } // namespace RoboidControl

15
TemperatureSensor.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "TemperatureSensor.h"
TemperatureSensor::TemperatureSensor()
{
this->type = (unsigned int) SensorType::Temperature;
}
float TemperatureSensor::InCelsius()
{
return this->temperature;
}
void* TemperatureSensor::GetValue() {
return &this->temperature;
}

19
TemperatureSensor.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "Sensor.h"
#include "NetworkSync.h"
namespace Passer::RoboidControl
{
class TemperatureSensor : public Sensor {
public:
TemperatureSensor();
float InCelsius();
virtual void* GetValue() override;
// virtual void BroadcastState() override;
protected:
float temperature = 0; // in Celsius
};
} // namespace Passer::RoboidControl
using namespace Passer::RoboidControl;