Commit 5fa21b48 authored by Mohamed Ahmed's avatar Mohamed Ahmed

Working ROS Proxy

parent ed7e90d7
......@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.3)
project(timeClient)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
......@@ -10,8 +10,6 @@ add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
genmsg
)
## System dependencies are found with CMake's conventions
......@@ -48,12 +46,11 @@ find_package(catkin REQUIRED COMPONENTS
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
add_message_files(
FILES
time.msg
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
)
# )
## Generate services in the 'srv' folder
# add_service_files(
......@@ -70,11 +67,10 @@ add_message_files(
# )
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
timeClient
)
# generate_messages(
# DEPENDENCIES
# std_msgs
# )
################################################
## Declare ROS dynamic reconfigure parameters ##
......@@ -106,7 +102,6 @@ add_message_files(
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
CATKIN_DEPENDS message_runtime
# INCLUDE_DIRS include
# LIBRARIES timeClient
# CATKIN_DEPENDS roscpp std_msgs
......
string proxy_id
int8 hours
int8 minutes
int8 seconds
\ No newline at end of file
......@@ -51,8 +51,6 @@
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
......
......@@ -2,10 +2,6 @@
#include "timeClient_activity.hpp"
#include<ros/ros.h>
#include "ros/this_node.h"
#include "ros/master.h"
class Watchapp : public APapp
{
public:
......
......@@ -3,6 +3,14 @@
#include "ara/com/timeservice_common.h"
#include "ara/core/future.h"
#include "ara/core/promise.h"
// ROS includes
#include <ros/ros.h>
#include "ros/this_node.h"
#include "ros/master.h"
#include "std_msgs/Int64.h"
#include "std_msgs/Bool.h"
namespace methods
{
......@@ -10,7 +18,14 @@ namespace methods
class getTime
{
public:
getTime();
bool flag;
timeService::getTimeOutput returnValue;
ros::NodeHandle rosHandler;
ros::Publisher proxyTimePublisher;
ros::Subscriber proxyTimeSubscriber;
void proxyCallBack(const std_msgs::Int64 &msg);
/**
* \brief Operation will call the method.
*
......
#include <memory>
// includes for used services
#include "timeservice_proxy.hpp"
// ROS includes
#include <ros/ros.h>
#include "ros/this_node.h"
#include "ros/master.h"
#include "std_msgs/Int64.h"
#include "std_msgs/Bool.h"
#include "CTime.hpp"
#define DEADLINE 500
......@@ -21,12 +15,9 @@ class Timeactivity
public:
Timeactivity();
~Timeactivity();
ros::NodeHandle rosHandler;
ros::Publisher getTimePublisher;
ros::Subscriber getTimeSubscriber;
void init();
void run(const std_msgs::Int64 &msg);
void run();
private:
int serviceInstances_;
......
......@@ -9,6 +9,8 @@
#include "getTime.hpp"
namespace ara
{
namespace com
......@@ -54,6 +56,7 @@ class timeServiceProxy
public:
class HandleType;
/**
* \brief The proxy can only be created using a specific handle which identifies a service.
*
......
#include "WatchappProxy.hpp"
#include<ros/ros.h>
#include "ros/this_node.h"
#include "ros/master.h"
#include "std_msgs/Int64.h"
#include "std_msgs/Bool.h"
using std::cout;
using std::endl;
......@@ -25,21 +18,10 @@ void Watchapp::InitializeApp()
void Watchapp::RunningLoop()
{
ros::Rate loop_rate(1);
while(ros::ok)
{
//auto vNodes;
//while(!ros::master::getNodes(vNodes));
std_msgs::Bool getMsg;
getMsg.data = true;
_timeClientActivity.getTimePublisher.publish(getMsg);
ros::spinOnce();
loop_rate.sleep();
}
//_timeClientActivity.run();
_timeClientActivity.run();
}
void Watchapp::TerminatingApp()
{
}
......@@ -2,8 +2,30 @@
using namespace methods;
getTime::getTime(): flag(false)
{
proxyTimePublisher = rosHandler.advertise<std_msgs::Bool>("/request",100);
proxyTimeSubscriber = rosHandler.subscribe("/timeTopic_",100,&getTime::proxyCallBack,this);
}
ara::core::Future<timeService::getTimeOutput> getTime::operator()()
{
std_msgs::Bool tMsg;
tMsg.data = true;
proxyTimePublisher.publish(tMsg);
while(flag == false)
{}
ara::core::Promise<timeService::getTimeOutput> promise;
promise.set_value(returnValue);
return promise.get_future();
}
void getTime::proxyCallBack(const std_msgs::Int64 &msg)
{
ROS_INFO("%i",msg.data);
returnValue.seconds = msg.data;
flag = true;
}
......@@ -2,11 +2,18 @@
#include <memory>
int main(int argc, char **argv)
void spinThread();
int main(int argc, char* argv[])
{
ros::init(argc, argv, "Client");
auto app = std::make_shared<Watchapp>();
app->Run();
ros::init(argc, argv, "test2");
std::thread t(spinThread);
auto app = std::make_shared<Watchapp>();
return app->Run();
}
void spinThread()
{
ros::spin();
}
\ No newline at end of file
......@@ -5,12 +5,7 @@
#include "../../common/inc/colors.hpp"
#include "ara/core/future.h"
Timeactivity::Timeactivity() : m_proxy(nullptr){
getTimePublisher = rosHandler.advertise<std_msgs::Bool>("queryTime_", 10);
getTimeSubscriber = rosHandler.subscribe("timeTopic_",100, &Timeactivity::run, this);
}
Timeactivity::Timeactivity() : m_proxy(nullptr){}
Timeactivity::~Timeactivity()
{
......@@ -26,89 +21,76 @@ void Timeactivity::init()
});
}
void Timeactivity::run(const std_msgs::Int64 &msg)
void Timeactivity::run()
{
std::this_thread::sleep_for(std::chrono::milliseconds(DELAY));
if (nullptr != m_proxy)
while(1)
{
std::chrono::time_point<std::chrono::system_clock> deadline
= std::chrono::system_clock::now() + std::chrono::milliseconds(DEADLINE);
ROS_INFO("############3\n###############3");
std::this_thread::sleep_for(std::chrono::milliseconds(DELAY));
timeService::getTimeOutput time_output={0};
if (nullptr != m_proxy)
{
ROS_INFO("############3\n**************83");
std::chrono::time_point<std::chrono::system_clock> deadline
= std::chrono::system_clock::now() + std::chrono::milliseconds(DEADLINE);
auto time_future = m_proxy->getTime();
timeService::getTimeOutput time_output={0};
if (time_future.wait_until(deadline) != ara::core::Future<bool>::Status::kReady)
{
//std::cout << "Request took too long!!" << std::endl;
}
else
time_output = time_future.get();
CTime currentTime(time_output.hours, time_output.minutes, time_output.seconds);
auto time_future = m_proxy->getTime();
if (time_future.wait_until(deadline) != ara::core::Future<bool>::Status::kReady)
{
//std::cout << "Request took too long!!" << std::endl;
}
else
time_output = time_future.get();
CTime currentTime(time_output.hours, time_output.minutes, time_output.seconds);
if(serviceInstances_ == 20)
{
std::cout << currentTime << " From service instance: " << INSTANCE_COLOR_20 << "20" << END_COLOR << std::endl;
}
else if(serviceInstances_ == 40)
{
std::cout << currentTime << " From service instance: " << INSTANCE_COLOR_40 << "40" << END_COLOR << std::endl;
}
if(serviceInstances_ == 20)
{
std::cout << currentTime << " From service instance: " << INSTANCE_COLOR_20 << "20" << END_COLOR << std::endl;
}
else if(serviceInstances_ == 40)
else
{
std::cout << currentTime << " From service instance: " << INSTANCE_COLOR_40 << "40" << END_COLOR << std::endl;
//std::cout << "\rERROR: There is no handle!!" << std::endl;
}
}
else
{
//std::cout << "\rERROR: There is no handle!!" << std::endl;
}
}
void Timeactivity::serviceAvailabilityCallback(ara::com::ServiceHandleContainer<proxy::timeServiceProxy::HandleType> handles,
ara::com::FindServiceHandle handler)
{
if (handles.size() > 0)
{
//std::cout << handles.size() << " handles are/is found" << std::endl;
std::cout << END_COLOR << START_COLOR_CLIENT << " Time service is found!" << std::endl;
serviceInstances_ = handles[0].GetInstanceId().GetInstanceId();
std::string str_srvID = std::to_string(serviceInstances_);
//getTimePublisher = rosHandler.advertise<std_msgs::Bool>("queryTime_" + str_srvID, 10);
//getTimeSubscriber = rosHandler.subscribe("timeTopic_" + str_srvID,100, &Timeactivity::run, this);
//serviceInstances_ = handles[0].GetInstanceId().GetInstanceId();
//int i=1;
/*for(auto& item : handles)
{
if(ros::master::getNodes(v))
{
ROS_INFO("Error");
}
rosHandler.advertise<timeClient::time>(
"queryTime", 10);
getTimeSubscriber = rosHandler.subscribe("timeTopic",
100, &ROSAxis::nodeCommand, this);
//std::cout << i << ": " << (item.GetInstanceId().GetInstanceId()) << std::endl;
i++;
}*/
if(handles[0].GetInstanceId().GetInstanceId() == SERVICE_ECU1 || handles[0].GetInstanceId().GetInstanceId() == SERVICE_ECU2)
{
//if(handles[0].GetInstanceId().GetInstanceId() == SERVICE_ECU1 || handles[0].GetInstanceId().GetInstanceId() == SERVICE_ECU2)
//{
if(m_proxy == nullptr) // first time
m_proxy = std::make_shared<proxy::timeServiceProxy>(handles[0]);
else
m_proxy.reset(new proxy::timeServiceProxy(handles[0]));
}
// }
}
else
{
......
......@@ -7,7 +7,7 @@ timeServiceProxy::HandleType::HandleType(){}
timeServiceProxy::timeServiceProxy(HandleType &handle)
{
}
bool timeServiceProxy::HandleType::operator==(const HandleType &other) const
......@@ -27,7 +27,13 @@ ara::com::FindServiceHandle timeServiceProxy::StartFindService( ara::com::FindSe
ara::com::InstanceIdentifier instance)
{
// Below two line just to avoid warning: return void from non void type
ara::com::FindServiceHandle handle;
return handle;
std::vector<HandleType> v;
HandleType h;
//h.GetInstanceId();
v.push_back(h);
ara::com::FindServiceHandle f;
handler(v, f);
return f;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment