云跡機器人底盤調用還是比較友好的,就是純socket收發指令就能實現,今天實現一個底盤移動到指定點位功能。底盤的默認IP是192.168.10.10通訊端口是31001,測試機與底盤接入統一網絡后直接發指令即可。本文給出兩種語言調用源碼,選擇一種使用即可。
python調用源碼:
import socket
import jsonyunji_ip = "192.168.10.10"#設定地盤ip地址,需要根據機器人實際ip地址修改#底盤-移動到指定為止
def YunjiGo(target):print(f"---------------------YunjiGo:{target}---------------------")port = 31001command = "/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 Nonestr_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_dataand "target" in json_data["data"]and json_data["data"]["target"] == target):print("Task finished successfully!")client.close() # 關閉 socketreturn True # 返回成功except json.JSONDecodeError:print("Invalid JSON data, continue waiting...")continueexcept socket.timeout:print("Timeout reached, no matching data received.")return Noneexcept Exception as e:print(f"Error occurred: {e}")client.close() # 確保異常時關閉 socketreturn 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(); // 關閉 socketreturn; // 返回成功}} 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!