User Tools

Site Tools


getting_started:tutorials:mockrobot

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
getting_started:tutorials:mockrobot [2019/03/28 12:02] – [The Full Picture] grafgetting_started:tutorials:mockrobot [2021/03/31 17:45] (current) – [Safety System] ursgraf
Line 1: Line 1:
 ====== Mock Robot Example ====== ====== Mock Robot Example ======
-This tutorial demonstrates an EEROS application complete with a control system, a safety system, and a sequencer. You can find the code in the directory with examplesNavigate to examples/mockRobotExample+This tutorial demonstrates an EEROS application complete with a control system, a safety system, and a sequencer. You can find the code under [[https://github.com/eeros-project/eeros-framework/blob/master/examples/system/MockRobotExample.cpp|MockRobotExample.cpp]]. 
 + 
 +Start the program with  
 +<code cpp> 
 +$ sudo ./examples/system/mockRobotExample 
 +</code>
  
 ===== The Full Picture ===== ===== The Full Picture =====
 This example does not need any hardware. The moving of a robot is pretended by using integrators summing up. This represents the position on an axis. Before a robot can be moved it has usually to be homed.  This example does not need any hardware. The moving of a robot is pretended by using integrators summing up. This represents the position on an axis. Before a robot can be moved it has usually to be homed. 
  
-The main application defines a control system and a safety system. The safety system is assigned as main task and passed to the executor. Further, a main sequence is defined and added to the sequencer ans started. After this the executor starts running.+The main application defines a control system and a safety system. The safety system is assigned as main task and passed to the executor. Further, a main sequence is defined and added to the sequencer and started. After this the executor starts running.
  
 <code cpp> <code cpp>
Line 21: Line 26:
   MainSequence mainSeq("Main Sequence", sequencer, cs, ss, sp);   MainSequence mainSeq("Main Sequence", sequencer, cs, ss, sp);
   sequencer.addSequence(mainSeq);   sequencer.addSequence(mainSeq);
-  mainSeq.start();+  mainSeq();
   
   ...   ...
Line 43: Line 48:
 ===== Safety System ===== ===== Safety System =====
 The safety system has four levels and a couple of safety events to switch between these levels. The safety system has four levels and a couple of safety events to switch between these levels.
-[{{ :getting_started:tutorials:mockrobotsafetysystem.png?400 |//Safety system with levels and events.//}}] +[{{ :getting_started:tutorials:mockrobotss.jpg?220 |//Safety system with levels and events.//}}] 
-The event ''abort'' can be triggered in all levels except ''slOff''. The safety system starts in the level ''slHoming''This level defines a level action as follows:+The event ''abort'' can be triggered in all levels except ''slOff''. The safety system starts in the level ''slHoming''As soon as the homing is complete the safety level switches to ''slReady''. The sequencer can then cause the safety system to change to level ''slMoving''
  
 +===== Sequencer =====
 +The main sequence comprises of two subsequences and a step. The first of the two sequences is responsible for homing while the latter moves the robot up and down. The main sequence does the following action:
 <code cpp> <code cpp>
-  slHoming.setLevelAction([&](SafetyContext* privateContext) { +  int action() { 
-    if (slHoming.getNofActivations() == 1+    while(Sequencer::running) { 
-      Sequencer::instance().getSequenceByName("Homing Sequence")->start(); +      if (ss.getCurrentLevel() == sp.slHoming{ 
-    if (cs.iX.getOut().getSignal().getValue() >1.0cs.setpointX.setValue(0); +        homing(); 
-    if (cs.iY.getOut().getSignal().getValue() >= 1.0) cs.setpointY.setValue(0); +      } else if(ss.getCurrentLevel() == sp.slReady
-    if (Sequencer::instance().getSequenceByName("Homing Sequence")->getRunningState() == SequenceState::terminated+        wait(2); 
-      privateContext->triggerEvent(homingDone); +        cs.sw.switchToInput(1)
-  });+        ss.triggerEvent(sp.startMoving); 
 +      } else if(ss.getCurrentLevel() == sp.slMoving
 +        upDown(); 
 +      
 +      wait(0.1); 
 +    } 
 +    return 0; 
 +  }
 </code> </code>
- +Depending on the safety level the homing or the upDown sequence is started. In ''slReady'' the robot pauses for 2 secondsThe action is stopped as soon as the sequencer stops runningThe wait(0.1) is necessary to wait for the safety system to actually switch levelsWithout waiting the situation could arise that a safety event is triggered which takes effect in the next cycles when the executor runs the safety system. 
-As a first step the sequence "Homing Sequence", which was registered in the sequencer at startup, is started.  +
-This sequence moves the robot slowly to its limit, see next chapterAs soon as one of the axis reaches this limit, the movement stops. After the "Homing Sequence" has terminated a safety event is triggered which causes a switch to the next safety level. \\ +
-This level does nothing except wait for 2 seconds until it switches to the level ''slMoving''.  +
- +
-<code cpp> +
-  slReady.setLevelAction([=](SafetyContext* privateContext) { +
-    if (slReady.getNofActivations() * ts >= 2) +
-      privateContext->triggerEvent(startMoving); +
-    }); +
-  }); +
-</code> +
- +
-This level simply starts the "UpAndDown Sequence"+
-<code cpp> +
-  slMoving.setLevelAction([&](SafetyContext* privateContext) { +
-    if (slMoving.getNofActivations() == 1) +
-      Sequencer::instance().getSequenceByName("UpAndDown Sequence")->start(); +
-  }); +
-</code> +
- +
-===== Sequencer ===== +
-The main programm registered two sequences. The first of the two is responsible for homingIt is defined as nonblocking, which makes it run in parallel to the main program and notably to the safety system which calls the start method of this sequence.+
  
 <code cpp> <code cpp>
 class HomingSequence : public Sequence { class HomingSequence : public Sequence {
 public: public:
-  HomingSequence(std::string name, Sequencer& seq, MockRobotControlSystem& cs) : Sequence(name, seq), cs(cs) {  }+  HomingSequence(std::string name, Sequence* caller, MockRobotControlSystem& cs, SafetySystem& ss, MockRobotSafetyProperties& sp) :  
 +    Sequence(name, caller, true),  
 +    cs(cs), ss(ss), sp(sp) { }
   
   int action() {   int action() {
-    cs.setpointX.setValue(0.1); +    cs.setpoint.setValue({0.1, 0.1}); 
-    cs.setpointY.setValue(0.1);+  } 
 +  bool checkExitCondition() { 
 +    Matrix<2,1,double> val = cs.i.getOut().getSignal().getValue(); 
 +    bool done = val[0] >= 1.0 && val[1] >= 1.0; 
 +    if (val[0] >= 1.0) cs.setpoint.setValue({0, cs.setpoint.getValue()[1]})
 +    if (val[1] >= 1.0) cs.setpoint.setValue({cs.setpoint.getValue()[0], 0}); 
 +    if (done) ss.triggerEvent(sp.homingDone); 
 +    return done;
   }   }
-  bool checkExitCondition() {return cs.iX.getOut().getSignal().getValue() >= 1.0 && cs.iY.getOut().getSignal().getValue() >= 1.0;} 
-private: 
   MockRobotControlSystem& cs;   MockRobotControlSystem& cs;
 +  SafetySystem& ss;
 +  MockRobotSafetyProperties& sp;
 }; };
 </code> </code>
  
-What does the sequence do? It's action method simply adjusts the set points of the two axis to a small positive value. That causes the position of the robot (represented by the two integrators) to slowly move upwards. The sequence, which in this case is a simple step, terminates as soon as its exit condition is met. That is when both axis reach the position of 1.0.+What does the sequence do? It's action method simply adjusts the set points of the two axis to a small positive value. That causes the position of the robot (represented by the two integrators) to slowly move upwards. The sequence, which in this case is a simple step, terminates as soon as its exit condition is met. That is when both axis reach the position of 1.0. As soon as one of the axis reaches the position of 1.0 its setpoint is zeroed in order to stop it moving further. On termination a safety event is triggered.
  
 The second sequence "UpAndDown Sequence" is as follows: The second sequence "UpAndDown Sequence" is as follows:
Line 101: Line 102:
 class MoveUp : public Step { class MoveUp : public Step {
 public: public:
-  MoveUp(std::string name, Sequencer& sequencer, BaseSequence* caller, MockRobotControlSystem& cs) : Step(name, sequencer, caller), cs(cs) { }+  MoveUp(std::string name, Sequence* caller, MockRobotControlSystem& cs) : Step(name, caller), cs(cs) { }
   int action() {   int action() {
-    cs.setpointX.setValue(0.7)+    Matrix<2,1,double> dest{8.2, 27.0}
-    cs.setpointY.setValue(0.3);+    cs.pp.move(dest); 
 +    return 0;
   }   }
-  bool checkExitCondition() {return cs.iX.getOut().getSignal().getValue() >= 5.0;} +  bool checkExitCondition() {return cs.pp.endReached();}
-private:+
   MockRobotControlSystem& cs;   MockRobotControlSystem& cs;
 }; };
Line 113: Line 114:
 class MoveDown : public Step { class MoveDown : public Step {
 public: public:
-  MoveDown(std::string name, Sequencer& sequencer, BaseSequence* caller, MockRobotControlSystem& cs) : Step(name, sequencer, caller), cs(cs) { }+  MoveDown(std::string name, Sequence* caller, MockRobotControlSystem& cs) : Step(name, caller), cs(cs) { }
   int action() {   int action() {
-    cs.setpointX.setValue(-0.6); +    Matrix<2,1,double> dest{-5, 3}; 
-    cs.setpointY.setValue(-0.3);+    cs.pp.move(dest); 
 +    return 0;
   }   }
-  bool checkExitCondition() {return cs.iX.getOut().getSignal().getValue() <= -5.0;} +  bool checkExitCondition() {return cs.pp.endReached();}
-private:+
   MockRobotControlSystem& cs;   MockRobotControlSystem& cs;
 }; };
Line 125: Line 126:
 class UpAndDownSequence : public Sequence { class UpAndDownSequence : public Sequence {
 public: public:
-  UpAndDownSequence(std::string name, Sequencer& seq, MockRobotControlSystem& cs) : Sequence(name, seq)cs(cs), moveUp("move up", seq, this, cs), moveDown("move down", seq, this, cs) { }+  UpAndDownSequence(std::string name, Sequence* caller, MockRobotControlSystem& cs) :  
 +    Sequence(name, callertrue),  
 +    moveUp("move up", this, cs),  
 +    moveDown("move down", this, cs) { }
   
-  int action() { +    int action() { 
-    while (Sequencer::running) { +      while (Sequencer::running) { 
-      moveUp(); +        moveUp(); 
-      moveDown();+        moveDown()
 +      } 
 +      return 0;
     }     }
-  } 
-private: 
-  MockRobotControlSystem& cs; 
   MoveUp moveUp;   MoveUp moveUp;
   MoveDown moveDown;   MoveDown moveDown;
getting_started/tutorials/mockrobot.1553770962.txt.gz · Last modified: 2019/03/28 12:02 by graf