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

Dependents:   robotic_fish_ver_4_9_pixy

Files at this revision

API Documentation at this revision

Comitter:
sandwich
Date:
Thu Jun 19 21:04:19 2014 +0000
Parent:
1:0aa1a6ccf5fe
Commit message:
sawtooth gait works. working on mixed signals gait;

Changed in this revision

fishgait.cpp Show annotated file Show diff for this revision Revisions of this file
fishgait.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0aa1a6ccf5fe -r d95c40ac8a3e fishgait.cpp
--- a/fishgait.cpp	Thu Jun 19 19:51:59 2014 +0000
+++ b/fishgait.cpp	Thu Jun 19 21:04:19 2014 +0000
@@ -18,7 +18,31 @@
     t=NULL;
     t=tObject;
 }
+/*
+mixedGait::mixedGait() : count(0)
+{
+}
 
+void mixedGait::attach(fishgait gait, float weight)
+{
+    gaits[count]=gait;
+    weights[count]=weight;
+    ++count;
+    return;
+}
+
+void mixedGait::compute()
+{
+    float out=0;
+    if (count==0) //derp
+        return 0.5;
+    //do the mixing
+    for (int i=0; i<count; ++i) {
+        out+=gaits[count].compute()*weights[count];
+    }
+    return out;
+}
+*/
 triangleGait::triangleGait(float freq, float amplitude) : fishgait(), frq(freq), amp(amplitude)
 {
 }
@@ -72,9 +96,35 @@
         t->reset();
     }
     if (direction==true)
-        out=1.0f;
+        out=0.5f+0.5*amp;
+    if (out>1)
+        out=1;
     else if (direction==false)
-        out=0.0f;
-    out*=amp;
+        out=0.5f-0.5*amp;
+    if (out<0)
+        out=0;
+    return out;
+}
+
+sawGait::sawGait(float freq, float amplitude) : fishgait(), frq(freq), amp(amplitude)
+{
+}
+
+float sawGait::compute()
+{
+    float period=(1.0f/frq)*1000; //the period in ms
+    int curtime=t->read_ms(); //read time
+    float out=0;
+    if (curtime>period) {
+        t->reset();
+    }
+    out=float(curtime)/period;
+    out-=0.5; //recenter the wave around 0 instead of 0.5
+    out*=amp; //factor in amplitude
+    out+=0.5; //recenter around 0.5
+    if (out>1)
+        out=1;
+    else if (out<0)
+        out=0;
     return out;
 }
\ No newline at end of file
diff -r 0aa1a6ccf5fe -r d95c40ac8a3e fishgait.h
--- a/fishgait.h	Thu Jun 19 19:51:59 2014 +0000
+++ b/fishgait.h	Thu Jun 19 21:04:19 2014 +0000
@@ -1,5 +1,6 @@
 #pragma once
 #include "mbed.h"
+#define MAXGAITS 10
 
 class fishgait
 {
@@ -11,25 +12,48 @@
     void setTimer(Timer* tObject); //give the instance a timer to base its calculations off of instead of making a new one
     virtual float compute()=0; //asbtract function to compute output duty cycle. Must redefine in child classes
 };
-
+/*
+class mixedGait : public fishgait
+{
+    private:
+    fishgait gaits[MAXGAITS];
+    float weights[MAXGAITS];
+    int count;
+    public:
+    mixedGait();
+    void attach(fishgait gait, float weight); //gait to attach and its weight in the overall result. weights will be normalized
+    float compute(); //return the mixed signal pattern
+};
+*/
 class triangleGait: public fishgait
 {
 private:
-float frq;
-float amp;
+    float frq;
+    float amp;
 public:
-triangleGait(float freq, float amplitude);
+    triangleGait(float freq, float amplitude);
 //~triangleGait();
-float compute(); //this does a triangle wave pattern. frequency and amplitude controlled
+    float compute(); //this does a triangle wave pattern. frequency and amplitude controlled
 };
 
 class squareGait: public fishgait
 {
 private:
-float frq;
-float amp;
+    float frq;
+    float amp;
 public:
-squareGait(float freq, float amplitude);
+    squareGait(float freq, float amplitude);
 //~triangleGait();
-float compute(); //this does a triangle wave pattern. frequency and amplitude controlled
+    float compute(); //this does a triangle wave pattern. frequency and amplitude controlled
+};
+
+class sawGait: public fishgait
+{
+private:
+    float frq;
+    float amp;
+public:
+    sawGait(float freq, float amplitude);
+//~triangleGait();
+    float compute(); //this does a triangle wave pattern. frequency and amplitude controlled
 };
\ No newline at end of file