Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/distance_sensor replace std::vector usage with px4::Array #11242

Merged
merged 3 commits into from
Jan 20, 2019
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
6 changes: 3 additions & 3 deletions src/drivers/distance_sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
#
############################################################################

add_subdirectory(cm8jl65)
#add_subdirectory(hc_sr04) # not currently supported
add_subdirectory(leddar_one)
add_subdirectory(ll40ls)
add_subdirectory(mb12xx)
add_subdirectory(pga460)
add_subdirectory(sf0x)
add_subdirectory(sf1xx)
add_subdirectory(srf02)
add_subdirectory(teraranger)
add_subdirectory(tfmini)
add_subdirectory(ulanding)
add_subdirectory(leddar_one)
add_subdirectory(vl53lxx)
add_subdirectory(pga460)
add_subdirectory(cm8jl65)
10 changes: 5 additions & 5 deletions src/drivers/distance_sensor/hc_sr04/hc_sr04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <px4_workqueue.h>
#include <drivers/device/device.h>
#include <px4_defines.h>
#include <containers/Array.hpp>

#include <sys/types.h>
#include <stdint.h>
Expand All @@ -54,7 +55,6 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <vector>

#include <perf/perf_counter.h>
#include <systemlib/err.h>
Expand Down Expand Up @@ -86,7 +86,7 @@
class HC_SR04 : public cdev::CDev
{
public:
HC_SR04(unsigned sonars = 6);
HC_SR04();
virtual ~HC_SR04();

virtual int init();
Expand Down Expand Up @@ -123,9 +123,9 @@ class HC_SR04 : public cdev::CDev
int _cycling_rate; /* */
uint8_t _index_counter; /* temporary sonar i2c address */

std::vector<float>
px4::Array<float, 6>
_latest_sonar_measurements; /* vector to store latest sonar measurements in before writing to report */
unsigned _sonars;
unsigned _sonars{6};
struct GPIOConfig {
uint32_t trig_port;
uint32_t echo_port;
Expand Down Expand Up @@ -200,7 +200,7 @@ const HC_SR04::GPIOConfig HC_SR04::_gpio_tab[] = {
extern "C" __EXPORT int hc_sr04_main(int argc, char *argv[]);
static int sonar_isr(int irq, void *context);

HC_SR04::HC_SR04(unsigned sonars) :
HC_SR04::HC_SR04() :
CDev(SR04_DEVICE_PATH, 0),
_min_distance(SR04_MIN_DISTANCE),
_max_distance(SR04_MAX_DISTANCE),
Expand Down
8 changes: 2 additions & 6 deletions src/drivers/distance_sensor/mb12xx/mb12xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <px4_config.h>
#include <px4_getopt.h>
#include <px4_workqueue.h>
#include <containers/Array.hpp>

#include <drivers/device/i2c.h>

Expand All @@ -56,7 +57,6 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <vector>

#include <perf/perf_counter.h>

Expand Down Expand Up @@ -131,10 +131,7 @@ class MB12XX : public device::I2C
uint8_t _cycle_counter; /* counter in cycle to change i2c adresses */
int _cycling_rate; /* */
uint8_t _index_counter; /* temporary sonar i2c address */
std::vector<uint8_t> addr_ind; /* temp sonar i2c address vector */
std::vector<float>
_latest_sonar_measurements; /* vector to store latest sonar measurements in before writing to report */

px4::Array<uint8_t, MB12XX_MAX_RANGEFINDERS> addr_ind; /* temp sonar i2c address vector */

/**
* Test whether the device supported by the driver is present at a
Expand Down Expand Up @@ -277,7 +274,6 @@ MB12XX::init()
if (ret2 == 0) { /* sonar is present -> store address_index in array */
addr_ind.push_back(_index_counter);
PX4_DEBUG("sonar added");
_latest_sonar_measurements.push_back(200);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/drivers/distance_sensor/sf1xx/sf1xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <vector>

#include <perf/perf_counter.h>

Expand Down
8 changes: 2 additions & 6 deletions src/drivers/distance_sensor/srf02/srf02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <px4_defines.h>
#include <px4_getopt.h>
#include <px4_workqueue.h>
#include <containers/Array.hpp>

#include <drivers/device/i2c.h>

Expand All @@ -55,7 +56,6 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <vector>

#include <perf/perf_counter.h>

Expand Down Expand Up @@ -131,10 +131,7 @@ class SRF02 : public device::I2C
uint8_t _cycle_counter; /* counter in cycle to change i2c adresses */
int _cycling_rate; /* */
uint8_t _index_counter; /* temporary sonar i2c address */
std::vector<uint8_t> addr_ind; /* temp sonar i2c address vector */
std::vector<float>
_latest_sonar_measurements; /* vector to store latest sonar measurements in before writing to report */

px4::Array<uint8_t, MB12XX_MAX_RANGEFINDERS> addr_ind; /* temp sonar i2c address vector */

/**
* Test whether the device supported by the driver is present at a
Expand Down Expand Up @@ -277,7 +274,6 @@ SRF02::init()
if (ret2 == 0) { /* sonar is present -> store address_index in array */
addr_ind.push_back(_index_counter);
PX4_DEBUG("sonar added");
_latest_sonar_measurements.push_back(200);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/drivers/distance_sensor/ulanding/ulanding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <vector>
#include <perf/perf_counter.h>
#include <systemlib/err.h>
#include <drivers/device/ringbuffer.h>
Expand Down
102 changes: 25 additions & 77 deletions src/modules/logger/array.h → src/include/containers/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,13 @@
namespace px4
{

template <typename TYPE, size_t N>
template <class T, size_t N>
class Array
{
typedef TYPE &reference;
typedef const TYPE &const_reference;
typedef TYPE *iterator;
typedef const TYPE *const_iterator;

public:
Array()
: _size(0), _overflow(false)
{}

bool push_back(const TYPE &x)
bool push_back(const T &x)
{
if (_size == N) {
_overflow = true;
Expand All @@ -75,86 +68,41 @@ class Array
}
}

reference operator[](size_t n)
void erase(T *item)
{
return _items[n];
}

const_reference operator[](size_t n) const
{
return _items[n];
}

reference at(size_t n)
{
return _items[n];
}

const_reference at(size_t n) const
{
return _items[n];
}

size_t size() const
{
return _size;
}

size_t max_size() const
{
return N;
}

size_t capacity() const
{
return N;
}
if (item - _items < static_cast<int>(_size)) {
--_size;

bool empty() const
{
return _size == 0;
for (T *it = item; it != &_items[_size]; ++it) {
*it = *(it + 1);
}
}
}

bool is_overflowed()
{
return _overflow;
}
T &operator[](size_t n) { return _items[n]; }
const T &operator[](size_t n) const { return _items[n]; }

iterator begin()
{
return &_items[0];
}
T &at(size_t n) { return _items[n]; }
const T &at(size_t n) const { return _items[n]; }

iterator end()
{
return &_items[_size];
}
size_t size() const { return _size; }
size_t max_size() const { return N; }
size_t capacity() const { return N; }

const_iterator begin() const
{
return &_items[0];
}
bool empty() const { return _size == 0; }

const_iterator end() const
{
return &_items[_size];
}
bool is_overflowed() { return _overflow; }

void erase(iterator item)
{
if (item - _items < static_cast<int>(_size)) {
--_size;
T *begin() { return &_items[0]; }
T *end() { return &_items[_size]; }

for (iterator it = item; it != &_items[_size]; ++it) {
*it = *(it + 1);
}
}
}
const T *begin() const { return &_items[0]; }
const T *end() const { return &_items[_size]; }

private:
TYPE _items[N];
size_t _size;
bool _overflow;
T _items[N];
size_t _size{0};
bool _overflow{false};
};

}
2 changes: 1 addition & 1 deletion src/modules/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#include "log_writer.h"
#include "messages.h"
#include "array.h"
#include <containers/Array.hpp>
#include "util.h"
#include <px4_defines.h>
#include <drivers/drv_hrt.h>
Expand Down