Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 #define MIN_SV_PULSE 0.475 //最小パルス幅 0.475ms 00004 #define MAX_SV_PULSE 2.475 //最大パルス幅 2.475ms 00005 #define SV_PERIOD 0.020 //PWM周期20ms, 周波数50Hz 00006 #define MAX_ANGLE 180 //最大動作角度 00007 //D4ピンをPWM出力ピン servo1ピンとして宣言 00008 PwmOut servo1(D4); 00009 //パソコンとの通信用にpcクラスを宣言(デフォルトでは通信速度は9600bps) 00010 Serial pc(USBTX, USBRX); 00011 00012 //パルス幅(s)を取得する関数 00013 float get_pulse_width(int angle){ 00014 float pulseMs = (MIN_SV_PULSE + (MAX_SV_PULSE - MIN_SV_PULSE) * angle / MAX_ANGLE) / 1000; 00015 return(pulseMs); 00016 } 00017 00018 int main(void){ 00019 servo1.period(SV_PERIOD); 00020 pc.printf("START\n"); 00021 while(1){ 00022 for(int angle = 0; angle < MAX_ANGLE; angle += 10) { 00023 servo1.pulsewidth(get_pulse_width(angle)); 00024 wait_ms(40); 00025 pc.printf("angle = %d, pulse_width = %f\n", angle, get_pulse_width(angle)); 00026 } 00027 for(int angle = MAX_ANGLE; angle >= 0; angle -= 10) { 00028 servo1.pulsewidth(get_pulse_width(angle)); 00029 wait_ms(40); 00030 pc.printf("angle = %d, pulse_width = %f\n", angle, get_pulse_width(angle)); 00031 } 00032 } 00033 }
Generated on Tue Aug 2 2022 22:51:12 by
1.7.2