RoboidControl-cpp/Arduino/Things/UltrasonicSensor.h

69 lines
1.8 KiB
C++

#pragma once
#include "Things/TouchSensor.h"
namespace RoboidControl {
namespace Arduino {
/// @brief An HC-SR04 ultrasonic distance sensor
class UltrasonicSensor : Thing {
public:
struct Configuration {
int triggerPin;
int echoPin;
};
/// @brief Setup an ultrasonic sensor
/// @param participant The participant to use
/// @param pinTrigger The pin number of the trigger signal
/// @param pinEcho The pin number of the echo signal
UltrasonicSensor(Configuration config, Participant* participant);
UltrasonicSensor(Configuration config, Thing* parent);
// parameters
/// @brief The distance at which the object is considered to be touched
// float touchDistance = 0.2f;
// state
/// @brief The last read distance
float distance = 0;
/// @brief erform an ultrasonic 'ping' to determine the distance to the
/// nearest object
/// @return the measured distance in meters to the nearest object
float GetDistance();
/// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs)
virtual void Update(bool recursive = false) override;
protected:
/// @brief The pin number of the trigger signal
unsigned char pinTrigger = 0;
/// @brief The pin number of the echo signal
unsigned char pinEcho = 0;
public:
class TouchSensor;
};
#pragma region Touch sensor
class UltrasonicSensor::TouchSensor : public RoboidControl::TouchSensor {
public:
TouchSensor(UltrasonicSensor::Configuration config, Thing& parent);
TouchSensor(UltrasonicSensor::Configuration config, Thing* parent);
float touchDistance = 0.2f;
/// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs)
virtual void Update(bool recursive = false) override;
protected:
UltrasonicSensor ultrasonic;
};
#pragma region Touch sensor
} // namespace Arduino
} // namespace RoboidControl