RosBridge Install & Start

sudo apt-get install ros-noetic-rosbridge-server
soruce /opt/ros/noetic/setup.bash
roslaunch rosbridge_server rosbridge_websocket.launch
hostname -I

# ws://192.168.64.3:9090

[ROS] ROS Bridge - RosBridge의 정의와 Protocol 탐구

[ROS] ROS Bridge - RosBridge의 정의와 Protocol 탐구

ROS - rosbridge

Spring Server ↔ ROSBridge ↔ ROS Navigation

자바 스프링 서버에서 ROS Bridge 서버로 연결해서 통신하는 코드이다.

private static WebSocketMessage<String> createWebSocketMessage() {
		JSONObject position = new JSONObject();
		position.put("x", -2.0);
		position.put("y", 0.5);
		position.put("z", 0.0);

		JSONObject orientation = new JSONObject();
		orientation.put("x", 0);
		orientation.put("y", 0);
		orientation.put("z", 0);
		orientation.put("w", 1);

		JSONObject pose = new JSONObject();
		pose.put("position", position);
		pose.put("orientation", orientation);

		JSONObject header = new JSONObject();
		header.put("frame_id", "map");

		JSONObject msg = new JSONObject();
		msg.put("op", "publish");
		msg.put("topic", "/move_base_simple/goal");
		msg.put("msg", new JSONObject().put("header", header).put("pose", pose));
		return new TextMessage(msg.toString());
	}

	@PostMapping("/control")
	public ResponseEntity<String> controlRobot() {

		try {
			WebSocketClient client = new StandardWebSocketClient();
			WebSocketSession session = client.execute(new RobotWebSocketHandler(), "ws://192.168.64.5:9090").get();
			WebSocketMessage<String> webSocketMessage = createWebSocketMessage();
			session.sendMessage(webSocketMessage);
			return new ResponseEntity<>("Message sent to ROS system", HttpStatus.OK);
		} catch (Exception e) {
			log.error(e.getMessage());
			return new ResponseEntity<>("Failed to send message", HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

%E1%84%92%E1%85%AA%E1%84%86%E1%85%A7%E1%86%AB%20%E1%84%80%E1%85%B5%E1%84%85%E1%85%A9%E1%86%A8%202024-06-03%20%E1%84%8B%E1%85%A9%E1%84%8C%E1%85%A5%E1%86%AB%201-2.mp4

트러블 슈팅

패키지 안 찾아질때

source /opt/ros/noetic/setup.bash