Skip to content

SDK 快速开始

randoms edited this page May 24, 2019 · 1 revision

获取当前局域网内所有机器人

GalileoSDK sdk = new GalileoSDK(); // 实例化SDK
List<ServerInfo> servers = sdk.GetServersOnline(); // 获取局域网内在线机器人
foreach (var server in servers)
{
    Console.WriteLine("server: " + server.ID); // 打印机器人ID
}
Thread.Sleep(3000); // 等待3s
servers = sdk.GetServersOnline(); // 再次获取机器人,因为获取机器人信息需要一定的初始化时间,所以第一次获取可能失败

连接机器人

下面这种连接方式是阻塞的,sdk会等待10000ms连接局域网内任意一个机器人。如果局域网内有多个机器人,程序会返回MULTI_SERVER_FOUND错误。当局域网内有且仅有唯一的一个机器人时,sdk自动连接。 如果10000ms没有成功连接机器人,Connect方法会返回超时TIMEOUT错误。

[TestMethod]
public void testConnect()
{
    GalileoSDK sdk = new GalileoSDK();
    var res = sdk.Connect("", true, 10000, null, null);
    if (res == GALILEO_RETURN_CODE.OK)
        Console.WriteLine("Connected to " + sdk.GetCurrentServer().ID);
    else
        Console.WriteLine("Connect to server failed");
}

连接机器人,异步方式

下面的连接方式是异步的,可以通过设置回调函数处理机器人连接状态。这个方法是非阻塞的,Connect方法会立即返回。当连接成功或超时时OnConnect回调会被调用。具体的状态信息可以通过回调函数的第一个参数获取

sdk.Connect("", true, 10000, (status, targetID) =>
{
    Console.WriteLine("Server connected");
    Console.WriteLine(targetID);
    Console.WriteLine("status: " + status);
}, (status, targetID) =>
{
    Console.WriteLine("Server disconnected");
    Console.WriteLine(targetID);
});

通过物联网跨局域网连接机器人

GalileoSDK sdk = new GalileoSDK();
// 通过物联网连接机器人,可以跨局域网连接。
// 参数为目标机器人id,超时时间timeout,目标机器人密码,回调函数
sdk.ConnectIOT("71329A5B0F2D68364BB7B44F3F125531E4C7F5BC3BCE2694DFE39B505FF9C730A614FF2790C1", 10000, "xiaoqiang", null, null);
while (true)
{
    var res = sdk.SendAudio("测试");
    if (res == GALILEO_RETURN_CODE.OK)
    {
        Console.WriteLine("Send audio message succeed");
    }
    else
    {
        Console.WriteLine("Send audio message failed");
    }

    Thread.Sleep(4000);
}

获取机器人当前电压

GalileoSDK sdk = new GalileoSDK();
sdk.Connect("", true, 10000, null, null);
while (true)
{
    GalileoStatus status = sdk.GetCurrentStatus();
    Console.WriteLine("Power: " + status.power);
    Thread.Sleep(1000);
}

控制机器人前进

GalileoSDK sdk = new GalileoSDK();
if (sdk.Connect("71329A5B0F2D68364BB7B44F3F125531E4C7F5BC3BCE2694DFE39B505FF9C730A614FF2790C1",
        true, 10000, null,
        null) != GALILEO_RETURN_CODE.OK)
{
    Console.WriteLine("Connect to server failed");
}
sdk.SetSpeed(0.1f, 0f);

控制机器人移动到特定坐标

GalileoSDK sdk = new GalileoSDK();
if (sdk.Connect("71329A5B0F2D68364BB7B44F3F125531E4C7F5BC3BCE2694DFE39B505FF9C730A614FF2790C1",
        true, 10000, null,
        null) != GALILEO_RETURN_CODE.OK)
{
    Console.WriteLine("Connect to server failed");
}
while (true)
{
    sdk.StartNav();
    GalileoStatus status = sdk.GetCurrentStatus();
    // 等待导航程序启动完成
    while (status.visualStatus != 1 || status.navStatus != 1)
    {
        Console.WriteLine("Wait for navigation initialization");
        Console.WriteLine("status.visualStatus: " + status.visualStatus);
        Console.WriteLine("status.navStatus: " + status.navStatus);
        status = sdk.GetCurrentStatus();
        Thread.Sleep(1000);
    }
    byte goalNum = 0;
    sdk.MoveTo((posX, posY, ref goalNum);
    // 等待移动完成
    status = sdk.GetCurrentStatus();
    while (status.targetStatus != 1)
    {
        Console.WriteLine("Wait for goal start");
        status = sdk.GetCurrentStatus();
        Thread.Sleep(1000);
    }
    Console.WriteLine("Goal started");
    while (status.targetStatus != 0 || status.targetNumID != goalNum)
    {
        Console.WriteLine("Wait for goal complete");
        Console.WriteLine("status.targetStatus: " + status.targetStatus);
        Console.WriteLine("status.targetNumID: " + status.targetNumID);
        Console.WriteLine("goalNum: " + goalNum);
        status = sdk.GetCurrentStatus();
        Thread.Sleep(1000);
    }
    Console.WriteLine("Move to " + (pos0_x + pos1_x) / 2 + " " + (pos0_y + pos1_y) / 2 + " complete");
    Thread.Sleep(10 * 1000);
    Console.WriteLine("All complete");
}