diff --git a/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/inc/temp_actuator.h b/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/inc/temp_actuator.h new file mode 100644 index 0000000000000000000000000000000000000000..7edc9167247ed40ef4f191a493ff32bc052460ba --- /dev/null +++ b/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/inc/temp_actuator.h @@ -0,0 +1,14 @@ +#ifndef INC_TEMP_ACTUATOR_H_ +#define INC_TEMP_ACTUATOR_H_ + +#include <stdint.h> + +void temp_actuator_initialize(const char * device); + +int temp_actuator_get_position(uint8_t channel); + +int temp_actuator_set_target(uint8_t channel, uint16_t target); + +void temp_actuator_terminate(); + +#endif /* INC_TEMP_ACTUATOR_H_ */ diff --git a/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/lib/libaf3pihal.a b/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/lib/libaf3pihal.a index f5a7f9b432da53a1d1764d0dcce667f065b59afe..3d6219dbd5f7d4fd2e36ff267d21144e75bbfbd9 100644 Binary files a/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/lib/libaf3pihal.a and b/org.fortiss.af3.platform.raspberry/trunk/code-gen-hal/lib/libaf3pihal.a differ diff --git a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/MainGenerator.java b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/MainGenerator.java index 74b343d8ebe8cb6dc3fb016c7f4c9bae6d920855..87dd2ae0e062de4cd3074b323ff7981e6af069d4 100644 --- a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/MainGenerator.java +++ b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/MainGenerator.java @@ -23,11 +23,23 @@ import java.util.List; import org.conqat.lib.commons.collections.Pair; import org.fortiss.af3.component.model.Component; +import org.fortiss.af3.component.model.InputPort; +import org.fortiss.af3.component.model.OutputPort; import org.fortiss.af3.component.model.Port; import org.fortiss.af3.generator.common.model.source.AbstractUnit; import org.fortiss.af3.platform.model.ExecutionUnit; import org.fortiss.af3.platform.model.PlatformConnectorUnit; +import org.fortiss.af3.platform.model.Receiver; +import org.fortiss.af3.platform.model.Transmitter; +import org.fortiss.af3.platform.raspberry.model.ActuatorPWM; import org.fortiss.af3.platform.raspberry.model.RaspberryPi; +import org.fortiss.af3.platform.raspberry.model.gamepad.Button1; +import org.fortiss.af3.platform.raspberry.model.gamepad.Button2; +import org.fortiss.af3.platform.raspberry.model.gamepad.Button3; +import org.fortiss.af3.platform.raspberry.model.gamepad.Button4; +import org.fortiss.af3.platform.raspberry.model.gamepad.GamepadReceiverBase; +import org.fortiss.af3.platform.raspberry.model.gamepad.Left_StickX_Position; +import org.fortiss.af3.platform.raspberry.model.gamepad.Left_StickY_Position; import org.fortiss.tooling.kernel.extension.data.ITransformationContext; /** @@ -43,35 +55,110 @@ class MainGenerator { public AbstractUnit createMain(RaspberryPi executionUnit, List<Pair<ExecutionUnit, Component>> deployedComponents, List<Pair<PlatformConnectorUnit, Port>> deployedPorts, ITransformationContext context) { - String includes = createIncludes(deployedComponents); + String includes = createIncludes(deployedComponents, deployedPorts); // TODO: syncbox code String initCode = createInitCode(deployedComponents); - String workerCode = createWorkerCode(deployedComponents); + String workerCode = createWorkerCode(deployedComponents, deployedPorts); return getMainCFile(executionUnit.getName(), executionUnit.isCoordinatorUnit(), executionUnit.getCanCoordinationID(), executionUnit.getCycleTime(), 250, includes, "// TODO\n", initCode, workerCode); } /** Creates the includes of the system headers. */ - private String createIncludes(List<Pair<ExecutionUnit, Component>> deployedComponents) { + private String createIncludes(List<Pair<ExecutionUnit, Component>> deployedComponents, + List<Pair<PlatformConnectorUnit, Port>> deployedPorts) { StringBuilder sb = new StringBuilder(); for(Pair<ExecutionUnit, Component> p : deployedComponents) { Component c = p.getSecond(); sb.append("#include <" + c.getName() + "_ID_" + c.getId() + ".h>\n"); } + boolean useGamepad = false; + boolean usePWM = false; + for(Pair<PlatformConnectorUnit, Port> p : deployedPorts) { + if(!useGamepad && p.getFirst() instanceof GamepadReceiverBase) { + sb.append("#include <gamepad.h>\n"); + useGamepad = true; + } + if(!usePWM && p.getFirst() instanceof ActuatorPWM) { + sb.append("#include <temp_actuator.h>\n"); + usePWM = true; + } + } return sb.toString(); } /** Create the worker code. */ - private String createWorkerCode(List<Pair<ExecutionUnit, Component>> deployedComponents) { + private String createWorkerCode(List<Pair<ExecutionUnit, Component>> deployedComponents, + List<Pair<PlatformConnectorUnit, Port>> deployedPorts) { StringBuilder sb = new StringBuilder(); + for(Pair<PlatformConnectorUnit, Port> p : deployedPorts) { + if(p.getFirst() instanceof Receiver && p.getSecond() instanceof InputPort) { + sb.append(createReadCode((Receiver)p.getFirst(), (InputPort)p.getSecond())); + } + } for(Pair<ExecutionUnit, Component> p : deployedComponents) { Component c = p.getSecond(); sb.append(makeCall("perform_step", c)); } + for(Pair<PlatformConnectorUnit, Port> p : deployedPorts) { + if(p.getFirst() instanceof Transmitter && p.getSecond() instanceof OutputPort) { + sb.append(createWriteCode((Transmitter)p.getFirst(), (OutputPort)p.getSecond())); + } + } return sb.toString(); } + /** Returns the port and ID identifier. */ + private String portName(Port port) { + return port.getName() + "_ID_" + port.getId(); + } + + /** Creates the read code for the given receiver and port. */ + private String createWriteCode(Transmitter transmitter, OutputPort outport) { + if(transmitter instanceof ActuatorPWM) { + ActuatorPWM act = (ActuatorPWM)transmitter; + String result = "if(!noval_" + portName(outport) + ") {\n"; + result += + "temp_actuator_set_target(" + act.getChannelID() + ", (uint16_t)" + + portName(outport) + ");\n"; + result += "}\n"; + return result; + } + return ""; + } + + /** Creates the read code for the given receiver and port. */ + private String createReadCode(Receiver receiver, InputPort inport) { + if(receiver instanceof Button1) { + return gamepadReadCode("gamepad_get_button_state", "GAMEPAD_BUTTON_1", inport); + } + if(receiver instanceof Button2) { + return gamepadReadCode("gamepad_get_button_state", "GAMEPAD_BUTTON_2", inport); + } + if(receiver instanceof Button3) { + return gamepadReadCode("gamepad_get_button_state", "GAMEPAD_BUTTON_3", inport); + } + if(receiver instanceof Button4) { + return gamepadReadCode("gamepad_get_button_state", "GAMEPAD_BUTTON_4", inport); + } + if(receiver instanceof Left_StickX_Position) { + return gamepadReadCode("gamepad_get_axis_position", + "GAMEPAD_AXIS_LEFT_STICK_HORIZONTAL", inport); + } + if(receiver instanceof Left_StickY_Position) { + return gamepadReadCode("gamepad_get_axis_position", "GAMEPAD_AXIS_LEFT_STICK_VERTICAL", + inport); + } + return ""; + } + + /** Creates read code for gamepad state. */ + private String gamepadReadCode(String gamepadFun, String btnId, InputPort inport) { + String noval = "noval_" + portName(inport) + " = false;\n"; + String setBtn = portName(inport) + " = " + gamepadFun + "(" + btnId + ");\n"; + return noval + setBtn; + } + /** Create the initialize code. */ private String createInitCode(List<Pair<ExecutionUnit, Component>> deployedComponents) { StringBuilder sb = new StringBuilder();