概述
由于网上大多贴出的都是核心代码,一些细节配置我是在github和ros wiki上找到的,下面给大家总结出来,可以直接配置运行了
Android篇:
1.在Android studio的build.gradle(Project:xx)中替换和添加下面代码
buildscript { apply from: "https://github.com/rosjava/android_core/raw/kinetic/buildscript.gradle" } subprojects { apply plugin: 'ros-android' afterEvaluate { project -> android { // Exclude a few files that are duplicated across our dependencies and // prevent packaging Android applications. packagingOptions { exclude "META-INF/LICENSE.txt" exclude "META-INF/NOTICE.txt" } } } }
2.在build.grade(Module:app)中的dependencies中添加
implementation 'org.ros.android_core:android_core_components:0.4.0'
3.在AndroidManifest.xml中添加
权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application>中添加
<activity android:name="org.ros.android.MasterChooser" /> <service android:name="org.ros.android.NodeMainExecutorService" > <intent-filter> <action android:name="org.ros.android.NodeMainExecutorService" /> </intent-filter> </service>
application的属性中添加
xmlns:tools="http://schemas.android.com/tools" tools:replace="android:icon"
4.代码部分
public class Listener extends AbstractNodeMain { @Override public GraphName getDefaultNodeName() { return GraphName.of("rosjava_pubsub/listener"); } @Override public void onStart(ConnectedNode connectedNode) { final Log log=connectedNode.getLog(); Subscriber<std_msgs.String> subscriber=connectedNode.newSubscriber("chatter", String._TYPE); subscriber.addMessageListener(new MessageListener<std_msgs.String>() { @Override public void onNewMessage(std_msgs.String message) { log.info("I heard: "" + message.getData() + """); } }); } }
public class Talker extends AbstractNodeMain { @Override public GraphName getDefaultNodeName() { return GraphName.of("rosjava_pubsub/talker"); } @Override public void onStart(ConnectedNode connectedNode) { final Publisher<std_msgs.String> publisher=connectedNode.newPublisher("chatter", String._TYPE); connectedNode.executeCancellableLoop(new CancellableLoop() { private int sequenceNumber; @Override protected void setup() { sequenceNumber=0; } @Override protected void loop() throws InterruptedException { std_msgs.String str=publisher.newMessage(); str.setData("Android Hello world! "+sequenceNumber); publisher.publish(str); sequenceNumber++; Thread.sleep(1000); } }); } }
public class RosTestActivity extends RosActivity { private RosTextView<std_msgs.String> rosTextView; public RosTestActivity() { super("Robot","Robot"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ros_test); rosTextView=findViewById(R.id.textRos); rosTextView.setTopicName("chatter"); rosTextView.setMessageType(String._TYPE); rosTextView.setMessageToStringCallable(new MessageCallable<java.lang.String, String>() { @Override public java.lang.String call(std_msgs.String message) { return message.getData(); } }); } @Override protected void init(NodeMainExecutor nodeMainExecutor) { NodeConfiguration configuration=NodeConfiguration.newPublic(getRosHostname()); Log.d("",InetAddressFactory.newNonLoopback().getHostAddress()); configuration.setMasterUri(getMasterUri()); Listener listener=new Listener(); Talker talker=new Talker(); nodeMainExecutor.execute(listener,configuration); nodeMainExecutor.execute(talker,configuration); nodeMainExecutor.execute(rosTextView,configuration); } @SuppressLint("StaticFieldLeak") @Override public void startMasterChooser() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); java.lang.String masterURI = prefs.getString("masterURI", "http://192.168.1.111:11311"); Intent data = new Intent(); data.putExtra("ROS_MASTER_URI", masterURI); onActivityResult(0, RESULT_OK, data); } }
总结:startMasterChooser()这个函数被我重写了,如果不重写大家可以试一下,会默认跳转到MasterChooser这个Activity里,这里在app UI会显示一个连接网络的界面,但是我们大多时候是不想用它提供的界面的,所以重写这个函数就很有必要了。
Java篇
直接上代码了(这里的Listener和Talker和Android的一样,就不重复贴出来了)
public class Test {
private static Talker talker;
private static Linstener linstener;
public static NodeMainExecutor mNodeMainExecutorService;
public static void main(String[] args) {
init();
}
private static void init() {
mNodeMainExecutorService=DefaultNodeMainExecutor.newDefault();
startTask();
}
private static String getDefaultHostAddress() {
return InetAddressFactory.newNonLoopback().getHostAddress();
}
private static void startTask() {
NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(getDefaultHostAddress());
linstener = new Linstener();
talker = new Talker();
mNodeMainExecutorService.execute(linstener, nodeConfiguration);
mNodeMainExecutorService.execute(talker, nodeConfiguration);
}
}
总结:这是最简化的代码,启程序的时候要先启动roscore(我是用命令行启的),如果想在代码中启动roscore需要使用RosCore
private RosCore rosCore;
rosCore = RosCore.newPublic("192.168.1.111", 11311);
rosCore.start();
rosCore.awaitStart();
希望能帮助到大家!
最后
以上就是威武马里奥为你收集整理的RosJava在Android和Java程序中的使用希望能帮助到大家! 的全部内容,希望文章能够帮你解决RosJava在Android和Java程序中的使用希望能帮助到大家! 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复