Ultrasonic TouchSensor subclass

This commit is contained in:
Pascal Serrarens 2025-05-14 12:04:29 +02:00
parent 40aab4dc7a
commit 23ea03fbaf
2 changed files with 34 additions and 17 deletions

View File

@ -8,7 +8,7 @@ namespace Arduino {
UltrasonicSensor::UltrasonicSensor(Participant* participant,
unsigned char pinTrigger,
unsigned char pinEcho)
: TouchSensor(participant) {
: Thing(participant) {
this->pinTrigger = pinTrigger;
this->pinEcho = pinEcho;
@ -51,25 +51,28 @@ float UltrasonicSensor::GetDistance() {
// if (distance > 30)
// distance = 0;
this->touchedSomething |= (this->distance > 0 && this->distance <= this->touchDistance);
// std::cout << "Ultrasonic " << this->touchedSomething << " | " << (this->distance > 0) << " " <<
// (this->distance <= this->touchDistance) << "\n";
return distance;
}
void UltrasonicSensor::Update(unsigned long currentTimeMs, bool recursive) {
this->touchedSomething = false;
GetDistance();
Thing::Update(currentTimeMs, recursive);
}
// void UltrasonicSensor::ProcessBinary(char* bytes) {
// this->touchedSomething = (bytes[0] == 1);
// if (this->touchedSomething)
// std::cout << "Touching something!\n";
// }
UltrasonicSensor::TouchSensor::TouchSensor(Thing& parent,
unsigned char pinTrigger,
unsigned char pinEcho)
: RoboidControl::TouchSensor(&parent),
ultrasonic(&parent, pinTrigger, pinEcho) {
this->touchedSomething = false;
}
void UltrasonicSensor::TouchSensor::Update(unsigned long currentTimeMs,
bool recursive) {
this->ultrasonic.Update(currentTimeMs, recursive);
this->touchedSomething = (this->ultrasonic.distance > 0 &&
this->ultrasonic.distance <= this->touchDistance);
}
} // namespace Arduino
} // namespace RoboidControl

View File

@ -6,11 +6,8 @@ namespace RoboidControl {
namespace Arduino {
/// @brief An HC-SR04 ultrasonic distance sensor
class UltrasonicSensor : public TouchSensor {
class UltrasonicSensor : Thing {
public:
// Inherit all constructors
//using TouchSensor::TouchSensor;
/// @brief Setup an ultrasonic sensor
/// @param participant The participant to use
/// @param pinTrigger The pin number of the trigger signal
@ -25,7 +22,7 @@ class UltrasonicSensor : public TouchSensor {
// parameters
/// @brief The distance at which the object is considered to be touched
float touchDistance = 0.2f;
// float touchDistance = 0.2f;
// state
@ -45,6 +42,23 @@ class UltrasonicSensor : public TouchSensor {
unsigned char pinTrigger = 0;
/// @brief The pin number of the echo signal
unsigned char pinEcho = 0;
public:
class TouchSensor;
};
class UltrasonicSensor::TouchSensor : public RoboidControl::TouchSensor {
public:
TouchSensor(Thing& parent, unsigned char pinTrigger, unsigned char pinEcho);
float touchDistance = 0.2f;
/// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs)
virtual void Update(unsigned long currentTimeMs,
bool recursive = false) override;
protected:
UltrasonicSensor ultrasonic;
};
} // namespace Arduino