diff --git a/lowcar/devices/Device/defs.h b/lowcar/devices/Device/defs.h index 913a941c..bcd2a8b6 100644 --- a/lowcar/devices/Device/defs.h +++ b/lowcar/devices/Device/defs.h @@ -60,8 +60,8 @@ enum class DeviceType : uint8_t { SERVO_CONTROL = 0x04, POLAR_BEAR = 0x05, KOALA_BEAR = 0x06, - PDB = 0x07 - // DISTANCE_SENSOR = 0x07 Uncomment when implemented + PDB = 0x07, + ULTRASONIC_SENSOR = 0x08 }; // identification for resulting status types diff --git a/lowcar/devices/UltrasonicSensor/UltrasonicSensor.cpp b/lowcar/devices/UltrasonicSensor/UltrasonicSensor.cpp new file mode 100644 index 00000000..528771ee --- /dev/null +++ b/lowcar/devices/UltrasonicSensor/UltrasonicSensor.cpp @@ -0,0 +1,32 @@ +#include "UltrasonicSensor.h" + +// pin definitions for the ultrasonic sensor +#define TRIG_PIN 16 +#define ECHO_PIN 14 + +UltrasonicSensor::UltrasonicSensor() : Device(DeviceType::ULTRASONIC_SENSOR, 17) { + ; +} + +size_t UltrasonicSensor::device_read(uint8_t param, uint8_t* data_buf) { + // trigger the ultrasonic pulse + digitalWrite(TRIG_PIN, LOW); + delayMicroseconds(2); + + digitalWrite(TRIG_PIN, HIGH); + delayMicroseconds(10); + digitalWrite(TRIG_PIN, LOW); + + long duration = pulseIn(ECHO_PIN, HIGH); + float distance = duration * 0.034f / 2.0f; // convert to cm + + float* float_buf = (float*) data_buf; + float_buf[0] = distance; + + return sizeof(float); +} + +void UltrasonicSensor::device_enable() { + pinMode(TRIG_PIN, OUTPUT); + pinMode(ECHO_PIN, INPUT); +} \ No newline at end of file diff --git a/lowcar/devices/UltrasonicSensor/UltrasonicSensor.h b/lowcar/devices/UltrasonicSensor/UltrasonicSensor.h new file mode 100644 index 00000000..53c1e4f2 --- /dev/null +++ b/lowcar/devices/UltrasonicSensor/UltrasonicSensor.h @@ -0,0 +1,21 @@ +#ifndef ULTRASONIC_SENSOR_H +#define ULTRASONIC_SENSOR_H + +#include +#include "Device.h" +#include "defs.h" + +class UltrasonicSensor : public Device { + public: + // Constructor + UltrasonicSensor(); + + virtual size_t device_read(uint8_t param, uint8_t* data_buf); + virtual void device_enable(); + + private: + static uint8_t trigPin; + static uint8_t echoPin; +}; + +#endif diff --git a/runtime_util/runtime_util.c b/runtime_util/runtime_util.c index cf7febaa..d0a03eb0 100644 --- a/runtime_util/runtime_util.c +++ b/runtime_util/runtime_util.c @@ -116,6 +116,13 @@ device_t PDB = { {.name = "dv_cell3", .type = FLOAT, .read = 1, .write = 0}, {.name = "network_switch", .type = BOOL, .read = 1, .write = 0}}}; +device_t UltrasonicSensor = { + .type = 8, + .name = "UltrasonicSensor", + .num_params = 1, + .params = { + {.name = "distance", .type = FLOAT, .read = 1, .write = 0}}}; + // *********************** VIRTUAL DEVICE DEFINITIONS *********************** // // A CustomDevice is unusual because the parameters are dynamic @@ -216,6 +223,7 @@ __attribute__((constructor)) void devices_arr_init() { DEVICES[PolarBear.type] = &PolarBear; DEVICES[KoalaBear.type] = &KoalaBear; DEVICES[PDB.type] = &PDB; + DEVICES[UltrasonicSensor.type] = &UltrasonicSensor; DEVICES[CustomDevice.type] = &CustomDevice; DEVICES[SoundDevice.type] = &SoundDevice; DEVICES[TimeTestDevice.type] = &TimeTestDevice;