サーボモーターの動作確認プログラム(nucleo版)

Dependencies:   mbed

main_access_servo_tougou.cpp

Committer:
taquto
Date:
2 weeks ago
Revision:
3:cf99afcea25f

File content as of revision 3:cf99afcea25f:

#include "mbed.h"

PwmOut servo(PB_6);
DigitalIn push(PA_6);
Serial pc(SERIAL_TX, SERIAL_RX);


float calc(float);
int a,k,count;
int main()
{    
     servo.period_ms(20);
    servo.pulsewidth((1.45+(2.4-0.5)/180*90)/1000); //90度回転
    k=1;
    a=0;
    while(1) {

       pc.printf("a=%d\r\n",a);
/*
       if(push == 1){
           a=1; //押された証拠
              
           while(push != 1){
            count = 0;
            wait(0.3); //チャタリング対策
           }
           while(push == 1){
            count ++;
            wait(0.3); //チャタリング対策
           }
        }

        pc.printf("a=%d\r\n",a);

        if(a == 1){
            if(k == 0){
                servo.pulsewidth((1.45+(2.4-0.5)/180*90)/1000); //90度回転
                k=1;
            }
            else if(k == 1){
                servo.pulsewidth((1.45+(2.4-0.5)/180*0)/1000); //0度回転
                k=0;
            }
            a=0;
        }*/

        if(push==1){
            a=1;
        }

        if(a==1){
            if(push==0){
                a = 2; //スイッチ押された判定
                wait(0.3); //チャタリング対策
            }
        }

        if(k == 0){
            if(a == 2){
                servo.pulsewidth((1.45+(2.4-0.5)/180*90)/1000); //90度回転
                k = 1;
                a = 0;
            }
        }
        else if(k == 1){
            if(a == 2){
                servo.pulsewidth((1.45+(2.4-0.5)/180*0)/1000); //0度回転
                k = 0;
                a = 0;
            }
        }
           
    }       
    return 0;
}

/*
float calc(float x)
{
    return (1.45+(2.4-0.5)/180*x)/1000;
}
*/