Commit 1b08257d authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Merge branch 'improved_array_handling_feedback' into 'master'

Improved array handling: feedback fixed

See merge request !16
parents 1940782c 66ff2a49
Pipeline #191042 passed with stages
in 4 minutes and 15 seconds
......@@ -5,3 +5,18 @@
## Warning
This generator is part of an composite generator and does not create an executable. Look at [EMAM2Middleware](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/generators/EMAM2Middleware) if you want to generate one.
## How does the new array handling work?
To give an example of the improved array handling:
`tag test.array with RosConnection = { topic = (/test/input, nav_msgs/Path), msgField=data[5:9].position.orientation.x }`
There are two new features that can be used regarding arrays in EMAM:
1. A range can be defined, from which the **input** values will be taken
--> in this example the array named "array" from the component "test" will get the input values from the topic /test/input on indices: 5 to 9.
All other values in "array" (if it contains more than 5 values) will be filled with zeros.
2. Here "/test/input" is an array of structs (e.g poses). It is now possible to use the message field to get values out of structs (like in many other programming languages)
by using "dots" like in: `struct.attributes`
\ No newline at end of file
......@@ -21,12 +21,12 @@ public class SetStructPortInstruction{
public static String getStructInstruction(EMAPortSymbol port, RosMsg rosMsg, String fieldPrefix) {
String inst;
if (rosMsg.getName().startsWith("std_msgs/")) {
if (rosMsg.getName().startsWith("std_msgs/")) {
inst = NameHelper.getAllFieldNames(rosMsg).stream()
.map(field -> "component->" + NameHelper.getPortNameTargetLanguage(port) + " = msg->" + fieldPrefix + field + ";")
.sorted()
.collect(Collectors.joining("\n"));
} else {
} else { //port is a struct -> further handling necessary
StructSymbol structSymbol = (StructSymbol) port.getTypeReference().getReferencedSymbol();
List<String> structFieldNames = NameHelper.getAllFieldNames(structSymbol);
......@@ -79,29 +79,34 @@ public class SetStructPortInstruction{
inst += "for(int " + curInd + " = 0; " + curInd + " < " + dimSizes.get(i) + "; " + curInd + "++){\n";
}
//part of the improved array handling
String upperBound;
String lowerBound;
if (!fieldPrefix.isEmpty() && fieldPrefix.contains(":")){
if (!fieldPrefix.isEmpty() && fieldPrefix.contains(":")){ //syntax correct?
String split[] = fieldPrefix.split(":", 2);
boolean boundExists = !split[0].replaceAll("[^0-9]", "").isEmpty();
boolean boundExists = !split[0].replaceAll("[^0-9]", "").isEmpty() && !split[0].replaceAll("[^0-9]", "").equals("0"); //0 is not a valid bound in EMAM
lowerBound = boundExists ? split[0].replaceAll("[^0-9]", "") : "0";
boundExists = !split[1].replaceAll("[^0-9]", "").isEmpty();
upperBound = boundExists ? split[1].replaceAll("[^0-9]", "") : dimSizes.get(dimSizes.size()-1)+"-1";
} else {
//extract bounds from msgField
lowerBound = boundExists ? split[0].replaceAll("[^0-9]", "")+"-1" : "0";
boundExists = !split[1].replaceAll("[^0-9]", "").isEmpty() && !split[1].replaceAll("[^0-9]", "").equals("0"); //0 is not a valid bound in EMAM;
upperBound = boundExists ? split[1].replaceAll("[^0-9]", "")+"-1" : dimSizes.get(dimSizes.size()-1)+"-1";
} else { //no bounds given
lowerBound = "0";
upperBound = dimSizes.get(dimSizes.size()-1)+"-1";
upperBound = dimSizes.get(dimSizes.size()-1)+"-1";
}
if(!upperBound.equals("")){
// if (Integer.parseInt(lowerBound) > Integer.parseInt(upperBound)) { If
// Log.error(" ArrayBoundsHandler: lowerBound > upperBound!");
// }
inst += "if(" + lowerBound + " <= counter && counter <= " + upperBound + "){\n";
}
//three if-cases will be generated: lB <= counter <= uB, counter < lB, counter > uB
String tmp;
if (fieldPrefix.isEmpty()){
tmp = "(component->" + NameHelper.getPortNameTargetLanguage(port) + ")(" + indexString + ") = msg->" + fieldPrefix + "data[counter]";
} else {
......@@ -126,13 +131,13 @@ public class SetStructPortInstruction{
inst += "else{\n";
inst += "(component->" + NameHelper.getPortNameTargetLanguage(port) + ")(" + indexString + ") = 0" + ";\n";
inst += "}\n";
}
}
inst += "counter++;\n";
inst += "counter++;\n";
for (int i = 0; i < dimSizes.size(); i++) {
inst += "}\n";
}
return inst;
for (int i = 0; i < dimSizes.size(); i++) {
inst += "}\n";
}
return inst;
}
}
......@@ -106,11 +106,11 @@ class RosAdapter_tests_structs_arrayHandlingComp: public IAdapter_tests_structs_
int counter = 0;
for(int i0 = 0; i0 < 1; i0++){
for(int i1 = 0; i1 < 7; i1++){
if(2 <= counter && counter <= 6){
(component->in4)(i0, i1-2) = msg->poses[counter].pose.orientation.x;
if(2-1 <= counter && counter <= 6-1){
(component->in4)(i0, i1-2-1) = msg->poses[counter].pose.orientation.x;
}
else if(2 > counter){
(component->in4)(i0, i1+6-2+1) = 0;
else if(2-1 > counter){
(component->in4)(i0, i1+6-1-2-1+1) = 0;
}
else{
(component->in4)(i0, i1) = 0;
......@@ -154,4 +154,4 @@ class RosAdapter_tests_structs_arrayHandlingComp: public IAdapter_tests_structs_
publish_name6Publisher();
}
};
};
\ No newline at end of file
......@@ -6,7 +6,9 @@
#include <std_msgs/Bool.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Int32.h>
class RosAdapter_tests_msg_basicTypesComp: public IAdapter_tests_msg_basicTypesComp{
tests_msg_basicTypesComp* component;
ros::Subscriber topic7Subscriber;
ros::Subscriber topic8Subscriber;
......@@ -26,48 +28,54 @@ class RosAdapter_tests_msg_basicTypesComp: public IAdapter_tests_msg_basicTypesC
int i = 0;
ros::init(i, &tmp, "RosAdapter_tests_msg_basicTypesComp_node");
ros::NodeHandle node_handle = ros::NodeHandle();
topic7Subscriber = node_handle.subscribe("topic7", 5, &RosAdapter_tests_msg_basicTypesComp::topic7Callback, this, ros::TransportHints().tcpNoDelay());
topic8Subscriber = node_handle.subscribe("topic8", 5, &RosAdapter_tests_msg_basicTypesComp::topic8Callback, this, ros::TransportHints().tcpNoDelay());
topic9Subscriber = node_handle.subscribe("topic9", 5, &RosAdapter_tests_msg_basicTypesComp::topic9Callback, this, ros::TransportHints().tcpNoDelay());
topic7Publisher = node_handle.advertise<std_msgs::Float64>("topic7",5);
topic8Publisher = node_handle.advertise<std_msgs::Int32>("topic8",5);
topic9Publisher = node_handle.advertise<std_msgs::Bool>("topic9",5);
ros::spin();
}
void topic7Callback(const std_msgs::Float64::ConstPtr& msg){
component->inQ = msg->data;
}
void topic8Callback(const std_msgs::Int32::ConstPtr& msg){
component->inZ = msg->data;
}
void topic9Callback(const std_msgs::Bool::ConstPtr& msg){
component->inB = msg->data;
}
void publishtopic7Publisher(){
void topic7Callback(const std_msgs::Float64::ConstPtr& msg){
component->inQ = msg->data;
}
void topic8Callback(const std_msgs::Int32::ConstPtr& msg){
component->inZ = msg->data;
}
void topic9Callback(const std_msgs::Bool::ConstPtr& msg){
component->inB = msg->data;
}
void publishtopic7Publisher(){
std_msgs::Float64 tmpMsg;
tmpMsg.data = component->outQ;
topic7Publisher.publish(tmpMsg);
}
void publishtopic8Publisher(){
std_msgs::Int32 tmpMsg;
tmpMsg.data = component->outZ;
topic8Publisher.publish(tmpMsg);
}
void publishtopic9Publisher(){
std_msgs::Bool tmpMsg;
tmpMsg.data = component->outB;
topic9Publisher.publish(tmpMsg);
}
void tick(){
publishtopic7Publisher();
publishtopic8Publisher();
publishtopic9Publisher();
}
};
void tick(){
publishtopic7Publisher();
publishtopic8Publisher();
publishtopic9Publisher();
}
};
\ No newline at end of file
......@@ -109,4 +109,4 @@ class RosAdapter_tests_structs_matrixTypesComp: public IAdapter_tests_structs_ma
publish_name4Publisher();
}
};
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment