einfacher TP, TP1Ordn.cpp und .h sind wichtig

Dependencies:   Serial_HL mbed

Fork of ProcVisDemo by michael hollegha

Files at this revision

API Documentation at this revision

Comitter:
mSeiser
Date:
Wed May 03 16:04:57 2017 +0000
Parent:
1:e88b745f2ca2
Commit message:
nur einfacher TP;

Changed in this revision

BtnEventM0.h Show annotated file Show diff for this revision Revisions of this file
FunkGen.cpp Show annotated file Show diff for this revision Revisions of this file
FunkGen.h Show annotated file Show diff for this revision Revisions of this file
Main.cpp Show annotated file Show diff for this revision Revisions of this file
ProcVisDemo.cpp Show diff for this revision Revisions of this file
Serial_HL.lib Show annotated file Show diff for this revision Revisions of this file
TP1Ord.cpp Show annotated file Show diff for this revision Revisions of this file
TP1Ord.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BtnEventM0.h	Wed May 03 16:04:57 2017 +0000
@@ -0,0 +1,174 @@
+
+// V4.0
+
+class BtnEventM0 {
+    public:
+        int16_t pressed;
+        
+        BtnEventM0(PinName pin) : _isr(pin)
+            { pressed=0; }
+
+        // Ist eine steigende Flanke aufgetreten ?
+        int CheckFlag();
+    
+    // 1..Button is pressed  else 0
+        int CheckButton()
+            { return _isr.read(); }
+        
+        void Init();
+            // { _isr.rise(this,&BtnEventM0::RisingISR); }
+
+        void RisingISR();
+        
+    protected:
+        InterruptIn _isr;
+};
+
+void BtnEventM0::Init()
+            { _isr.rise(this,&BtnEventM0::RisingISR); }
+
+void BtnEventM0::RisingISR()
+{
+    if( _isr.read() )
+        pressed = 1;
+}
+
+int BtnEventM0::CheckFlag()
+{
+  if( pressed )
+      { pressed=0; return 1; }
+  return 0;
+}
+
+
+
+
+
+/*
+class BtnEventM02 : public BtnEventM0
+{
+public:
+    BtnEventM02(PinName pin) : BtnEventM0(pin) {
+        _tm.stop();
+        _tm.reset();
+        _state=1;
+    }
+
+    void Init() {
+        _isr.rise(this, &BtnEventM02::RisingISR);
+    }
+
+    void RisingISR() {
+        if( !_isr.read() )
+            return;
+        pressed = 1;
+        _tm.start();
+        _state = 2;
+    }
+
+    void CheckButton() {
+        if( _state==1 )
+            return;
+        if( _state==2 ) {
+            if( !_isr.read() ) {
+                _state = 1;
+                return;
+            }
+            if( _tm.read_ms()>500 ) {
+                _tm.reset();
+                _state = 3;
+                pressed = 1;
+            }
+        } else if( _state==3 ) {
+            if( !_isr.read() ) {
+                _state = 1;
+                return;
+            }
+            if( _tm.read_ms()>100 ) {
+                _tm.reset();
+                _state = 3;
+                pressed = 1;
+            }
+        }
+    }
+private:
+    int16_t _state;
+    Timer _tm;
+};
+*/
+
+class AnalogInHL : public AnalogIn
+{
+public:
+    AnalogInHL(PinName pin) : AnalogIn(pin) { }
+    int Read() {
+        return read_u16()>>6;
+    }
+};
+
+class BtnEventM0S {
+    public:
+    BtnEventM0S(PinName pin) : _btn(pin) { }
+
+    void Init() {}
+
+        // Ist eine steigende Flanke aufgetreten ?
+        int CheckFlag()
+    {
+      if( _btn )
+        { wait_ms(100); return 1; }
+      else
+        return 0;
+    }
+
+  protected:
+        DigitalIn _btn;
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FunkGen.cpp	Wed May 03 16:04:57 2017 +0000
@@ -0,0 +1,111 @@
+
+#include "FunkGen.h"
+
+SignedRampGen::SignedRampGen()
+{
+  val = 0;
+  SetPointsPerPeriod(10); // Default ist 10Pts/Periode
+}
+
+void SignedRampGen::SetPointsPerPeriod(float aPoints)
+{
+  _inc = 2.0/aPoints;
+}
+
+void SignedRampGen::SetFrequ(float aFrequ)
+{
+  SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void SignedRampGen::CalcOneStep()
+{
+  val = val + _inc;
+  if( val>1.0 )
+    val = -1 + (val - 1.0); 
+}
+
+
+
+TriangleGen::TriangleGen()
+{
+  val=_phase=0;
+  SetPointsPerPeriod(100);
+}
+
+void TriangleGen::SetPointsPerPeriod(float aPoints)
+{
+  _inc = 4.0/aPoints;
+}
+
+void TriangleGen::SetFrequ(float aFrequ)
+{
+  SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void TriangleGen::CalcOneStep()
+{
+  _phase = _phase + _inc;
+  if( _phase>1.0 ) {
+    _phase = -1 + (_phase - 1.0);
+    if( _state==1 )
+      _state=2;
+    else
+      _state=1;
+  }
+  if( _state==1 )
+    val = _phase;
+  else
+    val = -_phase;
+}
+
+
+
+RectGen::RectGen()
+{
+  val=0; _phase=0; _thrs=0;
+  SetPointsPerPeriod(10); // Default ist 10Pts/Periode
+}
+
+void RectGen::SetPointsPerPeriod(float aPoints)
+{
+  _inc = 2.0/aPoints;
+}
+
+void RectGen::SetFrequ(float aFrequ)
+{
+  SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void RectGen::SetPulsWidth(float aPercent)
+{
+  _thrs = 1.0-aPercent;
+}
+
+void RectGen::CalcOneStep()
+{
+  _phase = _phase + _inc;
+  if( _phase>1.0 )
+    _phase = -1 + (_phase - 1.0); 
+    if( _phase>_thrs )
+    val = 1.0;
+  else
+    val = -1.0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FunkGen.h	Wed May 03 16:04:57 2017 +0000
@@ -0,0 +1,86 @@
+
+#ifndef FunkGen_h
+#define FunkGen_h
+
+// Amplituden fix auf +/-1
+
+class SignedRampGen {
+    public:
+        float val; // momentaner Ausgangswert
+  private:
+        float _inc; 
+    public:
+        SignedRampGen(); // Konstruktor
+    
+        void SetPointsPerPeriod(float aPoints);
+    
+      // bezogen auf Fsample 0..0.5
+      void SetFrequ(float aFrequ);
+
+    // Einen Abtastwert berechnen
+      // wird bei z.B. Fsample=100Hz  100x pro sec afgerufen
+        void CalcOneStep();
+};
+
+
+class TriangleGen {
+    public:
+        float val; // momentaner Ausgangswert
+  private:
+    float _inc;
+    int   _state;
+    float _phase;
+    public:
+        TriangleGen();
+    
+        void SetPointsPerPeriod(float aPoints);
+    
+        // bezogen auf Fsample 0..0.5
+      void SetFrequ(float aFrequ);
+    
+        // Einen Abtastwert berechnen
+        void CalcOneStep();
+};
+
+
+class RectGen {
+    public:
+        float val; // momentaner Ausgangswert
+    private:
+        float _inc;
+        float _phase;
+    float _thrs; // Threshold für die PulsWidth
+    public:
+        RectGen();
+    
+        void SetPointsPerPeriod(float aPoints);
+    
+        void SetFrequ(float aFrequ);
+
+    // Dauer des ON-Pulses in Prozent ( 0..1 )
+        void SetPulsWidth(float aPercent);
+
+    // Einen Abtastwert berechnen
+        void CalcOneStep();
+};
+
+#endif
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Main.cpp	Wed May 03 16:04:57 2017 +0000
@@ -0,0 +1,105 @@
+#include "mbed.h"
+#include "Serial_HL.h"
+#include "FunkGen.h"
+#include "TP1Ord.h"
+
+// 2 Generatoren mit switch umschalten
+ 
+SerialBLK pc(USBTX, USBRX);
+SvProtocol ua0(&pc);
+
+void CommandHandler();
+void ExecSignalChain();
+
+SignedRampGen fg1;
+RectGen fg2;
+TP1Ord tp1;
+float amp1 = 1.0;
+float v1; // Ausgangswert mit amp1 multipliziert
+
+
+int swt=1; // 1..v1 auf fg1 geschaltet    2..v1 auf fg2 geschaltet
+
+int main(void)
+{
+    pc.format(8,SerialBLK::None,1); pc.baud(500000); // 115200
+  
+  ua0.SvMessage("FuncGenMain3"); // Meldung zum PC senden
+  
+  Timer stw; stw.start();
+  while(1) //  while(1)-Loop der mit dem PC kommuniziert
+  {
+    CommandHandler();
+    if( (stw.read_ms()>10) ) // 100Hz
+    { // dieser Teil wird mit 100Hz aufgerufen
+      stw.reset();
+      ExecSignalChain(); // Funktionsgeneratoren rechnen
+      if( ua0.acqON ) {
+        ua0.WriteSV(1, tp1.y);
+      }
+    }
+  }
+  return 1;
+}
+
+void ExecSignalChain()
+{
+  fg1.CalcOneStep();
+    fg2.CalcOneStep();
+    if( swt==1 )
+        v1 = amp1*fg1.val;
+    if( swt==2 )
+        v1 = amp1*fg2.val;
+        
+    tp1.CalcOneStep(v1);
+}
+
+void CommandHandler()
+{
+  uint8_t cmd;
+  if( !pc.IsDataAvail() )
+    return;
+  cmd = ua0.GetCommand();
+
+  // mithilfe von Kommandos vom PC Frequenz und Amplitude verstellen
+
+  if( cmd==2 ) // Frequenz
+  {
+        float frequ = ua0.ReadF();
+    fg1.SetFrequ(frequ); fg2.SetFrequ(frequ);
+        ua0.SvMessage("Set Frequ");
+  }
+  
+  if( cmd==3 ) // Amplitute
+  {
+    amp1 = ua0.ReadF();
+    ua0.SvMessage("Set Frequ");
+  }
+    
+    if( cmd==4 ) // Funktionsgeneratoren umschalten
+    {
+        swt = ua0.ReadI16();
+        ua0.SvMessage("Switch FG");
+    }
+    if( cmd==5 ) // Funktionsgeneratoren umschalten
+    {
+       tp1.SetAlpha(ua0.ReadF());
+        ua0.SvMessage("Set Alpha");
+    }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- a/ProcVisDemo.cpp	Fri Oct 09 07:58:26 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-#include "mbed.h"
-#include "Serial_HL.h"
-
-SerialBLK pc(USBTX, USBRX);
-SvProtocol ua0(&pc);
-
-// V2.0
-// BusOut leds(LED1,LED2,LED3,LED4); Bertl14
-// M0-Board
-BusOut leds(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
-
-
-void CommandHandler();
-
-int main(void)
-{
-    pc.format(8,SerialBLK::None,1);
-    pc.baud(115200);
-    leds = 9;
-
-    ua0.SvMessage("SvTest_Serial_HL"); // Meldung zum PC senden
-
-    int16_t sv1=0, sv2=100;
-    Timer stw;
-    stw.start();
-    while(1) {
-        CommandHandler();
-        if( ua0.acqON && (stw.read_ms()>100) ) { // 10Hz
-            // dieser Teil wird mit 10Hz aufgerufen
-            stw.reset();
-            sv1++;
-            sv2++;
-            if( ua0.acqON ) {
-                // nur wenn vom PC aus das Senden eingeschaltet wurde
-                // wird auch etwas gesendet
-                ua0.WriteSvI16(1, sv1);
-                ua0.WriteSvI16(2, sv2);
-            }
-        }
-    }
-    return 1;
-}
-
-void CommandHandler()
-{
-    uint8_t cmd;
-    int16_t idata1, idata2;
-
-    // Fragen ob überhaupt etwas im RX-Reg steht
-    if( !pc.IsDataAvail() )
-        return;
-
-    // wenn etwas im RX-Reg steht
-    // Kommando lesen
-    cmd = ua0.GetCommand();
-
-    if( cmd==2 ) {
-        // cmd2 hat 2 int16 Parameter
-        idata1 = ua0.ReadI16();
-        idata2 = ua0.ReadI16();
-        // für die Analyse den Wert einfach nur zum PC zurücksenden
-        ua0.SvPrintf("Command2 %d %d", idata1, idata2);
-    }
-}
-
-
--- a/Serial_HL.lib	Fri Oct 09 07:58:26 2015 +0000
+++ b/Serial_HL.lib	Wed May 03 16:04:57 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/hollegha2/code/Serial_HL/#b958bdf108cf
+https://developer.mbed.org/users/mSeiser/code/Serial_HL/#277c25d23b8e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TP1Ord.cpp	Wed May 03 16:04:57 2017 +0000
@@ -0,0 +1,18 @@
+#include "TP1Ord.h"
+
+TP1Ord :: TP1Ord()
+{
+    //sinnvolle fg setzen
+    SetAlpha(0.1);
+    
+}
+void TP1Ord ::CalcOneStep(float aX)
+{
+    y=aX*_alpha+y*_beta;
+      
+}
+void TP1Ord :: SetAlpha(float aAlpha)
+{
+    _alpha=aAlpha;
+    _beta=(1.0-aAlpha);   
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TP1Ord.h	Wed May 03 16:04:57 2017 +0000
@@ -0,0 +1,23 @@
+#ifndef TP1Ord_h
+#define TP1Ord_h
+
+class TP1Ord{
+    public:
+        float y; //ausgangswert
+    private:
+        float _alpha, _beta; //Koeffizient für fg 
+    public:
+        TP1Ord();
+        
+    //einen Abtastschritt des Filters rechnen 
+    //es entsteht neues y
+   void CalcOneStep(float aX);
+   
+   
+   //fg(Zeitkonstante des Filters setzen
+   //aAlpha 0...1
+   void SetAlpha(float aAlpha);
+    
+};
+
+#endif
\ No newline at end of file