代码拉取完成,页面将自动刷新
同步操作将从 大师兄法号随缘/RosSharp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
博客地址:(https://www.cnblogs.com/zjwno1/p/14085226.html)
本代码是基于uml-robotics的ROS.NET修改的 改善了Service服务端与客户端通信的数据问题,并修改成多平台使用 包括了 .netcore 3.0、.netcore 3.1 、.net5 、.netframework 4.0 、.netframework 4.5 、.netframework 4.6
ROS.Sharp是一款用C#开发ROS的库 我们可以使用这个库编写订阅者和发布者,Serice客户端和Service服务端,以及参数服务器操作。
Install-Package ROS.Sharp -Version 1.1.0
class Program
{
static Thread thread;
static void Main(string[] args)
{
ROS.ROS_MASTER_URI = "http://192.168.20.132:11311";
ROS.ROS_HOSTNAME = "192.168.20.1";
ROS.ROS_IP = "192.168.20.132";
string nodeName = "bbb";
ROS.Init(nodeName);
NodeHandle nh = new NodeHandle();
Publisher<Messages.std_msgs.String> pub = nh.Advertise<Messages.std_msgs.String>("/chatter", 1, false);
thread = new Thread(() =>
{
int i = 0;
while (ROS.ok)
{
i++;
pub.Publish(new Messages.std_msgs.String { data = "Message" + i });
if (i == 65536)
i = 0;
Thread.Sleep(1000);
}
});
thread.Start();
Console.CancelKeyPress += Console_CancelKeyPress;
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
ROS.Shutdown();
ROS.WaitForShutdown();
}
}
class Program
{
static void Main(string[] args)
{
ROS.ROS_MASTER_URI = "http://192.168.20.132:11311";
ROS.ROS_HOSTNAME = "192.168.20.1";
ROS.ROS_IP = "192.168.20.132";
string nodeName = "aaa";
ROS.Init(nodeName);
NodeHandle nh = new NodeHandle();
Subscriber<Messages.std_msgs.String> sub = nh.Subscribe<Messages.std_msgs.String>("/chatter", 10, subCallback);
Console.CancelKeyPress += Console_CancelKeyPress;
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
ROS.Shutdown();
ROS.WaitForShutdown();
}
private static void subCallback(Messages.std_msgs.String argument)
{
Console.WriteLine(argument.data);
}
}
class Program
{
static void Main(string[] args)
{
ROS.ROS_MASTER_URI = "http://192.168.20.132:11311";
ROS.ROS_HOSTNAME = "192.168.20.1";
ROS.ROS_IP = "192.168.20.132";
ROS.Init("ServiceServerTest");
NodeHandle nodeHandle = new NodeHandle();
ServiceServer server = nodeHandle.AdvertiseService<TwoInts.Request, TwoInts.Response>("/add_two_ints", addition);
Console.CancelKeyPress += Console_CancelKeyPress;
}
private static bool addition(TwoInts.Request req, ref TwoInts.Response resp)
{
resp.sum = req.a + req.b;
return true;
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
ROS.Shutdown();
}
}
class Program
{
static void Main(string[] args)
{
ROS.ROS_MASTER_URI = "http://192.168.20.132:11311";
ROS.ROS_HOSTNAME = "192.168.20.1";
ROS.ROS_IP = "192.168.20.132";
ROS.Init("ServiceClientTest");
NodeHandle nodeHandle = new NodeHandle();
new Thread(new ThreadStart(() =>
{
Random r = new Random();
while (ROS.ok)
{
try
{
TwoInts.Request req = new TwoInts.Request() { a = r.Next(100), b = r.Next(100) };
TwoInts.Response resp = new TwoInts.Response();
DateTime before = DateTime.Now;
bool res = nodeHandle.ServiceClient<TwoInts.Request, TwoInts.Response>("/add_two_ints").Call(req, ref resp);
TimeSpan dif = DateTime.Now.Subtract(before);
string str = "";
if (res)
str = "" + req.a + " + " + req.b + " = " + resp.sum + "\n";
else
str = "call failed after\n";
str += Math.Round(dif.TotalMilliseconds, 2) + " ms";
Console.WriteLine(str);
}
catch
{
}
Thread.Sleep(500);
}
})).Start();
Console.CancelKeyPress += Console_CancelKeyPress;
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
ROS.Shutdown();
}
}
class Program
{
static void Main(string[] args)
{
ROS.ROS_MASTER_URI = "http://192.168.20.132:11311";
ROS.ROS_HOSTNAME = "192.168.20.1";
ROS.ROS_IP = "192.168.20.132";
IDictionary remappings;
RemappingHelper.GetRemappings(out remappings);
Network.Init(remappings);
Master.Init(remappings);
ThisNode.Init("", remappings, (int)(InitOption.AnonymousName | InitOption.NoRousout));
Param.Init(remappings);
//获得所有参数
List<string> list = Param.List();
foreach (string s in list)
{
Console.WriteLine(s);
}
//获取参数值
foreach (string s in list)
{
bool flag = Param.Has(s);
if (!flag)
{
Console.WriteLine("不存在参数{0}", s);
continue;
}
object obj = Param.Get(s);
if (obj == null)
{
Console.WriteLine("获取参数失败");
continue;
}
Console.WriteLine("参数:{0},值:{1}", s, obj);
}
//设置参数值
//Param.set(key, value);
//Param.del(key)
Console.ReadKey();
}
}
static void Main(string[] args)
{
string inputDir = AppDomain.CurrentDomain.BaseDirectory + "common_msgs/";
string outputdir = AppDomain.CurrentDomain.BaseDirectory + "Messages";
Console.WriteLine("开始生成ROS Messages...\n");
GenerateHelper.GenerateMsgAndSrv(inputDir, outputdir);
Console.WriteLine("生成完成");
Console.ReadLine();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。