ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2012-10-06 16:48:43 -0600

sam gravatar image

I suggest you can ask each simple question seperately to let the one who know ROS can focus.

Here is my own way to run C# program on Ubuntu with ROS nodes.

streamvis-wrappers-ros

  make c# so file
  create new package
  roscreate-pkg sam_wrappers_cs_basic roscpp topic_tools
  cd sam_wrappers_cs_basic/

  vim src/wrapper.cpp
  // Copyright c Julian Brunner 2009 - 2011

  // This file is part of Stream Visualizer (streamvis).
  // 
  // Stream Visualizer is free software: you can redistribute it and/or modify it
  // under the terms of the GNU General Public License as published by the Free
  // Software Foundation, either version 3 of the License, or (at your option) any
  // later version.
  // 
  // Stream Visualizer is distributed in the hope that it will be useful, but
  // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  // details.
  // 
  // You should have received a copy of the GNU General Public License along with
  // Stream Visualizer. If not, see <http://www.gnu.org/licenses/>.

  #include "sam_wrappers_cs_basic/wrapper.h"

  #include <string>

  #include <ros/ros.h>
  #include <ros/serialization.h>
  #include <topic_tools/shape_shifter.h>

  //New
  #include "std_msgs/String.h"

  using namespace std;
  using namespace ros;
  using namespace ros::serialization;
  using namespace topic_tools;

  //New
  void chatterCallback(const std_msgs::String::ConstPtr& msg)
  {
        ROS_INFO("I heard: [%s]", msg->data.c_str());
  }

  extern "C" void InitializeRos(/*char* node*/)
  {
      int argc = 0;
      char** argv = NULL;

      //string nodeName(node);
      init(argc, argv, "IRA_CSharp_Node");
      NodeHandle n;
      Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
      spin();
  }

  void ShutdownRos()
  {
      shutdown();
  }
  void RosSpin()
  {
      spin();
  }

  //My subscriber
  extern "C" void InitializeSub(/*char* topic*/)
  {
      NodeHandle n;

      //string topicName(topic);
      Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
  }

  NodeHandle* CreateNode()
  {
      return new NodeHandle();
  }
  void DisposeNode(NodeHandle* node)
  {
      delete node;
  }

  Subscriber* CreateSubscriber(NodeHandle* node, const char* topicName, unsigned int queueLength, void (*callback)(ShapeShifter::ConstPtr))
  {
      return new Subscriber(node->subscribe<ShapeShifter>(topicName, queueLength, callback));
  }
  void DisposeSubscriber(Subscriber* subscriber)
  {
      delete subscriber;
  }

  // TODO: Memory leaks?
  const char* ShapeShifterGetDataType(const ShapeShifter::ConstPtr message)
  {
      string info = message->getDataType();

      char* result = new char[info.size() + 1];
      strcpy(result, info.c_str());
      return result;  
  }
  const char* ShapeShifterGetDefinition(const ShapeShifter::ConstPtr message)
  {
      string info = message->getMessageDefinition();

      char* result = new char[info.size() + 1];
      strcpy(result, info.c_str());
      return result;
  }
  unsigned char* ShapeShifterGetData(const ShapeShifter::ConstPtr message)
  {
      unsigned char* data = new unsigned char[message->size()];

      OStream stream(data, message->size());
      message->write(stream);

      return data;
  }
  unsigned int ShapeShifterGetDataLength(const ShapeShifter::ConstPtr message)
  {
      return message->size();
  }
  vim include/sam_wrappers_cs_basic/wrapper.h
  // Copyright c Julian Brunner 2009 - 2011

  // This file is part of Stream Visualizer (streamvis).
  // 
  // Stream Visualizer is free software: you can redistribute it and/or modify it
  // under the terms of the GNU General Public License as published by the Free
  // Software Foundation, either version 3 of the License, or (at your option) any
  // later version.
  // 
  // Stream Visualizer is distributed in the hope that it will be useful, but
  // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  // details.
  // 
  // You should have received a copy of the GNU General Public License along with
  // Stream Visualizer. If not, see <http://www.gnu.org/licenses/>.

  #ifndef __STREAMVIS_WRAPPERS_ROS_H__
  #define __STREAMVIS_WRAPPERS_ROS_H__

  #include <ros/ros.h>
  #include <topic_tools/shape_shifter.h>

  using namespace ros;
  using namespace topic_tools;

  extern "C"
  {
      void InitializeRos();
      void ShutdownRos();
      void RosSpin();

      NodeHandle* CreateNode();
      void DisposeNode(NodeHandle* node);

      Subscriber* CreateSubscriber(NodeHandle* node, const char* topicName, unsigned int queueLength, void (*callback)(const ShapeShifter::ConstPtr));
      void DisposeSubscriber(Subscriber* subscriber);

      const char* ShapeShifterGetDataType(const ShapeShifter::ConstPtr message);
      const char* ShapeShifterGetDefinition(const ShapeShifter::ConstPtr message);
      unsigned char* ShapeShifterGetData(const ShapeShifter::ConstPtr message);
      unsigned int ShapeShifterGetDataLength(const ShapeShifter::ConstPtr messsage);
  }

  #endif

  vim CMakeLists.txt
  cmake_minimum_required(VERSION 2.4.6)
  include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

  # Set the build type.  Options are:
  #  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
  #  Debug          : w/ debug symbols, w/o optimization
  #  Release        : w/o debug symbols, w/ optimization
  #  RelWithDebInfo : w/ debug symbols, w/ optimization
  #  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
  #set(ROS_BUILD_TYPE RelWithDebInfo)

  rosbuild_init()

  #set the default path for built executables to the "bin" directory
  set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
  #set the default path for built libraries to the "lib" directory
  set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

  #uncomment if you have defined messages
  #rosbuild_genmsg()
  #uncomment if you have defined services
  #rosbuild_gensrv()

  #common commands for building c++ executables and libraries
  #rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
  #target_link_libraries(${PROJECT_NAME} another_library)
  #rosbuild_add_boost_directories()
  #rosbuild_link_boost(${PROJECT_NAME} thread)
  #rosbuild_add_executable(example examples/example.cpp)
  #target_link_libraries(example ${PROJECT_NAME})
  rosbuild_add_library(${PROJECT_NAME} src/wrapper.cpp)

  rosmake
  Now it will generate libsam_wrappers_cs_basic.so

  Test that so file
  mkdir test
  cd test/
  copy that so file
  cp ../lib/libsam_wrappers_cs_basic.so .
  vim ros_init.cs
  using System;
  using System.Diagnostics;
  using System.Runtime.InteropServices;
  using System.Text.RegularExpressions;
  using System.Threading;
  using Data.Ros;

  namespace Data.Ros
  {
      public class RosNetwork 
      {
          static public void Main ()
          {   
              InitializeRos();
              //InitializeSub();
              //RosSpin();    
          }   

          [DllImport("libsam_wrappers_cs_basic")]
          static extern void InitializeRos(/*string node*/);
  /*
          [DllImport("streamvis-wrappers-ros")]
          static extern void InitializeSub(string topic);

          [DllImport("streamvis-wrappers-ros")]
          static extern void RosSpin();
  */
      }
  }
  use mono to compile .cs program to generate ros_init.exe
  gmcs ros_init.cs
  run talker
  rostopic pub /chatter std_msgs/String -r 5 "abcde"
  get the result
  ira@ira-K42JP:~/code/ros/sam_cs/sam_wrappers_cs_basic/test$ ./ros_init.exe 
  [ INFO] [1345021492.310405185]: I heard: [abcde]
  [ INFO] [1345021492.510243616]: I heard: [abcde]
  [ INFO] [1345021492.710016020]: I heard: [abcde]
  …
  you can view on rxgraph