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

View File

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

View File

@ -7,8 +7,12 @@ TouchSensor::TouchSensor(Thing* parent) : Thing(parent) {
this->name = "Touch sensor";
}
bool TouchSensor::IsTouching() {
return this->internalTouch || this->externalTouch;
}
void TouchSensor::PrepareForUpdate() {
this->touchedSomething = this->externalTouch;
//this->internalTouch = this->externalTouch;
}
void TouchSensor::Update(bool recursive) {
@ -16,7 +20,8 @@ void TouchSensor::Update(bool recursive) {
}
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;
}

View File

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

View File

@ -27,10 +27,10 @@ int main() {
while (true) {
// The left wheel turns forward when nothing is touched on the right side
// 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
// 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
// and the roboid will move backwards
bb2b.SetWheelVelocity(leftWheelSpeed, rightWheelSpeed);