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 import com.github.c77.base_driver.BaseStatus; 00021 00022 import org.apache.commons.logging.Log; 00023 import org.apache.commons.logging.LogFactory; 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.Publisher; 00029 00030 import java.util.concurrent.CountDownLatch; 00031 00032 import std_msgs.Byte; 00033 00037 public class BaseStatusPublisher extends AbstractNodeMain { 00038 00039 private Thread basePublisherThread; 00040 00041 private final BaseDevice baseDevice; 00042 00043 private Publisher<std_msgs.Byte> bumperPublisher; 00044 private Publisher<std_msgs.Byte> wheelDropPublisher; 00045 private Publisher<std_msgs.Byte> cliffPublisher; 00046 private Publisher<std_msgs.Byte> chargerPublisher; 00047 private Publisher<std_msgs.Byte> batteryPublisher; 00048 00049 private static final Log log = LogFactory.getLog(BaseStatusPublisher.class); 00050 00051 public BaseStatusPublisher(BaseDevice baseDevice) { 00052 this.baseDevice = baseDevice; 00053 } 00054 00055 @Override 00056 public GraphName getDefaultNodeName() { 00057 return GraphName.of("mobile_base/state_publisher"); 00058 } 00059 00060 @Override 00061 public void onStart(ConnectedNode connectedNode) { 00062 basePublisherThread = new Thread() { 00063 @Override 00064 public void run() { 00065 BaseStatus baseStatus = new BaseStatus(); 00066 try { 00067 while(true){ 00068 baseStatus = baseDevice.getBaseStatus(); 00069 BaseStatusPublisher.this.publishState(baseStatus); 00070 Thread.sleep(10); 00071 } 00072 } catch (Throwable t) { 00073 log.error("Exception occurred during state publisher loop.", t); 00074 } 00075 } 00076 }; 00077 00078 bumperPublisher = connectedNode.newPublisher("mobile_base/bumper", "std_msgs/Byte"); 00079 wheelDropPublisher = connectedNode.newPublisher("mobile_base/wheel_drop", "std_msgs/Byte"); 00080 cliffPublisher = connectedNode.newPublisher("mobile_base/cliff", "std_msgs/Byte"); 00081 chargerPublisher = connectedNode.newPublisher("mobile_base/charger", "std_msgs/Byte"); 00082 batteryPublisher = connectedNode.newPublisher("mobile_base/battery", "std_msgs/Byte"); 00083 00084 basePublisherThread.start(); 00085 } 00086 00087 private void publishState(BaseStatus baseStatus) { 00088 Byte bumper = bumperPublisher.newMessage(); 00089 bumper.setData(baseStatus.getBumper()); 00090 bumperPublisher.publish(bumper); 00091 00092 Byte wheelDrop = wheelDropPublisher.newMessage(); 00093 wheelDrop.setData(baseStatus.getWheelDrop()); 00094 wheelDropPublisher.publish(wheelDrop); 00095 00096 Byte cliff = cliffPublisher.newMessage(); 00097 cliff.setData(baseStatus.getCliff()); 00098 cliffPublisher.publish(cliff); 00099 00100 Byte charger = chargerPublisher.newMessage(); 00101 charger.setData(baseStatus.getCharger()); 00102 chargerPublisher.publish(charger); 00103 00104 Byte battery = batteryPublisher.newMessage(); 00105 battery.setData(baseStatus.getBattery()); 00106 batteryPublisher.publish(battery); 00107 } 00108 00109 @Override 00110 public void onShutdown(Node node) { 00111 super.onShutdown(node); 00112 } 00113 00114 @Override 00115 public void onShutdownComplete(Node node) { 00116 super.onShutdownComplete(node); 00117 } 00118 00119 }