Commit b046691a authored by Svetlana's avatar Svetlana

Application using jar of EMAM2Middleware (zero java code in application)

parent 65a5dcc4
#!/bin/bash
echo "Cleaning target directory..."
rm -rf target/*
echo "Generating code..."
java -cp embedded-montiarc-math-middleware-generator-0.0.9-SNAPSHOT-jar-with-dependencies.jar de.monticore.lang.monticar.generator.middleware.DistributedTargetGeneratorCli --models-dir=src/main/ --root-model=dp.mastercomponent --output-dir=target/Mastercomponent --generators=emadlcpp,roscpp --backend=MXNET
echo "Copying build and run scripts..."
cp -rf resources/scripts/* target/
chmod u+x target/*.sh
echo "Copying CNN weights..."
cp -rf resources/torcs target/torcs
chmod u+x resources/torcs/*.sh
cp -rf resources/dpnet target/model
echo "Building generated code..."
cd target
./build_all.sh
echo "Please start TORCS, configure race and run ./run_all.sh"
......@@ -4,7 +4,7 @@
#include <ml.h>
#include <highgui.h>
#include <cv.h>
#include <cv.hpp>
#include <ros/ros.h>
#include <std_msgs/Float32.h>
#include <std_msgs/Float32MultiArray.h>
......
......@@ -13,50 +13,50 @@ component Denormalizer {
Q newMinAngle = -0.5;
Q newRangeAngle = 0.5 - newMinAngle;
affordance.angle = (((normalizedPredictions(0) - oldMin) * newRangeAngle) / oldRange) + newMinAngle;
affordance.angle = (((normalizedPredictions(1) - oldMin) * newRangeAngle) / oldRange) + newMinAngle;
Q newMinMarkingL = -7;
Q newRangeMarkingL = -2.5 - newMinMarkingL;
affordance.toMarkingL = (((normalizedPredictions(1) - oldMin) * newRangeMarkingL) / oldRange) + newMinMarkingL;
affordance.toMarkingL = (((normalizedPredictions(2) - oldMin) * newRangeMarkingL) / oldRange) + newMinMarkingL;
Q newMinMarkingM = -2;
Q newRangeMarkingM = 3.5 - newMinMarkingM;
affordance.toMarkingM = (((normalizedPredictions(2) - oldMin) * newRangeMarkingM) / oldRange) + newMinMarkingM;
affordance.toMarkingM = (((normalizedPredictions(3) - oldMin) * newRangeMarkingM) / oldRange) + newMinMarkingM;
Q newMinMarkingR = 2.5;
Q newRangeMarkingR = newMinMarkingR - 7;
affordance.toMarkingR = (((normalizedPredictions(3) - oldMin) * newRangeMarkingR) / oldRange) + newMinMarkingR;
affordance.toMarkingR = (((normalizedPredictions(4) - oldMin) * newRangeMarkingR) / oldRange) + newMinMarkingR;
Q newMinDist = 0;
Q newRangeDist = 75 - newMinDist;
affordance.distL = (((normalizedPredictions(4) - oldMin) * newRangeDist) / oldRange) + newMinDist;
affordance.distR = (((normalizedPredictions(5) - oldMin) * newRangeDist) / oldRange) + newMinDist;
affordance.distL = (((normalizedPredictions(5) - oldMin) * newRangeDist) / oldRange) + newMinDist;
affordance.distR = (((normalizedPredictions(6) - oldMin) * newRangeDist) / oldRange) + newMinDist;
Q newMinMarkingLL = -9.5;
Q newRangeMarkingLL = -4 - newMinMarkingLL;
affordance.toMarkingLL = (((normalizedPredictions(6) - oldMin) * newRangeMarkingLL) / oldRange) + newMinMarkingLL;
affordance.toMarkingLL = (((normalizedPredictions(7) - oldMin) * newRangeMarkingLL) / oldRange) + newMinMarkingLL;
Q newMinMarkingML = -5.5;
Q newRangeMarkingML = -0.5 - newMinMarkingML;
affordance.toMarkingML = (((normalizedPredictions(7) - oldMin) * newRangeMarkingML) / oldRange) + newMinMarkingML;
affordance.toMarkingML = (((normalizedPredictions(8) - oldMin) * newRangeMarkingML) / oldRange) + newMinMarkingML;
Q newMinMarkingMR = 0.5;
Q newRangeMarkingMR = 5.5 - newMinMarkingMR;
affordance.toMarkingMR = (((normalizedPredictions(8) - oldMin) * newRangeMarkingMR) / oldRange) + newMinMarkingMR;
affordance.toMarkingMR = (((normalizedPredictions(9) - oldMin) * newRangeMarkingMR) / oldRange) + newMinMarkingMR;
Q newMinMarkingRR = 4;
Q newRangeMarkingRR = 9.5 - newMinMarkingRR;
affordance.toMarkingRR = (((normalizedPredictions(9) - oldMin) * newRangeMarkingRR) / oldRange) + newMinMarkingRR;
affordance.toMarkingRR = (((normalizedPredictions(10) - oldMin) * newRangeMarkingRR) / oldRange) + newMinMarkingRR;
Q newMinDistLMR = 0;
Q newRangeDistLMR = 75 - newMinDistLMR;
affordance.distLL = (((normalizedPredictions(10) - oldMin) * newRangeDistLMR) / oldRange) + newMinDistLMR;
affordance.distMM = (((normalizedPredictions(11) - oldMin) * newRangeDistLMR) / oldRange) + newMinDistLMR;
affordance.distRR = (((normalizedPredictions(12) - oldMin) * newRangeDistLMR) / oldRange) + newMinDistLMR;
affordance.distLL = (((normalizedPredictions(11) - oldMin) * newRangeDistLMR) / oldRange) + newMinDistLMR;
affordance.distMM = (((normalizedPredictions(12) - oldMin) * newRangeDistLMR) / oldRange) + newMinDistLMR;
affordance.distRR = (((normalizedPredictions(13) - oldMin) * newRangeDistLMR) / oldRange) + newMinDistLMR;
Q newMinFast = 0;
Q newRangeFast = 1 - newMinFast;
affordance.fast = (((normalizedPredictions(13) - oldMin) * newRangeFast) / oldRange) + newMinFast;
affordance.fast = (((normalizedPredictions(14) - oldMin) * newRangeFast) / oldRange) + newMinFast;
}
}
configuration Dpnet{
num_epoch : 100
batch_size : 64
context:cpu
normalize:true
eval_metric : mse
context : gpu
load_checkpoint : true
normalize : true
optimizer : sgd{
learning_rate : 0.01
learning_rate_decay : 0.9
......
......@@ -57,7 +57,7 @@ component DriverController {
desiredSpeed = 20; // 20m/s = 72 kmh
if (affordanceIn.fast != 1)
desiredSpeed = 20 - abs(steeringRecordIn(0) + steeringRecordIn(1) + steeringRecordIn(2) + steeringRecordIn(3) + steeringRecordIn(4)) * 4.5;
desiredSpeed = 20 - abs(steeringRecordIn(1) + steeringRecordIn(2) + steeringRecordIn(3) + steeringRecordIn(4) + steeringRecordIn(5)) * 4.5;
end
if (desiredSpeed < 10)
......@@ -88,4 +88,4 @@ component DriverController {
commandsOut(1) = 0; // accelCmd
end
}
}
\ No newline at end of file
}
target/
output/
venv/
.idea/
.git
*.iml
src/main/resources/dpnet/
# How-to
1. Run Generator in src/main/java
2. Start TORCS, choose 'Quick Race', go to 'Configure', select some map and choose 'chenyi' as the only driver. Start race.
* To set specific screen resolution go to torcs config folder (e.g. ./torcs/config) and edit screen.xml. Our toolchain uses 280x210.
3. Go to target/generated-sources-torcs-dl and run `build_all.sh` to compile generated code
4. Go to target/generated-sources-torcs-dl and run `run_all.sh` to start ROS and ROS node including TORCS connectors. This will open a separate console for each ROS node
and another console for ROS core.
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>torcs-dl</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc</artifactId>
<version>0.0.11c-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-generator</artifactId>
<version>0.0.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-roscpp-generator</artifactId>
<version>0.0.1c-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-middleware-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-deeplearning</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-emadl-generator</artifactId>
<version>0.2.1-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));
cmake.setFileContent(cmake.getFileContent() + "target_link_libraries(" + name + " -lmxnet)");
return cmake;
}
}
package de.monticore.lang.monticar.torcs_dl;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosToEmamTagSchema;
import de.monticore.lang.monticar.emadl.generator.AbstractSymtab;
import de.monticore.lang.monticar.generator.middleware.MiddlewareGenerator;
import de.monticore.lang.monticar.generator.middleware.impls.RosCppGenImpl;
import de.monticore.lang.monticar.generator.roscpp.helper.TagHelper;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Generator {
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 = AbstractSymtab.createSymTabAndTaggingResolver(MODELS_PATH);
RosToEmamTagSchema.registerTagTypes(taggingResolver);
try {
System.out.println("Code generation for Mastercomponent");
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("dp."+ "Mastercomponent".toLowerCase(), ExpandedComponentInstanceSymbol.KIND).orElse(null);
assert componentInstanceSymbol != null;
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 + "Mastercomponent");
middlewareGenerator.generate(componentInstanceSymbol, taggingResolver);
} catch (IOException e1) {
System.err.println("Failed to generate sources. Sorry.");
e1.printStackTrace();
}
try {
Files.copy(Paths.get(RESOURCES_PATH+"scripts/build_all.sh"), Paths.get(TARGET_PATH_GENERATED+"build_all.sh"), StandardCopyOption.REPLACE_EXISTING);
Runtime.getRuntime().exec("chmod u+x "+TARGET_PATH_GENERATED+"build_all.sh");
Files.copy(Paths.get(RESOURCES_PATH+"scripts/run_all.sh"), Paths.get(TARGET_PATH_GENERATED+"run_all.sh"), StandardCopyOption.REPLACE_EXISTING);
Runtime.getRuntime().exec("chmod u+x "+TARGET_PATH_GENERATED+"run_all.sh");
FileUtils.copyDirectory(new File(RESOURCES_PATH+"torcs"), new File(TARGET_PATH_GENERATED+"torcs"));
Runtime.getRuntime().exec("chmod u+x "+TARGET_PATH_GENERATED+"torcs/build.sh");
FileUtils.copyDirectory(new File(RESOURCES_PATH+"dpnet"), new File(TARGET_PATH_GENERATED+"model"));
} catch (IOException e) {
System.err.println("Failed to copy build and start scripts.");
e.printStackTrace();
}
}
}
#pragma once
#include "${compName}.h"
class IAdapter_${compName}{
public:
virtual ~IAdapter_${compName}(){}
virtual void init(${compName}* comp) = 0;
virtual void tick() = 0;
};
cmake_minimum_required(VERSION 3.5)
project(${compName} LANGUAGES CXX)
add_library(${compName} ${compName}.h)
target_include_directories(${compName} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(${compName} PROPERTIES LINKER_LANGUAGE CXX)
export(TARGETS ${compName} FILE ${compName}.cmake)
cmake_minimum_required(VERSION 3.5)
project (Coordinator_${compName} CXX)
set (CMAKE_CXX_STANDARD 11)
set (THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(Coordinator_${compName} Coordinator_${compName}.cpp)
set_target_properties(Coordinator_${compName} PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(Coordinator_${compName} ${targets} Threads::Threads)
target_include_directories(Coordinator_${compName} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
export(TARGETS Coordinator_${compName} FILE Coordinator_${compName}.cmake)
\ No newline at end of file
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include <list>
#include "IAdapter_${compName}.h"
${includes}
using namespace std;
using namespace chrono;
static int exeMs = 100;
bool parseCommandLineParameters(int argc, char* argv[]){
if(argc == 1){
return true;
}
if(argc == 3 && string(argv[1]) == "-t"){
try{
int tmp = stoi(argv[2]);
if(tmp >= 0){
exeMs = tmp;
return true;
}
}catch(...){
//Ignore
}
}
cout << "Usage: " << argv[0] << " [-h | -t sleepTimeMs]\n";
return false;
}
void startMiddleware(IAdapter_${compName}& adapter,${compName}& comp,atomic<bool>& done){
adapter.init(&comp);
done = true;
}
int main(int argc, char* argv[])
{
if(!parseCommandLineParameters(argc,argv)){
return 1;
}
atomic<bool> done(false);
${compName} comp;
comp.init();
list<IAdapter_${compName}*> adapters;
${addAdapters}
list<thread*> threads;
for(auto a : adapters){
threads.push_back(new thread(startMiddleware,ref(*a),ref(comp),ref(done)));
}
cout << "waiting for all middleware to start\n";
this_thread::sleep_for(seconds(3));
cout << "started! Executing every " << exeMs << "ms\n";
time_point<system_clock> start, end;
while(!done){
start = system_clock::now();
comp.execute();
for(auto a : adapters){
(*a).tick();
}
end = system_clock::now();
int elapsedMs = duration_cast<milliseconds>(end-start).count();
int newSleep = exeMs - elapsedMs;
if(newSleep <= 0){
cout << "Cant keep up! "<< (-newSleep) <<"ms late!\n";
}else{
this_thread::sleep_for(milliseconds(newSleep));
}
}
return 0;
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project (struct_msgs)
find_package(genmsg REQUIRED)
FILE(GLOB MSG_FILES_RAW RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../*/roscpp/struct_msgs/*.msg)
#generate struct_msgs iff .msg files where found
if(MSG_FILES_RAW)
#filter: add each struct msg only once (distinct by filename without path)
foreach(CUR_MSG_FILE ${MSG_FILES_RAW})
get_filename_component(TMP_MSG_NAME ${CUR_MSG_FILE} NAME)
IF(NOT MSG_DEFINED_${TMP_MSG})
LIST(APPEND MSG_FILES ${CUR_MSG_FILE})
SET(MSG_DEFINED_${TMP_MSG} TRUE)
ENDIF()
endforeach(CUR_MSG_FILE)
#generate messages
add_message_files(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} FILES ${MSG_FILES})
generate_messages()
#export the include_dirs, so that other subprojects can use it
set(struct_msgs_INCLUDE_DIRS ${struct_msgs_INCLUDE_DIRS} PARENT_SCOPE)
endif()
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