Improved touch sensor

This commit is contained in:
Pascal Serrarens 2025-06-06 09:36:24 +02:00
parent 2a83dbe7ca
commit b8eb9bb712
5 changed files with 15 additions and 9 deletions

View File

@ -29,7 +29,7 @@ DigitalInput::TouchSensor::TouchSensor(unsigned char pin, Thing* parent)
: RoboidControl::TouchSensor(parent), digitalInput(pin, parent) {} : RoboidControl::TouchSensor(parent), digitalInput(pin, parent) {}
void DigitalInput::TouchSensor::Update(bool recursive) { void DigitalInput::TouchSensor::Update(bool recursive) {
this->touchedSomething = digitalInput.isLow; this->internalTouch = digitalInput.isLow;
} }
#pragma endregion Touch sensor #pragma endregion Touch sensor
@ -98,8 +98,8 @@ void DigitalInput::RelativeEncoder::Update(bool recursive) {
this->pulseFrequency = pulseCount / timeStep; this->pulseFrequency = pulseCount / timeStep;
this->rotationSpeed = pulseFrequency / pulsesPerRevolution; this->rotationSpeed = pulseFrequency / pulsesPerRevolution;
std::cout << "pulses: " << pulseCount << " per second " << pulseFrequency // std::cout << "pulses: " << pulseCount << " per second " << pulseFrequency
<< " timestep " << timeStep << std::endl; // << " timestep " << timeStep << std::endl;
this->lastUpdateTime = currentTimeMs; this->lastUpdateTime = currentTimeMs;
} }

View File

@ -65,7 +65,7 @@ UltrasonicSensor::TouchSensor::TouchSensor(Configuration config, Thing* parent)
void UltrasonicSensor::TouchSensor::Update(bool recursive) { void UltrasonicSensor::TouchSensor::Update(bool recursive) {
RoboidControl::TouchSensor::Update(recursive); RoboidControl::TouchSensor::Update(recursive);
this->ultrasonic.Update(false); this->ultrasonic.Update(false);
this->touchedSomething |= (this->ultrasonic.distance > 0 && this->internalTouch |= (this->ultrasonic.distance > 0 &&
this->ultrasonic.distance <= this->touchDistance); this->ultrasonic.distance <= this->touchDistance);
} }

View File

@ -7,8 +7,12 @@ TouchSensor::TouchSensor(Thing* parent) : Thing(parent) {
this->name = "Touch sensor"; this->name = "Touch sensor";
} }
bool TouchSensor::IsTouching() {
return this->internalTouch || this->externalTouch;
}
void TouchSensor::PrepareForUpdate() { void TouchSensor::PrepareForUpdate() {
this->touchedSomething = this->externalTouch; //this->internalTouch = this->externalTouch;
} }
void TouchSensor::Update(bool recursive) { void TouchSensor::Update(bool recursive) {
@ -16,7 +20,8 @@ void TouchSensor::Update(bool recursive) {
} }
int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) { int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) {
bytes[(*ix)++] = this->touchedSomething ? 1 : 0; std::cout << "BinaryMsg Touch " << this->internalTouch << std::endl;
bytes[(*ix)++] = this->internalTouch ? 1 : 0;
return 1; return 1;
} }

View File

@ -16,7 +16,7 @@ class TouchSensor : public Thing {
/// @brief Value which is true when the sensor is touching something, false /// @brief Value which is true when the sensor is touching something, false
/// otherwise /// otherwise
bool touchedSomething = false; bool IsTouching();
virtual void PrepareForUpdate() override; virtual void PrepareForUpdate() override;
virtual void Update(bool recursive) override; virtual void Update(bool recursive) override;
@ -30,6 +30,7 @@ class TouchSensor : public Thing {
virtual void ProcessBinary(char* bytes) override; virtual void ProcessBinary(char* bytes) override;
protected: protected:
bool externalTouch = false; bool externalTouch = false;
bool internalTouch = false;
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -27,10 +27,10 @@ int main() {
while (true) { while (true) {
// The left wheel turns forward when nothing is touched on the right side // The left wheel turns forward when nothing is touched on the right side
// and turn backward when the roboid hits something on the right // and turn backward when the roboid hits something on the right
float leftWheelSpeed = (touchRight.touchedSomething) ? -600.0f : 600.0f; float leftWheelSpeed = (touchRight.internalTouch) ? -600.0f : 600.0f;
// The right wheel does the same, but instead is controlled by // The right wheel does the same, but instead is controlled by
// touches on the left side // touches on the left side
float rightWheelSpeed = (touchLeft.touchedSomething) ? -600.0f : 600.0f; float rightWheelSpeed = (touchLeft.internalTouch) ? -600.0f : 600.0f;
// When both sides are touching something, both wheels will turn backward // When both sides are touching something, both wheels will turn backward
// and the roboid will move backwards // and the roboid will move backwards
bb2b.SetWheelVelocity(leftWheelSpeed, rightWheelSpeed); bb2b.SetWheelVelocity(leftWheelSpeed, rightWheelSpeed);