Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* jrborelli - ROS2
***********************************************************************************************/

package br.unicamp.cst.bindings.ros2java;

Expand Down Expand Up @@ -145,121 +155,3 @@ protected void processServiceResponse(AddTwoIntsResponseMessage response) {
}
*/
}




/* Fisrt verstion...

package br.unicamp.cst.bindings.ros2java;

import br.unicamp.cst.core.entities.Codelet;
import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.exceptions.CodeletActivationBoundsException;
import id.jrosmessages.Message;
//import pinorobotics.jros2client.JRos2Client;
import id.jros2client.JRos2Client;
import id.jros2client.JRos2ClientFactory;
import pinorobotics.jros2services.JRos2ServiceClient;
//import pinorobotics.jrosservices.JRosServiceClient;
import pinorobotics.jros2services.JRos2ServicesFactory;

import java.util.concurrent.Semaphore;


public abstract class RosServiceClientCodelet<S extends Message, T extends Message> extends Codelet {

protected String serviceName;
protected Class<S> requestType = (Class<S>) AddTwoIntsRequestMessage.class;;
protected Class<T> responseType = (Class<T>) AddTwoIntsResponseMessage.class;

protected Memory inputMemory;

protected S requestMessage;
protected JRos2ServiceClient<S, T> serviceClient;
protected JRos2Client ros2Client;

private final Semaphore callInProgressSemaphore = new Semaphore(1);

public RosServiceClientCodelet(String serviceName, Class<S> requestType, Class<T> responseType) {
this.serviceName = serviceName;
this.requestType = requestType;
this.responseType = responseType;
this.setName("Ros2Client:" + serviceName);
}

@Override
public void start() {
try {

ros2Client = new JRos2ClientFactory().createClient();

// Assuming you have a ServiceDefinition class for your service, e.g. AddTwoIntsServiceDefinition
serviceClient = new JRos2ServicesFactory().createClient(ros2Client, new AddTwoIntsServiceDefinition(), serviceName);

} catch (Exception e) {
throw new RuntimeException("Failed to initialize ROS 2 client for service: " + serviceName, e);
}

super.start();
}

@Override
public void stop() {
try {
if (serviceClient != null) serviceClient.close();
if (ros2Client != null) ros2Client.close();
} catch (Exception e) {
e.printStackTrace();
}

super.stop();
}

@Override
public void accessMemoryObjects() {
if (inputMemory == null) {
inputMemory = this.getInput(serviceName, 0);
}
}

@Override
public void calculateActivation() {
try {
setActivation(1.0); // Always run if needed
} catch (CodeletActivationBoundsException e) {
e.printStackTrace();
}
}

@Override
public void proc() {
if (inputMemory == null || inputMemory.getI() == null) return;

try {
requestMessage = createNewRequest();
if (formatServiceRequest(inputMemory, requestMessage)) {
callInProgressSemaphore.acquire();

T response = serviceClient.sendRequestAsync(requestMessage).get();

if (response != null) {
processServiceResponse(response);
}
}
} catch (Exception e) {
System.err.println("Error in ROS 2 service call: " + e.getMessage());
} finally {
callInProgressSemaphore.release();
}
}

protected abstract S createNewRequest();


protected abstract boolean formatServiceRequest(Memory memory, S request);

protected abstract void processServiceResponse(T response);
}

*/
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* jrborelli - ROS2
***********************************************************************************************/
package br.unicamp.cst.bindings.ros2java;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* jrborelli - ROS2
***********************************************************************************************/


Expand Down Expand Up @@ -109,240 +110,3 @@ public void setEnabled(boolean enabled) {
}


/* // Second Version, almost there, see reference.
package br.unicamp.cst.bindings.ros2java;

import br.unicamp.cst.core.entities.Codelet;
import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.exceptions.CodeletActivationBoundsException;
import id.jrosmessages.Message;
import id.jros2client.JRos2Client;
import id.jros2client.JRos2ClientFactory;
import id.jros2client.qos.PublisherQos;
import id.jros2client.qos.SubscriberQos;
//import id.jros2client.publisher.JRos2Publisher;
import id.jrosclient.TopicSubmissionPublisher;

public abstract class RosTopicOneShotPublisherCodelet<T extends Message> extends Codelet {

protected String topic;
protected Class<T> messageType;
protected Memory motorMemory;

protected JRos2Client ros2Client;
//protected JRos2Publisher<T> publisher;
protected TopicSubmissionPublisher publisher;
protected T message;

private volatile boolean enabled = false;

public RosTopicOneShotPublisherCodelet(String topic, Class<T> messageType) {
this.topic = topic;
this.messageType = messageType;
setName("Ros2Publisher:" + topic);
}

@Override
public synchronized void start() {

// Exemplo:
//https://github.com/lambdaprime/jros2client/blob/main/jros2client.examples/generic/src/PublisherApp.java
// var configBuilder = new JRos2ClientConfiguration.Builder();
// use configBuilder to override default parameters (network interface, RTPS settings etc)
// var client = new JRos2ClientFactory().createClient(configBuilder.build());
// String topicName = "/helloRos";
// var publisher = new TopicSubmissionPublisher<>(StringMessage.class, topicName);
// register a new publisher for a new topic with ROS
// client.publish(publisher);
// while (true) {
// publisher.submit(new StringMessage().withData("Hello ROS"));
// System.out.println("Published");
// Thread.sleep(1000);
//}


ros2Client = new JRos2ClientFactory().createClient();
publisher = new TopicSubmissionPublisher<>(messageType, topic);

//publisher = ros2Client.createPublisher(topic, messageType);
//message = publisher.newMessage();
super.start();
}

@Override
public synchronized void stop() {
try {
if (publisher != null) publisher.close();
if (ros2Client != null) ros2Client.close();
} catch (Exception e) {
e.printStackTrace();
}
super.stop();
}

@Override
public void accessMemoryObjects() {
if (motorMemory == null) {
motorMemory = getInput(topic, 0);
}
}

@Override
public void calculateActivation() {
try {
setActivation(enabled ? 1.0 : 0.0);
} catch (CodeletActivationBoundsException e) {
e.printStackTrace();
}
}

@Override
public void proc() {
if (!enabled) return;
if (motorMemory == null) return;

fillMessageToBePublished(motorMemory, message);
//publisher.publish(message);
enabled = false; // publish only once per enable
}


public abstract void fillMessageToBePublished(Memory motorMemory, T message);

public boolean getEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

}

*/

/* //First Version:
package br.unicamp.cst.bindings.ros2java;

import br.unicamp.cst.core.entities.Codelet;
import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.exceptions.CodeletActivationBoundsException;

import id.jrosmessages.Message;
import id.jrosmessages.MessageDescriptor;
import id.jros2client.JRos2Client;
import id.jros2client.JRos2ClientFactory;
import id.jros2client.publisher.JRos2Publisher; // your publisher interface/class

import java.util.concurrent.atomic.AtomicBoolean;


//ROS 2 Topic One-Shot Publisher Codelet that creates and holds a publisher instance.

//@param <T> ROS 2 Message type extending Message

public abstract class RosTopicOneShotPublisherCodelet<T extends Message> extends Codelet {

protected String topic;
protected Class<T> messageTypeClass; // the class of message type (for createPublisher)
protected MessageDescriptor<T> messageDescriptor; // optional, if your client uses descriptors

protected Memory motorMemory;
protected T message;

protected JRos2Client ros2Client;
protected JRos2Publisher<T> publisher;

private final AtomicBoolean enabled = new AtomicBoolean(false);

public RosTopicOneShotPublisherCodelet(String topic, Class<T> messageTypeClass, MessageDescriptor<T> messageDescriptor) {
super();
this.topic = topic;
this.messageTypeClass = messageTypeClass;
this.messageDescriptor = messageDescriptor;
setName("Ros2Publisher:" + topic);
}

@Override
public synchronized void start() {
try {
ros2Client = new JRos2ClientFactory().createClient();
if (messageDescriptor != null) {
publisher = ros2Client.createPublisher(topic, messageDescriptor);
} else {
publisher = ros2Client.createPublisher(topic, messageTypeClass);
}
} catch (Exception e) {
throw new RuntimeException("Failed to initialize ROS 2 client or create publisher", e);
}
super.start();
}

@Override
public synchronized void stop() {
try {
if (ros2Client != null) ros2Client.close();
} catch (Exception e) {
e.printStackTrace();
}
super.stop();
}

@Override
public void accessMemoryObjects() {
if (motorMemory == null) {
motorMemory = this.getInput(topic, 0);
}
}

@Override
public void calculateActivation() {
try {
setActivation(0.0d);
} catch (CodeletActivationBoundsException e) {
e.printStackTrace();
}
}

@Override
public void proc() {
if (!enabled.get()) return;
if (motorMemory == null || motorMemory.getI() == null) return;

if (message == null) {
message = createNewMessage();
}

fillMessageToBePublished(motorMemory, message);

try {
if (messageDescriptor != null) {
ros2Client.publish(topic, messageDescriptor, publisher);
} else {
ros2Client.publish(topic, messageTypeClass, publisher);
}
} catch (Exception e) {
System.err.println("Failed to publish message on topic " + topic + ": " + e.getMessage());
}

enabled.set(false); // one-shot disable after publish
}


protected abstract T createNewMessage();


protected abstract void fillMessageToBePublished(Memory motorMemory, T message);

public boolean isEnabled() {
return enabled.get();
}

public void setEnabled(boolean enabled) {
this.enabled.set(enabled);
}
}

*/


Loading
Loading