Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lowcar/devices/Device/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions lowcar/devices/UltrasonicSensor/UltrasonicSensor.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
21 changes: 21 additions & 0 deletions lowcar/devices/UltrasonicSensor/UltrasonicSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef ULTRASONIC_SENSOR_H
#define ULTRASONIC_SENSOR_H

#include <stdint.h>
#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
8 changes: 8 additions & 0 deletions runtime_util/runtime_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down