diff --git a/Arduino/Sensors/DigitalInput.cpp b/Arduino/Sensors/DigitalInput.cpp new file mode 100644 index 0000000..b601281 --- /dev/null +++ b/Arduino/Sensors/DigitalInput.cpp @@ -0,0 +1,21 @@ +#include "DigitalInput.h" + +#include + +namespace RoboidControl { +namespace Arduino { + +DigitalInput::DigitalInput(RemoteParticipant* participant, unsigned char pin) : TouchSensor(participant) { + this->pin = pin; + + pinMode(pin, INPUT); +} + +void DigitalInput::Update(unsigned long currentTimeMs) { + this->touchedSomething = digitalRead(pin) == LOW; + + // std::cout << "DigitalINput pin " << (int)this->pin << ": " << this->touchedSomething << "\n"; +} + +} // namespace Arduino +} // namespace RoboidControl \ No newline at end of file diff --git a/Arduino/Sensors/DigitalInput.h b/Arduino/Sensors/DigitalInput.h new file mode 100644 index 0000000..fa5d12f --- /dev/null +++ b/Arduino/Sensors/DigitalInput.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Sensors/TouchSensor.h" + +namespace RoboidControl { +namespace Arduino { + +class DigitalInput : public TouchSensor { + public: + DigitalInput(RemoteParticipant* participant, unsigned char pin); + + virtual void Update(unsigned long currentTimeMs) override; + + protected: + unsigned char pin = 0; +}; + +} // namespace Arduino +} // namespace RoboidControl \ No newline at end of file diff --git a/Arduino/Sensors/UltrasonicSensor.cpp b/Arduino/Sensors/UltrasonicSensor.cpp new file mode 100644 index 0000000..33a9a33 --- /dev/null +++ b/Arduino/Sensors/UltrasonicSensor.cpp @@ -0,0 +1,56 @@ +#include "UltrasonicSensor.h" + +#include + +namespace RoboidControl { +namespace Arduino { + +UltrasonicSensor::UltrasonicSensor(RemoteParticipant* participant, unsigned char pinTrigger, unsigned char pinEcho) + : TouchSensor(participant) { + this->pinTrigger = pinTrigger; + this->pinEcho = pinEcho; + + pinMode(pinTrigger, OUTPUT); // configure the trigger pin to output mode + pinMode(pinEcho, INPUT); // configure the echo pin to input mode +} + +float UltrasonicSensor::GetDistance() { + // Start the ultrasonic 'ping' + digitalWrite(pinTrigger, LOW); + delayMicroseconds(5); + digitalWrite(pinTrigger, HIGH); + delayMicroseconds(10); + digitalWrite(pinTrigger, LOW); + + // Measure the duration of the pulse on the echo pin + float duration_us = pulseIn(pinEcho, HIGH, 100000); // the result is in microseconds + + // Calculate the distance: + // * Duration should be divided by 2, because the ping goes to the object + // and back again. The distance to the object is therefore half the duration + // of the pulse: duration_us /= 2; + // * Then we need to convert from microseconds to seconds: duration_sec = + // duration_us / 1000000; + // * Now we calculate the distance based on the speed of sound (340 m/s): + // distance = duration_sec * 340; + // * The result calculation is therefore: + this->distance = duration_us / 2 / 1000000 * 340; + + // Filter faulty measurements. The sensor can often give values > 30 m which + // are not correct + // if (distance > 30) + // distance = 0; + + this->touchedSomething = (this->distance <= this->touchDistance); + +// std::cout << "Ultrasonic " << this->distance << " " << this->touchedSomething << "\n"; + + return distance; +} + +void UltrasonicSensor::Update(unsigned long currentTimeMs) { + GetDistance(); +} + +} // namespace Arduino +} // namespace RoboidControl \ No newline at end of file diff --git a/Arduino/Sensors/UltrasonicSensor.h b/Arduino/Sensors/UltrasonicSensor.h new file mode 100644 index 0000000..3354701 --- /dev/null +++ b/Arduino/Sensors/UltrasonicSensor.h @@ -0,0 +1,29 @@ +#pragma once + +#include "Sensors/TouchSensor.h" + +namespace RoboidControl { +namespace Arduino { + +class UltrasonicSensor : public TouchSensor { + public: + UltrasonicSensor(RemoteParticipant* participant, unsigned char pinTrigger, unsigned char pinEcho); + + // parameters + + float touchDistance = 0.2f; + + // state + + float distance = 0; + float GetDistance(); + + virtual void Update(unsigned long currentTimeMs) override; + + protected: + unsigned char pinTrigger = 0; + unsigned char pinEcho = 0; +}; + +} // namespace Arduino +} // namespace RoboidControl \ No newline at end of file