00001 /* 00002 * Copyright (C) 2011 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 00005 * use this file except in compliance with the License. You may obtain a copy of 00006 * the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 00013 * License for the specific language governing permissions and limitations under 00014 * the License. 00015 */ 00016 00017 package com.github.rosjava.android_extras.android_tutorial_hokuyo; 00018 00019 import android.os.Bundle; 00020 import org.ros.address.InetAddressFactory; 00021 import org.ros.android.android_acm_serial.AcmDevice; 00022 import org.ros.android.android_acm_serial.AcmDeviceActivity; 00023 import org.ros.exception.RosRuntimeException; 00024 import com.github.rosjava.rosjava_extras.hokuyo.LaserScanPublisher; 00025 import com.github.rosjava.rosjava_extras.hokuyo.scip20.Device; 00026 import org.ros.namespace.GraphName; 00027 import org.ros.node.NodeConfiguration; 00028 import org.ros.node.NodeMainExecutor; 00029 import org.ros.time.NtpTimeProvider; 00030 00031 import java.util.concurrent.CountDownLatch; 00032 import java.util.concurrent.TimeUnit; 00033 00037 public class MainActivity extends AcmDeviceActivity { 00038 00039 private final CountDownLatch nodeRunnerServiceLatch; 00040 00041 private NodeMainExecutor nodeMainExecutor; 00042 00043 public MainActivity() { 00044 super("Hokuyo Node", "Hokuyo Node"); 00045 nodeRunnerServiceLatch = new CountDownLatch(1); 00046 } 00047 00048 @Override 00049 public void onCreate(Bundle savedInstanceState) { 00050 super.onCreate(savedInstanceState); 00051 setContentView(R.layout.main); 00052 } 00053 00054 @Override 00055 protected void init(NodeMainExecutor nodeMainExecutor) { 00056 nodeRunnerServiceLatch.countDown(); 00057 this.nodeMainExecutor = nodeMainExecutor; 00058 } 00059 00060 private void startLaserScanPublisher(AcmDevice acmDevice) { 00061 try { 00062 nodeRunnerServiceLatch.await(); 00063 } catch (InterruptedException e) { 00064 throw new RosRuntimeException(e); 00065 } 00066 NodeConfiguration nodeConfiguration = 00067 NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress(), 00068 getMasterUri()); 00069 nodeConfiguration.setNodeName(GraphName.newAnonymous()); 00070 NtpTimeProvider ntpTimeProvider = 00071 new NtpTimeProvider(InetAddressFactory.newFromHostString("192.168.0.1"), 00072 nodeMainExecutor.getScheduledExecutorService()); 00073 ntpTimeProvider.startPeriodicUpdates(1, TimeUnit.MINUTES); 00074 nodeConfiguration.setTimeProvider(ntpTimeProvider); 00075 Device scipDevice = 00076 new Device(acmDevice.getInputStream(), acmDevice.getOutputStream(), ntpTimeProvider); 00077 LaserScanPublisher laserScanPublisher = new LaserScanPublisher(scipDevice); 00078 nodeMainExecutor.execute(laserScanPublisher, nodeConfiguration); 00079 } 00080 00081 @Override 00082 public void onPermissionGranted(final AcmDevice acmDevice) { 00083 new Thread() { 00084 @Override 00085 public void run() { 00086 startLaserScanPublisher(acmDevice); 00087 }; 00088 }.start(); 00089 } 00090 00091 @Override 00092 public void onPermissionDenied() { 00093 } 00094 }