云迹机器人底盘调用

云迹机器人底盘调用还是比较友好的,就是纯socket收发指令就能实现,今天实现一个底盘移动到指定点位功能。底盘的默认IP是192.168.10.10通讯端口是31001,测试机与底盘接入统一网络后直接发指令即可。本文给出两种语言调用源码,选择一种使用即可。

python调用源码:

import socket
import json

yunji_ip = "192.168.10.10"#设定地盘ip地址,需要根据机器人实际ip地址修改

#底盘-移动到指定为止
def YunjiGo(target):
    print(f"---------------------YunjiGo:{target}---------------------")

    port = 31001
    command = "/api/move?marker=" + target + "&distance_tolerance=0&theta_tolerance=0"

    client = socket.socket()
    client.connect((yunji_ip, port))
    print(f"command:{command}")
    client.send(command.encode())

    while True:
        try:
            data = client.recv(1024)
            if not data:
                print("Socket closed by peer.")
                return None

            str_data = data.decode('utf-8')
            split_data = [s for s in str_data.split('\n') if s]

            for s in split_data:
                try:
                    json_data = json.loads(s)
                    print(json_data.get("description"))

                    # 检查条件:任务完成且目标匹配
                    if (
                        json_data.get("description") == "The move task is finished."
                        and "data" in json_data
                        and "target" in json_data["data"]
                        and json_data["data"]["target"] == target
                    ):
                        print("Task finished successfully!")
                        client.close()  # 关闭 socket
                        return True     # 返回成功

                except json.JSONDecodeError:
                    print("Invalid JSON data, continue waiting...")
                    continue

        except socket.timeout:
            print("Timeout reached, no matching data received.")
            return None
        except Exception as e:
            print(f"Error occurred: {e}")
            client.close()  # 确保异常时关闭 socket
            return None
YunjiGo("huahua")#移动到点位huahua

JAVA调用源码:(需要下载org.json-20161124.jar)

package examples;
import java.io.*;
import java.net.*;
import org.json.JSONObject;
import org.json.JSONException;

/*
依赖安装:
sudo apt update
sudo apt install language-pack-zh-hans  # 简体中文
sudo apt install fonts-noto-cjk        # 中文字体
sudo apt install default-jdk

*/

//make:
/*
cd /home/java
javac -encoding UTF-8 -cp .:\
org.json-20161124.jar \
examples/RobotControl.java
*/
//run : 
/*
cd /home/java
java -Dfile.encoding=UTF-8 -cp .:\
org.json-20161124.jar \
examples.RobotControl
*/

public class RobotControl {
    String yunji_ip  = "192.168.10.10";  // 底盘ip地址
    int yunji_port = 31001;//底盘通讯端口

    private static RobotControl mInstance =null;

    public static synchronized RobotControl getInstance() 
    {
        if (mInstance == null) {
            mInstance = new RobotControl();
        }
        return mInstance;
    }


    //底盘移动
    public void YunjiGo(String target) throws Exception
    {
        String command = "/api/move?marker=" + target + "&distance_tolerance=0.01&theta_tolerance=0.01";

        try {
            Socket client = new Socket(yunji_ip, yunji_port);
            //System.out.println("command:" + command);
            OutputStream out = client.getOutputStream();
            out.write(command.getBytes());
            out.flush();

            InputStream in = client.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String strData;
            while ((strData = reader.readLine()) != null) {
                //System.out.println("strData:" + strData);
                try {
                    JSONObject jsonData = new JSONObject(strData);
                    System.out.println(jsonData.optString("description"));

                    // 检查条件:任务完成且目标匹配
                    if (jsonData.optString("description").equals("The move task is finished.")
                            && jsonData.has("data")
                            && jsonData.getJSONObject("data").has("target")
                            && jsonData.getJSONObject("data").getString("target").equals(target)) {
                        System.out.println("Task finished successfully!");
                        client.close(); // 关闭 socket
                        return;    // 返回成功
                    }
                } catch (JSONException e) {
                    System.out.println("Invalid JSON data, continue waiting...");
                    continue;
                }
            }

            throw new Exception("Socket closed by peer.");
        } catch (SocketTimeoutException e) {
            throw new Exception("Timeout reached, no matching data received.");
        } catch (Exception e) {
            throw new Exception("Error occurred: " + e.getMessage());
        }
    }

    public static void main(String[] args) {  
        RobotControl robotControl = RobotControl.getInstance();

        try
        {
            System.out.println("------------YunjiGo------------");
            robotControl.YunjiGo("huahua");
        } catch (Exception e) {
            System.out.println("YunjiGo Error occurred: " + e.getMessage());
        }
        System.out.println();
    }
}

运行输出:


------------YunjiGo------------

The move task is started.
Start to leave charging pile..
Charge status off.
Succeed to leave charging pile.
traffic turn right.
traffic turn right.
The move task is finished.
Task finished successfully!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值