polymorphic base structures for easily implementing different gaits. Triangle wave is already in there

Dependents:   robotic_fish_ver_4_9_pixy

Revision:
0:aec7653a5b19
Child:
1:0aa1a6ccf5fe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fishgait.cpp	Thu Jun 19 17:53:17 2014 +0000
@@ -0,0 +1,51 @@
+#include "fishgait.h"
+
+fishgait::fishgait()
+{
+    t=new Timer();
+    t->start();
+}
+
+fishgait::~fishgait()
+{
+    t->stop();
+    delete t;
+}
+
+void fishgait::setTimer(Timer* tObject)
+{
+    delete t; //not sure if this is safe
+    t=NULL;
+    t=tObject;
+}
+
+triangleGait::triangleGait(float freq, float amplitude) : fishgait(), frq(freq), amp(amplitude)
+{
+}
+/*
+triangleGait::~triangleGait()
+{
+}
+*/
+
+float triangleGait::compute()
+{
+    float halfperiod=(1.0f/(frq/2.0f))*1000; //the half period in ms
+    int curtime=t->read_ms(); //read time
+    static bool direction=true; //true-> up, false->down
+    float out=0;
+    if (curtime>halfperiod) {
+        direction=!direction;
+        t->reset();
+    }
+    if (direction==true)
+        out=float(curtime)/halfperiod;
+    else if (direction==false)
+        out=1.0f-(float(curtime)/halfperiod);
+    out*=amp;
+    if (out>1)
+        out=1;
+    else if (out<0)
+        out=0;
+    return out;
+}
\ No newline at end of file