Skip to content

Commit

Permalink
use shared pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilritz authored and bresch committed Dec 16, 2019
1 parent 00cd720 commit f13f2f8
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 49 deletions.
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

include(gtest.cmake)

add_subdirectory(SensorSimulator)
add_subdirectory(sensor_simulator)

set(SRCS
main.cpp
Expand Down
11 changes: 8 additions & 3 deletions test/sensor_simulator/baro.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "Baro.h"
#include "baro.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

Baro::Baro(Ekf* ekf):Sensor(ekf)
Baro::Baro(std::shared_ptr<Ekf> ekf):Sensor(ekf)
{
}

Expand All @@ -21,3 +23,6 @@ void Baro::setData(float baro)
{
_baro_data = baro;
}

} // namespace sensor
} // namespace sensor_simulator
7 changes: 5 additions & 2 deletions test/sensor_simulator/baro.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@

#include "sensor.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

class Baro: public Sensor
{
public:
Baro(Ekf* ekf);
Baro(std::shared_ptr<Ekf> ekf);
~Baro();

void setData(float baro);
Expand All @@ -57,4 +59,5 @@ class Baro: public Sensor

};

} // namespace sensor
} // namespace sensor_simulator::sensor
11 changes: 7 additions & 4 deletions test/sensor_simulator/gps.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "Gps.h"
#include "gps.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

Gps::Gps(Ekf* ekf):Sensor(ekf)
Gps::Gps(std::shared_ptr<Ekf> ekf):Sensor(ekf)
{
}

Expand All @@ -17,9 +19,10 @@ void Gps::send(uint32_t time)
_ekf->setGpsData(time, _gps_data);
}

void Gps::setData(gps_message gps)
void Gps::setData(const gps_message& gps)
{
_gps_data = gps;
}

} // namespace sensor
} // namespace sensor_simulator::sensor
9 changes: 6 additions & 3 deletions test/sensor_simulator/gps.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@

#include "sensor.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

class Gps: public Sensor
{
public:
Gps(Ekf* ekf);
Gps(std::shared_ptr<Ekf> ekf);
~Gps();

void setData(gps_message gps);
void setData(const gps_message& gps);

private:
gps_message _gps_data;
Expand All @@ -57,4 +59,5 @@ class Gps: public Sensor

};

} // namespace sensor
} // namespace sensor_simulator::sensor
15 changes: 9 additions & 6 deletions test/sensor_simulator/imu.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "Imu.h"
#include "imu.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

Imu::Imu(Ekf* ekf):Sensor(ekf)
Imu::Imu(std::shared_ptr<Ekf> ekf):Sensor(ekf)
{
}

Expand All @@ -25,20 +27,21 @@ void Imu::send(uint32_t time)
_time_last_data_sent = time;
}

void Imu::setData(Vector3f accel, Vector3f gyro)
void Imu::setData(const Vector3f& accel, const Vector3f& gyro)
{
setAccelData(accel);
setGyroData(gyro);
}

void Imu::setAccelData(Vector3f accel)
void Imu::setAccelData(const Vector3f& accel)
{
_accel_data = accel;
}

void Imu::setGyroData(Vector3f gyro)
void Imu::setGyroData(const Vector3f& gyro)
{
_gyro_data = gyro;
}

} // namespace sensor
} // namespace sensor_simulator::sensor
13 changes: 8 additions & 5 deletions test/sensor_simulator/imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@

#include "sensor.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

class Imu: public Sensor
{
public:
Imu(Ekf* ekf);
Imu(std::shared_ptr<Ekf> ekf);
~Imu();

void setData(Vector3f accel, Vector3f gyro);
void setAccelData(Vector3f accel);
void setGyroData(Vector3f gyro);
void setData(const Vector3f& accel, const Vector3f& gyro);
void setAccelData(const Vector3f& accel);
void setGyroData(const Vector3f& gyro);

private:
Vector3f _accel_data;
Expand All @@ -60,4 +62,5 @@ class Imu: public Sensor

};

} // namespace sensor
} // namespace sensor_simulator::sensor
11 changes: 7 additions & 4 deletions test/sensor_simulator/mag.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "Mag.h"
#include "mag.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

Mag::Mag(Ekf* ekf):Sensor(ekf)
Mag::Mag(std::shared_ptr<Ekf> ekf):Sensor(ekf)
{
}

Expand All @@ -19,9 +21,10 @@ void Mag::send(uint32_t time)
_time_last_data_sent = time;
}

void Mag::setData(Vector3f mag)
void Mag::setData(const Vector3f& mag)
{
_mag_data = mag;
}

} // namespace sensor
} // namespace sensor_simulator::sensor
9 changes: 6 additions & 3 deletions test/sensor_simulator/mag.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@

#include "sensor.h"

namespace sensor_simulator::sensor
namespace sensor_simulator
{
namespace sensor
{

class Mag: public Sensor
{
public:
Mag(Ekf* ekf);
Mag(std::shared_ptr<Ekf> ekf);
~Mag();

void setData(Vector3f mag);
void setData(const Vector3f& mag);

private:
Vector3f _mag_data;
Expand All @@ -57,4 +59,5 @@ class Mag: public Sensor

};

} // namespace sensor
} // namespace sensor_simulator::sensor
7 changes: 3 additions & 4 deletions test/sensor_simulator/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace sensor_simulator
{

Sensor::Sensor(Ekf* ekf)
Sensor::Sensor(std::shared_ptr<Ekf> ekf): _ekf{ekf}
{
_ekf = ekf;
}

Sensor::~Sensor()
Expand All @@ -20,12 +19,12 @@ void Sensor::update(uint32_t time)
}
}

bool Sensor::should_send(uint32_t time)
bool Sensor::should_send(uint32_t time) const
{
return _is_running && is_time_to_send(time);
}

bool Sensor::is_time_to_send(uint32_t time)
bool Sensor::is_time_to_send(uint32_t time) const
{
return (time >= _time_last_data_sent) && ((time - _time_last_data_sent) >= _update_period);
}
Expand Down
11 changes: 6 additions & 5 deletions test/sensor_simulator/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "EKF/ekf.h"
#include <math.h>
#include <memory>

namespace sensor_simulator
{
Expand All @@ -48,32 +49,32 @@ class Sensor
{
public:

Sensor(Ekf* ekf);
Sensor(std::shared_ptr<Ekf> ekf);
virtual ~Sensor();

void update(uint32_t time);

void setRate(uint32_t rate){ _update_period = uint32_t(1000000)/rate; }

bool isRunning(){ return _is_running; }
bool isRunning() const { return _is_running; }

void start(){ _is_running = true; }

void stop(){ _is_running = false; }

protected:

Ekf* _ekf;
std::shared_ptr<Ekf> _ekf;
// time in microseconds
uint32_t _update_period;
uint32_t _time_last_data_sent{0};

bool _is_running{false};

bool should_send(uint32_t time);
bool should_send(uint32_t time) const;

// Checks that the right amount time passed since last send data to fulfill rate
bool is_time_to_send(uint32_t time);
bool is_time_to_send(uint32_t time) const;

// call set*Data function of Ekf
virtual void send(uint32_t time) = 0;
Expand Down
6 changes: 3 additions & 3 deletions test/sensor_simulator/sensor_simulator.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "SensorSimulator.h"
#include "sensor_simulator.h"


SensorSimulator::SensorSimulator(Ekf* ekf):
SensorSimulator::SensorSimulator(std::shared_ptr<Ekf> ekf):
_ekf{ekf},
_imu{ekf},
_mag{ekf},
_baro{ekf},
_gps{ekf}
{
_ekf = ekf;

// set default sensor rate in Hz
_imu.setRate(250);
Expand Down
7 changes: 5 additions & 2 deletions test/sensor_simulator/sensor_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@

#pragma once

#include <memory>

#include "imu.h"
#include "mag.h"
#include "baro.h"
#include "gps.h"
#include "EKF/ekf.h"

using namespace sensor_simulator::sensor;

class SensorSimulator
{
public:
SensorSimulator(Ekf* ekf);
SensorSimulator(std::shared_ptr<Ekf> ekf);
~SensorSimulator();

void setImuRate(uint32_t rate){ _imu.setRate(rate); }
Expand All @@ -68,7 +71,7 @@ class SensorSimulator
void setImuBias(Vector3f accel_bias, Vector3f gyro_bias);

private:
Ekf* _ekf;
std::shared_ptr<Ekf> _ekf;

Imu _imu;
Mag _mag;
Expand Down
8 changes: 4 additions & 4 deletions test/test_EKF_basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@

#include <gtest/gtest.h>
#include <math.h>
#include <memory>
#include "EKF/ekf.h"
#include "SensorSimulator/SensorSimulator.h"
#include "sensor_simulator/sensor_simulator.h"


class EkfInitializationTest : public ::testing::Test {
public:

Ekf* _ekf;
std::shared_ptr<Ekf> _ekf;
SensorSimulator* _sensor_simulator;

// Duration of initalization with only providing baro,mag and IMU
Expand All @@ -49,7 +50,7 @@ class EkfInitializationTest : public ::testing::Test {
// Setup the Ekf with synthetic measurements
void SetUp() override
{
_ekf = new Ekf();
_ekf = std::make_shared<Ekf>();
_ekf->init(0);
_sensor_simulator = new SensorSimulator(_ekf);

Expand All @@ -60,7 +61,6 @@ class EkfInitializationTest : public ::testing::Test {
void TearDown() override
{
delete _sensor_simulator;
delete _ekf;
}
};

Expand Down

0 comments on commit f13f2f8

Please sign in to comment.