ProcVisDemo_Accel

Dependencies:   MMA7660 mbed

Files at this revision

API Documentation at this revision

Comitter:
Wizo
Date:
Thu Nov 15 18:04:08 2018 +0000
Commit message:
ProcVisDemo_Accel

Changed in this revision

MMA7660.lib Show annotated file Show diff for this revision Revisions of this file
ProcVisDemo_Accel.cpp Show annotated file Show diff for this revision Revisions of this file
SerialHL/Serial_HL.cpp Show annotated file Show diff for this revision Revisions of this file
SerialHL/Serial_HL.h Show annotated file Show diff for this revision Revisions of this file
SerialHL/SvProtocol.h Show annotated file Show diff for this revision Revisions of this file
TP1.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fca9076d3362 MMA7660.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660.lib	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Sissors/code/MMA7660/#36a163511e34
diff -r 000000000000 -r fca9076d3362 ProcVisDemo_Accel.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ProcVisDemo_Accel.cpp	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,78 @@
+#include "mbed.h"
+#include "Serial_HL.h"
+#include "MMA7660.h"
+#include "TP1.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();
+
+MMA7660 accel(p28, p27);
+
+// 3 Filter anlegen
+TP1Ord tpx, tpy, tpz;
+int accVal[3];
+
+void Do100Hz_Work();
+
+int main(void)
+{
+    pc.format(8,SerialBLK::None,1);
+    pc.baud(115200);
+
+
+    ua0.SvMessage("AccelTest"); // Meldung zum PC senden
+
+    accel.setSampleRate(120);
+
+    Timer stw;
+    stw.start();
+ 
+
+    while(1) {
+        CommandHandler();
+        if (stw.read_ms()>10) { // 100Hz
+            stw.reset();
+            Do100Hz_Work();
+            if(ua0.acqON) {
+                ua0.WriteSvI16(1, accVal[0]); // X,Y,Z zum PC senden
+                ua0.WriteSvI16(2, tpx.y);                
+                //ua0.WriteSvI16(3, accVal[1]);
+                //ua0.WriteSvI16(4, tpy.y);
+            }
+        }
+    }
+}
+
+
+
+
+void Do100Hz_Work()
+{
+    accel.readData(accVal); // X,Y,Z vom Sensor lesen
+    tpx.CalcOneStep (accVal[0]);
+    tpy.CalcOneStep (accVal[1]);
+    tpz.CalcOneStep (accVal[2]);
+    
+}
+
+
+void CommandHandler()
+{
+    uint8_t cmd;
+
+    if( !pc.IsDataAvail() )
+        return;
+
+    cmd = ua0.GetCommand();
+
+}
+
+
diff -r 000000000000 -r fca9076d3362 SerialHL/Serial_HL.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialHL/Serial_HL.cpp	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,130 @@
+
+#include "Serial_HL.h"
+#include <stdarg.h>
+#include <stdio.h>
+
+namespace mbed {
+
+SerialBLK::SerialBLK(PinName tx, PinName rx) {
+  serial_init(&_serial, tx, rx);
+  _baud = 9600;
+  serial_irq_handler(&_serial, SerialBLK::_irq_handler, (uint32_t)this);
+}
+
+void SerialBLK::baud(int baudrate) {
+  serial_baud(&_serial, baudrate);
+  _baud = baudrate;
+}
+
+void SerialBLK::format(int bits, Parity parity, int stop_bits) {
+  serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
+}
+
+int SerialBLK::readable() {
+  return serial_readable(&_serial);
+}
+
+int SerialBLK::writeable() {
+  return serial_writable(&_serial);
+}
+
+void SerialBLK::attach(void (*fptr)(void), IrqType type) {
+  if (fptr) {
+      _irq[type].attach(fptr);
+      serial_irq_set(&_serial, (SerialIrq)type, 1);
+  } else {
+      serial_irq_set(&_serial, (SerialIrq)type, 0);
+	}
+}
+
+void SerialBLK::_irq_handler(uint32_t id, SerialIrq irq_type) {
+  SerialBLK *obj = (SerialBLK*)id;
+  obj->_irq[irq_type].call();
+}
+
+int SerialBLK::GetChar() {
+  return serial_getc(&_serial);
+}
+
+void SerialBLK::PutChar(int aCh) {
+  serial_putc(&_serial, aCh);
+}
+
+void SerialBLK::Write(void* aData, uint32_t aLenBytes)
+{
+	uint8_t* ptr = (uint8_t*)aData;
+	for(int i=0; i<aLenBytes; i++) {
+		this->PutChar(*ptr); ptr++;
+	}
+}
+
+void SerialBLK::Read(void* aData, uint32_t aLenBytes)
+{
+	uint8_t* ptr = (uint8_t*)aData;
+  for(int i=0; i<aLenBytes; i++) {
+    *ptr=this->GetChar(); ptr++;
+  }
+}
+
+
+
+
+void SvProtocol::Puts(char* aCStr)
+{
+  while( *aCStr != '\0' )
+  {
+    if( *aCStr=='\n' )
+      _st->PutChar('\r');
+    _st->PutChar(*aCStr);
+    aCStr++;
+  }
+	_st->PutChar(0); // terminate with 0
+}
+
+static char sBuffer[50];
+
+void SvProtocol::Printf(const char *format, ...)
+{
+  va_list vArgs;
+  va_start(vArgs, format);
+  vsprintf(sBuffer, (char const *)format, vArgs);
+  va_end(vArgs);
+  Puts(sBuffer);
+}
+
+void SvProtocol::SvPrintf(const char *format, ...)
+{
+  va_list vArgs;
+  va_start(vArgs, format);
+  vsprintf(sBuffer, (char const *)format, vArgs);
+  va_end(vArgs);
+  if( !svMessageON ) return;
+  _st->PutChar(10);
+  Puts(sBuffer);
+}
+
+void SvProtocol::WriteSV3p13(int aId, float aData) {
+  // int16_t val = To3p13(aData);
+  // PutChar(aId); Write(&val,2);
+}
+
+int SvProtocol::GetCommand()
+{
+  uint8_t cmd = _st->GetChar();
+  if( cmd==1 )
+  {
+    this->acqON = _st->GetChar();
+    if( this->acqON )
+      this->SvMessage("AcqON");
+    else
+      this->SvMessage("AcqOFF");
+    return 0;
+  }
+  return cmd;
+}
+
+} // namespace mbed
+
+
+
+
diff -r 000000000000 -r fca9076d3362 SerialHL/Serial_HL.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialHL/Serial_HL.h	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,69 @@
+
+#ifndef Serial_HL_h
+#define Serial_HL_h
+
+#include "platform.h"
+// #include "Stream.h"
+#include "FunctionPointer.h"
+#include "serial_api.h"
+#include "SvProtocol.h"
+
+namespace mbed
+{
+
+class SerialBLK : public IStreamHL
+{
+public:
+    SerialBLK(PinName tx, PinName rx);
+    void baud(int baudrate);
+
+    virtual void PutChar(int aCh);
+    virtual int GetChar();
+    virtual void Write(void* aData, uint32_t aLenBytes);
+    virtual void Read(void* aData, uint32_t aLenBytes);
+
+    enum Parity {
+        None = 0,
+        Odd,
+        Even,
+        Forced1,
+        Forced0
+    };
+    enum IrqType {
+        RxIrq = 0,
+        TxIrq
+    };
+
+    void format(int bits=8, Parity parity=SerialBLK::None, int stop_bits=1);
+
+    int readable();
+    int writeable();
+    int IsDataAvail() {
+        return readable();
+    }
+
+    // fptr A pointer to a void function, or 0 to set as none
+    // type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
+    void attach(void (*fptr)(void), IrqType type=RxIrq);
+
+    // tptr pointer to the object to call the member function on
+    // mptr pointer to the member function to be called
+    // type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
+    template<typename T>
+    void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
+        if((mptr != NULL) && (tptr != NULL)) {
+            _irq[type].attach(tptr, mptr);
+            serial_irq_set(&_serial, (SerialIrq)type, 1);
+        }
+    }
+
+    static void _irq_handler(uint32_t id, SerialIrq irq_type);
+protected:
+    serial_t        _serial;
+    FunctionPointer _irq[2];
+    int             _baud;
+};
+
+} // namespace mbed
+
+#endif
diff -r 000000000000 -r fca9076d3362 SerialHL/SvProtocol.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialHL/SvProtocol.h	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,83 @@
+
+#ifndef SvProtocol_h
+#define SvProtocol_h
+
+#include "stdint.h"
+
+namespace mbed {
+
+class IStreamHL {
+	public:
+		virtual void PutChar(int aCh) = 0;
+		virtual int GetChar() = 0;
+		virtual void Write(void* aData, uint32_t aLenBytes) = 0;
+		virtual void Read(void* aData, uint32_t aLenBytes) = 0;
+};
+
+class SvProtocol {
+	public:
+		IStreamHL* _st;
+		uint8_t acqON;
+    uint8_t svMessageON;
+	public:
+		SvProtocol(IStreamHL* aStrm) {
+			acqON=0; svMessageON=1;
+			_st=aStrm; 
+		}
+	
+		// Check's first for acqOn/Off Command
+    // ret 0 if acqOn/Off was handled in GetCommand
+    int GetCommand();
+		
+		void Puts(char* aCStr); // Terminate with 0
+
+    // \r\n is appended automatically
+    void Printf(const char* format, ...);
+
+    void SvPrintf(const char *format, ...);
+    
+    void WriteSV(int aId, char* aData) {
+      if( !svMessageON ) return;
+      _st->PutChar(aId); Puts(aData); 
+    }
+    
+    void SvMessage(char* aTxt) {
+      if( !svMessageON ) return;
+      _st->PutChar(10); Puts(aTxt);
+    }
+    
+    void VectHeader(int aId, int aNVals)
+      { _st->PutChar(aId); _st->PutChar(aNVals); }
+    
+    void WriteSvI16(int aId, int16_t aData)
+      { _st->PutChar(aId+10); _st->Write(&aData,2); }
+    
+    void WriteSvI32(int aId, int32_t aData)
+      { _st->PutChar(aId); _st->Write(&aData,4); }
+    
+    void WriteSV(int aId, float aData)
+      { _st->PutChar(aId+20); _st->Write(&aData,4); }
+    
+    // float in 3.13 Format
+    void WriteSV3p13(int aId, float aData);
+
+    int16_t ReadI16()
+      { int16_t ret; _st->Read(&ret,2); return ret; }
+
+    int32_t ReadI32()
+      { int32_t ret; _st->Read(&ret,4); return ret; }
+    
+    float ReadF()
+      { float ret; _st->Read(&ret,4); return ret; }
+};
+
+} // namespace mbed
+
+// SV-Id Ranges and DataTypes for SvVis3 Visualisation-Tool
+//----------------------------------------------------------
+// Id = 10       : string
+// Id = 1 .. 9   : format 3.13  2 Bytes
+// Id = 11 .. 20 : short        2 Bytes
+// Id = 21 .. 30 : float        4 Bytes
+
+#endif
diff -r 000000000000 -r fca9076d3362 TP1.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TP1.h	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,41 @@
+// Digitaler Tiefpass 1. Ordnung
+
+class TP1Ord
+{
+private:
+    float alpha; // Filterkonstante
+public:
+    float yn_1;
+    float y; // momentaner Ausgangswert des Filters
+public:    
+    
+    // Konstruktor wird automatisch aufgerufen
+    // Variable der Klasse TP1Ord angelegt wird
+    // interne Variablen sinnvoll initialisieren
+    TP1Ord(); 
+    
+    // einen Abtastschritt des Filters rechnen
+    // es entsteht ein neues y
+    void CalcOneStep (float aX);
+    
+    void SetAlpha (float aAlpha);
+};
+
+TP1Ord::TP1Ord()
+{
+    y = 0; yn_1 = 0;
+    SetAlpha(0.1); // sinnvolles Alpha einstellen
+}
+
+void TP1Ord::SetAlpha (float aAlpha)
+{   
+    // 0...1 absichern
+    if(aAlpha >= 0 && aAlpha <= 1)
+        alpha = aAlpha; 
+}
+
+void TP1Ord::CalcOneStep (float aX)
+{
+    y = alpha*aX + (1-alpha)*yn_1;
+    yn_1 = y; // y einmal merken (zwischspeichern)
+}
\ No newline at end of file
diff -r 000000000000 -r fca9076d3362 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 15 18:04:08 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac
\ No newline at end of file