Carlo Collodi / kangaroo

Dependencies:   QEI mbed

FishMotors.cpp

Committer:
calamaridudeman
Date:
2013-11-19
Revision:
20:97ab3d4c07b6
Parent:
8:dc13c3be6838

File content as of revision 20:97ab3d4c07b6:

 /*
#include "mbed.h"       // mbed library
#include "QEI.h"

LocalFileSystem local("local");               // Create the local filesystem under the name "local"

Ticker myTicker;                // creates an instance of the ticker class, which can be used for running functions at a specified frequency.
Ticker myTicker1;

FILE *fp;

// Declaring pins
DigitalOut dA(p19);    // pin 5 set as a digital output.  Pins 5-30 can be used as digital outputs.
DigitalOut dB(p20);      // pin 6 set as digital input.  Pins 5-30 can be used as digital inputs.
PwmOut pwmOut1(p21);     // pin 21 set as PWM output.  Pins 21-26 can be used as PWM outputs.
AnalogIn current(p15); 

QEI motor1(p26, p25, NC, 1200, QEI::X4_ENCODING); // (encoder channel 1 pin, encoder channel 2 pin, index (n/a here), counts per revolution, mode (X4, X2))
// Use line above verbatim to avoid difficulties. Only certain pins can be used for counting encoder ticks.

float speed = 0;
float pos = 0;
float timet = 0;
float dCurrent1 = 0;
float duty1 = 0;
float mCurrent1 = 0;
float dAngle1=0; // Desired Angle
float dAngularVelocity1=0; // Desired Angular velocity
float Angle1 = 0;


float filterLowPass(float old, float current, float alpha){
    return (old+alpha*(current-old));
}

void Control()
{
float preAngle1=Angle1; // Storing the angle in the previous time step
Angle1 = motor1.getPulses()/1200.0*6.283; // Updating new motor angle
speed =filterLowPass(speed,(Angle1-preAngle1)/.001, .2); // Updating new motor angluar vel
mCurrent1 = current.read() *3.3/.14; //measure current

dCurrent1 = (3*(dAngle1-Angle1)+0.04*(dAngularVelocity1-speed));

if (dCurrent1>0){
dA=1;
dB=0;
duty1= (abs(dCurrent1)*3.27+0.174*speed+10*(abs(dCurrent1)-mCurrent1))/12.0;
//2.10- Resistance 0.2-Kv 5- current control gain, 12- source voltage

} else if (dCurrent1<0) {
dA=0;
dB=1;
duty1= (abs(dCurrent1)*3.27-0.174*speed+10*(abs(dCurrent1)-mCurrent1))/12.0;}

if (duty1>0.99){
duty1 =1;
}

pwmOut1.write(duty1);
}

void Printer(){
fprintf(fp, "%f    %f    %f\n", duty1, mCurrent1, speed);
}


int main() {
    dCurrent1 = 3;

    fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
    myTicker1.attach(&Printer, .005);
    myTicker.attach(&Control, .001);
    
    pwmOut1.period_us(100);
        
    //pwmOut.write(.5);
    wait(150);
    dCurrent1 = 0;
    myTicker.detach();
    myTicker1.detach();
    fclose(fp);

    pwmOut1.write(0);
        
}
*/