diff --git a/Arduino/Things/UltrasonicSensor.cpp b/Arduino/Things/UltrasonicSensor.cpp index 804c2bc..8fa940d 100644 --- a/Arduino/Things/UltrasonicSensor.cpp +++ b/Arduino/Things/UltrasonicSensor.cpp @@ -8,7 +8,7 @@ namespace Arduino { UltrasonicSensor::UltrasonicSensor(Configuration config, Thing* parent) : Thing(parent) { - this->type = Type::DistanceSensor; + this->type = Type::DistanceSensor; this->name = "Ultrasonic sensor"; this->pinTrigger = config.trigger; this->pinEcho = config.echo; @@ -57,6 +57,28 @@ void UltrasonicSensor::Update(bool recursive) { Thing::Update(recursive); } +#pragma region Distance sensor + +UltrasonicSensor::DistanceSensor::DistanceSensor( + UltrasonicSensor::Configuration config, + Thing* parent) + : RoboidControl::DistanceSensor(parent), ultrasonic(config, this) {} + +void UltrasonicSensor::DistanceSensor::Update(bool recursive) { + RoboidControl::DistanceSensor::Update(recursive); + this->ultrasonic.Update(false); + if (this->ultrasonic.distance > 0) + this->internalDistance = this->ultrasonic.distance; + else +#if ARDUNIO + this->internalDistance = INFINITY; +#else + this->internalDistance = std::numeric_limits::infinity(); +#endif +} + +#pragma endregion Distance sensor + #pragma region Touch sensor UltrasonicSensor::TouchSensor::TouchSensor(Configuration config, Thing* parent) @@ -66,10 +88,10 @@ void UltrasonicSensor::TouchSensor::Update(bool recursive) { RoboidControl::TouchSensor::Update(recursive); this->ultrasonic.Update(false); this->internalTouch = (this->ultrasonic.distance > 0 && - this->ultrasonic.distance <= this->touchDistance); + this->ultrasonic.distance <= this->touchDistance); } -#pragma region Touch sensor +#pragma endregion Touch sensor } // namespace Arduino } // namespace RoboidControl \ No newline at end of file diff --git a/Arduino/Things/UltrasonicSensor.h b/Arduino/Things/UltrasonicSensor.h index 8e72887..c991e23 100644 --- a/Arduino/Things/UltrasonicSensor.h +++ b/Arduino/Things/UltrasonicSensor.h @@ -1,5 +1,6 @@ #pragma once +#include "Things/DistanceSensor.h" #include "Things/TouchSensor.h" namespace RoboidControl { @@ -39,9 +40,26 @@ class UltrasonicSensor : Thing { unsigned char pinEcho = 0; public: + class DistanceSensor; class TouchSensor; }; +#pragma region Distance sensor + +class UltrasonicSensor::DistanceSensor : public RoboidControl::DistanceSensor { + public: + DistanceSensor(UltrasonicSensor::Configuration config, + Thing* parent = Thing::LocalRoot()); + + /// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs) + virtual void Update(bool recursive = false) override; + + protected: + UltrasonicSensor ultrasonic; +}; + +#pragma endregion Distance sensor + #pragma region Touch sensor class UltrasonicSensor::TouchSensor : public RoboidControl::TouchSensor { @@ -58,7 +76,7 @@ class UltrasonicSensor::TouchSensor : public RoboidControl::TouchSensor { UltrasonicSensor ultrasonic; }; -#pragma region Touch sensor +#pragma endregion Touch sensor } // namespace Arduino } // namespace RoboidControl \ No newline at end of file diff --git a/Things/DistanceSensor.cpp b/Things/DistanceSensor.cpp new file mode 100644 index 0000000..c33d64a --- /dev/null +++ b/Things/DistanceSensor.cpp @@ -0,0 +1,29 @@ +#include "DistanceSensor.h" + +#include "Messages/LowLevelMessages.h" + +namespace RoboidControl { + +DistanceSensor::DistanceSensor(Thing* parent) : Thing(parent) { + this->type = Type::DistanceSensor; + this->name = "Distance sensor"; +} + +float DistanceSensor::GetDistance() { + if (this->externalDistance < this->internalDistance) + return this->externalDistance; + else + return this->internalDistance; +} + +int DistanceSensor::GenerateBinary(char* bytes, unsigned char* ix) { + LowLevelMessages::SendFloat16(bytes, ix, this->internalDistance); + return *ix; +} + +void DistanceSensor::ProcessBinary(char* bytes) { + unsigned char ix = 0; + this->externalDistance = LowLevelMessages::ReceiveFloat16(bytes, &ix); +} + +} // namespace RoboidControl \ No newline at end of file diff --git a/Things/DistanceSensor.h b/Things/DistanceSensor.h new file mode 100644 index 0000000..08e324d --- /dev/null +++ b/Things/DistanceSensor.h @@ -0,0 +1,41 @@ +#pragma once + +#if !NO_STD +#include +#endif + +#include "Thing.h" + +namespace RoboidControl { + +/// @brief A sensor measuring distance +class DistanceSensor : public Thing { + public: + /// @brief Create a new child touch sensor + /// @param parent The parent thing + /// @param thingId The ID of the thing, leave out or set to zero to generate + /// an ID + DistanceSensor(Thing* parent = Thing::LocalRoot()); + + /// @brief Get the current distance + float GetDistance(); + + /// @brief Function used to generate binary data for this sensor + /// @param buffer The byte array for thw binary data + /// @param ix The starting position for writing the binary data + int GenerateBinary(char* bytes, unsigned char* ix) override; + /// @brief Function used to process binary data received for this sensor + /// @param bytes The binary data to process + virtual void ProcessBinary(char* bytes) override; + + protected: +#if ARDUNIO + float internalDistance = INFINITY; + float externalDistance = INFINITY; +#else + float internalDistance = std::numeric_limits::infinity(); + float externalDistance = std::numeric_limits::infinity(); +#endif +}; + +} // namespace RoboidControl diff --git a/Things/TouchSensor.h b/Things/TouchSensor.h index 9890ef8..bdc5279 100644 --- a/Things/TouchSensor.h +++ b/Things/TouchSensor.h @@ -6,7 +6,7 @@ namespace RoboidControl { /// @brief A sensor which can detect touches class TouchSensor : public Thing { -// Why finishing this release (0.3), I notice that this is equivalent to a digital sensor +// When finishing this release (0.3), I notice that this is equivalent to a digital sensor public: /// @brief Create a new child touch sensor /// @param parent The parent thing