发布时间:2025-12-09 11:46:55 浏览次数:1
import org.ros.node.topic.Publisher; //导入依赖的package包/类@Overridepublic synchronized void onStart(final ConnectedNode connectedNode) { m_node = connectedNode; m_cmdPublisher = connectedNode.newPublisher("command", CommandStamped._TYPE); m_cmdPublisher.addListener(new DefaultPublisherListener<CommandStamped>() { @Override public void onNewSubscriber(Publisher<CommandStamped> publisher, SubscriberIdentifier subscriberIdentifier) { while (!m_queue.isEmpty()) { CommandStamped cmd = m_queue.poll(); publisher.publish(cmd); } synchronized(RobotNodeMain.this) { m_ready = true; } } }); Subscriber<AckStamped> subscriber = connectedNode.newSubscriber("mgt/ack", AckStamped._TYPE); subscriber.addMessageListener(this); Subscriber<EkfState> ekfSub = connectedNode.newSubscriber("gnc/ekf", EkfState._TYPE); ekfSub.addMessageListener(new MessageListener<EkfState>() { @Override public void onNewMessage(final EkfState ekfState) { synchronized(m_kinematics_lock) { m_kinematics = new DefaultKinematics(ekfState); } } });} import org.ros.node.topic.Publisher; //导入依赖的package包/类@Overridepublic void onStart(final ConnectedNode connectedNode) { // This CancellableLoop will be canceled automatically when the node shuts // down. final Publisher<Message> publisher = connectedNode.newPublisher(nodeName, publishType); connectedNode.executeCancellableLoop(new CancellableLoop() { @Override protected void loop() throws InterruptedException { semaphore.acquire(); publishMe.ifPresent(publishAction -> { final Message message = publisher.newMessage(); publishAction.convert(message, connectedNode.getTopicMessageFactory()); publisher.publish(message); }); } });} import org.ros.node.topic.Publisher; //导入依赖的package包/类@Overridepublic void onStart(ConnectedNode connectedNode) { final Publisher<std_msgs.String> publisher = connectedNode.newPublisher(GraphName.of("time"), std_msgs.String._TYPE); final CancellableLoop loop = new CancellableLoop() { @Override protected void loop() throws InterruptedException { // retrieve current system time String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); Log.i(TAG, "publishing the current time: " + time); // create and publish a simple string message std_msgs.String str = publisher.newMessage(); str.setData("The current time is: " + time); publisher.publish(str); // go to sleep for one second Thread.sleep(1000); } }; connectedNode.executeCancellableLoop(loop);} import org.ros.node.topic.Publisher; //导入依赖的package包/类@Overridepublic void onStart(ConnectedNode connectedNode) { final Publisher<DiagnosticArray> batteryPublisher = connectedNode.newPublisher(TOPIC_NAME, DiagnosticArray._TYPE); final DiagnosticArray diagnosticArray = batteryPublisher.newMessage(); final DiagnosticStatus deviceBattery = mMessageFactory.newFromType(DiagnosticStatus._TYPE); deviceBattery.setName("/Power System/Laptop Battery"); KeyValue deviceKeyValueCapacity = mMessageFactory.newFromType(KeyValue._TYPE); deviceKeyValueCapacity.setKey("Capacity (Ah)"); deviceKeyValueCapacity.setValue("100.0"); final KeyValue deviceKeyValueCharge = mMessageFactory.newFromType(KeyValue._TYPE); deviceKeyValueCharge.setKey("Charge (Ah)"); deviceBattery.setValues(Arrays.asList(deviceKeyValueCapacity, deviceKeyValueCharge)); diagnosticArray.setStatus(Arrays.asList(deviceBattery)); connectedNode.executeCancellableLoop(new CancellableLoop() { @Override protected void loop() throws InterruptedException { diagnosticArray.getHeader().setStamp(Time.fromMillis(System.currentTimeMillis())); double deviceBatteryPercentage = mBatteryLevel.get(); if (deviceBatteryPercentage > 0) { deviceKeyValueCharge.setValue(Double.toString(deviceBatteryPercentage)); } batteryPublisher.publish(diagnosticArray); Thread.sleep(1000); } });} import org.ros.node.topic.Publisher; //导入依赖的package包/类@Overridepublic void onStart(ConnectedNode connectedNode) { final Publisher<DiagnosticArray> batteryPublisher = connectedNode.newPublisher(TOPIC_NAME, DiagnosticArray._TYPE); final DiagnosticArray diagnosticArray = batteryPublisher.newMessage(); final DiagnosticStatus robotBattery = mMessageFactory.newFromType(DiagnosticStatus._TYPE); robotBattery.setName("/Power System/Battery"); final KeyValue robotKeyValueCapacity = mMessageFactory.newFromType(KeyValue._TYPE); robotKeyValueCapacity.setKey("Capacity (Ah)"); robotKeyValueCapacity.setValue("100.0"); final KeyValue robotKeyValueCharge = mMessageFactory.newFromType(KeyValue._TYPE); robotKeyValueCharge.setKey("Charge (Ah)"); robotBattery.setValues(Arrays.asList(robotKeyValueCapacity, robotKeyValueCharge)); diagnosticArray.setStatus(Arrays.asList(robotBattery)); connectedNode.executeCancellableLoop(new CancellableLoop() { @Override protected void loop() throws InterruptedException { diagnosticArray.getHeader().setStamp(Time.fromMillis(System.currentTimeMillis())); double baseBatteryVoltage = UnsignedBytes.toInt(mBaseDevice.getBaseStatus().getBattery()) * 0.1; double baseBatteryPercentage = (baseBatteryVoltage - BASE_MIN_VOLTAGE) / (BASE_MAX_VOLTAGE - BASE_MIN_VOLTAGE) * 100.0; robotKeyValueCharge.setValue(Double.toString(baseBatteryPercentage)); batteryPublisher.publish(diagnosticArray); Thread.sleep(1000); } });}