ROBOSTEP / Mbed 2 deprecated hybrid_arm_fan

Dependencies:   mbed

Fork of hybrid_arm_main by ROBOSTEP_SHARE

main.cpp

Committer:
hare
Date:
2015-10-24
Revision:
0:6f7d125a0503
Child:
1:0f1e77683604

File content as of revision 0:6f7d125a0503:

#include "IR.h"
#include "count.h"
DigitalIn start_sw(p23);

PwmOut motor_up(p22);
PwmOut motor_down(p21);

DigitalIn encoder_A(p16);
DigitalIn encoder_B(p17);

AnalogIn sensor(p20);

DigitalIn manual_up(p24);
DigitalIn manual_down(p28);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

PwmOut fan(p25);

int main(){
  //////////////////////////waiting for start/////////////////////////// 
   while(1){
       motor_up.period_us(20);
       motor_down.period_us(20);
       printf("waiting \n\r");
       motor_up=0;
       motor_down=0;
        led1=1;     
      if(start_sw==1){break;}
      }
//////////////////////////calibration start//////////////////////////
 /*      while(1){
       motor_down=0.1;
       motor_up=0;
       led1=0;
       led2=1;
       printf("auto calibrating\n\r"); 
       if(sensor>0.5){
           motor_down=0;
           motor_up=0;
           led2=0;
           printf("over\n\r");
           break;}
       }*/
//////////////////////manual calibration 1/////////////////
     motor_up=0;
     motor_down=0;
     wait_ms(400);
     printf("asd\n\r");
    A_s.rise(&A_s_rise);
    A_s.fall(&A_s_fall);
    B_s.rise(&B_s_rise);
    B_s.fall(&B_s_fall);
      while(1){
           if(manual_up==1 && manual_down==0){
               
               motor_down=0;
               motor_up=0.2;
               led1=1;
               led2=0;
               printf("manual_up,%d\n\r",rotation);
               }
           else if(manual_up==0 && manual_down==1){
               motor_up=0;
               motor_down=0.2;
               printf("manual_down,%d\n\r",rotation);
               }
               
          else if(start_sw==1){
                rotation=0;
                break;}
        else{motor_up=0;
                motor_down=0;}
           }
           wait_ms(300);
 ///////////////manual calibration 2///////////////////
        while(1){
           if(manual_up==1 && manual_down==0){
               
               motor_down=0;
               motor_up=0.2;
               led1=1;
               led2=0;
               printf("manual_up,%d\n\r",rotation);
               }//9116
           else if(manual_up==0 && manual_down==1){
               motor_up=0;
               motor_down=0.2;
               printf("manual_down,%d\n\r",rotation);
               }
               
          else if(start_sw==1){
                break;}
        else{motor_up=0;
                motor_down=0;}
           }
    IR_data.attach(&cal_distance,0.005);  
    motor_up=0;
    motor_down=0;
    wait(1);  

int original=avr_distance;

double duty;
    printf("motor controlling\n\r");
int d;
int I_d;
int D_d;
int pre_d;
float Kp=0.03;
float Ki=0;
float Kd=0.005;

while(1){
    //printf("%d %d %f,%f\n\r",avr_distance,original,(float)motor_up,(float)motor_down);
    d=avr_distance-original;
    I_d +=d;
    D_d=d-pre_d;
    if(rotation>=12500){
        motor_up=0;
        motor_down=0;
        led1=1;
        led2=1;
        led3=1;
        break;}
    else if(rotation <=150){
            motor_up=0.2;
            motor_down=0;
            wait(1);}
    if(avr_distance<60){
        motor_up=0.3;
        motor_down=0;
        wait(0.5);}
    if(d<=1 && d>=-1){motor_up=0;motor_down=0;}
    
    else {duty=Kp*d+Ki*I_d+Kd*D_d;}
    
    if(-0.5<duty && duty<0.5){      
      if(0<=duty){
        motor_up=0;
        motor_down=duty;}
         else if(duty<0){
            duty=-duty;
            motor_up=duty;
            motor_down=0;}}
      else{
       motor_up=0;
       motor_down=0;}
       pre_d=d;  
       }
       
       motor_up=0;
       motor_down=0;
       }