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

View File

@ -6,11 +6,8 @@ namespace RoboidControl {
namespace Arduino { namespace Arduino {
/// @brief An HC-SR04 ultrasonic distance sensor /// @brief An HC-SR04 ultrasonic distance sensor
class UltrasonicSensor : public TouchSensor { class UltrasonicSensor : Thing {
public: public:
// Inherit all constructors
//using TouchSensor::TouchSensor;
/// @brief Setup an ultrasonic sensor /// @brief Setup an ultrasonic sensor
/// @param participant The participant to use /// @param participant The participant to use
/// @param pinTrigger The pin number of the trigger signal /// @param pinTrigger The pin number of the trigger signal
@ -25,7 +22,7 @@ class UltrasonicSensor : public TouchSensor {
// parameters // parameters
/// @brief The distance at which the object is considered to be touched /// @brief The distance at which the object is considered to be touched
float touchDistance = 0.2f; // float touchDistance = 0.2f;
// state // state
@ -45,6 +42,23 @@ class UltrasonicSensor : public TouchSensor {
unsigned char pinTrigger = 0; unsigned char pinTrigger = 0;
/// @brief The pin number of the echo signal /// @brief The pin number of the echo signal
unsigned char pinEcho = 0; 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 } // namespace Arduino