svoe

Dependencies:   mbed mbed-STM32F103C8T6 MPU6050_1

motor.h

Committer:
Stas285
Date:
2017-05-14
Revision:
4:904b737ef08a
Parent:
0:e9488589a8ee
Child:
6:6e89cdc3db92

File content as of revision 4:904b737ef08a:

float accel = 5;
/*float speed_right;
float speed_left;*/
float motor_speed[2] = {1,1};

DigitalOut  dir_left(PC_15);
DigitalOut  dir_right(PA_2);
DigitalOut  motor_enable(PA_0);//sleep inverted
PwmOut step_left(PB_5);
PwmOut step_right(PA_1);

void skorost(int right, float motor_target_speed){ //(0-left,1-right), cm/s [-400;400] //V=1 => nu = 500 Hz => 500/3200 = 0.15625 rps (d = 7.8cm) => V_real = Pi*d*0.15625 = 0.038 m/s = 3.8 cm/s //motor_speed = -
float nu_l;
    if ((-motor_target_speed + motor_speed[right]) > accel)  {motor_speed[right] = motor_speed[right] - accel;} // calculate new curent speed
    if ((-motor_speed[right] + motor_target_speed) > accel)  motor_speed[right] = motor_speed[right] + accel; // calculate new curent speed
    if (abs(-motor_target_speed + motor_speed[right]) <= accel)  motor_speed[right] = motor_target_speed;  // calculate new curent speed  
    motor_speed[right] -= 2*ay; 
    if (right) {if (motor_speed[right] > 0) dir_right = 1; else dir_right = 0;}
    else {if (motor_speed[right] > 0) dir_left = 0; else dir_left = 1;}
    nu_l = abs(motor_speed[right])*500/3.8; //frequency in herz !!!!(cm)
    if (nu_l < 1) nu_l = 1;
    if(right) step_right.period_us (1e6/nu_l); else step_left.period_us (1e6/nu_l);
   // pc.printf("%u \n", nu_l);
    }
    
void motor_init(){
    step_left = 0.5;   step_right = 0.5;
    motor_enable = 1;
    skorost(0,0.01); skorost(1,0.01);
    }