ros android ndk,android_ndk/Tutorials/SimpleNativeActivityApplication - ROS Wiki

Note: This tutorial assumes that you have completed the previous tutorials: building native ROS packages.

6da015a8d37b40830bd0ceda913832f2.png Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Simple Native Activity Application

Description: This tutorial shows how to create a sample Android native application running ROS native nodes, and run it on an Android device

Keywords: Native Activity

Tutorial Level: BEGINNER

Next Tutorial: move_base Native Activity Application

Simple Native Activity Application

The Code

If you've followed the previous tutorial, the sample app code can be found in:

~/ros-android-ndk/roscpp_android/files/sample_app/jni/src/test.cpp

Here you can see the code:

#include "ros/ros.h"

#include

#include

int loop_count_ = 0;

ros::Publisher chatter_pub;

void log(const char *msg, ...) {

va_list args;

va_start(args, msg);

__android_log_vprint(ANDROID_LOG_INFO, "ROSCPP_NDK_EXAMPLE", msg, args);

va_end(args);

// from android samples

/* return current time in seconds */

static double now(void) {

struct timespec res;

clock_gettime(CLOCK_REALTIME, &res);

return res.tv_sec + (double) res.tv_nsec / 1e9;

#define LASTERR strerror(errno)

void chatterCallback(const std_msgs::StringConstPtr& 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);

log(msg->data.c_str());

void android_main(android_app *papp) {

// Make sure glue isn't stripped

app_dummy();

int argc = 3;

// TODO: don't hardcode ip addresses

char *argv[] = {"nothing_important" , "__master:=http://192.168.1.100:11311", "__ip:=192.168.1.101"};

//strcpy(argv[0], 'nothing_important');

//argv[1] = '__master:=http://10.52.90.103:11311';

//argv[2] = '__ip:=10.52.90.246';

//argv[3] = '__hostname:=10.52.90.246';

log("GOING TO ROS INIT");

for(int i = 0; i < argc; i++){

log(argv[i]);

ros::init(argc, &argv[0], "android_ndk_native_cpp");

log("GOING TO NODEHANDLE");

std::string master_uri = ros::master::getURI();

if(ros::master::check()){

log("ROS MASTER IS UP!");

} else {

log("NO ROS MASTER.");

log(master_uri.c_str());

ros::NodeHandle n;

log("GOING TO PUBLISHER");

chatter_pub = n.advertise<:string>("a_chatter", 1000);

ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

ros::WallRate loop_rate(100);

log("GOING TO SPIN");

while(ros::ok() && !papp->destroyRequested){

ros::spinOnce();

loop_rate.sleep();

The Code Explained

Now, let's break the code down.

#include "ros/ros.h"

#include

We include the usual ROS header ros/ros.h and a message header.

#include

Android header for using a native activity.

void chatterCallback(const std_msgs::StringConstPtr& 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);

log(msg->data.c_str());

We define a callback function for our subscriber.

Our simple node will listen to messages published on the ´chatter´ topic and when it receives a message, we will publish a message to the topic ´a_chatter´.

Main entry point

void android_main(android_app *papp) {

// Make sure glue isn't stripped

app_dummy();

int argc = 3;

// TODO: don't hardcode ip addresses

char *argv[] = {"nothing_important" , "__master:=http://192.168.1.100:11311", "__ip:=192.168.1.101"};

//strcpy(argv[0], 'nothing_important');

//argv[1] = '__master:=http://10.52.90.103:11311';

//argv[2] = '__ip:=10.52.90.246';

//argv[3] = '__hostname:=10.52.90.246';

log("GOING TO ROS INIT");

for(int i = 0; i < argc; i++){

log(argv[i]);

ros::init(argc, &argv[0], "android_ndk_native_cpp");

log("GOING TO NODEHANDLE");

std::string master_uri = ros::master::getURI();

if(ros::master::check()){

log("ROS MASTER IS UP!");

} else {

log("NO ROS MASTER.");

log(master_uri.c_str());

ros::NodeHandle n;

log("GOING TO PUBLISHER");

chatter_pub = n.advertise<:string>("a_chatter", 1000);

ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

ros::WallRate loop_rate(100);

log("GOING TO SPIN");

while(ros::ok() && !papp->destroyRequested){

ros::spinOnce();

loop_rate.sleep();

This is the main entry point of the activity.

char *argv[] = {"nothing_important" , "__master:=http://192.168.1.100:11311", "__ip:=192.168.1.101"};

Here you need to specify "master" to be the URI of your workstation. You also need to set "ip" to the IP address of your Android device.

ros::init(argc, &argv[0], "android_ndk_native_cpp");

Like any ROS program, we do a ´ros::init()´, passing the configuration arguments previously defined and also specifying the name of the node.

std::string master_uri = ros::master::getURI();

if(ros::master::check()){

log("ROS MASTER IS UP!");

} else {

log("NO ROS MASTER.");

log(master_uri.c_str());

ros::NodeHandle n;

We do a test to check if the specified ROS master is running before creating the nodeHandle.

chatter_pub = n.advertise<:string>("a_chatter", 1000);

ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

ros::WallRate loop_rate(100);

We create a publisher to the topic ´a_chatter´, a subscriber to the topic ´chatter´ and set the loop time.

while(ros::ok() && !papp->destroyRequested){

ros::spinOnce();

loop_rate.sleep();

Lastly we start spinning to get the callback function called when a new message arrives.

Installing app on Android device

First, install android-tools-adb by running:$ sudo apt-get install android-tools-adb

After setting the correct master URI and IP, you need to re-build the app, the easiest way is to run the ´do_docker.sh´ again.

To install the sample app into your Android device, connect the device via an USB cable to your PC (you need to enable Development Mode) and run$ adb install -r output/sample_app/bin/sample_app-debug.apk

Running the sample app

First, launch the roscore in a new terminal window:roscore

Next, run the application on the Android device. Make sure it is on the same network as the ROS Master.

Next, in a new terminal,send a message to the sample app :rostopic pub /chatter std_msgs/String "Hello!" -r 1

Finally, in a new terminal, listen to the published topic from the sample app.rostopic echo a_chatter

You should see something like:hello world from android ndk 1

hello world from android ndk 2

hello world from android ndk 3

hello world from android ndk 4

Congratulations, your ROS Native Activity is running on Android.

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值