标签:
ros和Android
ros基本要素:
4// This header defines the standard ROS classes .
5#include<ros / ros.h>
6
7int main (int argc ,char** argv ){
8// Initialize the ROS system .
9 ros::init ( argc , argv ," hello _ros ");
10
11// Establ ish this program as a ROS node .
12 ros::NodeHandle nh ;
13
14// Send some output as a log message .
15 ROS_INFO_STREAM(" Hello , ? ROS! ");
16}
1// This program publishes randomly−generated velocity
2// messages for turtlesim .
3#include<ros / ros.h>
4#include<geometry_msgs /Twist. h>// For geometry_msgs:: Twist
5#include<stdlib.h>// For rand() and RAND_MAX
6
7int main (int argc ,char** argv ){
8// Initialize the ROS system and become a node .
9 ros::init ( argc , argv ," publish _ velocity ");
10 ros::NodeHandle nh ;
11
12// Create a publisher obj ect .
13 ros::Publisher pub = nh . advertise <geometry_msgs::Twist>(
14" turtle1 /cmd_vel",1000);
15
16// Seed the random number generator .
17 srand ( time (0));
18
19// Loop at 2Hz until the node is shut down.
20 ros::Raterate(2);
21while( ros::ok ()){
22// Create and fill in the message . The other four
23// fields , which are ignored by turtl esim , default to 0.
24 geometry_msgs ::Twist msg ;
25 msg . linear . x =double( rand ())/double(RAND_MAX);
26 msg.angular.z =2*double( rand ())/double(RAND_MAX)−1;
27
28// Publish the message .
29 pub . publish ( msg );
30
31// Send a message to rosout with the details .
32 ROS_INFO_STREAM("Sending random velocity command : "
33<<" linear="<< msg.linear.x
34<<" angular="<< msg.angular.z);
35
36// Wait untilit‘s time for another iteration .
37 rate.sleep ();
38}
39}
表3
1// This program subscribes to turtle1/pose and shows its
2// messages on the screen .
3#include<ros / ros.h>
4#include<turtlesim /Pose.h>
5#include<iomanip>// for std::setprecision and std::fixed
6
7// A callback function . Executed each time a new pose
8// message arrives .
9void poseMessageReceived (const turtlesim::Pose& msg ){
10 ROS_INFO_STREAM( std::setprecision (2)<< std::fixed
11<<" position =("<< msg . x <<" , "<< msg . y <<" ) "
12<<" *direction="<< msg . theta );
13}
14
15int main (int argc ,char** argv ){
16// Initialize the ROS system and become a node .
17 ros::init ( argc , argv ," subscribe_to _pose ");
18 ros::NodeHandle nh ;
19
20// Create a subscri ber obj ect .
21 ros::Subscriber sub = nh.subscribe (" turtle1/pose ",1000,
22&poseMessageReceived );
23
24// Let ROS take over .
25 ros::spin ();
26}
/**
* 写
Publisher必须继承NodeMain 重写NodeMain的getDefaultNodeName() */
publicclassImuPublisherimplementsNodeMain
{
privateImuThread imuThread;
privateSensorListener sensorListener;
privateSensorManager sensorManager;
privatePublisher<Imu> publisher;
privateclassImuThreadextendsThread
{
privatefinalSensorManager sensorManager;
privateSensorListener sensorListener;
privateLooper threadLooper;
privatefinalSensor accelSensor;
privatefinalSensor gyroSensor;
privatefinalSensor quatSensor;
privateImuThread(SensorManager sensorManager,SensorListener sensorListener)
{
this.sensorManager = sensorManager;
this.sensorListener = sensorListener;
this.accelSensor =this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
this.gyroSensor =this.sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
this.quatSensor =this.sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
publicvoid run()
{
Looper.prepare();
this.threadLooper =Looper.myLooper();
this.sensorManager.registerListener(this.sensorListener,this.accelSensor,SensorManager.SENSOR_DELAY_FASTEST);
this.sensorManager.registerListener(this.sensorListener,this.gyroSensor,SensorManager.SENSOR_DELAY_FASTEST);
this.sensorManager.registerListener(this.sensorListener,this.quatSensor,SensorManager.SENSOR_DELAY_FASTEST);
Looper.loop();
}
publicvoid shutdown()
{
this.sensorManager.unregisterListener(this.sensorListener);
if(this.threadLooper !=null)
{
this.threadLooper.quit();
}
}
}
privateclassSensorListenerimplementsSensorEventListener
{
privatePublisher<Imu> publisher;
privateImu imu;
privateSensorListener(Publisher<Imu> publisher,boolean hasAccel,boolean hasGyro,boolean hasQuat)
{
this.publisher = publisher;
this.imu =this.publisher.newMessage();
}
// @Override
publicvoid onSensorChanged(SensorEvent event)
{
this.imu.getLinearAcceleration().setX(event.values[0]);
this.imu.getLinearAcceleration().setY(event.values[1]);
this.imu.getLinearAcceleration().setZ(event.values[2]);
this.imu.setLinearAccelerationCovariance(tmpCov);
this.imu.getAngularVelocity().setX(event.values[0]);
this.imu.getAngularVelocity().setY(event.values[1]);
this.imu.getAngularVelocity().setZ(event.values[2]);
double[] tmpCov ={0.0025,0,0,0,0.0025,0,0,0,0.0025};// TODO Make Parameter
this.imu.setAngularVelocityCovariance(tmpCov);
this.imu.getOrientation().setW(quaternion[0]);
this.imu.getOrientation().setX(quaternion[1]);
this.imu.getOrientation().setY(quaternion[2]);
this.imu.getOrientation().setZ(quaternion[3]);
double[] tmpCov ={0.001,0,0,0,0.001,0,0,0,0.001};// TODO Make Parameter
this.imu.setOrientationCovariance(tmpCov);
//组装header
// Convert event.timestamp (nanoseconds uptime) into system time, use that as the header stamp
long time_delta_millis =System.currentTimeMillis()-SystemClock.uptimeMillis();
this.imu.getHeader().setStamp(Time.fromMillis(time_delta_millis + event.timestamp/1000000));
this.imu.getHeader().setFrameId("/imu");// TODO Make parameter
publisher.publish(this.imu);
// Create a new message ,清空了
this.imuthis.imu =this.publisher.newMessage();
// Reset times
this.accelTime =0;
this.gyroTime =0;
this.quatTime =0;
}
}
}
publicImuPublisher(SensorManager manager)
{
this.sensorManager = manager;
}
publicGraphName getDefaultNodeName()
{
returnGraphName.of("android_sensors_driver/imuPublisher");
}
publicvoid onError(Node node,Throwable throwable)
{
}
publicvoid onStart(ConnectedNode node)
{
try
{
this.publisher = node.newPublisher("android/imu","sensor_msgs/Imu");
......
}
catch(Exception e)
{
if(node !=null)
{
node.getLog().fatal(e);
}
else
{
e.printStackTrace();
}
}
}
//@Override
publicvoid onShutdown(Node arg0)
{
}
//@Override
publicvoid onShutdownComplete(Node arg0)
{
}
}
publicinterfaceNodeMainextendsNodeListener{
GraphName getDefaultNodeName();
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package sensor_msgs;
import geometry_msgs.Quaternion;
import geometry_msgs.Vector3;
import org.ros.internal.message.Message;
import std_msgs.Header;
publicinterfaceImuextendsMessage{
String _TYPE ="sensor_msgs/Imu";
String _DEFINITION ="# This is a message to hold data from an IMU (Inertial Measurement Unit)\n#\n# Accelerations should be in m/s^2 (not in g\‘s), and rotational velocity should be in rad/sec\n#\n# If the covariance of the measurement is known, it should be filled in (if all you know is the \n# variance of each measurement, e.g. from the datasheet, just put those along the diagonal)\n# A covariance matrix of all zeros will be interpreted as \"covariance unknown\", and to use the\n# data a covariance will have to be assumed or gotten from some other source\n#\n# If you have no estimate for one of the data elements (e.g. your IMU doesn\‘t produce an orientation \n# estimate), please set element 0 of the associated covariance matrix to -1\n# If you are interpreting this message, please check for a value of -1 in the first element of each \n# covariance matrix, and disregard the associated estimate.\n\nHeader header\n\ngeometry_msgs/Quaternion orientation\nfloat64[9] orientation_covariance # Row major about x, y, z axes\n\ngeometry_msgs/Vector3 angular_velocity\nfloat64[9] angular_velocity_covariance # Row major about x, y, z axes\n\ngeometry_msgs/Vector3 linear_acceleration\nfloat64[9] linear_acceleration_covariance # Row major x, y z \n";
Header getHeader();
void setHeader(Header var1);
Quaternion getOrientation();
void setOrientation(Quaternion var1);
double[] getOrientationCovariance();
void setOrientationCovariance(double[] var1);
Vector3 getAngularVelocity();
void setAngularVelocity(Vector3 var1);
double[] getAngularVelocityCovariance();
void setAngularVelocityCovariance(double[] var1);
Vector3 getLinearAcceleration();
void setLinearAcceleration(Vector3 var1);
double[] getLinearAccelerationCovariance();
void setLinearAccelerationCovariance(double[] var1);
}
publicclass XActivityextendsRosActivity
{
privateImuPublisher imu_pub;
privateSensorManager mSensorManager;
public XActivity()
{
super("Ros Android Sensors Driver","Ros Android Sensors Driver");
}
@Override
protectedvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager =(SensorManager)this.getSystemService(SENSOR_SERVICE);
}
@Override
protectedvoid onResume()
{
super.onResume();
}
@Override
protectedvoid init(NodeMainExecutor nodeMainExecutor)
{
NodeConfiguration nodeConfiguration3 =NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress());
nodeConfiguration3.setMasterUri(getMasterUri());
nodeConfiguration3.setNodeName("android_sensors_driver_imu");
this.imu_pub =newImuPublisher(mSensorManager);
nodeMainExecutor.execute(this.imu_pub, nodeConfiguration3);
}
}
publicGraphName getDefaultNodeName()
{
//节点名称
returnGraphName.of("android_sensors_driver/imuPublisher");
}
@Override
protectedvoid init(NodeMainExecutor nodeMainExecutor)
{
...
}
protectedRosActivity(String notificationTicker,String notificationTitle, URI customMasterUri){
super();
this.notificationTicker = notificationTicker;
this.notificationTitle = notificationTitle;
nodeMainExecutorServiceConnection =newNodeMainExecutorServiceConnection(customMasterUri);
}
@Override
protectedvoid onStart(){
super.onStart();
bindNodeMainExecutorService();//启动service
}
//
startService+bindService
protectedvoid bindNodeMainExecutorService(){
Intent intent =newIntent(this,NodeMainExecutorService.class);
intent.setAction(NodeMainExecutorService.ACTION_START);
intent.putExtra(NodeMainExecutorService.EXTRA_NOTIFICATION_TICKER, notificationTicker);
intent.putExtra(NodeMainExecutorService.EXTRA_NOTIFICATION_TITLE, notificationTitle);
startService(intent);//启动服务
Preconditions.checkState(
bindService(intent, nodeMainExecutorServiceConnection, BIND_AUTO_CREATE),//绑定连接状态回调
"Failed to bind NodeMainExecutorService.");
}
privatefinalclassNodeMainExecutorServiceConnectionimplementsServiceConnection{
private URI customMasterUri;
publicNodeMainExecutorServiceConnection(URI customUri){
super();
customMasterUri = customUri;
}
@Override
publicvoid onServiceConnected(ComponentName name,IBinder binder){
nodeMainExecutorService =((NodeMainExecutorService.LocalBinder) binder).getService();//取出发布的
操作类
if(customMasterUri !=null){
nodeMainExecutorService.setMasterUri(customMasterUri);
nodeMainExecutorService.setRosHostname(getDefaultHostAddress());
}
nodeMainExecutorService.addListener(newNodeMainExecutorServiceListener(){
@Override
publicvoid onShutdown(NodeMainExecutorService nodeMainExecutorService){
// We may have added multiple shutdown listeners and we only want to
// call finish() once.
if(!RosActivity.this.isFinishing()){
RosActivity.this.finish();
}
}
});
if(getMasterUri()==null){
startMasterChooser();
}else{
init();
}
}
@Override
publicvoid onServiceDisconnected(ComponentName name){
}
};
protectedvoid init(){
// Run init() in a new thread as a convenience since it often requires
// network access.
newAsyncTask<Void,Void,Void>(){
@Override
protectedVoid doInBackground(Void... params){
RosActivity.this.init(nodeMainExecutorService);//调子类的方法,传递发布的操作类
returnnull;
}
}.execute();
}
@Override
publicvoid execute(NodeMain nodeMain,NodeConfiguration nodeConfiguration,
Collection<NodeListener> nodeListeneners){
nodeMainExecutor.execute(nodeMain, nodeConfiguration, nodeListeneners);//主界面调用的
execute到了这里}
@Override
publicvoid execute(NodeMain nodeMain,NodeConfiguration nodeConfiguration){
execute(nodeMain, nodeConfiguration,null);
}
publicNodeMainExecutorService(){
super();
rosHostname =null;
nodeMainExecutor =DefaultNodeMainExecutor.newDefault();//初始化
binder =newLocalBinder();
listeners =
newListenerGroup<NodeMainExecutorServiceListener>(
nodeMainExecutor.getScheduledExecutorService());
}
publicstaticNodeMainExecutor newDefault(){
return newDefault(newDefaultScheduledExecutorService());
}
publicstaticNodeMainExecutor newDefault(ScheduledExecutorService executorService){
returnnewDefaultNodeMainExecutor(newDefaultNodeFactory(executorService), executorService);
}
privateDefaultNodeMainExecutor(NodeFactory nodeFactory,ScheduledExecutorService scheduledExecutorService){
this.nodeFactory = nodeFactory;
this.scheduledExecutorService = scheduledExecutorService;
this.connectedNodes =Multimaps.synchronizedMultimap(HashMultimap.create());
this.nodeMains =Maps.synchronizedBiMap(HashBiMap.create());
Runtime.getRuntime().addShutdownHook(newThread(newRunnable(){
publicvoid run(){
DefaultNodeMainExecutor.this.shutdown();
}
}));
}
publicvoid execute(finalNodeMain nodeMain,NodeConfiguration nodeConfiguration,finalCollection<NodeListener> nodeListeners){
finalNodeConfiguration nodeConfigurationCopy =NodeConfiguration.copyOf(nodeConfiguration);
nodeConfigurationCopy.setDefaultNodeName(nodeMain.getDefaultNodeName());
Preconditions.checkNotNull(nodeConfigurationCopy.getNodeName(),"Node name not specified.");
this.scheduledExecutorService.execute(newRunnable(){
publicvoid run(){
ArrayList nodeListenersCopy =Lists.newArrayList();
nodeListenersCopy.add(DefaultNodeMainExecutor.this.newRegistrationListener(null));
nodeListenersCopy.add(nodeMain);
if(nodeListeners !=null){
nodeListenersCopy.addAll(nodeListeners);
}
Node node =DefaultNodeMainExecutor.this.nodeFactory.newNode(nodeConfigurationCopy, nodeListenersCopy);
DefaultNodeMainExecutor.this.nodeMains.put(node, nodeMain);
}
});
}
publicNodeConfiguration setDefaultNodeName(GraphName nodeName){
if(this.nodeName ==null){
this.setNodeName(nodeName);
}
returnthis;
}
Node node =DefaultNodeMainExecutor.this.nodeFactory.newNode(nodeConfigurationCopy, nodeListenersCopy);
publicclassDefaultNodeFactoryimplementsNodeFactory{
privatefinalScheduledExecutorService scheduledExecutorService;
publicDefaultNodeFactory(ScheduledExecutorService scheduledExecutorService){
this.scheduledExecutorService =newSharedScheduledExecutorService(scheduledExecutorService);
}
publicNode newNode(NodeConfiguration nodeConfiguration,Collection<NodeListener> listeners){
returnnewDefaultNode(nodeConfiguration, listeners,this.scheduledExecutorService);
}
publicNode newNode(NodeConfiguration nodeConfiguration){
returnthis.newNode(nodeConfiguration,(Collection)null);
}
}
DefaultNode.java
publicDefaultNode(NodeConfiguration nodeConfiguration,Collection<NodeListener> nodeListeners,ScheduledExecutorService scheduledExecutorService){
this.nodeConfiguration =NodeConfiguration.copyOf(nodeConfiguration);
this.nodeListeners =newListenerGroup(scheduledExecutorService);
this.nodeListeners.addAll(nodeListeners);
this.scheduledExecutorService = scheduledExecutorService;
this.masterUri = nodeConfiguration.getMasterUri();
this.masterClient =newMasterClient(this.masterUri);
this.topicParticipantManager =newTopicParticipantManager();
this.serviceManager =newServiceManager();
this.parameterManager =newParameterManager(scheduledExecutorService);
GraphName basename = nodeConfiguration.getNodeName();
NameResolver parentResolver = nodeConfiguration.getParentResolver();
this.nodeName = parentResolver.getNamespace().join(basename);
this.resolver =newNodeNameResolver(this.nodeName, parentResolver);
this.slaveServer =newSlaveServer(this.nodeName, nodeConfiguration.getTcpRosBindAddress(), nodeConfiguration.getTcpRosAdvertiseAddress(),
nodeConfiguration.getXmlRpcBindAddress(), nodeConfiguration.getXmlRpcAdvertiseAddress(),this.masterClient,this.topicParticipantManager,
this.serviceManager,this.parameterManager, scheduledExecutorService);
//
nodeConfiguration.getTcpRosBindAddress()默认情况下端口为0this.slaveServer.start();
NodeIdentifier nodeIdentifier =this.slaveServer.toNodeIdentifier();
this.parameterTree =DefaultParameterTree.newFromNodeIdentifier(nodeIdentifier,this.masterClient.getRemoteUri(),this.resolver,this.parameterManager);
this.publisherFactory =newPublisherFactory(nodeIdentifier,this.topicParticipantManager, nodeConfiguration.getTopicMessageFactory(), scheduledExecutorService);
this.subscriberFactory =newSubscriberFactory(nodeIdentifier,this.topicParticipantManager, scheduledExecutorService);
this.serviceFactory =newServiceFactory(this.nodeName,this.slaveServer,this.serviceManager, scheduledExecutorService);
this.registrar =newRegistrar(this.masterClient, scheduledExecutorService);
this.topicParticipantManager.setListener(this.registrar);
this.serviceManager.setListener(this.registrar);
scheduledExecutorService.execute(newRunnable(){
publicvoid run(){
DefaultNode.this.start();
}
});
}
publicClient(URI uri,Class<T> interfaceClass){
this.uri = uri;
XmlRpcClientConfigImpl config =newXmlRpcClientConfigImpl();
try{
config.setServerURL(uri.toURL());
NodeConfiguration nodeConfiguration3 =NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress());
nodeConfiguration3.setMasterUri(getMasterUri());//如果没有设置默认值为 "http://localhost:11311/"
nodeConfiguration3.setNodeName("android_sensors_driver_imu");
privateclassRegistrationListenerimplementsNodeListener{
privateRegistrationListener(){
}
publicvoid onStart(ConnectedNode connectedNode){
DefaultNodeMainExecutor.this.registerNode(connectedNode);
}
publicvoid onShutdown(Node node){
}
publicvoid onShutdownComplete(Node node){
DefaultNodeMainExecutor.this.unregisterNode(node);
}
publicvoid onError(Node node,Throwable throwable){
DefaultNodeMainExecutor.log.error("Node error.", throwable);
DefaultNodeMainExecutor.this.unregisterNode(node);
}
}
}
//@Override
publicvoid onStart(ConnectedNode node)
{
try
{
this.publisher = node.newPublisher("android/fix","sensor_msgs/NavSatFix");
this.navSatFixListener =newNavSatListener(publisher);
this.navSatThread =newNavSatThread(this.locationManager,this.navSatFixListener);
this.navSatThread.start();
}
catch(Exception e)
{
if(node !=null)
{
node.getLog().fatal(e);
}
else
{
e.printStackTrace();
}
}
}
//@Override
publicvoid onShutdown(Node arg0){
this.navSatThread.shutdown();
try{
this.navSatThread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
}
//@Override
publicvoid onShutdownComplete(Node arg0){
}
publicvoid onError(Node node,Throwable throwable)
{
}
标签:
原文地址:http://www.cnblogs.com/baldermurphy/p/5789310.html