Aufgrund einer Wartung wird GitLab am 17.08. zwischen 8:30 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 17.08. between 8:30 and 9:00 am.

Commit 4be0024d authored by Svetlana's avatar Svetlana

generation for emadl component

parent 4fdfd26f
%!/bin/bash
source devel/setup.bash
#source devel/setup.bash
catkin_make
<component name="libraryTable">
<library name="Maven: de.monticore.lang.monticar:embedded-montiarc-behaviour:0.0.10-SNAPSHOT">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/de/monticore/lang/monticar/embedded-montiarc-behaviour/0.0.10-SNAPSHOT/embedded-montiarc-behaviour-0.0.10-20180122.004624-1.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/de/monticore/lang/monticar/embedded-montiarc-behaviour/0.0.10-SNAPSHOT/embedded-montiarc-behaviour-0.0.10-20180122.004624-1-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/de/monticore/lang/monticar/embedded-montiarc-behaviour/0.0.10-SNAPSHOT/embedded-montiarc-behaviour-0.0.10-20180122.004624-1-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
......@@ -56,7 +56,6 @@
<orderEntry type="library" name="Maven: org.jscience:jscience:4.3.1" level="project" />
<orderEntry type="library" name="Maven: org.javolution:javolution:5.2.3" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:embedded-montiarc-math-generator:0.0.6-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:embedded-montiarc-behaviour:0.0.10-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:embedded-montiarc-math:0.0.10-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang:math:0.0.10-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
......@@ -85,5 +84,6 @@
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:embedded-montiarc-deeplearning:0.2.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:cnn-arch:0.2.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:cnn-train:0.2.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.monticore.lang.monticar:embedded-montiarc-behaviour:0.0.11-SNAPSHOT" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -46,6 +46,11 @@
<artifactId>embedded-montiarc-deeplearning</artifactId>
<version>0.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-behaviour</artifactId>
<version>0.0.11-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package de.monticore.lang.monticar.torcs_dl;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.emadl.generator.EMADLGenerator;
import de.monticore.lang.monticar.generator.middleware.helpers.NameHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.TemplateHelper;
import de.monticore.lang.monticar.generator.middleware.impls.GeneratorImpl;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class EMADLGeneratorImpl implements GeneratorImpl {
private String generationTargetPath;
private EMADLGenerator emadlGenerator;
public EMADLGeneratorImpl(String modelPath){
emadlGenerator = new EMADLGenerator();
emadlGenerator.setModelsPath(modelPath);
}
@Override
public List<File> generate(ExpandedComponentInstanceSymbol componentInstanceSymbol, TaggingResolver taggingResolver) throws IOException {
List<File> files = new ArrayList<>();
emadlGenerator.setGenerationTargetPath(generationTargetPath);
List<FileContent> fileContents = emadlGenerator.generateStrings(taggingResolver, componentInstanceSymbol, taggingResolver);
for (FileContent fileContent : fileContents) {
files.add(emadlGenerator.getEmamGen().generateFile(fileContent));
}
files.add(emadlGenerator.getEmamGen().generateFile((generateCMake(componentInstanceSymbol))));
return files;
}
@Override
public void setGenerationTargetPath(String path) {
this.generationTargetPath = path;
}
private FileContent generateCMake(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
FileContent cmake = new FileContent();
cmake.setFileName("CMakeLists.txt");
String name = NameHelper.getNameTargetLanguage(componentInstanceSymbol.getFullName());
cmake.setFileContent(TemplateHelper.getCmakeCppTemplate().replace("${compName}", name));
return cmake;
}
}
package de.monticore.lang.monticar.torcs_dl;
import de.monticore.ModelingLanguageFamily;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.embeddedmontiarc.LogConfig;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConstantPortSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.EmbeddedMontiArcMathLanguage;
import de.monticore.lang.embeddedmontiarc.tagging.RosToEmamTagSchema;
import de.monticore.lang.monticar.emadl.generator.Generator;
import de.monticore.lang.monticar.Utils;
import de.monticore.lang.monticar.emadl._symboltable.EMADLLanguage;
import de.monticore.lang.monticar.enumlang._symboltable.EnumLangLanguage;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.middleware.MiddlewareGenerator;
import de.monticore.lang.monticar.generator.middleware.impls.CPPGenImpl;
import de.monticore.lang.monticar.generator.middleware.impls.RosCppGenImpl;
import de.monticore.lang.monticar.generator.optimization.ThreadingOptimizer;
import de.monticore.lang.monticar.generator.order.nfp.TagBreakpointsTagSchema.TagBreakpointsTagSchema;
import de.monticore.lang.monticar.generator.order.nfp.TagDelayTagSchema.TagDelayTagSchema;
import de.monticore.lang.monticar.generator.order.nfp.TagExecutionOrderTagSchema.TagExecutionOrderTagSchema;
......@@ -14,58 +24,95 @@ import de.monticore.lang.monticar.generator.order.nfp.TagMinMaxTagSchema.TagMinM
import de.monticore.lang.monticar.generator.order.nfp.TagTableTagSchema.TagTableTagSchema;
import de.monticore.lang.monticar.generator.order.nfp.TagThresholdTagSchema.TagThresholdTagSchema;
import de.monticore.lang.monticar.generator.roscpp.helper.TagHelper;
import de.monticore.lang.monticar.streamunits._symboltable.StreamUnitsLanguage;
import de.monticore.lang.monticar.struct._symboltable.StructLanguage;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.Splitters;
import de.se_rwth.commons.logging.Log;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import static de.monticore.lang.monticar.generator.order.simulator.AbstractSymtab.createSymTab;
import de.monticore.lang.monticar.generator.order.simulator.AbstractSymtab;
public class Experiments {
public static final String TARGET_PATH_GENERATED = "./target/generated-sources-torcs-dl/";
public static final String MODELS_PATH = "src/main/models/";
public static final String RESOURCES_PATH = "src/main/resources/";
public static void main(String[] args) {
System.out.println("Starting code generation...");
TaggingResolver taggingResolver = createSymTabAndTaggingResolver(MODELS_PATH);
RosToEmamTagSchema.registerTagTypes(taggingResolver);
generateForEmamComponent("Imagepreprocessing");
generateForEmamComponent("Safetycontroller");
generateForEmamComponent("Drivercontroller");
generateForEmadlComponent("Featureextraction");
LinkedList<String> inputs = new LinkedList<String>();
inputs.add("Imagepreprocessing");
inputs.add("Featureextraction");
inputs.add("Safetycontroller");
inputs.add("Drivercontroller");
try {
Files.copy(Paths.get(RESOURCES_PATH+"scripts/build_all.sh"), Paths.get(TARGET_PATH_GENERATED+"build_all.sh"), StandardCopyOption.REPLACE_EXISTING);
Files.copy(Paths.get(RESOURCES_PATH+"scripts/run_all.sh"), Paths.get(TARGET_PATH_GENERATED+"run_all.sh"), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.err.println("Failed to copy build and start scripts.");
e.printStackTrace();
}
}
private static void generateForEmamComponent(String componentName) {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver( MODELS_PATH);
RosToEmamTagSchema.registerTagTypes(taggingResolver);
try {
for (String inpu : inputs) {
System.out.println("Generating for " + inpu);
System.out.println("Generating for " + componentName);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("dp."+componentName.toLowerCase(), ExpandedComponentInstanceSymbol.KIND).orElse(null);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("dp."+inpu.toLowerCase(), ExpandedComponentInstanceSymbol.KIND).orElse(null);
MiddlewareGenerator middlewareGenerator = new MiddlewareGenerator();
middlewareGenerator.add(new CPPGenImpl(), "cpp");
middlewareGenerator.add(new RosCppGenImpl(), "roscpp");
middlewareGenerator.setGenerationTargetPath(TARGET_PATH_GENERATED +componentName);
middlewareGenerator.generate(componentInstanceSymbol, taggingResolver);
} catch (IOException e) {
System.err.println("Failed to generate sources. Sorry.");
e.printStackTrace();
}
}
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
private static void generateForEmadlComponent(String componentName) {
TaggingResolver taggingResolver = de.monticore.lang.monticar.emadl.generator.AbstractSymtab.createSymTabAndTaggingResolver(MODELS_PATH);
RosToEmamTagSchema.registerTagTypes(taggingResolver);
MiddlewareGenerator middlewareGenerator = new MiddlewareGenerator();
try {
System.out.println("Generating for " + componentName);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("dp."+componentName.toLowerCase(), ExpandedComponentInstanceSymbol.KIND).orElse(null);
middlewareGenerator.add(new CPPGenImpl(), "cpp");
middlewareGenerator.add(new RosCppGenImpl(), "roscpp");
middlewareGenerator.setGenerationTargetPath(TARGET_PATH_GENERATED +inpu);
middlewareGenerator.generate(componentInstanceSymbol, taggingResolver);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
}
MiddlewareGenerator middlewareGenerator = new MiddlewareGenerator();
middlewareGenerator.add(new EMADLGeneratorImpl(MODELS_PATH), "cpp");
middlewareGenerator.add(new RosCppGenImpl(), "roscpp");
middlewareGenerator.setGenerationTargetPath(TARGET_PATH_GENERATED +componentName);
middlewareGenerator.generate(componentInstanceSymbol, taggingResolver);
} catch (IOException e) {
System.err.println("Failed to generate sources. Sorry.");
e.printStackTrace();
}
}
public static TaggingResolver createSymTabAndTaggingResolver(String... modelPath) {
Scope scope = createSymTab(modelPath);
public static TaggingResolver createSymTabAndTaggingResolver( String... modelPath) {
Scope scope = AbstractSymtab.createSymTab(modelPath);
TaggingResolver tagging = new TaggingResolver(scope, Arrays.asList(modelPath));
TagMinMaxTagSchema.registerTagTypes(tagging);
TagTableTagSchema.registerTagTypes(tagging);
......@@ -77,10 +124,4 @@ public class Experiments {
return tagging;
}
private void generateEMADL(String qualifiedName) throws IOException, TemplateException {
Path modelPath = Paths.get(MODELS_PATH);
Generator gen = new Generator();
gen.generate(modelPath, qualifiedName);
}
}
component Alexnet{
ports in Z(0:255)^{3, 224, 224} image,
out Q(0:1)^{10,1,1} predictions;
implementation CNN {
def split1(i){
[i] ->
Convolution(kernel=(5,5), channels=128) ->
Lrn(nsize=5, alpha=0.0001, beta=0.75) ->
Pooling(pool_type="max", kernel=(3,3), stride=(2,2), padding="no_loss") ->
Relu()
}
def split2(i){
[i] ->
Convolution(kernel=(3,3), channels=192) ->
Relu() ->
Convolution(kernel=(3,3), channels=128) ->
Pooling(pool_type="max", kernel=(3,3), stride=(2,2), padding="no_loss") ->
Relu()
}
def fc(){
FullyConnected(units=4096) ->
Relu() ->
Dropout()
}
image ->
Convolution(kernel=(11,11), channels=96, stride=(4,4), padding="no_loss") ->
Lrn(nsize=5, alpha=0.0001, beta=0.75) ->
Pooling(pool_type="max", kernel=(3,3), stride=(2,2), padding="no_loss") ->
Relu() ->
Split(n=2) ->
split1(i=[0|1]) ->
Concatenate() ->
Convolution(kernel=(3,3), channels=384) ->
Relu() ->
Split(n=2) ->
split2(i=[0|1]) ->
Concatenate() ->
fc(->=2) ->
FullyConnected(units=10) ->
Softmax() ->
predictions
}
}
\ No newline at end of file
configuration Featureextraction{
num_epoch : 100
batch_size : 64
optimizer : sgd{
learning_rate : 0.01
}
}
package dp;
component Featureextraction {
ports
in Z(0:255)^{3, 210, 280} imageIn,
out Q(0:1)^{13,1,1} affordanceOut;
implementation CNN {
def conv(kernel, channels, hasPool=true, convStride=(1,1)){
Convolution(kernel=kernel, channels=channels, stride=convStride) ->
Relu() ->
Pooling(pool_type="max", kernel=(3,3), stride=(2,2), ?=hasPool)
}
def fc(){
FullyConnected(units=4096) ->
Relu() ->
Dropout()
}
imageIn ->
conv(kernel=(11,11), channels=96, convStride=(4,4)) ->
conv(kernel=(5,5), channels=256, convStride=(4,4)) ->
conv(kernel=(3,3), channels=384, hasPool=false) ->
conv(kernel=(3,3), channels=384, hasPool=false) ->
conv(kernel=(3,3), channels=256) ->
fc() ->
fc() ->
FullyConnected(units=13) ->
Sigmoid() ->
affordanceOut
}
}
package dp;
component Featureextraction {
ports
in Z(0:255)^{640,480} imageIn,
out Q(0:1)^{13,1} affordanceOut;
}
%!/bin/bash
echo "Building DriverController"
cd TorcsEMAMGenerator/target/generated-sources-torcs-dl/Drivercontroller
cd Drivercontroller
mkdir -p build
cd build
cmake ..
make -j
cd ../../../../..
cd ../..
echo "Building SafetyController"
cd TorcsEMAMGenerator/target/generated-sources-torcs-dl/Safetycontroller
cd Safetycontroller
mkdir -p build
cd build
cmake ..
make -j
cd ../../../../..
cd ../..
echo "Building FeatureExtraction"
cd TorcsEMAMGenerator/target/generated-sources-torcs-dl/Featureextraction
cd Featureextraction
mkdir -p build
cd build
cmake ..
make -j
cd ../../../../..
cd ../..
echo "Building ImagePreprocessing"
cd TorcsEMAMGenerator/target/generated-sources-torcs-dl/Imagepreprocessing
cd Imagepreprocessing
mkdir -p build
cd build
cmake ..
make -j
cd ../../../../..
cd ../..
echo
echo "Well done!"
......
%!/bin/bash
xterm -T DriverController -e Drivercontroller/build/coordinator/Coordinator_dp_drivercontroller &
xterm -T SafetyController -e Safetycontroller/build/coordinator/Coordinator_dp_safetycontroller &
xterm -T FeatureExtraction -e Featureextraction/build/coordinator/Coordinator_dp_featureextraction &
xterm -T ImagePreprocessing -e Imagepreprocessing/build/coordinator/Coordinator_dp_imagepreprocessing &
%!/bin/bash
#source devel/setup.bash#
#catkin_make
cd src
cd camera
mkdir -p build
cd build
cmake ..
make -j
cd ../..
cd driver
mkdir -p build
cd build
cmake ..
make -j
cd ../..
%!/bin/bash
source devel/setup.bash
xterm -T driver -e src/driver/build/driver &
xterm -T camera -e src/camera/build/camera &
cmake_minimum_required(VERSION 3.5)
project (ROSCamera)
set (CXX_STANDARD_REQUIRED 14)
find_package(roscpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(camera src/main.cpp src/torcsclient.cpp)
set_property(TARGET camera PROPERTY CXX_STANDARD 14)
target_link_libraries(camera ${roscpp_LIBRARIES} ${std_msgs_LIBRARIES})
target_include_directories(camera PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${roscpp_INCLUDE_DIRS} ${std_msgs_INCLUDE_DIRS})
#include "ros/ros.h"
#include "std_msgs/Float64MultiArray.h"
#include "torcsclient.h"
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "camera");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::Float64MultiArray>("/camera", 1000);
ros::Rate loop_rate(10);
int count = 0;
torcs::TorcsClient client;
while (ros::ok())
{
std_msgs::Float64MultiArray::Ptr msg(new std_msgs::Float64MultiArray);
msg->data.resize(torcs::IMAGE_SIZE_BYTES);
const auto imageData = client.getScreenshot();
memcpy(msg->data.data(), imageData->data(), imageData->size());
ROS_INFO("%f", msg->data[0]);
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
#include "torcsclient.h"
#include <iostream>
#include <chrono>
#include <thread>
#include <cstring>
#include <sys/shm.h>
#include <assert.h>
using namespace torcs;
TorcsClient::TorcsClient()
{
std::cout << "TORCS Client!" << std::endl;
void *shm = NULL;
int shmid;
shmid = shmget((key_t)4567, sizeof(struct shared_use_st), 0666);
if(shmid < 0)
{
std::cerr << "Failed to get shared memory!" << std::endl;
return;
}
shm = shmat(shmid, 0, 0);
if(shm == (void*)-1)
{
std::cerr << "Failed to shmat()!" << std::endl;
return;
}
connected = true;
std::cout << "Started shared memory at " << std::hex << shm << std::endl;
shared = (struct shared_use_st*)shm;
shared->written = 0;
shared->control = 0;
shared->pause = 0;
shared->fast = 0.0;
shared->dist_L = 0.0;
shared->dist_R = 0.0;
shared->toMarking_L = 0.0;
shared->toMarking_M = 0.0;
shared->toMarking_R = 0.0;
shared->dist_LL = 0.0;
shared->dist_MM = 0.0;
shared->dist_RR = 0.0;
shared->toMarking_LL = 0.0;
shared->toMarking_ML = 0.0;
shared->toMarking_MR = 0.0;
shared->toMarking_RR = 0.0;
shared->toMiddle = 0.0;
shared->angle = 0.0;
shared->speed = 0.0;
shared->steerCmd = 0.0;
shared->accelCmd = 0.0;
shared->brakeCmd = 0.0;
}
std::unique_ptr<DataContainer> TorcsClient::getScreenshot()
{
assert(connected);
while(shared->written == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
auto result = std::make_unique<DataContainer>();
memcpy(result->data(), shared->data, result->size());
shared->written = 0;
return result;
}
void TorcsClient::sendCommand(Command cmd)
{
assert(connected);
while (shared->control == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
shared->control = 0;
//shared->accelCmd = 10.0;
//shared->steerCmd = 1.0;
}
/*void writeScreenshot(shared_use_st* shared, unsigned int& num)
{
std::cout << "Read image." << std::endl;
FIBITMAP* newShot = FreeImage_Allocate(IMAGE_WIDTH, IMAGE_HEIGHT, 24);
if (!newShot)
{
std::cerr << "Failed to allocate freeimage file." << std::endl;
FreeImage_Unload(newShot);
return;
}
BYTE *bits = (BYTE*)FreeImage_GetBits(newShot);
for (int h = 0; h < IMAGE_HEIGHT; h++) {
BYTE *pixel = (BYTE*)bits;
for (int w = 0; w < IMAGE_WIDTH; w++) {
pixel[FI_RGBA_RED] = shared->data[((h)*IMAGE_WIDTH+w)*3+0];
pixel[FI_RGBA_GREEN] = shared->data[((h)*IMAGE_WIDTH+w)*3+1];
pixel[FI_RGBA_BLUE] = shared->data[((h)*IMAGE_WIDTH+w)*3+2];
pixel += 3;
}
bits += 3*IMAGE_WIDTH;
}
FreeImage_Save(FIF_BMP, newShot, (std::string("shot-")+std::to_string(num)).c_str());
FreeImage_Unload(newShot);
shared->control = 0;
shared->written = 0;
std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << "Read image." << std::endl;
}*/
#pragma once
#include <cstdint>
#include <array>
#include <memory>
namespace torcs
{
constexpr auto IMAGE_WIDTH = 280;
constexpr auto IMAGE_HEIGHT = 210;
constexpr auto IMAGE_CHANNELS = 3;
constexpr auto IMAGE_SIZE_BYTES = IMAGE_WIDTH*IMAGE_HEIGHT*IMAGE_CHANNELS;
struct shared_use_st
{
int written = 0; //a label, if 1: available to read, if 0: available to write