Skip to content

Commit

Permalink
Merge pull request #3006 from randaz81/improved_fakeNav
Browse files Browse the repository at this point in the history
improved fakeNavigation device
  • Loading branch information
randaz81 authored Aug 30, 2023
2 parents 48cc073 + 8700493 commit ae55d5c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 32 deletions.
53 changes: 35 additions & 18 deletions src/devices/fakeNavigationDevice/fakeNavigationDev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,27 @@ bool fakeNavigation :: open(yarp::os::Searchable& config)
Property p;
p.fromString(config.toString());
#endif
navThread = new fakeNavigationThread(0.010, p);

if (!navThread->start())
if (rf.check("navigation_time"))
{
delete navThread;
return false;
m_navig_duration_param = rf.find ("navigation_time").asInt32();
}
if (rf.check("reached_time"))
{
m_reached_duration_param = rf.find("reached_time").asInt32();
}

this->start();
return true;
}

fakeNavigation::fakeNavigation()
fakeNavigation::fakeNavigation() : yarp::os::PeriodicThread(0.010)
{
navThread =NULL;
}

//module cleanup
bool fakeNavigation:: close()
{
navThread->stop();
delete navThread;
navThread =NULL;

return true;
}

Expand All @@ -85,6 +83,7 @@ bool fakeNavigation::gotoTargetByAbsoluteLocation(Map2DLocation loc)
{
m_status = NavigationStatusEnum::navigation_status_moving;
m_absgoal_loc = loc;
m_time_counter=m_navig_duration_param;
}
return true;
}
Expand Down Expand Up @@ -190,21 +189,39 @@ bool fakeNavigation::getRelativeLocationOfCurrentTarget(double& x, double& y, do
return true;
}

fakeNavigationThread::fakeNavigationThread(double _period, yarp::os::Searchable& _cfg) : PeriodicThread(_period)
{
}

bool fakeNavigationThread::threadInit()
bool fakeNavigation::threadInit()
{
return true;
}

void fakeNavigationThread::threadRelease()
void fakeNavigation::threadRelease()
{

}

void fakeNavigationThread::run()
void fakeNavigation::run()
{

if (m_status == NavigationStatusEnum::navigation_status_moving)
{
if (m_time_counter>0)
{
m_time_counter--;
}
else
{
m_status = NavigationStatusEnum::navigation_status_goal_reached;
m_time_counter = m_reached_duration_param;
}
}
if (m_status == NavigationStatusEnum::navigation_status_goal_reached)
{
if (m_time_counter > 0)
{
m_time_counter--;
}
else
{
m_status = NavigationStatusEnum::navigation_status_idle;
}
}
}
23 changes: 9 additions & 14 deletions src/devices/fakeNavigationDevice/fakeNavigationDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
#include <yarp/os/PeriodicThread.h>
#include <math.h>


class fakeNavigationThread :
public yarp::os::PeriodicThread
{
public:
fakeNavigationThread(double _period, yarp::os::Searchable& _cfg);
virtual bool threadInit() override;
virtual void threadRelease() override;
virtual void run() override;
};

/**
* @ingroup dev_impl_fake dev_impl_navigation
*
Expand All @@ -32,7 +21,8 @@ class fakeNavigation :
public yarp::dev::DeviceDriver,
public yarp::dev::Nav2D::INavigation2DTargetActions,
public yarp::dev::Nav2D::INavigation2DControlActions,
public yarp::dev::Nav2D::INavigation2DVelocityActions
public yarp::dev::Nav2D::INavigation2DVelocityActions,
public yarp::os::PeriodicThread
{
private:
yarp::dev::Nav2D::NavigationStatusEnum m_status = yarp::dev::Nav2D::NavigationStatusEnum::navigation_status_idle;
Expand All @@ -47,8 +37,9 @@ class fakeNavigation :
}
m_control_out;

public:
fakeNavigationThread *navThread=nullptr;
int m_reached_duration_param = 100;
int m_navig_duration_param = 500;
int m_time_counter= m_navig_duration_param;

public:
virtual bool open(yarp::os::Searchable& config) override;
Expand All @@ -75,4 +66,8 @@ class fakeNavigation :
bool recomputeCurrentNavigationPath() override;
bool applyVelocityCommand(double x_vel, double y_vel, double theta_vel, double timeout = 0.1) override;
bool getLastVelocityCommand(double& x_vel, double& y_vel, double& theta_vel) override;

bool threadInit() override;
void threadRelease() override;
void run() override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TEST_CASE("dev::FakeNavigationTest", "[yarp::dev]")
// Do tests
//yarp::dev::tests::exec_iNav2D_test_1(inav, imap);
//yarp::dev::tests::exec_iNav2D_test_2(inav, imap);
yarp::dev::tests::exec_iNav2D_test_3(inav_target, inav_control);

//"Close all polydrivers and check"
{
Expand Down
33 changes: 33 additions & 0 deletions src/libYARP_dev/src/yarp/dev/tests/INavigation2DTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ namespace yarp::dev::tests
b1 = inav->gotoTargetByLocationName("non-existing-loc"); CHECK(b1 == false);
b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
}

inline void exec_iNav2D_test_3(INavigation2DTargetActions* inav_trgt, INavigation2DControlActions* inav_ctl)
{
//////////"Checking INavigation2D methods
bool b;
Map2DLocation loc("test", 1, 2, 3);
yarp::dev::Nav2D::NavigationStatusEnum status;

b = inav_ctl->getNavigationStatus(status); CHECK(b);
CHECK(status== yarp::dev::Nav2D::NavigationStatusEnum::navigation_status_idle);

b = inav_trgt->gotoTargetByAbsoluteLocation(loc); CHECK(b);
b = inav_ctl->getNavigationStatus(status); CHECK(b);
CHECK(status == yarp::dev::Nav2D::NavigationStatusEnum::navigation_status_moving);

size_t count=0;
do
{
b = inav_ctl->getNavigationStatus(status); CHECK(b);
if (status == yarp::dev::Nav2D::NavigationStatusEnum::navigation_status_goal_reached)
{
break;
}
yarp::os::Time::delay(0.1);
count++;
if (count>200) {CHECK(0); break; }
}
while(1);

yarp::os::Time::delay(2);
b = inav_ctl->getNavigationStatus(status); CHECK(b);
CHECK(status == yarp::dev::Nav2D::NavigationStatusEnum::navigation_status_idle);
}
}

#endif

0 comments on commit ae55d5c

Please sign in to comment.