Script voor aansturen motoren

Dependencies:   Encoder HIDScope MODSERIAL mbed-dsp mbed

main.cpp

Committer:
jessekaiser
Date:
2014-10-30
Revision:
21:c856b7752d7d
Parent:
19:3e46c457091c
Child:
22:22cb158bcea4

File content as of revision 21:c856b7752d7d:

#include "mbed.h"
#include "encoder.h"
#include "HIDScope.h"
#include "PwmOut.h"

#define TSAMP 0.005
#define K_P (0.8)
#define K_I (0.01  *TSAMP)
#define I_LIMIT 1.
#define pwm1_min 0.06
#define pwm2_min 0
#define PI 3.14159265
#define l_arm 0.5

#define M1_PWM PTC8
#define M1_DIR PTC9
#define M2_PWM PTA5
#define M2_DIR PTA4

//Groene kabel moet op de GROUND en blauw op de 3.3v aansluiting

Serial pc(USBTX, USBRX);
DigitalOut led1(LED_RED);
HIDScope scope(2);

//motor 25D
Encoder motor1(PTD3,PTD5); //wit, geel
PwmOut pwm_motor1(M2_PWM);
DigitalOut motordir1(M2_DIR);

//motor2 37D
Encoder motor2(PTD2, PTD0); //wit, geel
PwmOut pwm_motor2(M1_PWM);
DigitalOut motordir2(M1_DIR);
void clamp(float * in, float min, float max);
float pid(float setpoint, float measurement);
float pwm1_percentage = 0;
float pwm2_percentage = 0;
int cur_pos_motor1;
int prev_pos_motor1 = 0;
int cur_pos_motor2;
int prev_pos_motor2 = 0;
float speed1_rad;
float speed2_rad;
float pos_motor1_rad;
float pos_motor2_rad;



/*
volatile bool looptimerflag;

bool flip=false;

void attime()
{
    flip = !flip;
}

void looper()
{
    motordir1=0;
    pwm_motor1.write(1);
    scope.set(0, motor1.getPosition());
    scope.set(1, motor2.getPosition());
    scope.send();
}*/

void clamp(float* in, float min, float max) // "*" is een pointer (verwijst naar het adres waar een variabele instaat). Dus je slaat niet de variabele op
// maar de locatie van de variabele.
{
*in > min ? /*(*/*in < max? /*niets doen*/ : *in = max/*)*/: *in = min; // a ? b : c --> als a waar is, dan doe je b, en anders c
    // *in = het getal dat staat op locatie van in --> waarde van new_pwm
}


float pid(float setpoint, float cur_pos_motor1)
{
    float error;
    float           out_p = 0;
    static float    out_i = 0;
    error  = (setpoint - cur_pos_motor1);
    out_p  = error*K_P;
    out_i += error*K_I;
    clamp(&out_i,-I_LIMIT,I_LIMIT);
    return out_p + out_i;
    }
int main()
{
    //Ticker log_timer;
    //log_timer.attach(looper, TSAMP);
    motor1.setPosition(0);
    pwm_motor1.period_us(100);
    float prev_setpoint = 0;
    float setpoint;
    speed1_rad = 0.05;


    while(1) {

        cur_pos_motor1 = motor1.getPosition();
        pos_motor1_rad = cur_pos_motor1/(4128/(2*PI));

        setpoint = prev_setpoint + TSAMP * speed1_rad;
        pwm1_percentage = pid(setpoint, pos_motor1_rad);

        if (pwm1_percentage < -100) {
            pwm1_percentage = -100;
        } 
        else if (pwm1_percentage >100){
                   pwm1_percentage =100;
                  }

        if(pwm1_percentage < 0) {
                motordir1 = 1;
            } 
            else {
                motordir1 = 0;
            }

        if(cur_pos_motor1 < 130) {
            pwm_motor1.write(abs(pwm1_percentage/100));//(abs(((1-pwm1_min)/100)*pwm1_percentage + pwm1_min));
            prev_setpoint = setpoint;
        } 
        else {
            pwm_motor1.write(0);
        }
    }
}