RoverHCSR04 and RoverGrooveUltrasonic classes added.
Signed-off-by: Mustafa Ozcelikors <mozcelikors@gmail.com>
diff --git a/rover/include/roverapi/rover_grooveultrasonic.hpp b/rover/include/roverapi/rover_grooveultrasonic.hpp
new file mode 100644
index 0000000..d3a63a8
--- /dev/null
+++ b/rover/include/roverapi/rover_grooveultrasonic.hpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2017 FH Dortmund and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Description:
+ * RoverGrooveUltrasonic API - Interfaces for Rover Groove ultrasonic sensor application development
+ * Header file
+ *
+ * Contributors:
+ * M.Ozcelikors <mozcelikors@gmail.com>, created RoverGrooveUltrasonic class 04.12.2017
+ *
+ */
+
+#ifndef ROVERAPI_ROVER_GROOVEULTRASONIC_HPP_
+#define ROVERAPI_ROVER_GROOVEULTRASONIC_HPP_
+
+#include <roverapi/rover_sensor.hpp>
+
+/**
+ * @brief Groove Sensor Pin for Front Sensor Socket (in wiringPi format)
+ */
+static const int SIG0 = 0; //BCM-17 -> WiringPi 0 //Same as ECHO0 pin, if some one wants to replace front sr04 with groove sensor
+
+/**
+ * @brief Groove Sensor Pin for Rear Sensor Socket (in wiringPi format)
+ */
+static const int SIG1 = 2; //BCM-27 -> WiringPi 2 //Same as ECHO1 pin, if some one wants to replace back sr04 with groove sensor
+
+namespace rover
+{
+ /**
+ * @brief RoverGrooveUltrasonic is a class that is inherited from RoverSensor abstract class. RoverGrooveUltrasonic class contains member functions and variables to set up and read from Groove ultrasonic sensors that are embedded on the rover.
+ */
+ class RoverGrooveUltrasonic : public RoverSensor
+ {
+ private:
+ /**
+ * @brief Sensor Identification
+ */
+ RoverSensorID_t sensorID;
+
+ /**
+ * @brief Signal pin
+ */
+ int sigPin;
+
+ public:
+ /**
+ * @brief Constructor for the RoverGrooveUltrasonic class
+ * @param sensor_id Sensor to address
+ */
+ explicit RoverGrooveUltrasonic (const RoverSensorID_t sensor_id);
+
+ /**
+ * @brief Destructor for the RoverGrooveUltrasonic class
+ */
+ ~RoverGrooveUltrasonic();
+
+ /**
+ * @brief Function to setup Groove Ultrasonic sensor
+ * @return void
+ */
+ void initialize (void);
+
+ /**
+ * @brief Member function to read from Groove ultrasonic sensor given its sensor id.
+ * @return sensor_val Sensor value read in centimeters from sensor
+ */
+ float read (void);
+
+ /**
+ * @brief Sets the private attribute sigPin for Groove Ultrasonic Sensor
+ * @param sig_pin Pin in wiringPi format for Groove Ultrasonic Sensor
+ * @return void
+ */
+ void setSigPin (const int sig_pin);
+
+ /**
+ * @brief Returns the private attribute sigPin for Groove Ultrasonic Sensor
+ * @return sig_pin Pin in wiringPi format for Groove Ultrasonic Sensor
+ */
+ int getSigPin (void);
+ };
+}
+
+
+
+#endif /* ROVERAPI_ROVER_GROOVEULTRASONIC_HPP_ */
diff --git a/rover/include/roverapi/rover_hcsr04.hpp b/rover/include/roverapi/rover_hcsr04.hpp
new file mode 100644
index 0000000..2f1374c
--- /dev/null
+++ b/rover/include/roverapi/rover_hcsr04.hpp
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2017 FH Dortmund and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Description:
+ * RoverHCSR04 API - Interfaces for Rover ultrasonic sensor application development
+ * Header file
+ *
+ * Contributors:
+ * M.Ozcelikors <mozcelikors@gmail.com>, created RoverHCSR04 class 04.12.2017
+ *
+ */
+
+#ifndef ROVERAPI_ROVER_HCSR04_HPP_
+#define ROVERAPI_ROVER_HCSR04_HPP_
+
+#include <roverapi/rover_sensor.hpp>
+
+/**
+ * @brief HC-SR04 Sensor Trigger Pin for Front Sensor Socket (in wiringPi format)
+ */
+static const int TRIG0 = 7; //BCM-4 -> WiringPi 7
+/**
+ * @brief HC-SR04 Sensor Echo Pin for Front Sensor Socket (in wiringPi format)
+ */
+static const int ECHO0 = 0; //BCM-17 -> WiringPi 0
+
+/**
+ * @brief HC-SR04 Sensor Trigger Pin for Rear Sensor Socket (in wiringPi format)
+ */
+static const int TRIG1 = 1; //BCM-18 -> WiringPi 1
+/**
+ * @brief HC-SR04 Sensor Echo Pin for Rear Sensor Socket (in wiringPi format)
+ */
+static const int ECHO1 = 2; //BCM-27 -> WiringPi 2
+
+namespace rover
+{
+ /**
+ * @brief RoverHCSR04 is a class that is inherited from RoverSensor abstract class. RoverHCSR04 class contains member functions and variables to set up and read from HCSR-04 ultrasonic sensors that are embedded on the rover.
+ */
+ class RoverHCSR04 : public RoverSensor
+ {
+ private:
+ /**
+ * @brief Sensor Identification
+ */
+ RoverSensorID_t sensorID;
+
+ /**
+ * @brief Trigger pin
+ */
+ int trigPin;
+
+ /**
+ * @brief Echo pin
+ */
+ int echoPin;
+
+ /**
+ * @brief Flag to hold if RoverHCSR04 is set up
+ */
+ int ROVERHCSR04_SETUP_;
+
+ public:
+ /**
+ * @brief Constructor for the RoverHCSR04 class
+ * @param sensor_id Sensor to address
+ */
+ explicit RoverHCSR04 (const RoverSensorID_t sensor_id);
+
+ /**
+ * @brief Destructor for the RoverHCSR04 class
+ */
+ ~RoverHCSR04();
+
+ /**
+ * @brief Function to setup HCSR04 sensor
+ * @return void
+ */
+ void initialize (void);
+
+ /**
+ * @brief Member function to read from HC-SR04 ultrasonic sensor given its sensor id.
+ * @return sensor_val Sensor value read in centimeters from sensor
+ */
+ float read (void);
+
+ /**
+ * @brief Sets the private attribute trigPin for HCSR04
+ * @param trig_pin Pin in wiringPi format for HCSR04
+ * @return void
+ */
+ void setTrigPin (const int trig_pin);
+
+ /**
+ * @brief Returns the private attribute trigPin for HCSR04
+ * @return trig_pin Pin in wiringPi format for HCSR04
+ */
+ int getTrigPin (void);
+
+ /**
+ * @brief Sets the private attribute echoPin for HCSR04
+ * @param echo_pin Pin in wiringPi format for HCSR04
+ * @return void
+ */
+ void setEchoPin (const int echo_pin);
+
+ /**
+ * @brief Returns the private attribute echoPin for HCSR04
+ * @return echo_pin Pin in wiringPi format for HCSR04
+ */
+ int getEchoPin (void);
+
+ };
+}
+
+
+
+#endif /* ROVERAPI_ROVER_HCSR04_HPP_ */
diff --git a/rover/src/roverapi/rover_grooveultrasonic.cpp b/rover/src/roverapi/rover_grooveultrasonic.cpp
new file mode 100644
index 0000000..c8eef3f
--- /dev/null
+++ b/rover/src/roverapi/rover_grooveultrasonic.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2017 FH Dortmund and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Description:
+ * RoverGrooveUltrasonic API - Interfaces for Rover Groove ultrasonic sensor application development
+ *
+ * Contributors:
+ * M.Ozcelikors <mozcelikors@gmail.com>, created RoverGrooveUltrasonic class 04.12.2017
+ *
+ */
+
+#include <roverapi/rover_grooveultrasonic.hpp>
+#include <wiringPi.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+rover::RoverGrooveUltrasonic::RoverGrooveUltrasonic (const RoverSensorID_t sensor_id)
+{
+ this->sensorID = sensor_id;
+ if (this->sensorID == ROVER_FRONT)
+ {
+ this->sigPin = SIG1;
+ }
+ else if (this->sensorID == ROVER_REAR)
+ {
+ this->sigPin = SIG0;
+ }
+ else
+ {
+ printf ("Invalid Sensor ID for the Groove Ultrasonic Sensor to constructor RoverGrooveUltrasonic::RoverGrooveUltrasonic().\n");
+ // Adding a default value for the pin in case there is an error
+ this->sigPin = SIG1;
+ }
+}
+
+rover::RoverGrooveUltrasonic::~RoverGrooveUltrasonic(){}
+
+void rover::RoverGrooveUltrasonic::initialize (void)
+{
+
+}
+
+float rover::RoverGrooveUltrasonic::read (void)
+{
+ long startTime, stopTime, elapsedTime, distance = 0;
+ pinMode(this->sigPin, OUTPUT);
+ digitalWrite(this->sigPin, LOW);
+ delayMicroseconds(2);
+ digitalWrite(this->sigPin, HIGH);
+ delayMicroseconds(5);
+ digitalWrite(this->sigPin, LOW);
+ pinMode(this->sigPin,INPUT);
+
+ startTime = micros();
+ while (digitalRead(this->sigPin) == LOW );
+ startTime = micros();
+ // For values above 40cm, groove sensor is unable to receive signals which causes it to stuck
+ // This is resolved by adding the timeout below.
+ // However, this timeout cause values bigger than 40 to fluctuate
+ while (digitalRead(this->sigPin) == HIGH && micros() < startTime + 100000);
+ stopTime = micros();
+ elapsedTime = stopTime - startTime;
+ distance = elapsedTime / 29 /2;
+ // The below protection is to ensure there is no value fluctuation
+ if (distance > 40 )
+ distance = 40;
+
+ return ((float)distance*1.0);
+}
+
+void rover::RoverGrooveUltrasonic::setSigPin (const int sig_pin)
+{
+ this->sigPin = sig_pin;
+}
+
+int rover::RoverGrooveUltrasonic::getSigPin (void)
+{
+ return this->sigPin;
+}
+
+
+
diff --git a/rover/src/roverapi/rover_hcsr04.cpp b/rover/src/roverapi/rover_hcsr04.cpp
new file mode 100644
index 0000000..c4bd02a
--- /dev/null
+++ b/rover/src/roverapi/rover_hcsr04.cpp
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2017 FH Dortmund and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Description:
+ * RoverHCSR04 API - Interfaces for Rover ultrasonic sensor application development
+ *
+ * Contributors:
+ * M.Ozcelikors <mozcelikors@gmail.com>, created RoverHCSR04 class 04.12.2017
+ *
+ */
+
+#include <roverapi/rover_hcsr04.hpp>
+#include <wiringPi.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+rover::RoverHCSR04::RoverHCSR04 (const RoverSensorID_t sensor_id)
+:ROVERHCSR04_SETUP_(0)
+{
+ this->sensorID = sensor_id;
+ if (this->sensorID == ROVER_FRONT)
+ {
+ this->trigPin = TRIG1;
+ this->echoPin = ECHO1;
+ }
+ else if (this->sensorID == ROVER_REAR)
+ {
+ this->trigPin = TRIG0;
+ this->echoPin = ECHO0;
+ }
+ else
+ {
+ printf ("Invalid Sensor ID for the Ultrasonic Sensor to constructor RoverHCSR04::RoverHCSR04().\n");
+ // Adding a default value for the pins in case there is an error
+ this->trigPin = TRIG0;
+ this->echoPin = ECHO0;
+ }
+}
+
+rover::RoverHCSR04::~RoverHCSR04(){}
+
+void rover::RoverHCSR04::initialize (void)
+{
+ pinMode(this->trigPin, OUTPUT);
+ pinMode(this->echoPin, INPUT);
+
+ //TRIG pin must start LOW
+ digitalWrite(this->trigPin, LOW);
+ delayMicroseconds(2);
+
+ this->ROVERHCSR04_SETUP_ = 1;
+}
+
+float rover::RoverHCSR04::read (void)
+{
+ if (this->ROVERHCSR04_SETUP_ != 1)
+ {
+ fprintf(stderr,"You havent set up RoverHCSR04. Use RoverHCSR04()::initialize() !\n");
+ }
+ else
+ {
+ int distance = 0;
+ //Send trig pulse
+ digitalWrite(this->trigPin, HIGH);
+ delayMicroseconds(10);
+ digitalWrite(this->trigPin, LOW);
+
+ //Wait for echo start
+ long startTime = micros();
+ while(digitalRead(this->echoPin) == LOW && micros() < startTime + 100000);
+
+ //Wait for echo end
+ startTime = micros();
+ while(digitalRead(this->echoPin) == HIGH);
+ long travelTime = micros() - startTime;
+
+ //Get distance in cm
+ distance = travelTime * 34300;
+ distance = distance / 1000000;
+ distance = distance / 2;
+ // The below protection is to ensure there is no value fluctuation due to timeout
+ if (distance > 40 )
+ distance = 40;
+
+ // printf("dist=%d\n",distance);
+ return ((float) distance*1.0);
+ }
+}
+
+void rover::RoverHCSR04::setTrigPin (const int trig_pin)
+{
+ this->trigPin = trig_pin;
+}
+
+int rover::RoverHCSR04::getTrigPin (void)
+{
+ return this->trigPin;
+}
+
+void rover::RoverHCSR04::setEchoPin (const int echo_pin)
+{
+ this->echoPin = echo_pin;
+}
+
+int rover::RoverHCSR04::getEchoPin (void)
+{
+ return this->echoPin;
+}
+