forked from ekumenlabs/roscpp_android
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Updating hello-ros example with basic ui #101
Open
ivanpauno
wants to merge
12
commits into
kinetic
Choose a base branch
from
hello-ros-with-basic-ui-reorganized
base: kinetic
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
19b80dd
Updating hello-ros example with basic ui
ivanpauno c66f8d6
Adding basic remapping arguments support to sample app.
acb2e5b
Adjusting gitignore for Android Studio files.
b49fa37
Adding clarification about remapping arguments in README.
fec07fd
Addressing comments and nits.
40f43ec
Changing namespace to org.ros.android.
efdb1c0
Adding shared preferences to remappings and ROS master IP.
31bbcf4
Adding ROS master port to preferences and shared preferences.
a75edb9
Merge branch 'kinetic' into hello-ros-with-basic-ui-reorganized
ac33e96
Merge branch 'kinetic' into hello-ros-with-basic-ui-reorganized
719f8df
Moving device's IP address to a spinner with a checkbox to make it op…
10a292b
Adding rosjava_build_tools as buildtool deps to samples.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,44 @@ | ||
This is a hello ROS example app. It subscribes to `/chatter` topic and when a message is received, the following message is published in `/a_chatter`: | ||
# Hello ROS (pubsub example - native Android) | ||
|
||
data: "hello world from android ndk __COUNTER_VALUE__" | ||
This is a hello ROS example app. It publishes a heartbeat to `/chatter` topic; when a message is received it is also logged using `ROS_INFO`. | ||
The heartbeat looks like this: | ||
|
||
USAGE | ||
------- | ||
data: "hello world from android ndk __COUNTER_VALUE__" | ||
|
||
1. IP addresses are hardcoded, so you must edit the master URI and the android device ip address in the following file: | ||
## Usage | ||
|
||
app/src/main/cpp/main.cpp | ||
1. Build the samples with `install` script using `--samples` option. | ||
If you already have compiled the basic catkin workspace, you can enter the docker container (`docker/run.sh`) and build the samples using `build_catkin_workspace.sh` script using `-w` and `-p` options like this (replace paths with your current configuration if necessary): | ||
|
||
2. Build the samples with do_everything script. | ||
/opt/ros_android/scripts/build_catkin_workspace.sh -w /opt/ros_android/example_workspace/ -p /opt/ros_android/output/ -v 1 -b Debug | ||
|
||
3. Install the app in your android device using adb -d install as explained here: | ||
2. The APK file should be inside `app/build/outputs/apk/debug`. Install it in your android device using adb -d install as explained here: | ||
<https://developer.android.com/studio/build/building-cmdline> | ||
|
||
4. Execute roscore in a terminal with ros sourced. Remember to export first your ip address: | ||
3. Execute roscore in a terminal with ros sourced. Remember to export first your ip address: | ||
|
||
export ROS_IP=__YOUR_IP_ADDRESS__ | ||
|
||
5. Subscribe to /a_chatter in another terminal with ros setup sourced: | ||
4. Subscribe to /chatter in another terminal with ros setup sourced: | ||
|
||
rostopic echo /a_chatter | ||
rostopic echo /chatter | ||
|
||
You should see the heartbeat coming out. | ||
|
||
6. Publish to /chatter, in another terminal with ros setup sourced and ROS_IP exported: | ||
5. Optionally, publish to /chatter, in another terminal with ros setup sourced and ROS_IP exported: | ||
|
||
rostopic pub /chatter std_msgs/String "__YOUR_MESSAGE__" -r __PUBLISHING_RATE__ | ||
|
||
If all is working well, you will receive multiple messages with an incresing counter value. | ||
The message you have sent is logged in android, you can check the result with logcat: | ||
The message you have sent is logged in Android, you can check the result with logcat: | ||
|
||
adb logcat | ||
|
||
You should see the heartbeat messages as well as any message you publish to `/chatter` topic in Android's log. | ||
|
||
### Remapping arguments | ||
|
||
adb logcat | ||
You can also remap topics as if you were launching a ROS console application. For example, to remap the topic enter the following: | ||
|
||
The log will be like: | ||
/chatter:=/custom_topic | ||
|
||
12-13 15:53:36.449 12078 12093 I ROSCPP_NDK_EXAMPLE: __YOUR_MESSAGE__ | ||
In this case, you will see the output in `/custom_topic` instead. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
example_workspace/src/hello_world_example_app/app/include/ros_android/main_thread.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef __ROS_ANDROID_MAIN_THREAD_H__ | ||
#define __ROS_ANDROID_MAIN_THREAD_H__ | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <string> | ||
|
||
namespace ros_android { | ||
class MainThread { | ||
public: | ||
MainThread(); | ||
explicit MainThread(std::string); | ||
virtual ~MainThread() = 0; | ||
|
||
typedef std::shared_ptr<MainThread> Ptr; | ||
static Ptr Instance(void); | ||
|
||
virtual void run() = 0; | ||
virtual void stop() = 0; | ||
bool check_ros_master(const std::vector<std::pair<std::string, std::string>> remapings); | ||
private: | ||
std::string node_name; | ||
|
||
static std::mutex s_instance_mutex; | ||
static Ptr s_instance; | ||
}; | ||
} // namespace ros_android | ||
|
||
#define MY_ROS_ANDROID_MAIN_THREAD(class_name) \ | ||
ros_android::MainThread::Ptr ros_android::MainThread::Instance() { \ | ||
Ptr instance = s_instance; \ | ||
if (!instance) { \ | ||
std::lock_guard<std::mutex> lock(s_instance_mutex); \ | ||
if (!s_instance) { \ | ||
instance = s_instance = std::make_shared<class_name>(); \ | ||
} \ | ||
} \ | ||
return instance; \ | ||
} | ||
#endif // #ifndef __ROS_ANDROID_MAIN_THREAD_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 36 additions & 78 deletions
114
example_workspace/src/hello_world_example_app/app/src/main/cpp/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,52 @@ | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <functional> | ||
#include <sstream> | ||
#include <map> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <vector> | ||
#include <set> | ||
#include <fstream> | ||
#include <android/log.h> | ||
#include <string> | ||
|
||
#include "ros/ros.h" | ||
#include <ros/ros.h> | ||
#include <ros_android/main_thread.h> | ||
#include <std_msgs/String.h> | ||
|
||
#include <android_native_app_glue.h> | ||
class HelloRos : public ros_android::MainThread { | ||
public: | ||
HelloRos() : ros_android::MainThread("hello_ros") {} | ||
|
||
int loop_count_ = 0; | ||
ros::Publisher chatter_pub; | ||
virtual void run() override { | ||
/* Write your main code here */ | ||
ros::NodeHandle n; | ||
ROS_INFO("Starting hello_ros main thread."); | ||
|
||
void chatterCallback(const std_msgs::String::ConstPtr& msg){ | ||
ROS_INFO("%s", msg->data.c_str()); | ||
loop_count_++; | ||
std_msgs::String msgo; | ||
std::stringstream ss; | ||
ss << "hello world from android ndk " << loop_count_; | ||
msgo.data = ss.str(); | ||
chatter_pub.publish(msgo); | ||
ROS_INFO_STREAM(msg->data.c_str()); | ||
} | ||
// Creating a publisher and a subscriber; do a loopback in /chatter topic. | ||
chatter_pub = n.advertise<std_msgs::String>("chatter", 1000); | ||
ros::Subscriber sub = n.subscribe<std_msgs::String>("chatter", 1000, std::bind(&HelloRos::chatterCallback, this, std::placeholders::_1)); | ||
|
||
void android_main(android_app *state) { | ||
// Rate 1Hz | ||
ros::WallRate loop_rate(1); | ||
|
||
int argc = 3; | ||
int loop_count = 0; | ||
while (ros::ok()) { | ||
ros::spinOnce(); | ||
|
||
//*********************** NOTE: HARDCODE rosmaster ip addresses in __master, and hardcode the ip address of the android device in __ip ************************* | ||
char* argv[] = {const_cast<char*>("nothing_important") , const_cast<char*>("__master:=http://10.34.0.120:11311"), const_cast<char*>("__ip:=10.34.0.121")}; | ||
//********************************************************************************************************************************************************* | ||
std_msgs::String msgo; | ||
std::stringstream ss; | ||
ss << "Hello world from android ndk " << loop_count++; | ||
msgo.data = ss.str(); | ||
chatter_pub.publish(msgo); | ||
|
||
for (int i = 0; i < argc; i++) { | ||
ROS_INFO("%s",argv[i]); | ||
loop_rate.sleep(); | ||
} | ||
} | ||
|
||
ros::init(argc, &argv[0], "android_ndk_native_cpp"); | ||
|
||
ROS_INFO("GOING TO NODEHANDLE"); | ||
std::string master_uri = ros::master::getURI(); | ||
|
||
if (ros::master::check()) { | ||
ROS_INFO("ROS MASTER IS UP!"); | ||
} else { | ||
ROS_INFO("NO ROS MASTER."); | ||
virtual void stop() override { | ||
/* Write your clean-up code here */ | ||
ros::shutdown(); | ||
} | ||
ROS_INFO("%s", master_uri.c_str()); | ||
|
||
ros::NodeHandle n; | ||
|
||
ROS_INFO("GOING TO PUBLISHER"); | ||
|
||
// Creating a publisher and a subscriber | ||
// When something is received in chatter topic, a message is published in a_chatter topic | ||
chatter_pub = n.advertise<std_msgs::String>("a_chatter", 1000); | ||
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); | ||
|
||
// Rate 1Hz | ||
ros::WallRate loop_rate(1); | ||
|
||
while(1) { | ||
int events; | ||
struct android_poll_source* source; | ||
private: | ||
ros::Publisher chatter_pub; | ||
|
||
// Poll android events, without locking | ||
while (ALooper_pollAll(0, NULL, &events, (void**)&source) >= 0) { | ||
// Process this event | ||
if (source != NULL) { | ||
source->process(state, source); | ||
} | ||
|
||
// Check if we are exiting. | ||
if (state->destroyRequested != 0) { | ||
ROS_INFO("APP DESTROYED BYE BYE"); | ||
return; | ||
} | ||
} | ||
|
||
ros::spinOnce(); | ||
|
||
if (!ros::ok()) { | ||
ROS_INFO("ROS ISN'T OK, BYE BYE"); | ||
return; | ||
} | ||
|
||
loop_rate.sleep(); | ||
void chatterCallback(const std_msgs::String::ConstPtr& msg) { | ||
ROS_INFO("Received message: %s", msg->data.c_str()); | ||
} | ||
} | ||
}; | ||
|
||
MY_ROS_ANDROID_MAIN_THREAD(HelloRos) |
30 changes: 30 additions & 0 deletions
30
example_workspace/src/hello_world_example_app/app/src/main/cpp/main_thread.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdarg.h> | ||
#include <sstream> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <ros/ros.h> | ||
#include <ros_android/main_thread.h> | ||
|
||
|
||
namespace ros_android { | ||
std::mutex MainThread::s_instance_mutex; | ||
MainThread::Ptr MainThread::s_instance; | ||
MainThread::MainThread() : node_name("ros_android_node") {} | ||
MainThread::MainThread(std::string name) : node_name(name) {} | ||
MainThread::~MainThread() {} | ||
|
||
bool MainThread::check_ros_master(const std::vector<std::pair<std::string, std::string>> remappings) { | ||
bool rv; | ||
|
||
ros::init(remappings, node_name.c_str()); | ||
rv = ros::master::check(); | ||
if (rv) { | ||
ROS_INFO("ROS MASTER IS UP!"); | ||
} else { | ||
ROS_INFO("NO ROS MASTER."); | ||
} | ||
ros::start(); | ||
return rv; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only call
ros::start()
ifros::master::check()
returnstrue
?Apparently
ros::start()
tries to contact the master, logsand waits forever. Android then shows the "RosHello isn't responding" dialog.