BaseControllerNode.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Creativa77.
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.c77.base_controller;
00018 
00019 import com.github.c77.base_driver.BaseDevice;
00020 
00021 import org.apache.commons.logging.Log;
00022 import org.apache.commons.logging.LogFactory;
00023 import org.ros.message.MessageListener;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.AbstractNodeMain;
00026 import org.ros.node.ConnectedNode;
00027 import org.ros.node.Node;
00028 import org.ros.node.topic.Subscriber;
00029 
00030 import geometry_msgs.Twist;
00031 
00036 public class BaseControllerNode extends AbstractNodeMain implements MessageListener<Twist> {
00037     private final BaseDevice baseDevice;
00038     private double linearVelX = 0.0;
00039     private double angVelZ = 0.0;
00040     private String CMD_VEL_TOPIC;
00041 
00042     private static final Log log = LogFactory.getLog(BaseControllerNode.class);
00043     Thread baseControllerThread;
00044 
00045     @Override
00046     public GraphName getDefaultNodeName() {
00047         return GraphName.of("android/move_base");
00048     }
00049 
00056     public BaseControllerNode(BaseDevice baseDevice, String vel_topic) {
00057         // TODO: Use ROS params to configure topic names
00058         CMD_VEL_TOPIC = vel_topic;
00059         this.baseDevice = baseDevice;
00060     }
00061 
00068     @Override
00069     public void onStart(ConnectedNode connectedNode) {
00070         log.info("Base controller starting.");
00071 
00078         baseControllerThread = new Thread() {
00079             @Override
00080             public void run() {
00081                 // thread to constantly send commands to the base
00082                 try {
00083                     while(true) {
00084                         baseDevice.move(linearVelX, angVelZ);
00085                         Thread.sleep(250);
00086                     }
00087                 } catch (Throwable t) {
00088                     log.error("Exception occurred during move loop", t);
00089                     // Whenever we get interrupted out of the loop, for any reason
00090                     // we try to stop the base, just in case
00091                     try {
00092                         setTwistValues(0.0, 0.0);
00093                         baseDevice.move(0.0, 0.0);
00094                     } catch(Throwable t0) {
00095                     }
00096                 }
00097             }
00098         };
00099 
00100         // Initialize base.
00101         baseDevice.initialize();
00102         baseControllerThread.start();
00103 
00104         // Start base_controller subscriber
00105         Subscriber<Twist> vel_listener = connectedNode.newSubscriber(CMD_VEL_TOPIC, Twist._TYPE);
00106         vel_listener.addMessageListener(this);
00107 
00108         log.info("Base controller initialized.");
00109     }
00110 
00111     @Override
00112     public void onShutdown(Node node) {
00113         // TODO: Make sure shutdown is working properly. i.e.: shutdown controller thread
00114         super.onShutdown(node);
00115     }
00116 
00117     @Override
00118     public void onShutdownComplete(Node node) {
00119         // TODO: Verify shutdown is working properly
00120         super.onShutdownComplete(node);
00121     }
00122 
00123     private synchronized void setTwistValues(double linearVelX, double angVelZ) {
00124         this.linearVelX = linearVelX;
00125         this.angVelZ = angVelZ;
00126         log.info("synchronized setting: (" + this.linearVelX + "," + this.angVelZ + ")");
00127     }
00128 
00135     @Override
00136     public void onNewMessage(Twist twist) {
00137         log.info("Current Twist msg: " + twist);
00138         setTwistValues(twist.getLinear().getX(), twist.getAngular().getZ());
00139     }
00140 }


android_base_controller
Author(s):
autogenerated on Fri Aug 28 2015 10:04:47