如何让Webots支持C#语言开发的控制器

Webots支持C、C++、Java、Python、Matlab这五种语言开发控制器,没有直接支持C#,但有个同事已经用C#写了大量的机器人控制代码,想在不把C#代码改写成C++的情况下,直接用webots仿真,那就得想想办法。(不过,让ChatGPT帮忙把一种语言的代码转成另外一种语言的代码,还是挺方便的,准确率很高。)

在Webots的安装路径下,找到lib\controller文件夹,里面有Controller.dll和Controller.lib文件,对于C#,我们只需要Controller.dll文件(C和C++的开发就需要Controller.lib文件),把这个dll文件复制到C#程序编译输出exe的文件夹里,通常是bin\x64\Debug\net8.0,(注意是跟输出的exe文件放在同一文件夹里,而不是跟C#的源代码放在同一文件夹里。既不需要,也不能在VS解决方案中添加对Controller.dll的引用,尝试添加它会报错。Controller.dll是64位的,所以C#程序也要编译成64位的。)

Controller.dll实现了(或者说导出了)Webots\include\controller\c\webots文件夹中所有的头文件中声明的全部函数(共932个),比如最常用wb_robot_init(), wb_robot_step(),wb_motor_set_position(), wb_motor_set_velocity(), wb_motor_get_target_position(),肯定都有的。而cpp的函数其实都是在调用c的函数,看看\Webots\src\controller\cpp里面的cpp文件就明白了。如果你装了VS 2022,可以打开开始菜单中Visual Studio 2022文件夹里的随便点开一个 xxx Command Prompt for VS 2022, 输入

bash 复制代码
dumpbin /exports "E:\Program Files\Webots\lib\controller\Controller.dll"

就能看到所有的导出函数了。

为了能使用Controller.dll,在C#的cs文件中,

cs 复制代码
using System.Runtime.InteropServices;

再去robot.h, motor.h等头文件中找你需要的函数的定义,然后在一个C#的class里面加入大量类似下面的语句

cs 复制代码
[DllImport("Controller.dll")]
public static extern void wb_robot_init();

看.h文件中的函数定义,一定会遇到WbDeviceTag,可以在Webots\include\controller\c\webots\types.h中找到对WbDeviceTag的定义:

cpp 复制代码
typedef unsigned short WbDeviceTag;

对应到C#中就是ushort类型。遇到const char *,就换成string. 把需要用到的函数都经过上面的声明后,就能和在C语言中一样使用这些函数了。示例C#代码如下:

cs 复制代码
using System.Runtime.InteropServices;

public class Program
{
    [DllImport("Controller.dll")]
    public static extern void wb_robot_init();

    [DllImport("Controller.dll")]
    public static extern void wb_robot_cleanup();

    [DllImport("Controller.dll")]
    public static extern void wb_robot_step(int duration);

    [DllImport("Controller.dll")]
    public static extern void wb_motor_set_position(ushort tag, double position);

    [DllImport("Controller.dll")]
    public static extern void wb_motor_set_velocity(ushort tag, double velocity);

    [DllImport("Controller.dll")]
    public static extern void wb_motor_get_target_position(ushort tag);

    [DllImport("Controller.dll")]
    public static extern ushort wb_robot_get_device(string name);

    public static void GetAllMotor(ref ushort[] motor)
    {       
        motor[10] = wb_robot_get_device("TA_joint_01");
        motor[9] = wb_robot_get_device("TA_joint_02");
        motor[8] = wb_robot_get_device("TA_joint_03");
        motor[7] = wb_robot_get_device("TA_joint_04");
        motor[6] = wb_robot_get_device("TA_joint_05");
    }

    public static void Main()
    {   wb_robot_init();
        ushort[] motor = new ushort[11];
        GetAllMotor(ref motor);
        wb_motor_set_position(motor[10], Math.PI/2); 
        wb_motor_set_position(motor[8], 3);   // 单位是弧度 
        wb_robot_step(500);
        wb_robot_cleanup();
        Console.ReadKey();
    }
}

启动C#程序后会看到如下报错,暂未发现有什么不良影响,还是能正常操作webots中的机器人。

Error: 找不到指定的模块。

(dynamic library)

Error: failed to load E:/Program Files/Webots/resources/projects/plugins/robot_windows/generic/generic.dll library.

generic.dll的路径是正确的,然而即使把generic.dll跟Controller.dll放在一起,也无法解决这个报错。

相关推荐
你怎么知道我是队长4 分钟前
JavaScript 事件介绍
开发语言·前端·javascript
雪靡7 分钟前
WPF PerMonitorV2 下的跨窗口问题
c#·wpf
CodeStats9 分钟前
【Linux IO】从文件描述符到硬件中断:Linux IO 系统底层完全拆解
java·linux·开发语言·io·socket
苍狼唤12 分钟前
SQL+C# 增删改查(练习)
数据库·sql·c#
大阿明14 分钟前
C++智能指针与RAII机制精讲:彻底根治内存泄漏与野指针
java·jvm·c++
一生了无挂17 分钟前
C++面向对象核心精讲:类、对象、封装、权限与生命周期全解析
java·jvm·c++
SoaringPigeon24 分钟前
世界动作模型(WAM)综述:从 VLA 到具身智能的下一前沿
人工智能·机器人·自动驾驶
着迷不白28 分钟前
Linux系统故障修复、日志管理与安全加固实战指南
开发语言·php
郝学胜-神的一滴34 分钟前
[简化版 GAMES 101] 计算机图形学 17:纹理技术从基础原理到多场景实战应用
c++·unity·游戏引擎·图形渲染·three.js·opengl·unreal
贺国亚37 分钟前
A2A协议与Agent互操作-Task生命周期
开发语言·qt