User Tools

Site Tools


getting_started:tutorials:firstproject

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
getting_started:tutorials:firstproject [2015/08/06 12:54] visentingetting_started:tutorials:firstproject [2016/10/07 14:02] gehrig
Line 1: Line 1:
 ====== Your First EEROS Project ====== ====== Your First EEROS Project ======
-First of all, make sure that you have set up your environment as described in [[..:install_and_setup_development_environment|Install and Setup Development Environment]]. The following example shows a typical EEROS application.  + 
 +  - First of all, make sure that you have set up your environment as described in [[..:install_and_setup_development_environment|Install and Setup Development Environment]].  
 +  - Then create a new project following the steps under "preparation" on [[getting_started:tutorials:helloworld|Getting Started]]. 
 +  - The following example shows a typical EEROS application.   
 +  - After creating all those files, modify the CMakeLists.txt and CMAkeCache.txt files accoring to [[getting_started:tutorials:helloworld|Getting Started]], "Say Hello World with EEROS", point 2 and 3.  
 <code c> <code c>
 +#include <iostream>
 +#include <unistd.h>
 +
 +#include <eeros/logger/Logger.hpp>
 +#include <eeros/logger/StreamLogWriter.hpp>
 +
 +#include "MySequencer.hpp"
 +#include "MySafetyProperties.hpp"
 +#include "MyControlSystem.hpp"
 +
 +
 +using namespace eeros;
 +using namespace eeros::hal;
 +using namespace eeros::control;
 +using namespace eeros::safety;
 +using namespace eeros::logger;
 +using namespace eeros::sequencer;
 +using namespace testproject;
 +
 +
 int main() { int main() {
   std::cout << "SCARA Robot Control started" << std::endl;   std::cout << "SCARA Robot Control started" << std::endl;
Line 26: Line 51:
   sequencer.start(&mainSequence);   sequencer.start(&mainSequence);
      
-  while(sequencer.getState() != state::terminated) { +//  while(sequencer.getState() != state::terminated) { 
-      usleep(10000); +//      usleep(10000); 
-  }+//  }
      
-  controlSystem.stop(); +  sequencer.join(); 
-  safetySystem.shutdown(); +   
-  sequencer.shutdown();+//  controlSystem.stop(); 
 +//  safetySystem.shutdown(); 
 +//  sequencer.shutdown();
      
   std::cout << "SCARA Robot Control stopped" << std::endl;   std::cout << "SCARA Robot Control stopped" << std::endl;
Line 39: Line 66:
 </code> </code>
  
-To start, create an instance of the [[eeros_architecture:control_system:start|Control System]] and [[eeros_architecture:safety_system:start|Safety System]]. The safety system is closely connected to the Hardware which is accessed through the [[eeros_architecture:safety_system:hal|Hardware Abstraction Layer]] (HAL). Next, inputs and outputs for the HAL have to be defined and assigned to the HAL. \\+To start, create an instance of the [[eeros_architecture:control_system:start|Control System]] and [[eeros_architecture:safety_system:start|Safety System]]. Since the safety system is closely connected to the Hardware which is accessed through the [[eeros_architecture:safety_system:hal|Hardware Abstraction Layer]] (HAL), inputs and outputs for the HAL have to be defined and assigned to the HAL. \\
  
 Then you have to define the [[eeros_architecture:safety_system:properties|Safety Properties]] and start the safety system. The safety system will power-up the system and do all the necessary initialization, such as enabling actors and homing them. \\ Then you have to define the [[eeros_architecture:safety_system:properties|Safety Properties]] and start the safety system. The safety system will power-up the system and do all the necessary initialization, such as enabling actors and homing them. \\
  
 Finally, a sequence has to be defined and assigned to a [[eeros_architecture:sequencer:usage|Sequencer]]. The program will run as long as the sequencer does not terminate. Finally, a sequence has to be defined and assigned to a [[eeros_architecture:sequencer:usage|Sequencer]]. The program will run as long as the sequencer does not terminate.
 +
 +Here follows the basic implementation of the three subsystems of a simple EEROS application: 
 +
 +=== Control System: ===
 +
 +== .hpp: ==
 +
 +<code c>
 +#ifndef CH_NTB_TEST_MYCONTROLSYSTEM_HPP_
 +#define CH_NTB_TEST_MYCONTROLSYSTEM_HPP_
 +
 +#include <eeros/control/TimeDomain.hpp>
 +#include <eeros/control/Constant.hpp>
 +#include <eeros/control/Gain.hpp>
 +
 +namespace testproject {
 +  
 +  class MyControlSystem {
 +  
 +  public:
 +    MyControlSystem();
 +    
 +    void start(); // starts timedomains
 +    void stop();  // stops timedomains
 +    
 +    // Define blocks here
 +    
 +  private:
 +    eeros::control::TimeDomain timedomain;
 +    
 +  };  // end class  
 +};    // end namepspace
 +
 +#endif //CH_NTB_TEST_MYCONTROLSYSTEM_HPP_
 +</code>
 +
 +== .cpp: ==
 +
 +<code c>
 +#include "MyControlSystem.hpp"
 +
 +using namespace testproject;
 +
 +MyControlSystem::MyControlSystem() :
 + 
 +timedomain("Main time domain", 0.001, true)
 +
 +{
 +    // Configure Blocks
 +    
 +    // Connect Blocks
 +    // e.g. gain.getIn().connect(constant.getOut());
 +    
 +    // Run Blocks
 +    // e.g. timedomain.addBlock(&constant);
 +}
 +</code>
 +
 +=== Safety System: === 
 +
 +== .hpp: ==
 +
 +<code c>
 +#ifndef CH_NTB_TEST_MYSAFETYPROPERTIES_HPP_
 +#define CH_NTB_TEST_MYSAFETYPROPERTIES_HPP_
 +
 +#include <eeros/safety/SafetyProperties.hpp>
 +#include <eeros/hal/HAL.hpp>
 +#include <eeros/hal/PeripheralOutput.hpp>
 +#include <eeros/hal/PeripheralInput.hpp>
 +#include <eeros/hal/ScalablePeripheralInput.hpp>
 +
 +namespace testproject {
 +    
 +    class MyControlSystem;
 +    
 +    // ***** Define events ***** //
 +    enum {
 + // e.g. doPowerUp = 1,
 +    };
 +    
 +    // ***** Define levels ***** //
 +    enum {
 + // e.g. off = 1,
 +    };
 +    
 +    class MySafetyProperties : public eeros::safety::SafetyProperties {
 +
 +    public: 
 + MySafetyProperties(MyControlSystem* cs);
 + virtual ~MySafetyProperties();
 +
 + // ***** Define critical outputs ***** //
 +  // e.g. eeros::hal::PeripheralOutput<bool>* watchdog;
 +
 + // ***** Define critical inputs ***** //
 +  //e.g. eeros::hal::PeripheralInput<bool>* emergencyButton;
 +
 +    private:
 + MyControlSystem* controlSys;
 +
 +    }; // end class
 +};     // end namespace
 +
 +#endif //CH_NTB_TEST_MYSAFETYPROPERTIES_HPP_
 +</code>
 +
 +== .cpp: ==
 +
 +<code c>
 +#include "MySafetyProperties.hpp"
 +#include "MyControlSystem.hpp"
 +
 +#include <eeros/hal/HAL.hpp>
 +#include <eeros/safety/InputAction.hpp>
 +#include <eeros/safety/inputActions.hpp>
 +#include <eeros/safety/OutputAction.hpp>
 +
 +#include <iostream>
 +
 +using namespace testproject; 
 +using namespace eeros::hal;
 +using namespace eeros::control;
 +using namespace eeros::safety;
 +
 +MySafetyProperties::MySafetyProperties(MyControlSystem* cs) : controlSys(cs) {
 +
 +    // Create HAL instance
 +    HAL& hal = HAL::instance();
 +    
 +    // ***** Define critical outputs ***** //
 +    watchdog = hal.getLogicPeripheralOutput("watchdog");
 +    criticalOutputs = {watchdog};
 +    
 +    // ***** Define critical inputs ***** //
 +    emergencyButton = hal.getLogicPeripheralInput("emergencyButton");
 +    criticalInputs = {emergencyButton};
 +    
 +    // ***** Define levels ***** //
 +    levels = {
 + {off,       "system off",        },
 +    };
 +    
 +    // ***** Add events to levels ***** //
 +    level(off      ).addEvent(doPowerUp, poweredUp, kPublicEvent);
 +    
 +    // ***** Define inputs actions ***** //
 +     level(off      ).setInputActions({ ignore(emergencyButton), ignore(encoder)});
 +    
 +    // ***** Define output actions ***** //
 +     level(off      ).setOutputActions({ set(watchdog, false), set(enable, false)});
 +    
 +    // ***** Define level functions ***** //
 +    level(off).setLevelAction([&](SafetyContext* privateContext) {
 + controlSys->start();
 + privateContext->triggerEvent(doPowerUp);
 +    });
 +    
 +    entryLevel = off;
 +}
 +    
 +MySafetyProperties::~MySafetyProperties() {
 +   // nothing to do
 +}
 +
 +</code>
 +
 +=== Main Sequencer: ===
 +
 +== .hpp: ==
 +
 +<code c>
 +#ifndef CH_NTB_TEST_MYSEQUENCER_HPP_
 +#define CH_NTB_TEST_MYSEQUENCER_HPP_
 +
 +#include <eeros/sequencer/Sequence.hpp>
 +#include <eeros/safety/SafetySystem.hpp>
 +#include "MyControlSystem.hpp"
 +
 +
 +namespace testproject {
 +    
 +    class MySequencer : public eeros::sequencer::Sequence<void> {
 +    
 +    public:
 + MySequencer(eeros::sequencer::Sequencer* sequencer, testproject::MyControlSystem* controlSys, eeros::safety::SafetySystem* safetySys);
 +
 + virtual bool checkPreCondition();
 + virtual void run();
 + virtual void exit();
 +
 +    private: 
 + // Define subsequences here
 + // Example: Homing homingSequence; 
 +
 + bool isTerminating();
 +
 + testproject::MyControlSystem* controlSys;
 + eeros::safety::SafetySystem* safetySys;
 +
 +    }; // end class
 +};     // end namespace
 +
 +#endif // CH_NTB_TEST_MYSEQUENCER_HPP_
 +</code>
 +
 +== .cpp: ==
 +
 +<code c>
 +#include "MySequencer.hpp"
 +#include "MySafetyProperties.hpp"
 +#include <unistd.h>
 +
 +using namespace testproject;
 +using namespace eeros::sequencer;
 +using namespace eeros::safety;
 +
 +MySequencer::MySequencer(Sequencer* sequencer, MyControlSystem* controlSys, SafetySystem* safetySys) : 
 + Sequence< void >("main", sequencer), controlSys(controlSys), safetySys(safetySys) {
 +    // nothing to do 
 +}
 +
 +bool MySequencer::checkPreCondition() {
 +    return safetySys->getCurrentLevel().getId() >= off;   
 +}
 +
 +void MySequencer::run() {
 +    log.info() << "Start sequence";
 +    sleep(1);   
 +}
 +
 +void MySequencer::exit() {
 +    log.info() << "Exit sequence";
 +}
 +
 +inline bool MySequencer::isTerminating() {
 +    return sequencer->getState() == state::terminating;
 +}
 +</code>
 +
 +The related CMake File is: 
 +
 +<code c>
 +cmake_minimum_required(VERSION 2.8)
 +
 +project(test-project)
 +
 +include_directories(${ADDITIONAL_INCLUDE_DIRS})
 +link_directories(${ADDITIONAL_LINK_DIRS})
 +
 +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
 +
 +add_executable(test-project 
 +       main.cpp
 +       MyControlSystem.cpp
 +       MySafetyProperties.cpp
 +       MySequencer.cpp)
 +
 +target_link_libraries(test-project eeros)
 +</code>